import { FC, Fragment, useEffect, useMemo, useState } from "react"; import Link from "next/link"; import { observer } from "mobx-react-lite"; import { CheckCircle } from "lucide-react"; import { v4 as uuidV4 } from "uuid"; import cloneDeep from "lodash/cloneDeep"; // hooks import { useView, useViewDetail } from "hooks/store"; import useToast from "hooks/use-toast"; // components import { ViewRoot, ViewCreateEditForm, ViewEditDropdown, ViewLayoutRoot, ViewFiltersDropdown, ViewFiltersEditDropdown, ViewDisplayFiltersDropdown, ViewAppliedFiltersRoot, ViewDuplicateConfirmationModal, ViewDeleteConfirmationModal, } from "."; // ui import { Spinner } from "@plane/ui"; // constants import { EViewPageType, viewLocalPayload } from "constants/view"; // types import { TViewOperations } from "./types"; import { TView, TViewTypes } from "@plane/types"; type TGlobalViewRoot = { workspaceSlug: string; projectId: string | undefined; viewId: string; viewType: TViewTypes; viewPageType: EViewPageType; 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, viewPageType, 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 viewDetailCreateEditStore = useViewDetail( workspaceSlug, projectId, viewOperationsToggle?.viewId || viewId, viewType ); const viewOperations: TViewOperations = useMemo( () => ({ localViewCreateEdit: (viewId: string | undefined, currentView = undefined) => { if (viewId === undefined) { if (currentView !== undefined) { // creating new view const currentViewPayload = cloneDeep({ ...currentView, id: uuidV4() }); handleViewOperationsToggle("CREATE", currentViewPayload.id); viewStore?.localViewCreate(currentViewPayload as TView); } else { // if current view is available, create a new view with the same data const viewPayload = viewLocalPayload; handleViewOperationsToggle("CREATE", viewPayload.id); viewStore?.localViewCreate(viewPayload as TView); } } else { handleViewOperationsToggle("EDIT", viewId); viewDetailCreateEditStore?.setIsEditable(true); } }, localViewCreateEditClear: async (viewId: string | undefined) => { if (viewDetailCreateEditStore?.is_create && viewId) viewStore?.remove(viewId); if (viewDetailCreateEditStore?.is_editable && viewId) viewDetailCreateEditStore.resetChanges(); handleViewOperationsToggle(undefined, undefined); }, fetch: async () => { try { await viewStore?.fetch(); } catch { setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again later or contact the support team.", }); } }, create: async (data: Partial) => { try { await viewStore?.create(data); handleViewOperationsToggle(undefined, undefined); setToastAlert({ type: "success", title: "Success!", message: "View created successfully.", }); } catch { setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again later or contact the support team.", }); } }, remove: async (viewId: string) => { try { await viewStore?.remove(viewId); handleViewOperationsToggle(undefined, undefined); setToastAlert({ type: "success", title: "Success!", message: "View removed successfully.", }); } catch { setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again later or contact the support team.", }); } }, update: async () => { try { await viewDetailStore?.saveChanges(); handleViewOperationsToggle(undefined, undefined); setToastAlert({ type: "success", title: "Success!", message: "View updated successfully.", }); } catch { setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again later or contact the support team.", }); } }, }), [viewStore, viewDetailStore, setToastAlert, viewDetailCreateEditStore] ); // 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) && ( )} )}
); });