attempt smart background load

This commit is contained in:
2025-02-09 23:34:08 +02:00
parent 4b30e27578
commit ad76b5e83c
2 changed files with 13 additions and 6 deletions

View File

@@ -36,7 +36,11 @@ export function Settings({ className }: SettingsProps) {
<Modal <Modal
centered centered
radius="lg" radius="lg"
title={<Title order={3}>Settings</Title>} title={
<Text size="xl" fw="700">
Settings
</Text>
}
size="lg" size="lg"
padding="lg" padding="lg"
opened={isOpen} opened={isOpen}
@@ -115,7 +119,7 @@ export function Settings({ className }: SettingsProps) {
color="red.9" color="red.9"
leftSection={<IconTrash size={16} />} leftSection={<IconTrash size={16} />}
onClick={() => { onClick={() => {
localStorage.removeItem('start-page-background'); localStorage.removeItem('vertex-backgrounds');
window.location.reload(); window.location.reload();
}} }}
> >

View File

@@ -6,13 +6,16 @@ import { BACKGROUND_FETCH_MINS } from '~/config';
import { Background } from '~/types'; import { Background } from '~/types';
export function useLoadBackgrounds() { export function useLoadBackgrounds() {
const { unsplashQuery } = useAtomValue(settingsAtom); const { unsplashQuery, useSavedBackgrounds } = useAtomValue(settingsAtom);
const [backgrounds, setBackgrounds] = useAtom(backgroundsAtom); const [backgrounds, setBackgrounds] = useAtom(backgroundsAtom);
useEffect(() => { useEffect(() => {
const shouldFetch = const shouldFetch =
backgrounds.backgrounds.length === 0 || useSavedBackgrounds === false &&
backgrounds.fetched_at < new Date().getTime() - 1000 * 60 * BACKGROUND_FETCH_MINS; unsplashQuery.length > 0 &&
(backgrounds.backgrounds.length === 0 ||
(backgrounds.fetched_at > 0 &&
backgrounds.fetched_at < new Date().getTime() - 1000 * 60 * BACKGROUND_FETCH_MINS));
if (shouldFetch) { if (shouldFetch) {
getRandomBackground(unsplashQuery).then((r) => getRandomBackground(unsplashQuery).then((r) =>
@@ -23,5 +26,5 @@ export function useLoadBackgrounds() {
); );
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, [backgrounds, useSavedBackgrounds, unsplashQuery]);
} }