2024-01-10 14:39:45 +00:00
|
|
|
import { FC, useMemo } from "react";
|
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
// components
|
2024-01-11 12:56:58 +00:00
|
|
|
import { LabelList, LabelCreate, IssueLabelSelectRoot } from "./";
|
2024-01-10 14:39:45 +00:00
|
|
|
// hooks
|
|
|
|
import { useIssueDetail, useLabel } from "hooks/store";
|
2024-03-06 08:48:41 +00:00
|
|
|
// ui
|
|
|
|
import { TOAST_TYPE, setToast } from "@plane/ui";
|
2024-01-10 14:39:45 +00:00
|
|
|
// types
|
|
|
|
import { IIssueLabel, TIssue } from "@plane/types";
|
|
|
|
|
|
|
|
export type TIssueLabel = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
|
|
|
disabled: boolean;
|
2024-02-27 11:28:46 +00:00
|
|
|
isInboxIssue?: boolean;
|
|
|
|
onLabelUpdate?: (labelIds: string[]) => void;
|
2024-01-10 14:39:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type TLabelOperations = {
|
|
|
|
updateIssue: (workspaceSlug: string, projectId: string, issueId: string, data: Partial<TIssue>) => Promise<void>;
|
|
|
|
createLabel: (workspaceSlug: string, projectId: string, data: Partial<IIssueLabel>) => Promise<any>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueLabel: FC<TIssueLabel> = observer((props) => {
|
2024-02-27 11:28:46 +00:00
|
|
|
const { workspaceSlug, projectId, issueId, disabled = false, isInboxIssue = false, onLabelUpdate } = props;
|
2024-01-10 14:39:45 +00:00
|
|
|
// hooks
|
|
|
|
const { updateIssue } = useIssueDetail();
|
2024-01-22 11:37:32 +00:00
|
|
|
const { createLabel } = useLabel();
|
2024-01-10 14:39:45 +00:00
|
|
|
|
|
|
|
const labelOperations: TLabelOperations = useMemo(
|
|
|
|
() => ({
|
|
|
|
updateIssue: async (workspaceSlug: string, projectId: string, issueId: string, data: Partial<TIssue>) => {
|
|
|
|
try {
|
2024-02-27 11:28:46 +00:00
|
|
|
if (onLabelUpdate) onLabelUpdate(data.label_ids || []);
|
|
|
|
else await updateIssue(workspaceSlug, projectId, issueId, data);
|
2024-01-10 14:39:45 +00:00
|
|
|
} catch (error) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
2024-01-10 14:39:45 +00:00
|
|
|
title: "Issue update failed",
|
2024-03-06 08:48:41 +00:00
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-10 14:39:45 +00:00
|
|
|
message: "Issue update failed",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
createLabel: async (workspaceSlug: string, projectId: string, data: Partial<IIssueLabel>) => {
|
|
|
|
try {
|
|
|
|
const labelResponse = await createLabel(workspaceSlug, projectId, data);
|
2024-02-27 11:28:46 +00:00
|
|
|
if (!isInboxIssue)
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
2024-02-27 11:28:46 +00:00
|
|
|
title: "Label created successfully",
|
2024-03-06 08:48:41 +00:00
|
|
|
type: TOAST_TYPE.SUCCESS,
|
2024-02-27 11:28:46 +00:00
|
|
|
message: "Label created successfully",
|
|
|
|
});
|
2024-01-10 14:39:45 +00:00
|
|
|
return labelResponse;
|
|
|
|
} catch (error) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
2024-01-10 14:39:45 +00:00
|
|
|
title: "Label creation failed",
|
2024-03-06 08:48:41 +00:00
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-10 14:39:45 +00:00
|
|
|
message: "Label creation failed",
|
|
|
|
});
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
2024-03-06 08:48:41 +00:00
|
|
|
[updateIssue, createLabel, onLabelUpdate]
|
2024-01-10 14:39:45 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="relative flex flex-wrap items-center gap-1">
|
|
|
|
<LabelList
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
projectId={projectId}
|
|
|
|
issueId={issueId}
|
|
|
|
labelOperations={labelOperations}
|
2024-01-11 12:56:58 +00:00
|
|
|
disabled={disabled}
|
2024-01-10 14:39:45 +00:00
|
|
|
/>
|
|
|
|
|
2024-01-11 12:56:58 +00:00
|
|
|
{!disabled && (
|
|
|
|
<IssueLabelSelectRoot
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
projectId={projectId}
|
|
|
|
issueId={issueId}
|
|
|
|
labelOperations={labelOperations}
|
|
|
|
/>
|
|
|
|
)}
|
2024-01-10 14:39:45 +00:00
|
|
|
|
2024-01-11 12:56:58 +00:00
|
|
|
{!disabled && (
|
|
|
|
<LabelCreate
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
projectId={projectId}
|
|
|
|
issueId={issueId}
|
|
|
|
labelOperations={labelOperations}
|
|
|
|
/>
|
|
|
|
)}
|
2024-01-10 14:39:45 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|