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

35 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
import { TViewTypes } from "@plane/types";
// constants
import { VIEW_TYPES } from "constants/view";
2024-02-02 08:22:38 +00:00
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 VIEW_TYPES.WORKSPACE_PRIVATE_VIEWS:
return context.view.workspacePrivateViewStore;
case VIEW_TYPES.WORKSPACE_PUBLIC_VIEWS:
return context.view.workspacePublicViewStore;
case VIEW_TYPES.PROJECT_PRIVATE_VIEWS:
if (!projectId) return undefined;
return context.view.projectPrivateViewStore;
case VIEW_TYPES.PROJECT_PUBLIC_VIEWS:
if (!projectId) return undefined;
return context.view.projectPublicViewStore;
2024-02-02 08:22:38 +00:00
default:
2024-02-05 14:39:17 +00:00
return undefined;
2024-02-02 08:22:38 +00:00
}
};