Implement caching for tags list in TagsPage component

This commit is contained in:
2025-06-08 14:02:40 +08:00
parent 35b65cc810
commit 3c3f9e9d35

View File

@@ -5,14 +5,21 @@ import showToast from "../components/toast.ts";
import Loading from "../components/loading.tsx"; import Loading from "../components/loading.tsx";
import Badge from "../components/badge.tsx"; import Badge from "../components/badge.tsx";
import { useNavigate } from "react-router"; import { useNavigate } from "react-router";
import { useAppContext } from "../components/AppContext.tsx";
export default function TagsPage() { export default function TagsPage() {
const [tags, setTags] = useState<TagWithCount[] | null>(null); const [tags, setTags] = useState<TagWithCount[] | null>(null);
const context = useAppContext();
useEffect(() => { useEffect(() => {
const storageKey = "tags_list";
if (context.get(storageKey)) {
setTags(context.get(storageKey));
} else {
network.getAllTags().then((res) => { network.getAllTags().then((res) => {
if (res.success) { if (res.success) {
setTags(res.data!); setTags(res.data!);
context.set(storageKey, res.data!);
} else { } else {
showToast({ showToast({
type: "error", type: "error",
@@ -20,7 +27,8 @@ export default function TagsPage() {
}); });
} }
}); });
}, []); }
}, [context]);
const navigate = useNavigate(); const navigate = useNavigate();