import { FC, Fragment, useEffect, useMemo, useState } from "react"; import Link from "next/link"; import { observer } from "mobx-react-lite"; import { CheckCircle, Pencil } from "lucide-react"; // hooks import { useView, useViewDetail } from "hooks/store"; import useToast from "hooks/use-toast"; // components import { ViewRoot, ViewCreateEditForm, ViewLayoutRoot, ViewFiltersDropdown, ViewDisplayFiltersDropdown, ViewAppliedFiltersRoot, ViewDuplicateConfirmationModal, ViewDeleteConfirmationModal, ViewEditDropdown, } from "."; // ui import { Spinner } from "@plane/ui"; // constants import { viewLocalPayload } from "constants/view"; // types import { TViewOperations } from "./types"; import { TView, TViewFilters, TViewDisplayFilters, TViewDisplayProperties, TViewTypes } from "@plane/types"; type TGlobalViewRoot = { workspaceSlug: string; projectId: string | undefined; viewId: string; viewType: TViewTypes; baseRoute: string; workspaceViewTabOptions: { key: TViewTypes; title: string; href: string }[]; }; type TViewOperationsToggle = { type: "CREATE" | "EDIT" | "DUPLICATE" | "DELETE" | undefined; viewId: string | undefined; }; export const GlobalViewRoot: FC = observer((props) => { const { workspaceSlug, projectId, viewId, viewType, baseRoute, workspaceViewTabOptions } = props; // hooks const viewStore = useView(workspaceSlug, projectId, viewType); const viewDetailStore = useViewDetail(workspaceSlug, projectId, viewId, viewType); const { setToastAlert } = useToast(); // states const [viewOperationsToggle, setViewOperationsToggle] = useState({ type: undefined, viewId: undefined, }); const handleViewOperationsToggle = (type: TViewOperationsToggle["type"], viewId: string | undefined) => setViewOperationsToggle({ type, viewId }); const viewDetailCreateStore = useViewDetail( workspaceSlug, projectId, viewOperationsToggle?.viewId || viewId, viewType ); const viewOperations: TViewOperations = useMemo( () => ({ setName: (name: string) => viewDetailStore?.setName(name), setDescription: (name: string) => viewDetailStore?.setDescription(name), setFilters: (filterKey: keyof TViewFilters | undefined, filterValue: "clear_all" | string) => { if (viewOperationsToggle.type && ["CREATE", "EDIT"].includes(viewOperationsToggle.type)) viewDetailCreateStore?.setFilters(filterKey, filterValue); else viewDetailStore?.setFilters(filterKey, filterValue); }, setDisplayFilters: (display_filters: Partial) => viewDetailStore?.setDisplayFilters(display_filters), setDisplayProperties: (displayPropertyKey: keyof TViewDisplayProperties) => viewDetailStore?.setDisplayProperties(displayPropertyKey), localViewCreateEdit: (viewId: string | undefined) => { if (viewId === undefined) { const viewPayload = viewLocalPayload; handleViewOperationsToggle("CREATE", viewPayload.id); viewStore?.localViewCreate(viewPayload as TView); } else handleViewOperationsToggle("EDIT", viewId); }, localViewCreateEditClear: async (viewId: string | undefined) => { if (viewId) viewStore?.remove(viewId); handleViewOperationsToggle(undefined, undefined); }, fetch: async () => await viewStore?.fetch(), create: async (data: Partial) => { try { await viewStore?.create(data); handleViewOperationsToggle(undefined, undefined); } catch { setToastAlert({ title: "Error", message: "Error creating view", type: "error" }); } }, remove: async (viewId: string) => { try { await viewStore?.remove(viewId); handleViewOperationsToggle(undefined, undefined); } catch { setToastAlert({ title: "Error", message: "Error removing view", type: "error" }); } }, update: async () => { try { await viewDetailStore?.saveChanges(); handleViewOperationsToggle(undefined, undefined); } catch { setToastAlert({ title: "Error", message: "Error updating view", type: "error" }); } }, }), [viewStore, viewDetailStore, setToastAlert, viewOperationsToggle, viewDetailCreateStore] ); // fetch all views useEffect(() => { const fetchViews = async () => { await viewStore?.fetch(viewStore?.viewIds.length > 0 ? "mutation-loader" : "init-loader"); }; if (workspaceSlug && viewType && viewStore) fetchViews(); }, [workspaceSlug, projectId, viewType, viewStore]); // fetch view by id useEffect(() => { const fetchViews = async () => { viewId && (await viewStore?.fetchById(viewId)); }; if (workspaceSlug && viewId && viewType && viewStore) fetchViews(); }, [workspaceSlug, projectId, viewId, viewType, viewStore]); return (
All Issues
{workspaceViewTabOptions.map((tab) => ( {tab.title} ))}
{viewStore?.loader && viewStore?.loader === "init-loader" ? (
) : ( <>
)} {/* create edit modal */} {viewOperationsToggle.type && viewOperationsToggle.viewId && ( {["CREATE", "EDIT"].includes(viewOperationsToggle.type) && ( )} {["DUPLICATE"].includes(viewOperationsToggle.type) && ( )} {["DELETE"].includes(viewOperationsToggle.type) && ( )} )}
); });