-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathuseTaskContextMenu.ts
More file actions
170 lines (161 loc) · 5.43 KB
/
Copy pathuseTaskContextMenu.ts
File metadata and controls
170 lines (161 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {
resolveExternalAppPath,
resolveTaskContextMenuIntent,
} from "@posthog/core/tasks/contextMenuActions";
import { useHostTRPCClient } from "@posthog/host-router/react";
import { PROJECT_BLUEBIRD_FLAG } from "@posthog/shared";
import type { Task } from "@posthog/shared/domain-types";
import { useArchiveTask } from "@posthog/ui/features/archive/useArchiveTask";
import { useChannels } from "@posthog/ui/features/canvas/hooks/useChannels";
import { useChannelTaskMutations } from "@posthog/ui/features/canvas/hooks/useChannelTasks";
import { useExternalAppAction } from "@posthog/ui/features/external-apps/useExternalAppAction";
import { useFeatureFlag } from "@posthog/ui/features/feature-flags/useFeatureFlag";
import { useRestoreTask } from "@posthog/ui/features/suspension/useRestoreTask";
import { useSuspendTask } from "@posthog/ui/features/suspension/useSuspendTask";
import { useDeleteTask } from "@posthog/ui/features/tasks/useTaskCrudMutations";
import { toast } from "@posthog/ui/primitives/toast";
import { logger } from "@posthog/ui/shell/logger";
import { useCallback, useState } from "react";
const log = logger.scope("context-menu");
export function useTaskContextMenu() {
const [editingTaskId, setEditingTaskId] = useState<string | null>(null);
const hostClient = useHostTRPCClient();
const openExternalApp = useExternalAppAction();
const { deleteWithConfirm } = useDeleteTask();
const { archiveTask } = useArchiveTask();
const { suspendTask } = useSuspendTask();
const { restoreTask } = useRestoreTask();
// "File to…" is a Project Bluebird feature. Gate the channel fetch behind the
// flag so the submenu (and its API request) never reaches ungated users.
const bluebirdEnabled = useFeatureFlag(
PROJECT_BLUEBIRD_FLAG,
import.meta.env.DEV,
);
const { channels } = useChannels({ enabled: bluebirdEnabled });
const { fileTask } = useChannelTaskMutations();
const showContextMenu = useCallback(
async (
task: Pick<Task, "id" | "title">,
event: React.MouseEvent,
options?: {
worktreePath?: string;
folderPath?: string;
isPinned?: boolean;
isSuspended?: boolean;
isInCommandCenter?: boolean;
hasEmptyCommandCenterCell?: boolean;
onTogglePin?: () => void;
onArchive?: (taskId: string) => void;
onArchivePrior?: (taskId: string) => void;
onAddToCommandCenter?: () => void;
},
) => {
event.preventDefault();
event.stopPropagation();
const {
worktreePath,
folderPath,
isPinned,
isSuspended,
isInCommandCenter,
hasEmptyCommandCenterCell,
onTogglePin,
onArchive,
onArchivePrior,
onAddToCommandCenter,
} = options ?? {};
try {
const result = await hostClient.contextMenu.showTaskContextMenu.mutate({
taskTitle: task.title,
worktreePath,
folderPath,
isPinned,
isSuspended,
isInCommandCenter,
hasEmptyCommandCenterCell,
channels: channels.map(({ id, name }) => ({ id, name })),
});
if (!result.action) return;
const intent = resolveTaskContextMenuIntent(result.action, {
isSuspended,
});
switch (intent.type) {
case "rename":
setEditingTaskId(task.id);
break;
case "pin":
onTogglePin?.();
break;
case "suspend":
await suspendTask({ taskId: task.id, reason: "manual" });
break;
case "restore":
await restoreTask(task.id);
break;
case "archive":
if (onArchive) {
onArchive(task.id);
} else {
await archiveTask({ taskId: task.id });
}
break;
case "archive-prior":
await onArchivePrior?.(task.id);
break;
case "delete":
await deleteWithConfirm({
taskId: task.id,
taskTitle: task.title,
hasWorktree: !!worktreePath,
});
break;
case "add-to-command-center":
onAddToCommandCenter?.();
break;
case "file-to-channel":
try {
await fileTask(intent.channelId, task.id, task.title);
} catch (error) {
toast.error("Couldn't file task to channel", {
description:
error instanceof Error ? error.message : String(error),
});
}
break;
case "external-app": {
const effectivePath = resolveExternalAppPath(
worktreePath,
folderPath,
);
if (effectivePath) {
const workspaces = await hostClient.workspace.getAll.query();
const workspace = workspaces[task.id] ?? null;
await openExternalApp(intent.action, effectivePath, task.title, {
workspace,
mainRepoPath: workspace?.folderPath,
});
}
break;
}
}
} catch (error) {
log.error("Failed to show context menu", error);
}
},
[
archiveTask,
channels,
deleteWithConfirm,
fileTask,
restoreTask,
suspendTask,
hostClient,
openExternalApp,
],
);
return {
showContextMenu,
editingTaskId,
setEditingTaskId,
};
}