feat: add tag filter

This commit is contained in:
Steven 2023-07-09 11:36:26 +08:00
parent 0b659ba124
commit 9455824a2d
4 changed files with 82 additions and 6 deletions

View File

@ -0,0 +1,28 @@
import useFilterStore from "../stores/v1/filter";
import Icon from "./Icon";
const FilterView = () => {
const filterStore = useFilterStore();
const filter = filterStore.filter;
const shouldShowFilters = filter.tag !== undefined;
if (!shouldShowFilters) {
return <></>;
}
return (
<div className="w-full flex flex-row justify-start items-center mb-4 pl-2">
<span className="text-gray-400">Filters:</span>
{filter.tag && (
<button
className="ml-2 px-2 py-1 flex flex-row justify-center items-center bg-gray-200 rounded-full text-gray-500 text-sm hover:line-through"
onClick={() => filterStore.setFilter({ tag: undefined })}
>
<Icon.Tag className="w-4 h-auto mr-1" />#{filter.tag}
</button>
)}
</div>
);
};
export default FilterView;

View File

@ -5,6 +5,7 @@ import { useTranslation } from "react-i18next";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import { shortcutService } from "../services"; import { shortcutService } from "../services";
import useFaviconStore from "../stores/v1/favicon"; import useFaviconStore from "../stores/v1/favicon";
import useFilterStore from "../stores/v1/filter";
import useUserStore from "../stores/v1/user"; import useUserStore from "../stores/v1/user";
import { absolutifyLink } from "../helpers/utils"; import { absolutifyLink } from "../helpers/utils";
import { showCommonDialog } from "./Alert"; import { showCommonDialog } from "./Alert";
@ -22,6 +23,7 @@ const ShortcutView = (props: Props) => {
const { shortcut, handleEdit } = props; const { shortcut, handleEdit } = props;
const { t } = useTranslation(); const { t } = useTranslation();
const currentUser = useUserStore().getCurrentUser(); const currentUser = useUserStore().getCurrentUser();
const filterStore = useFilterStore();
const faviconStore = useFaviconStore(); const faviconStore = useFaviconStore();
const [favicon, setFavicon] = useState<string | undefined>(undefined); const [favicon, setFavicon] = useState<string | undefined>(undefined);
const [showQRCodeDialog, setShowQRCodeDialog] = useState<boolean>(false); const [showQRCodeDialog, setShowQRCodeDialog] = useState<boolean>(false);
@ -116,11 +118,15 @@ const ShortcutView = (props: Props) => {
</div> </div>
{shortcut.description && <p className="mt-1 text-gray-400 text-sm">{shortcut.description}</p>} {shortcut.description && <p className="mt-1 text-gray-400 text-sm">{shortcut.description}</p>}
{shortcut.tags.length > 0 && ( {shortcut.tags.length > 0 && (
<div className="mt-1 flex flex-row justify-start items-start gap-2"> <div className="mt-2 ml-1 flex flex-row justify-start items-start gap-2">
<Icon.Tag className="text-gray-400 w-4 h-auto" /> <Icon.Tag className="text-gray-400 w-4 h-auto" />
{shortcut.tags.map((tag) => { {shortcut.tags.map((tag) => {
return ( return (
<span key={tag} className="text-gray-400 text-sm font-mono leading-4"> <span
key={tag}
className="text-gray-400 text-sm font-mono leading-4 cursor-pointer hover:text-gray-600"
onClick={() => filterStore.setFilter({ tag: tag })}
>
#{tag} #{tag}
</span> </span>
); );

View File

@ -2,26 +2,45 @@ import { Button, Tab, TabList, Tabs } from "@mui/joy";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { shortcutService } from "../services"; import { shortcutService } from "../services";
import { useAppSelector } from "../stores"; import { useAppSelector } from "../stores";
import useFilterStore, { Filter } from "../stores/v1/filter";
import useUserStore from "../stores/v1/user"; import useUserStore from "../stores/v1/user";
import useLoading from "../hooks/useLoading"; import useLoading from "../hooks/useLoading";
import Icon from "../components/Icon"; import Icon from "../components/Icon";
import ShortcutListView from "../components/ShortcutListView"; import ShortcutListView from "../components/ShortcutListView";
import CreateShortcutDialog from "../components/CreateShortcutDialog"; import CreateShortcutDialog from "../components/CreateShortcutDialog";
import FilterView from "../components/FilterView";
interface State { interface State {
showCreateShortcutDialog: boolean; showCreateShortcutDialog: boolean;
} }
const getFilteredShortcutList = (shortcutList: Shortcut[], filter: Filter, currentUser: User) => {
const { tag, mineOnly } = filter;
const filteredShortcutList = shortcutList.filter((shortcut) => {
if (tag) {
if (!shortcut.tags.includes(tag)) {
return false;
}
}
if (mineOnly) {
if (shortcut.creatorId !== currentUser.id) {
return false;
}
}
return true;
});
return filteredShortcutList;
};
const Home: React.FC = () => { const Home: React.FC = () => {
const loadingState = useLoading(); const loadingState = useLoading();
const currentUser = useUserStore().getCurrentUser(); const currentUser = useUserStore().getCurrentUser();
const filterStore = useFilterStore();
const { shortcutList } = useAppSelector((state) => state.shortcut); const { shortcutList } = useAppSelector((state) => state.shortcut);
const [state, setState] = useState<State>({ const [state, setState] = useState<State>({
showCreateShortcutDialog: false, showCreateShortcutDialog: false,
}); });
const [selectedFilter, setSelectFilter] = useState<"ALL" | "PRIVATE">("ALL"); const filteredShortcutList = getFilteredShortcutList(shortcutList, filterStore.filter, currentUser);
const filteredShortcutList =
selectedFilter === "ALL" ? shortcutList : shortcutList.filter((shortcut) => shortcut.creatorId === currentUser.id);
useEffect(() => { useEffect(() => {
Promise.all([shortcutService.getMyAllShortcuts()]).finally(() => { Promise.all([shortcutService.getMyAllShortcuts()]).finally(() => {
@ -44,7 +63,7 @@ const Home: React.FC = () => {
</div> </div>
<div className="w-full flex flex-row justify-between items-center mb-4"> <div className="w-full flex flex-row justify-between items-center mb-4">
<div className="flex flex-row justify-start items-center"> <div className="flex flex-row justify-start items-center">
<Tabs defaultValue={"ALL"} size="sm" onChange={(_, value) => setSelectFilter(value as any)}> <Tabs defaultValue={"ALL"} size="sm" onChange={(_, value) => filterStore.setFilter({ mineOnly: value !== "ALL" })}>
<TabList> <TabList>
<Tab value={"ALL"}>All</Tab> <Tab value={"ALL"}>All</Tab>
<Tab value={"PRIVATE"}>Mine</Tab> <Tab value={"PRIVATE"}>Mine</Tab>
@ -57,6 +76,9 @@ const Home: React.FC = () => {
</Button> </Button>
</div> </div>
</div> </div>
<FilterView />
{loadingState.isLoading ? ( {loadingState.isLoading ? (
<div className="py-12 w-full flex flex-row justify-center items-center opacity-80"> <div className="py-12 w-full flex flex-row justify-center items-center opacity-80">
<Icon.Loader className="mr-2 w-5 h-auto animate-spin" /> <Icon.Loader className="mr-2 w-5 h-auto animate-spin" />

View File

@ -0,0 +1,20 @@
import { create } from "zustand";
export interface Filter {
tag?: string;
mineOnly?: boolean;
}
interface FilterState {
filter: Filter;
setFilter: (filter: Partial<Filter>) => void;
}
const useFilterStore = create<FilterState>()((set, get) => ({
filter: {},
setFilter: (filter: Partial<Filter>) => {
set({ filter: { ...get().filter, ...filter } });
},
}));
export default useFilterStore;