import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // hooks import useMyIssuesFilters from "hooks/my-issues/use-my-issues-filter"; import useToast from "hooks/use-toast"; // components import { FiltersList } from "components/core"; import { PrimaryButton } from "components/ui"; import { CreateUpdateViewModal } from "components/views"; // icon import { PlusIcon } from "lucide-react"; // constant import { WORKSPACE_VIEWS_LIST } from "constants/fetch-keys"; // service import workspaceService from "services/workspace.service"; // type import { ICurrentUserResponse, IIssueFilterOptions } from "types"; type Props = { user: ICurrentUserResponse | undefined; }; export const WorkspaceViewsNavigation: React.FC = ({ user }) => { const [createViewModal, setCreateViewModal] = useState(null); const router = useRouter(); const { workspaceSlug, workspaceViewId } = router.query; const { setToastAlert } = useToast(); const { filters, setFilters } = useMyIssuesFilters(workspaceSlug?.toString()); const { data: workspaceViews } = useSWR( workspaceSlug ? WORKSPACE_VIEWS_LIST(workspaceSlug.toString()) : null, workspaceSlug ? () => workspaceService.getAllViews(workspaceSlug.toString()) : null ); const isSelected = (pathName: string) => router.pathname.includes(pathName); const tabsList = [ { key: "all", label: "All Issues", selected: isSelected("workspace-views/all-issues"), onClick: () => router.push(`/${workspaceSlug}/workspace-views/all-issues`), }, { key: "assigned", label: "Assigned", selected: isSelected("workspace-views/assigned"), onClick: () => router.push(`/${workspaceSlug}/workspace-views/assigned`), }, { key: "created", label: "Created", selected: isSelected("workspace-views/created"), onClick: () => router.push(`/${workspaceSlug}/workspace-views/created`), }, { key: "subscribed", label: "Subscribed", selected: isSelected("workspace-views/subscribed"), onClick: () => router.push(`/${workspaceSlug}/workspace-views/subscribed`), }, ]; const nullFilters = Object.keys(filters).filter( (key) => filters[key as keyof IIssueFilterOptions] === null ); const areFiltersApplied = Object.keys(filters).length > 0 && nullFilters.length !== Object.keys(filters).length; return ( <> setCreateViewModal(null)} viewType="workspace" preLoadedData={createViewModal} user={user} />
{tabsList.map((tab) => ( ))} {workspaceViews && workspaceViews.length > 0 && workspaceViews?.map((view) => ( ))}
{areFiltersApplied && ( <>
setFilters(updatedFilter)} labels={[]} members={[]} states={[]} clearAllFilters={() => setFilters({ assignees: null, created_by: null, labels: null, priority: null, state: null, start_date: null, target_date: null, }) } /> { if (workspaceViewId) { setFilters({}); setToastAlert({ title: "View updated", message: "Your view has been updated", type: "success", }); } else setCreateViewModal({ query: filters, }); }} className="flex items-center gap-2 text-sm" > {!workspaceViewId && } {workspaceViewId ? "Update" : "Save"} view
{
} )} ); };