From 1639581d6569281746ec4637e504e29476057314 Mon Sep 17 00:00:00 2001 From: Georgi Gardev Date: Sat, 18 Nov 2023 19:44:43 +0200 Subject: [PATCH] Add option to remove backgrounds from favorites --- src/components/ui/Sidebar.tsx | 16 +++++++++----- src/components/widgets/Settings.tsx | 4 ++-- src/state/SettingsProvider.tsx | 34 +++++++++++++++-------------- src/state/create-backgrounds.ts | 32 ++++++++++++++++++++++++--- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/src/components/ui/Sidebar.tsx b/src/components/ui/Sidebar.tsx index 621803f..546ca54 100644 --- a/src/components/ui/Sidebar.tsx +++ b/src/components/ui/Sidebar.tsx @@ -5,7 +5,7 @@ import { useSettings } from '~/state/SettingsProvider'; import { Settings } from '../widgets/Settings'; import style from './Sidebar.module.css'; -import { HiSolidStar } from 'solid-icons/hi'; +import { HiOutlineStar, HiSolidStar } from 'solid-icons/hi'; type SidebarProps = { activeTag?: string | null; @@ -14,7 +14,7 @@ type SidebarProps = { export function Sidebar(props: SidebarProps) { const tagOrder = createTagOrder(); - const { onSaveBackground } = useSettings(); + const { onSaveBackground, onUnsaveBackground, isBackgroundSaved } = useSettings(); return (
@@ -30,9 +30,15 @@ export function Sidebar(props: SidebarProps) { ))}
- - - + {isBackgroundSaved() ? ( + + + + ) : ( + + + + )} diff --git a/src/components/widgets/Settings.tsx b/src/components/widgets/Settings.tsx index 4bcd93a..0b5980a 100644 --- a/src/components/widgets/Settings.tsx +++ b/src/components/widgets/Settings.tsx @@ -50,7 +50,7 @@ export function Settings(props: SettingsProps) { })) } > - Use saved backgrounds + Use favorited backgrounds - Reload background + Reload backgrounds diff --git a/src/state/SettingsProvider.tsx b/src/state/SettingsProvider.tsx index c63fe22..4227037 100644 --- a/src/state/SettingsProvider.tsx +++ b/src/state/SettingsProvider.tsx @@ -5,7 +5,6 @@ import { sample } from '~/lib/array'; import { createLocalStore } from '~/lib/create-local-store'; import { SavedBackgrounds } from '~/types'; import { createBackgrounds } from './create-backgrounds'; -import { random } from '~/lib/math'; type Settings = { showLocations: boolean; viewAsTable: boolean; useSavedBackgrounds: boolean }; @@ -14,7 +13,9 @@ type SettingsContextType = { setAppSettings: SetStoreFunction; savedBackgrounds: SavedBackgrounds; onSaveBackground(): void; + onUnsaveBackground(): void; currentBackground: Accessor; + isBackgroundSaved: Accessor; }; const SettingsContext = createContext({ @@ -23,6 +24,8 @@ const SettingsContext = createContext({ currentBackground: () => undefined, savedBackgrounds: [], onSaveBackground: () => {}, + onUnsaveBackground: () => {}, + isBackgroundSaved: () => false, }); export function SettingsProvider(props: { children: JSX.Element }) { @@ -32,32 +35,31 @@ export function SettingsProvider(props: { children: JSX.Element }) { useSavedBackgrounds: false, }); - const [savedBackgrounds, setSavedBackgrounds] = createLocalStore( - 'start-page-saved-backgrounds', - [] - ); - - const { currentBackground: currentRandomBackground } = createBackgrounds(); - - const onSaveBackground = () => { - setSavedBackgrounds((prev) => { - if (prev.find((background) => background.id === currentRandomBackground()?.id)) { - return prev; - } - return [...prev, currentRandomBackground()!]; - }); - }; + const { + currentRandomBackground, + onSaveBackground, + savedBackgrounds, + isBackgroundSaved, + onUnsaveBackground, + } = createBackgrounds(); const currentBackground = createMemo(() => appSettings.useSavedBackgrounds ? sample(savedBackgrounds) : currentRandomBackground() ); + const isCurrentBackgroundSaved = createMemo(() => isBackgroundSaved(currentBackground()?.id)); + + const onUnsaveCurrentBackground = () => + currentBackground() && onUnsaveBackground(currentBackground()!.id); + return ( ( @@ -13,6 +13,11 @@ export function createBackgrounds() { { fetched_at: 0, backgrounds: null } ); + const [savedBackgrounds, setSavedBackgrounds] = createLocalStore( + 'start-page-saved-backgrounds', + [] + ); + const shouldFetch = !backgrounds?.backgrounds || backgrounds.fetched_at < new Date().getTime() - 1000 * 60 * BACKGROUND_FETCH_MINS; @@ -26,9 +31,30 @@ export function createBackgrounds() { ); } - const currentBackground = createMemo(() => + const currentRandomBackground = createMemo(() => 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, + }; }