From 953ec3dbc0d015d65cc24f2525354aed2c9c1d24 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 27 Jul 2023 20:59:21 +0800 Subject: [PATCH] feat: implement navigator --- web/src/components/Navigator.tsx | 49 +++++++++++++++++++++++++++++ web/src/components/ShortcutView.tsx | 3 +- web/src/pages/Home.tsx | 46 ++++++++++----------------- web/src/stores/v1/view.ts | 17 +++++----- 4 files changed, 76 insertions(+), 39 deletions(-) create mode 100644 web/src/components/Navigator.tsx diff --git a/web/src/components/Navigator.tsx b/web/src/components/Navigator.tsx new file mode 100644 index 0000000..c5a85a4 --- /dev/null +++ b/web/src/components/Navigator.tsx @@ -0,0 +1,49 @@ +import { uniq } from "lodash-es"; +import { Button } from "@mui/joy"; +import { useAppSelector } from "../stores"; +import useViewStore from "../stores/v1/view"; +import Icon from "./Icon"; + +const Navigator = () => { + const viewStore = useViewStore(); + const { shortcutList } = useAppSelector((state) => state.shortcut); + const tags = uniq(shortcutList.map((shortcut) => shortcut.tags).flat()); + const currentTab = viewStore.filter.tab || `tab:all`; + + return ( +
+ + + {tags.map((tag) => ( + + ))} +
+ ); +}; + +export default Navigator; diff --git a/web/src/components/ShortcutView.tsx b/web/src/components/ShortcutView.tsx index 5794629..2af139a 100644 --- a/web/src/components/ShortcutView.tsx +++ b/web/src/components/ShortcutView.tsx @@ -130,8 +130,7 @@ const ShortcutView = (props: Props) => { {shortcut.description &&

{shortcut.description}

} {shortcut.tags.length > 0 && ( -
- +
{shortcut.tags.map((tag) => { return ( { const [state, setState] = useState({ showCreateShortcutDialog: false, }); - const [selectedTab, setSelectedTab] = useState("ALL"); - const tags = shortcutList.map((shortcut) => shortcut.tags).flat(); const filter = viewStore.filter; const filteredShortcutList = getFilteredShortcutList(shortcutList, filter, currentUser); const orderedShortcutList = getOrderedShortcutList(filteredShortcutList, viewStore.order); @@ -45,44 +44,31 @@ const Home: React.FC = () => { return ( <>
-
- Shortcuts - } - endDecorator={ - filter.search && viewStore.setFilter({ search: "" })} /> - } - value={filter.search} - onChange={(e) => viewStore.setFilter({ search: e.target.value })} - /> -
+
+ Shortcuts
- viewStore.setFilter({ mineOnly: value !== "ALL" })} - > - - All - My Own - - + placeholder="Search" + startDecorator={} + endDecorator={ + filter.search && viewStore.setFilter({ search: "" })} /> + } + value={filter.search} + onChange={(e) => viewStore.setFilter({ search: e.target.value })} + />
- - {loadingState.isLoading ? (
diff --git a/web/src/stores/v1/view.ts b/web/src/stores/v1/view.ts index 9a97319..e2ccbca 100644 --- a/web/src/stores/v1/view.ts +++ b/web/src/stores/v1/view.ts @@ -2,8 +2,8 @@ import { create } from "zustand"; import { persist } from "zustand/middleware"; export interface Filter { + tab?: string; tag?: string; - mineOnly?: boolean; visibility?: Visibility; search?: string; } @@ -49,18 +49,13 @@ const useViewStore = create()( ); export const getFilteredShortcutList = (shortcutList: Shortcut[], filter: Filter, currentUser: User) => { - const { tag, mineOnly, visibility, search } = filter; + const { tab, tag, visibility, search } = filter; const filteredShortcutList = shortcutList.filter((shortcut) => { if (tag) { if (!shortcut.tags.includes(tag)) { return false; } } - if (mineOnly) { - if (shortcut.creatorId !== currentUser.id) { - return false; - } - } if (visibility) { if (shortcut.visibility !== visibility) { return false; @@ -76,6 +71,14 @@ export const getFilteredShortcutList = (shortcutList: Shortcut[], filter: Filter return false; } } + if (tab) { + if (tab === "tab:mine") { + return shortcut.creatorId === currentUser.id; + } else if (tab.startsWith("tag:")) { + const tag = tab.split(":")[1]; + return shortcut.tags.includes(tag); + } + } return true; }); return filteredShortcutList;