diff --git a/src/atoms.ts b/src/atoms.ts index 8a6d30d..30e669b 100644 --- a/src/atoms.ts +++ b/src/atoms.ts @@ -1,14 +1,28 @@ import { atom } from "jotai" import type { SectionID, SourceID } from "@shared/types" import { metadata, sourceList } from "@shared/data" -import { atomWithLocalStorage } from "./utils/atom" +import { atomWithLocalStorage } from "./hooks/atomWithLocalStorage" export const focusSourcesAtom = atomWithLocalStorage("focusSources", [], (stored) => { return stored.filter(item => item in sourceList) }) -const currentSectionIDAtom = atom("focus") +function initRefetchSource() { + let time = 0 + // useOnReload + // 没有放在 useOnReload 里面, 可以避免初始化后再修改 refetchSourceAtom,导致多次请求 API + const _ = localStorage.getItem("quitTime") + const now = Date.now() + const quitTime = _ ? Number(_) : 0 + if (!Number.isNaN(quitTime) && now - quitTime < 1000) { + time = now + } + return Object.fromEntries(Object.keys(sourceList).map(k => [k, time])) as Record +} +export const refetchSourceAtom = atom(initRefetchSource()) + +const currentSectionIDAtom = atom("focus") export const currentSectionAtom = atom((get) => { const id = get(currentSectionIDAtom) if (id === "focus") { diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 6d9cff8..6d64242 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -1,11 +1,9 @@ import { Link } from "@tanstack/react-router" import { useCallback } from "react" -import { useAtomValue } from "jotai" -import type { SourceID } from "@shared/types" -import { queryClient } from "~/main" +import { useAtomValue, useSetAtom } from "jotai" import logo from "~/assets/react.svg" import { useDark } from "~/hooks/useDark" -import { currentSectionAtom } from "~/atoms" +import { currentSectionAtom, refetchSourceAtom } from "~/atoms" function ThemeToggle() { const { toggleDark } = useDark() @@ -21,13 +19,14 @@ function ThemeToggle() { function RefreshButton() { const currentSection = useAtomValue(currentSectionAtom) - const refreshAll = useCallback(async () => { - await queryClient.refetchQueries({ - predicate(query) { - return currentSection.sourceList.includes(query.queryKey[0] as SourceID) - }, - }) - }, [currentSection]) + const setRefetchSource = useSetAtom(refetchSourceAtom) + const refreshAll = useCallback(() => { + const obj = Object.fromEntries(currentSection.sourceList.map(id => [id, Date.now()])) + setRefetchSource(prev => ({ + ...prev, + ...obj, + })) + }, [currentSection, setRefetchSource]) return (