feishin/src/renderer/store/settings.store.ts

453 lines
14 KiB
TypeScript
Raw Normal View History

2022-12-19 15:59:14 -08:00
/* eslint-disable prefer-destructuring */
/* eslint-disable @typescript-eslint/no-unused-vars */
2023-05-20 18:41:24 -07:00
import { ColDef } from '@ag-grid-community/core';
2023-03-31 05:56:32 -07:00
import isElectron from 'is-electron';
2022-12-19 15:59:14 -08:00
import merge from 'lodash/merge';
2023-06-03 05:36:38 -07:00
import { generatePath } from 'react-router';
2023-05-20 20:00:09 -07:00
import { create } from 'zustand';
2022-12-19 15:59:14 -08:00
import { devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
2023-05-10 18:45:22 -07:00
import { shallow } from 'zustand/shallow';
2023-06-09 02:36:38 -07:00
import { LibraryItem, LyricSource } from '/@/renderer/api/types';
2023-06-03 00:39:33 -07:00
import { AppRoute } from '/@/renderer/router/routes';
2022-12-19 15:59:14 -08:00
import { AppTheme } from '/@/renderer/themes/types';
import {
2023-07-01 19:10:05 -07:00
TableColumn,
CrossfadeStyle,
Play,
PlaybackStyle,
PlaybackType,
TableType,
Platform,
2022-12-19 15:59:14 -08:00
} from '/@/renderer/types';
const utils = isElectron() ? window.electron.utils : null;
2023-06-03 00:39:33 -07:00
export type SidebarItemType = {
2023-07-01 19:10:05 -07:00
disabled: boolean;
id: string;
label: string;
route: AppRoute | string;
2023-06-03 00:39:33 -07:00
};
export const sidebarItems = [
2023-07-01 19:10:05 -07:00
{ disabled: true, id: 'Now Playing', label: 'Now Playing', route: AppRoute.NOW_PLAYING },
{
disabled: true,
id: 'Search',
label: 'Search',
route: generatePath(AppRoute.SEARCH, { itemType: LibraryItem.SONG }),
},
{ disabled: false, id: 'Home', label: 'Home', route: AppRoute.HOME },
{ disabled: false, id: 'Albums', label: 'Albums', route: AppRoute.LIBRARY_ALBUMS },
{ disabled: false, id: 'Tracks', label: 'Tracks', route: AppRoute.LIBRARY_SONGS },
{
disabled: false,
id: 'Artists',
label: 'Artists',
route: AppRoute.LIBRARY_ALBUM_ARTISTS,
},
{ disabled: false, id: 'Genres', label: 'Genres', route: AppRoute.LIBRARY_GENRES },
{ disabled: true, id: 'Folders', label: 'Folders', route: AppRoute.LIBRARY_FOLDERS },
{ disabled: true, id: 'Playlists', label: 'Playlists', route: AppRoute.PLAYLISTS },
{ disabled: true, id: 'Settings', label: 'Settings', route: AppRoute.SETTINGS },
2023-06-03 00:39:33 -07:00
];
2022-12-19 15:59:14 -08:00
export type PersistedTableColumn = {
2023-07-01 19:10:05 -07:00
column: TableColumn;
extraProps?: Partial<ColDef>;
width: number;
2022-12-19 15:59:14 -08:00
};
export type DataTableProps = {
2023-07-01 19:10:05 -07:00
autoFit: boolean;
columns: PersistedTableColumn[];
followCurrentSong?: boolean;
rowHeight: number;
2022-12-19 15:59:14 -08:00
};
export type SideQueueType = 'sideQueue' | 'sideDrawerQueue';
type MpvSettings = {
2023-07-01 19:10:05 -07:00
audioExclusiveMode: 'yes' | 'no';
audioFormat?: 's16' | 's32' | 'float';
audioSampleRateHz?: number;
gaplessAudio: 'yes' | 'no' | 'weak';
replayGainClip: boolean;
replayGainFallbackDB?: number;
replayGainMode: 'no' | 'track' | 'album';
replayGainPreampDB?: number;
};
export enum BindingActions {
2023-07-01 19:10:05 -07:00
GLOBAL_SEARCH = 'globalSearch',
LOCAL_SEARCH = 'localSearch',
MUTE = 'volumeMute',
NEXT = 'next',
PAUSE = 'pause',
PLAY = 'play',
PLAY_PAUSE = 'playPause',
PREVIOUS = 'previous',
SHUFFLE = 'toggleShuffle',
SKIP_BACKWARD = 'skipBackward',
SKIP_FORWARD = 'skipForward',
STOP = 'stop',
TOGGLE_FULLSCREEN_PLAYER = 'toggleFullscreenPlayer',
TOGGLE_QUEUE = 'toggleQueue',
TOGGLE_REPEAT = 'toggleRepeat',
VOLUME_DOWN = 'volumeDown',
VOLUME_UP = 'volumeUp',
ZOOM_IN = 'zoomIn',
ZOOM_OUT = 'zoomOut',
}
2022-12-19 15:59:14 -08:00
export interface SettingsState {
2023-07-01 19:10:05 -07:00
general: {
defaultFullPlaylist: boolean;
2023-07-01 19:10:05 -07:00
followSystemTheme: boolean;
fontContent: string;
playButtonBehavior: Play;
resume: boolean;
showQueueDrawerButton: boolean;
sideQueueType: SideQueueType;
sidebarItems: SidebarItemType[];
sidebarPlaylistList: boolean;
skipButtons: {
enabled: boolean;
skipBackwardSeconds: number;
skipForwardSeconds: number;
};
theme: AppTheme;
themeDark: AppTheme;
themeLight: AppTheme;
volumeWheelStep: number;
zoomFactor: number;
};
hotkeys: {
bindings: Record<
BindingActions,
{ allowGlobal: boolean; hotkey: string; isGlobal: boolean }
>;
globalMediaHotkeys: boolean;
2023-03-30 06:44:33 -07:00
};
2023-07-01 19:10:05 -07:00
lyrics: {
delayMs: number;
fetch: boolean;
follow: boolean;
sources: LyricSource[];
};
playback: {
audioDeviceId?: string | null;
crossfadeDuration: number;
crossfadeStyle: CrossfadeStyle;
mpvExtraParameters: string[];
mpvProperties: MpvSettings;
muted: boolean;
scrobble: {
enabled: boolean;
scrobbleAtDuration: number;
scrobbleAtPercentage: number;
};
style: PlaybackStyle;
type: PlaybackType;
};
tab: 'general' | 'playback' | 'window' | 'hotkeys' | string;
tables: {
fullScreen: DataTableProps;
nowPlaying: DataTableProps;
sideDrawerQueue: DataTableProps;
sideQueue: DataTableProps;
songs: DataTableProps;
};
window: {
disableAutoUpdate: boolean;
exitToTray: boolean;
minimizeToTray: boolean;
windowBarStyle: Platform;
2022-12-19 15:59:14 -08:00
};
}
export interface SettingsSlice extends SettingsState {
2023-07-01 19:10:05 -07:00
actions: {
reset: () => void;
setSettings: (data: Partial<SettingsState>) => void;
setSidebarItems: (items: SidebarItemType[]) => void;
};
2022-12-19 15:59:14 -08:00
}
// Determines the default/initial windowBarStyle value based on the current platform.
const getPlatformDefaultWindowBarStyle = (): Platform => {
2023-07-01 19:10:05 -07:00
return isElectron() ? (utils.isMacOS() ? Platform.MACOS : Platform.WINDOWS) : Platform.WEB;
};
const platformDefaultWindowBarStyle: Platform = getPlatformDefaultWindowBarStyle();
2023-03-31 05:56:32 -07:00
const initialState: SettingsState = {
2023-07-01 19:10:05 -07:00
general: {
2023-07-16 13:40:50 -07:00
defaultFullPlaylist: true,
2023-07-01 19:10:05 -07:00
followSystemTheme: false,
2023-07-18 17:38:41 -07:00
fontContent: 'Inter',
2023-07-01 19:10:05 -07:00
playButtonBehavior: Play.NOW,
resume: false,
showQueueDrawerButton: false,
sideQueueType: 'sideQueue',
sidebarItems,
sidebarPlaylistList: true,
skipButtons: {
enabled: false,
skipBackwardSeconds: 5,
skipForwardSeconds: 10,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
theme: AppTheme.DEFAULT_DARK,
themeDark: AppTheme.DEFAULT_DARK,
themeLight: AppTheme.DEFAULT_LIGHT,
volumeWheelStep: 5,
zoomFactor: 100,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
hotkeys: {
bindings: {
globalSearch: { allowGlobal: false, hotkey: 'mod+k', isGlobal: false },
localSearch: { allowGlobal: false, hotkey: 'mod+f', isGlobal: false },
next: { allowGlobal: true, hotkey: '', isGlobal: false },
pause: { allowGlobal: true, hotkey: '', isGlobal: false },
play: { allowGlobal: true, hotkey: '', isGlobal: false },
playPause: { allowGlobal: true, hotkey: '', isGlobal: false },
previous: { allowGlobal: true, hotkey: '', isGlobal: false },
skipBackward: { allowGlobal: true, hotkey: '', isGlobal: false },
skipForward: { allowGlobal: true, hotkey: '', isGlobal: false },
stop: { allowGlobal: true, hotkey: '', isGlobal: false },
toggleFullscreenPlayer: { allowGlobal: false, hotkey: '', isGlobal: false },
toggleQueue: { allowGlobal: false, hotkey: '', isGlobal: false },
toggleRepeat: { allowGlobal: true, hotkey: '', isGlobal: false },
toggleShuffle: { allowGlobal: true, hotkey: '', isGlobal: false },
volumeDown: { allowGlobal: true, hotkey: '', isGlobal: false },
volumeMute: { allowGlobal: true, hotkey: '', isGlobal: false },
volumeUp: { allowGlobal: true, hotkey: '', isGlobal: false },
zoomIn: { allowGlobal: true, hotkey: '', isGlobal: false },
zoomOut: { allowGlobal: true, hotkey: '', isGlobal: false },
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
globalMediaHotkeys: true,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
lyrics: {
delayMs: 0,
fetch: false,
follow: true,
sources: [],
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
playback: {
audioDeviceId: undefined,
crossfadeDuration: 5,
crossfadeStyle: CrossfadeStyle.EQUALPOWER,
mpvExtraParameters: [],
mpvProperties: {
audioExclusiveMode: 'no',
audioFormat: undefined,
audioSampleRateHz: 0,
gaplessAudio: 'weak',
replayGainClip: true,
replayGainFallbackDB: undefined,
replayGainMode: 'no',
replayGainPreampDB: 0,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
muted: false,
scrobble: {
enabled: true,
scrobbleAtDuration: 240,
scrobbleAtPercentage: 75,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
style: PlaybackStyle.GAPLESS,
type: PlaybackType.LOCAL,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
tab: 'general',
tables: {
fullScreen: {
autoFit: true,
columns: [
{
column: TableColumn.TITLE_COMBINED,
width: 500,
},
{
column: TableColumn.DURATION,
width: 100,
},
{
column: TableColumn.USER_FAVORITE,
width: 100,
},
],
followCurrentSong: true,
rowHeight: 60,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
nowPlaying: {
autoFit: true,
columns: [
{
column: TableColumn.ROW_INDEX,
width: 50,
},
{
column: TableColumn.TITLE,
width: 500,
},
{
column: TableColumn.DURATION,
width: 100,
},
{
column: TableColumn.ALBUM,
width: 100,
},
{
column: TableColumn.ALBUM_ARTIST,
width: 100,
},
{
column: TableColumn.GENRE,
width: 100,
},
{
column: TableColumn.YEAR,
width: 100,
},
],
followCurrentSong: true,
rowHeight: 30,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
sideDrawerQueue: {
autoFit: true,
columns: [
{
column: TableColumn.TITLE_COMBINED,
width: 500,
},
{
column: TableColumn.DURATION,
width: 100,
},
],
followCurrentSong: true,
rowHeight: 60,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
sideQueue: {
autoFit: true,
columns: [
{
column: TableColumn.ROW_INDEX,
width: 50,
},
{
column: TableColumn.TITLE_COMBINED,
width: 500,
},
{
column: TableColumn.DURATION,
width: 100,
},
],
followCurrentSong: true,
rowHeight: 60,
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
songs: {
autoFit: true,
columns: [
{
column: TableColumn.ROW_INDEX,
width: 50,
},
{
column: TableColumn.TITLE_COMBINED,
width: 500,
},
{
column: TableColumn.DURATION,
width: 100,
},
{
column: TableColumn.ALBUM,
width: 300,
},
{
column: TableColumn.ARTIST,
width: 100,
},
{
column: TableColumn.YEAR,
width: 100,
},
],
rowHeight: 60,
2023-03-31 05:56:32 -07:00
},
},
2023-07-01 19:10:05 -07:00
window: {
disableAutoUpdate: false,
exitToTray: false,
minimizeToTray: false,
windowBarStyle: platformDefaultWindowBarStyle,
},
2023-03-31 05:56:32 -07:00
};
2022-12-19 15:59:14 -08:00
export const useSettingsStore = create<SettingsSlice>()(
2023-07-01 19:10:05 -07:00
persist(
devtools(
immer((set, get) => ({
actions: {
reset: () => {
if (!isElectron()) {
set({
...initialState,
playback: {
...initialState.playback,
type: PlaybackType.WEB,
},
});
} else {
set(initialState);
}
},
setSettings: (data) => {
set({ ...get(), ...data });
},
setSidebarItems: (items: SidebarItemType[]) => {
set((state) => {
state.general.sidebarItems = items;
});
},
2023-03-31 05:56:32 -07:00
},
2023-07-01 19:10:05 -07:00
...initialState,
})),
{ name: 'store_settings' },
),
{
merge: (persistedState, currentState) => {
return merge(currentState, persistedState);
},
name: 'store_settings',
version: 6,
2022-12-19 15:59:14 -08:00
},
),
);
export const useSettingsStoreActions = () => useSettingsStore((state) => state.actions);
2023-03-30 06:44:33 -07:00
export const usePlaybackSettings = () => useSettingsStore((state) => state.playback, shallow);
2022-12-19 15:59:14 -08:00
export const useTableSettings = (type: TableType) =>
2023-07-01 19:10:05 -07:00
useSettingsStore((state) => state.tables[type]);
2022-12-19 15:59:14 -08:00
2023-03-28 23:59:51 -07:00
export const useGeneralSettings = () => useSettingsStore((state) => state.general, shallow);
2022-12-25 01:55:00 -08:00
2023-03-30 06:44:33 -07:00
export const usePlayerType = () => useSettingsStore((state) => state.playback.type, shallow);
export const usePlayButtonBehavior = () =>
2023-07-01 19:10:05 -07:00
useSettingsStore((state) => state.general.playButtonBehavior, shallow);
2023-03-30 06:44:33 -07:00
export const useWindowSettings = () => useSettingsStore((state) => state.window, shallow);
2023-03-31 07:26:10 -07:00
export const useHotkeySettings = () => useSettingsStore((state) => state.hotkeys, shallow);
export const useMpvSettings = () =>
2023-07-01 19:10:05 -07:00
useSettingsStore((state) => state.playback.mpvProperties, shallow);
export const useLyricsSettings = () => useSettingsStore((state) => state.lyrics, shallow);