From 52594ccc4c4449aee6296036d7b59cc37973a9af Mon Sep 17 00:00:00 2001 From: Ray-D-Song Date: Mon, 11 Nov 2024 10:41:14 +0800 Subject: [PATCH] hotfix: query tag in showcase page cause error --- .../web/src/components/card-edit-dialog.tsx | 4 +- .../web/src/components/edit-tag-dialog.tsx | 4 +- packages/web/src/components/page-card.tsx | 4 +- .../web/src/components/side-bar-tag-menu.tsx | 4 +- packages/web/src/index.tsx | 12 ++--- packages/web/src/pages/(layout)/_layout.tsx | 52 ++++++++++++------- packages/web/src/store/app.ts | 5 -- packages/web/src/store/tag.ts | 12 +++++ 8 files changed, 56 insertions(+), 41 deletions(-) create mode 100644 packages/web/src/store/tag.ts diff --git a/packages/web/src/components/card-edit-dialog.tsx b/packages/web/src/components/card-edit-dialog.tsx index 328e288..e948948 100644 --- a/packages/web/src/components/card-edit-dialog.tsx +++ b/packages/web/src/components/card-edit-dialog.tsx @@ -16,7 +16,7 @@ import AutoCompleteTagInput from '@web-archive/shared/components/auto-complete-t import LoadingWrapper from '~/components/loading-wrapper' import { getPageDetail, updatePage } from '~/data/page' import { getAllFolder } from '~/data/folder' -import AppContext from '~/store/app' +import TagContext from '~/store/tag' interface CardEditDialogProps { open: boolean @@ -67,7 +67,7 @@ function Comp({ open, onOpenChange, pageId }: CardEditDialogProps) { }, ) - const { tagCache, refreshTagCache } = useContext(AppContext) + const { tagCache, refreshTagCache } = useContext(TagContext) const selectTags = tagCache?.filter(tag => tag.pageIds.includes(pageId)) const handleTagChange = ({ bindTags, diff --git a/packages/web/src/components/edit-tag-dialog.tsx b/packages/web/src/components/edit-tag-dialog.tsx index bdc6c1c..2632656 100644 --- a/packages/web/src/components/edit-tag-dialog.tsx +++ b/packages/web/src/components/edit-tag-dialog.tsx @@ -6,7 +6,7 @@ import { useRequest } from 'ahooks' import { useContext, useEffect, useState } from 'react' import toast from 'react-hot-toast' import { updateTag } from '~/data/tag' -import AppContext from '~/store/app' +import TagContext from '~/store/tag' interface EditTagProps { afterSubmit: () => void @@ -19,7 +19,7 @@ interface EditTagProps { } function EditTagDialog({ afterSubmit, open, setOpen, editTag }: EditTagProps) { - const { tagCache } = useContext(AppContext) + const { tagCache } = useContext(TagContext) const [tagName, setTagName] = useState(editTag?.name ?? '') useEffect(() => { setTagName(editTag?.name ?? '') diff --git a/packages/web/src/components/page-card.tsx b/packages/web/src/components/page-card.tsx index 479e552..f0bffd9 100644 --- a/packages/web/src/components/page-card.tsx +++ b/packages/web/src/components/page-card.tsx @@ -12,12 +12,12 @@ import ScreenshotView from './screenshot-view' import { useNavigate } from '~/router' import { updatePageShowcase } from '~/data/page' import CardEditDialog from '~/components/card-edit-dialog' -import AppContext from '~/store/app' +import TagContext from '~/store/tag' function Comp({ page, onPageDelete }: { page: Page, onPageDelete?: (page: Page) => void }) { const navigate = useNavigate() - const { tagCache, refreshTagCache } = useContext(AppContext) + const { tagCache, refreshTagCache } = useContext(TagContext) const bindTags = tagCache?.filter(tag => tag.pageIds.includes(page.id)) ?? [] const tagBadgeList = bindTags.map((tag) => { return ({tag.name}) diff --git a/packages/web/src/components/side-bar-tag-menu.tsx b/packages/web/src/components/side-bar-tag-menu.tsx index c2e1dc5..c340792 100644 --- a/packages/web/src/components/side-bar-tag-menu.tsx +++ b/packages/web/src/components/side-bar-tag-menu.tsx @@ -10,7 +10,7 @@ import { useContext, useState } from 'react' import toast from 'react-hot-toast' import EditTagDialog from './edit-tag-dialog' import { deleteTag } from '~/data/tag' -import AppContext from '~/store/app' +import TagContext from '~/store/tag' interface SidebarTagMenuProps { selectedTag: number | null @@ -59,7 +59,7 @@ function TagBadge({ tag, isSelected, onClick, onDelete, onEdit }: TagBadgeProps) } function SidebarTagMenu({ selectedTag, setSelectedTag }: SidebarTagMenuProps) { - const { tagCache: tags, refreshTagCache } = useContext(AppContext) + const { tagCache: tags, refreshTagCache } = useContext(TagContext) const [isTagsCollapseOpen, setIsTagsCollapseOpen] = useState(false) const handleClickTag = (tagId: number) => { diff --git a/packages/web/src/index.tsx b/packages/web/src/index.tsx index e03e67a..07955b1 100644 --- a/packages/web/src/index.tsx +++ b/packages/web/src/index.tsx @@ -1,12 +1,11 @@ import { createRoot } from 'react-dom/client' import { RouterProvider } from 'react-router-dom' import '@web-archive/shared/global.css' -import { useLocalStorageState, useRequest } from 'ahooks' +import { useLocalStorageState } from 'ahooks' import { useMemo } from 'react' import { ThemeProvider } from '@web-archive/shared/components/theme-provider' import AppContext from './store/app' import router from './utils/router' -import { getAllTag } from './data/tag' function Routes() { const [view, setView] = useLocalStorageState('view', { @@ -15,20 +14,15 @@ function Routes() { const [readMode, setReadMode] = useLocalStorageState('readMode', { defaultValue: false, }) - const { - data: tagCache, - runAsync: refreshTagCache, - } = useRequest(getAllTag) + return ( ({ view: view as 'card' | 'list', setView, - tagCache, - refreshTagCache, readMode: readMode as boolean, setReadMode, - }), [view, setView, tagCache, refreshTagCache, readMode, setReadMode]) + }), [view, setView, readMode, setReadMode]) } > diff --git a/packages/web/src/pages/(layout)/_layout.tsx b/packages/web/src/pages/(layout)/_layout.tsx index a1e1bd9..6ba5b5f 100644 --- a/packages/web/src/pages/(layout)/_layout.tsx +++ b/packages/web/src/pages/(layout)/_layout.tsx @@ -1,9 +1,12 @@ import { Toaster } from 'react-hot-toast' import { Outlet } from 'react-router-dom' -import { useState } from 'react' +import { useMemo, useState } from 'react' import { SidebarProvider } from '@web-archive/shared/components/side-bar' +import { useRequest } from 'ahooks' import SideBar from '~/components/side-bar' import Hamburger from '~/components/hamburger' +import { getAllTag } from '~/data/tag' +import TagContext from '~/store/tag' function Layout() { const [keyword, setKeyword] = useState('') @@ -13,31 +16,42 @@ function Layout() { const handleSearch = () => { setSearchTrigger(prev => !prev) } - + const { + data: tagCache, + runAsync: refreshTagCache, + } = useRequest(getAllTag) const [selectedTag, setSelectedTag] = useState(null) const setSelectedTagAndReload = (tag: number | null) => { setSelectedTag(tag) handleSearch() } return ( -
- - -
- -
- - + ({ + tagCache: tagCache || [], + refreshTagCache, + }), [tagCache, refreshTagCache]) + } + > +
+ + +
+ +
+ + +
-
- -
+ + + ) } diff --git a/packages/web/src/store/app.ts b/packages/web/src/store/app.ts index 4e508d0..71c0e06 100644 --- a/packages/web/src/store/app.ts +++ b/packages/web/src/store/app.ts @@ -1,18 +1,13 @@ -import type { Tag } from '@web-archive/shared/types' import { createContext } from 'react' const AppContext = createContext<{ view: 'card' | 'list' setView: (view: 'card' | 'list') => void - tagCache?: Array - refreshTagCache: () => Promise> readMode: boolean setReadMode: (readMode: boolean) => void }>({ view: 'card', setView: () => { }, - tagCache: [], - refreshTagCache: async () => { return [] }, readMode: false, setReadMode: (value: boolean) => { }, }) diff --git a/packages/web/src/store/tag.ts b/packages/web/src/store/tag.ts new file mode 100644 index 0000000..9e90d09 --- /dev/null +++ b/packages/web/src/store/tag.ts @@ -0,0 +1,12 @@ +import type { Tag } from '@web-archive/shared/types' +import { createContext } from 'react' + +const TagContext = createContext<{ + tagCache: Tag[] + refreshTagCache: () => Promise +}>({ + tagCache: [], + refreshTagCache: async () => [], + }) + +export default TagContext