mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
129 lines
4.8 KiB
TypeScript
129 lines
4.8 KiB
TypeScript
import { FC, useCallback } from "react";
|
|
import { observer } from "mobx-react-lite";
|
|
import { useRouter } from "next/router";
|
|
// hooks
|
|
import { EIssueFilterType, EIssueLayoutTypes, EIssuesStoreType } from "constants/issue";
|
|
import { EUserProjectRoles } from "constants/project";
|
|
import { useIssues, useUser } from "hooks/store";
|
|
import { useIssuesActions } from "hooks/use-issues-actions";
|
|
// views
|
|
// types
|
|
// constants
|
|
import { TIssue, IIssueDisplayFilterOptions } from "@plane/types";
|
|
import { IQuickActionProps } from "../list/list-view-types";
|
|
import { SpreadsheetView } from "./spreadsheet-view";
|
|
import useSWR from "swr";
|
|
import { IssueLayoutHOC } from "../issue-layout-HOC";
|
|
import { ALL_ISSUES } from "store/issue/helpers/base-issues.store";
|
|
|
|
export type SpreadsheetStoreType =
|
|
| EIssuesStoreType.PROJECT
|
|
| EIssuesStoreType.MODULE
|
|
| EIssuesStoreType.CYCLE
|
|
| EIssuesStoreType.PROJECT_VIEW;
|
|
interface IBaseSpreadsheetRoot {
|
|
viewId?: string;
|
|
QuickActions: FC<IQuickActionProps>;
|
|
storeType: SpreadsheetStoreType;
|
|
canEditPropertiesBasedOnProject?: (projectId: string) => boolean;
|
|
isCompletedCycle?: boolean;
|
|
}
|
|
|
|
export const BaseSpreadsheetRoot = observer((props: IBaseSpreadsheetRoot) => {
|
|
const { viewId, QuickActions, storeType, canEditPropertiesBasedOnProject, isCompletedCycle = false } = props;
|
|
// router
|
|
const router = useRouter();
|
|
const { projectId } = router.query;
|
|
// store hooks
|
|
const {
|
|
membership: { currentProjectRole },
|
|
} = useUser();
|
|
const { issues, issuesFilter } = useIssues(storeType);
|
|
const {
|
|
fetchIssues,
|
|
fetchNextIssues,
|
|
updateIssue,
|
|
removeIssue,
|
|
removeIssueFromView,
|
|
archiveIssue,
|
|
restoreIssue,
|
|
updateFilters,
|
|
} = useIssuesActions(storeType);
|
|
// derived values
|
|
const { enableInlineEditing, enableQuickAdd, enableIssueCreation } = issues?.viewFlags || {};
|
|
// user role validation
|
|
const isEditingAllowed = !!currentProjectRole && currentProjectRole >= EUserProjectRoles.MEMBER;
|
|
|
|
useSWR(
|
|
`ISSUE_SPREADSHEET_LAYOUT_${storeType}`,
|
|
() => fetchIssues("init-loader", { canGroup: false, perPageCount: 100 }),
|
|
{
|
|
revalidateOnFocus: false,
|
|
revalidateOnReconnect: false,
|
|
}
|
|
);
|
|
|
|
const canEditProperties = useCallback(
|
|
(projectId: string | undefined) => {
|
|
const isEditingAllowedBasedOnProject =
|
|
canEditPropertiesBasedOnProject && projectId ? canEditPropertiesBasedOnProject(projectId) : isEditingAllowed;
|
|
|
|
return enableInlineEditing && isEditingAllowedBasedOnProject;
|
|
},
|
|
[canEditPropertiesBasedOnProject, enableInlineEditing, isEditingAllowed]
|
|
);
|
|
|
|
const issueIds = issues.groupedIssueIds?.[ALL_ISSUES] ?? [];
|
|
const nextPageResults = issues.getPaginationData(ALL_ISSUES)?.nextPageResults;
|
|
|
|
const handleDisplayFiltersUpdate = useCallback(
|
|
(updatedDisplayFilter: Partial<IIssueDisplayFilterOptions>) => {
|
|
if (!projectId) return;
|
|
|
|
updateFilters(projectId.toString(), EIssueFilterType.DISPLAY_FILTERS, {
|
|
...updatedDisplayFilter,
|
|
});
|
|
},
|
|
[projectId, updateFilters]
|
|
);
|
|
|
|
const renderQuickActions = useCallback(
|
|
(issue: TIssue, customActionButton?: React.ReactElement, portalElement?: HTMLDivElement | null) => (
|
|
<QuickActions
|
|
customActionButton={customActionButton}
|
|
issue={issue}
|
|
handleDelete={async () => removeIssue(issue.project_id, issue.id)}
|
|
handleUpdate={async (data) => updateIssue && updateIssue(issue.project_id, issue.id, data)}
|
|
handleRemoveFromView={async () => removeIssueFromView && removeIssueFromView(issue.project_id, issue.id)}
|
|
handleArchive={async () => archiveIssue && archiveIssue(issue.project_id, issue.id)}
|
|
handleRestore={async () => restoreIssue && restoreIssue(issue.project_id, issue.id)}
|
|
portalElement={portalElement}
|
|
readOnly={!isEditingAllowed || isCompletedCycle}
|
|
/>
|
|
),
|
|
[isEditingAllowed, isCompletedCycle, removeIssue, updateIssue, removeIssueFromView, archiveIssue, restoreIssue]
|
|
);
|
|
|
|
if (!Array.isArray(issueIds)) return null;
|
|
|
|
return (
|
|
<IssueLayoutHOC storeType={storeType} layout={EIssueLayoutTypes.SPREADSHEET}>
|
|
<SpreadsheetView
|
|
displayProperties={issuesFilter.issueFilters?.displayProperties ?? {}}
|
|
displayFilters={issuesFilter.issueFilters?.displayFilters ?? {}}
|
|
handleDisplayFilterUpdate={handleDisplayFiltersUpdate}
|
|
issueIds={issueIds}
|
|
quickActions={renderQuickActions}
|
|
updateIssue={updateIssue}
|
|
canEditProperties={canEditProperties}
|
|
quickAddCallback={issues.quickAddIssue}
|
|
viewId={viewId}
|
|
enableQuickCreateIssue={enableQuickAdd}
|
|
disableIssueCreation={!enableIssueCreation || !isEditingAllowed || isCompletedCycle}
|
|
canLoadMoreIssues={!!nextPageResults}
|
|
loadMoreIssues={fetchNextIssues}
|
|
/>
|
|
</IssueLayoutHOC>
|
|
);
|
|
});
|