Add option to remove backgrounds from favorites

This commit is contained in:
Georgi Gardev
2023-11-18 19:44:43 +02:00
parent f4e029c4af
commit 1639581d65
4 changed files with 60 additions and 26 deletions
+11 -5
View File
@@ -5,7 +5,7 @@ import { useSettings } from '~/state/SettingsProvider';
import { Settings } from '../widgets/Settings'; import { Settings } from '../widgets/Settings';
import style from './Sidebar.module.css'; import style from './Sidebar.module.css';
import { HiSolidStar } from 'solid-icons/hi'; import { HiOutlineStar, HiSolidStar } from 'solid-icons/hi';
type SidebarProps = { type SidebarProps = {
activeTag?: string | null; activeTag?: string | null;
@@ -14,7 +14,7 @@ type SidebarProps = {
export function Sidebar(props: SidebarProps) { export function Sidebar(props: SidebarProps) {
const tagOrder = createTagOrder(); const tagOrder = createTagOrder();
const { onSaveBackground } = useSettings(); const { onSaveBackground, onUnsaveBackground, isBackgroundSaved } = useSettings();
return ( return (
<div class={style.Sidebar}> <div class={style.Sidebar}>
@@ -30,9 +30,15 @@ export function Sidebar(props: SidebarProps) {
))} ))}
</ul> </ul>
<div class={style.SettingsWrapper}> <div class={style.SettingsWrapper}>
<Tooltip withArrow placement="top" label="Save background"> {isBackgroundSaved() ? (
<HiSolidStar size={18} onClick={onSaveBackground} /> <Tooltip withArrow placement="top" label="Remove from favorites">
</Tooltip> <HiOutlineStar size={18} onClick={onUnsaveBackground} />
</Tooltip>
) : (
<Tooltip withArrow placement="top" label="Add to favorites">
<HiSolidStar size={18} onClick={onSaveBackground} />
</Tooltip>
)}
<Tooltip withArrow placement="top" label="Settings"> <Tooltip withArrow placement="top" label="Settings">
<Settings /> <Settings />
</Tooltip> </Tooltip>
+2 -2
View File
@@ -50,7 +50,7 @@ export function Settings(props: SettingsProps) {
})) }))
} }
> >
Use saved backgrounds Use favorited backgrounds
</Checkbox> </Checkbox>
<a <a
style={{ display: 'flex', 'align-items': 'center', gap: '8px', cursor: 'pointer' }} style={{ display: 'flex', 'align-items': 'center', gap: '8px', cursor: 'pointer' }}
@@ -60,7 +60,7 @@ export function Settings(props: SettingsProps) {
}} }}
> >
<HiOutlineTrash size={16} /> <HiOutlineTrash size={16} />
Reload background Reload backgrounds
</a> </a>
</Flex> </Flex>
</ModalBody> </ModalBody>
+18 -16
View File
@@ -5,7 +5,6 @@ import { sample } from '~/lib/array';
import { createLocalStore } from '~/lib/create-local-store'; import { createLocalStore } from '~/lib/create-local-store';
import { SavedBackgrounds } from '~/types'; import { SavedBackgrounds } from '~/types';
import { createBackgrounds } from './create-backgrounds'; import { createBackgrounds } from './create-backgrounds';
import { random } from '~/lib/math';
type Settings = { showLocations: boolean; viewAsTable: boolean; useSavedBackgrounds: boolean }; type Settings = { showLocations: boolean; viewAsTable: boolean; useSavedBackgrounds: boolean };
@@ -14,7 +13,9 @@ type SettingsContextType = {
setAppSettings: SetStoreFunction<Settings>; setAppSettings: SetStoreFunction<Settings>;
savedBackgrounds: SavedBackgrounds; savedBackgrounds: SavedBackgrounds;
onSaveBackground(): void; onSaveBackground(): void;
onUnsaveBackground(): void;
currentBackground: Accessor<Random | undefined>; currentBackground: Accessor<Random | undefined>;
isBackgroundSaved: Accessor<boolean>;
}; };
const SettingsContext = createContext<SettingsContextType>({ const SettingsContext = createContext<SettingsContextType>({
@@ -23,6 +24,8 @@ const SettingsContext = createContext<SettingsContextType>({
currentBackground: () => undefined, currentBackground: () => undefined,
savedBackgrounds: [], savedBackgrounds: [],
onSaveBackground: () => {}, onSaveBackground: () => {},
onUnsaveBackground: () => {},
isBackgroundSaved: () => false,
}); });
export function SettingsProvider(props: { children: JSX.Element }) { export function SettingsProvider(props: { children: JSX.Element }) {
@@ -32,32 +35,31 @@ export function SettingsProvider(props: { children: JSX.Element }) {
useSavedBackgrounds: false, useSavedBackgrounds: false,
}); });
const [savedBackgrounds, setSavedBackgrounds] = createLocalStore<SavedBackgrounds>( const {
'start-page-saved-backgrounds', currentRandomBackground,
[] onSaveBackground,
); savedBackgrounds,
isBackgroundSaved,
const { currentBackground: currentRandomBackground } = createBackgrounds(); onUnsaveBackground,
} = createBackgrounds();
const onSaveBackground = () => {
setSavedBackgrounds((prev) => {
if (prev.find((background) => background.id === currentRandomBackground()?.id)) {
return prev;
}
return [...prev, currentRandomBackground()!];
});
};
const currentBackground = createMemo(() => const currentBackground = createMemo(() =>
appSettings.useSavedBackgrounds ? sample(savedBackgrounds) : currentRandomBackground() appSettings.useSavedBackgrounds ? sample(savedBackgrounds) : currentRandomBackground()
); );
const isCurrentBackgroundSaved = createMemo(() => isBackgroundSaved(currentBackground()?.id));
const onUnsaveCurrentBackground = () =>
currentBackground() && onUnsaveBackground(currentBackground()!.id);
return ( return (
<SettingsContext.Provider <SettingsContext.Provider
value={{ value={{
currentBackground, currentBackground,
savedBackgrounds, savedBackgrounds,
onSaveBackground, onSaveBackground,
onUnsaveBackground: onUnsaveCurrentBackground,
isBackgroundSaved: isCurrentBackgroundSaved,
appSettings, appSettings,
setAppSettings, setAppSettings,
}} }}
+29 -3
View File
@@ -5,7 +5,7 @@ import { BACKGROUND_FETCH_MINS } from '~/config';
import { sample } from '~/lib/array'; import { sample } from '~/lib/array';
import { createLocalStore } from '~/lib/create-local-store'; import { createLocalStore } from '~/lib/create-local-store';
import { random } from '~/lib/math'; import { random } from '~/lib/math';
import { BackgroundStorage } from '~/types'; import { BackgroundStorage, SavedBackgrounds } from '~/types';
export function createBackgrounds() { export function createBackgrounds() {
const [backgrounds, setBackgrounds] = createLocalStore<BackgroundStorage>( const [backgrounds, setBackgrounds] = createLocalStore<BackgroundStorage>(
@@ -13,6 +13,11 @@ export function createBackgrounds() {
{ fetched_at: 0, backgrounds: null } { fetched_at: 0, backgrounds: null }
); );
const [savedBackgrounds, setSavedBackgrounds] = createLocalStore<SavedBackgrounds>(
'start-page-saved-backgrounds',
[]
);
const shouldFetch = const shouldFetch =
!backgrounds?.backgrounds || !backgrounds?.backgrounds ||
backgrounds.fetched_at < new Date().getTime() - 1000 * 60 * BACKGROUND_FETCH_MINS; backgrounds.fetched_at < new Date().getTime() - 1000 * 60 * BACKGROUND_FETCH_MINS;
@@ -26,9 +31,30 @@ export function createBackgrounds() {
); );
} }
const currentBackground = createMemo<Random | undefined>(() => const currentRandomBackground = createMemo<Random | undefined>(() =>
sample(backgrounds?.backgrounds ?? []) sample(backgrounds?.backgrounds ?? [])
); );
return { backgrounds, currentBackground }; const isBackgroundSaved = (id?: string) =>
Boolean(id && savedBackgrounds.find((b) => b.id === id));
const onUnsaveBackground = (id: string) =>
setSavedBackgrounds((prev) => prev.filter((b) => b.id !== id));
const onSaveBackground = () => {
setSavedBackgrounds((prev) => {
if (prev.find((background) => background.id === currentRandomBackground()?.id)) {
return prev;
}
return [...prev, currentRandomBackground()!];
});
};
return {
currentRandomBackground,
onSaveBackground,
savedBackgrounds,
isBackgroundSaved,
onUnsaveBackground,
};
} }