feishin/src/renderer/features/settings/components/general/sidebar-settings.tsx

164 lines
5.3 KiB
TypeScript
Raw Normal View History

2023-06-14 00:45:10 -07:00
import { ChangeEvent, useCallback, useState } from 'react';
2023-06-03 00:39:33 -07:00
import { Group } from '@mantine/core';
import { Reorder, useDragControls } from 'framer-motion';
import isEqual from 'lodash/isEqual';
import { useTranslation } from 'react-i18next';
2023-06-03 00:39:33 -07:00
import { MdDragIndicator } from 'react-icons/md';
import { Button, Checkbox, Switch } from '/@/renderer/components';
import { useSettingsStoreActions, useGeneralSettings } from '../../../../store/settings.store';
import { SettingsOptions } from '/@/renderer/features/settings/components/settings-option';
const DragHandle = ({ dragControls }: any) => {
2023-07-01 19:10:05 -07:00
return (
<MdDragIndicator
color="white"
style={{ cursor: 'grab' }}
onPointerDown={(event) => dragControls.start(event)}
/>
);
2023-06-03 00:39:33 -07:00
};
interface SidebarItem {
2023-07-01 19:10:05 -07:00
disabled: boolean;
id: string;
2023-06-03 00:39:33 -07:00
}
interface DraggableSidebarItemProps {
2023-07-01 19:10:05 -07:00
handleChangeDisabled: (id: string, e: boolean) => void;
item: SidebarItem;
2023-06-03 00:39:33 -07:00
}
const DraggableSidebarItem = ({ item, handleChangeDisabled }: DraggableSidebarItemProps) => {
2023-07-01 19:10:05 -07:00
const dragControls = useDragControls();
2023-06-03 00:39:33 -07:00
2023-07-01 19:10:05 -07:00
return (
<Reorder.Item
as="div"
dragControls={dragControls}
dragListener={false}
value={item}
>
<Group
noWrap
h="3rem"
style={{ boxShadow: '0 1px 3px rgba(0,0,0,.1)' }}
>
<Checkbox
checked={!item.disabled}
onChange={(e) => handleChangeDisabled(item.id, e.target.checked)}
/>
<DragHandle dragControls={dragControls} />
{item.id}
</Group>
</Reorder.Item>
);
2023-06-03 00:39:33 -07:00
};
export const SidebarSettings = () => {
const { t } = useTranslation();
2023-07-01 19:10:05 -07:00
const settings = useGeneralSettings();
const { setSidebarItems, setSettings } = useSettingsStoreActions();
2023-06-03 00:39:33 -07:00
2023-07-01 19:10:05 -07:00
const [localSidebarItems, setLocalSidebarItems] = useState(settings.sidebarItems);
2023-06-03 00:39:33 -07:00
2023-07-01 19:10:05 -07:00
const handleSave = () => {
setSidebarItems(localSidebarItems);
};
2023-06-03 00:39:33 -07:00
2023-07-01 19:10:05 -07:00
const handleChangeDisabled = useCallback((id: string, e: boolean) => {
setLocalSidebarItems((items) =>
items.map((item) => {
if (item.id === id) {
return {
...item,
disabled: !e,
};
}
2023-06-03 00:39:33 -07:00
2023-07-01 19:10:05 -07:00
return item;
}),
);
}, []);
2023-06-03 00:39:33 -07:00
2023-07-01 19:10:05 -07:00
const handleSetSidebarPlaylistList = (e: ChangeEvent<HTMLInputElement>) => {
setSettings({
general: {
...settings,
sidebarPlaylistList: e.target.checked,
},
});
};
2023-06-14 00:45:10 -07:00
const handleSetSidebarCollapsedNavigation = (e: ChangeEvent<HTMLInputElement>) => {
setSettings({
general: {
...settings,
sidebarCollapsedNavigation: e.target.checked,
},
});
};
2023-07-01 19:10:05 -07:00
const isSaveButtonDisabled = isEqual(settings.sidebarItems, localSidebarItems);
2023-06-03 00:39:33 -07:00
2023-07-01 19:10:05 -07:00
return (
<>
<SettingsOptions
control={
<Switch
checked={settings.sidebarPlaylistList}
onChange={handleSetSidebarPlaylistList}
/>
}
description={t('setting.sidebarPlaylistList', {
context: 'description',
postProcess: 'sentenceCase',
})}
title={t('setting.sidebarPlaylistList', { postProcess: 'sentenceCase' })}
2023-07-01 19:10:05 -07:00
/>
<SettingsOptions
control={
<Switch
checked={settings.sidebarCollapsedNavigation}
onChange={handleSetSidebarCollapsedNavigation}
/>
}
description={t('setting.sidebarPlaylistList', {
context: 'description',
postProcess: 'sentenceCase',
})}
title={t('setting.sidebarCollapsedNavigation', { postProcess: 'sentenceCase' })}
/>
2023-07-01 19:10:05 -07:00
<SettingsOptions
control={
<Button
compact
disabled={isSaveButtonDisabled}
variant="filled"
onClick={handleSave}
>
{t('common.save', { postProcess: 'titleCase' })}
2023-07-01 19:10:05 -07:00
</Button>
}
description={t('setting.sidebarCollapsedNavigation', {
context: 'description',
postProcess: 'sentenceCase',
})}
title={t('setting.sidebarConfiguration', { postProcess: 'sentenceCase' })}
2023-07-01 19:10:05 -07:00
/>
<Reorder.Group
axis="y"
values={localSidebarItems}
onReorder={setLocalSidebarItems}
>
{localSidebarItems.map((item) => (
<DraggableSidebarItem
key={item.id}
handleChangeDisabled={handleChangeDisabled}
item={item}
/>
))}
</Reorder.Group>
</>
);
2023-06-03 00:39:33 -07:00
};