plane/web/hooks/store/use-view.tsx

34 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-02-02 08:22:38 +00:00
import { useContext } from "react";
// mobx store
import { StoreContext } from "contexts/store-context";
// types
2024-02-05 14:39:17 +00:00
import { ViewRootStore } from "store/view/view-root.store";
2024-02-02 08:22:38 +00:00
// types
import { TViewTypes } from "@plane/types";
export const useView = (
workspaceSlug: string,
projectId: string | undefined,
viewType: TViewTypes | undefined
2024-02-05 14:39:17 +00:00
): ViewRootStore | undefined => {
2024-02-02 08:22:38 +00:00
const context = useContext(StoreContext);
if (context === undefined) throw new Error("useView must be used within StoreProvider");
2024-02-05 14:39:17 +00:00
if (!workspaceSlug || !viewType) return undefined;
2024-02-02 08:22:38 +00:00
switch (viewType) {
case "WORKSPACE_YOUR_VIEWS":
return context.view.workspaceViewMeStore;
2024-02-05 14:39:17 +00:00
case "WORKSPACE_VIEWS":
return context.view.workspaceViewStore;
2024-02-02 08:22:38 +00:00
case "PROJECT_YOUR_VIEWS":
if (!projectId) throw new Error("useView hook must require projectId");
return context.view.projectViewMeStore;
case "PROJECT_VIEWS":
if (!projectId) throw new Error("useView hook must require projectId");
return context.view.projectViewStore;
default:
2024-02-05 14:39:17 +00:00
return undefined;
2024-02-02 08:22:38 +00:00
}
};