plane/web/hooks/store/use-issues.ts
guru_sainath 4611ec0b83 chore: refactored and resolved build issues on the issues and issue detail page (#3340)
* fix: handled undefined issue_id in list layout

* dev: issue detail store and optimization

* dev: issue filter and list operations

* fix: typo on labels update

* dev: Handled all issues in the list layout in project issues

* dev: handled kanban and auick add issue in swimlanes

* chore: fixed peekoverview in kanban

* chore: fixed peekoverview in calendar

* chore: fixed peekoverview in gantt

* chore: updated quick add in the gantt chart

* chore: handled issue detail properties and resolved build issues

---------

Co-authored-by: pablohashescobar <nikhilschacko@gmail.com>
2024-01-22 13:19:43 +05:30

117 lines
4.1 KiB
TypeScript

import { useContext } from "react";
import merge from "lodash/merge";
// mobx store
import { StoreContext } from "contexts/store-context";
// types
import { IWorkspaceIssues, IWorkspaceIssuesFilter } from "store/issue/workspace";
import { IProfileIssues, IProfileIssuesFilter } from "store/issue/profile";
import { IProjectIssues, IProjectIssuesFilter } from "store/issue/project";
import { ICycleIssues, ICycleIssuesFilter } from "store/issue/cycle";
import { IModuleIssues, IModuleIssuesFilter } from "store/issue/module";
import { IProjectViewIssues, IProjectViewIssuesFilter } from "store/issue/project-views";
import { IArchivedIssues, IArchivedIssuesFilter } from "store/issue/archived";
import { IDraftIssues, IDraftIssuesFilter } from "store/issue/draft";
import { TIssueMap } from "@plane/types";
// constants
import { EIssuesStoreType } from "constants/issue";
type defaultIssueStore = {
issueMap: TIssueMap;
};
export type TStoreIssues = {
[EIssuesStoreType.GLOBAL]: defaultIssueStore & {
issues: IWorkspaceIssues;
issuesFilter: IWorkspaceIssuesFilter;
};
[EIssuesStoreType.PROFILE]: defaultIssueStore & {
issues: IProfileIssues;
issuesFilter: IProfileIssuesFilter;
};
[EIssuesStoreType.PROJECT]: defaultIssueStore & {
issues: IProjectIssues;
issuesFilter: IProjectIssuesFilter;
};
[EIssuesStoreType.CYCLE]: defaultIssueStore & {
issues: ICycleIssues;
issuesFilter: ICycleIssuesFilter;
};
[EIssuesStoreType.MODULE]: defaultIssueStore & {
issues: IModuleIssues;
issuesFilter: IModuleIssuesFilter;
};
[EIssuesStoreType.PROJECT_VIEW]: defaultIssueStore & {
issues: IProjectViewIssues;
issuesFilter: IProjectViewIssuesFilter;
};
[EIssuesStoreType.ARCHIVED]: defaultIssueStore & {
issues: IArchivedIssues;
issuesFilter: IArchivedIssuesFilter;
};
[EIssuesStoreType.DRAFT]: defaultIssueStore & {
issues: IDraftIssues;
issuesFilter: IDraftIssuesFilter;
};
[EIssuesStoreType.DEFAULT]: defaultIssueStore & {
issues: undefined;
issuesFilter: undefined;
};
};
export const useIssues = <T extends EIssuesStoreType>(storeType?: T): TStoreIssues[T] => {
const context = useContext(StoreContext);
if (context === undefined) throw new Error("useIssues must be used within StoreProvider");
const defaultStore: defaultIssueStore = {
issueMap: context.issue.issues.issuesMap,
};
switch (storeType) {
case EIssuesStoreType.GLOBAL:
return merge(defaultStore, {
issues: context.issue.workspaceIssues,
issuesFilter: context.issue.workspaceIssuesFilter,
}) as TStoreIssues[T];
case EIssuesStoreType.PROFILE:
return merge(defaultStore, {
issues: context.issue.profileIssues,
issuesFilter: context.issue.profileIssuesFilter,
}) as TStoreIssues[T];
case EIssuesStoreType.PROJECT:
return merge(defaultStore, {
issues: context.issue.projectIssues,
issuesFilter: context.issue.projectIssuesFilter,
}) as TStoreIssues[T];
case EIssuesStoreType.CYCLE:
return merge(defaultStore, {
issues: context.issue.cycleIssues,
issuesFilter: context.issue.cycleIssuesFilter,
}) as TStoreIssues[T];
case EIssuesStoreType.MODULE:
return merge(defaultStore, {
issues: context.issue.moduleIssues,
issuesFilter: context.issue.moduleIssuesFilter,
}) as TStoreIssues[T];
case EIssuesStoreType.PROJECT_VIEW:
return merge(defaultStore, {
issues: context.issue.projectViewIssues,
issuesFilter: context.issue.projectViewIssuesFilter,
}) as TStoreIssues[T];
case EIssuesStoreType.ARCHIVED:
return merge(defaultStore, {
issues: context.issue.archivedIssues,
issuesFilter: context.issue.archivedIssuesFilter,
}) as TStoreIssues[T];
case EIssuesStoreType.DRAFT:
return merge(defaultStore, {
issues: context.issue.draftIssues,
issuesFilter: context.issue.draftIssuesFilter,
}) as TStoreIssues[T];
default:
return merge(defaultStore, {
issues: undefined,
issuesFilter: undefined,
}) as TStoreIssues[T];
}
};