-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcontext-menu.ts
More file actions
417 lines (392 loc) · 11.8 KB
/
Copy pathcontext-menu.ts
File metadata and controls
417 lines (392 loc) · 11.8 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import {
CONTEXT_MENU_SERVICE,
type ContextMenuItem,
type IContextMenu,
} from "@posthog/platform/context-menu";
import { DIALOG_SERVICE, type IDialog } from "@posthog/platform/dialog";
import { inject, injectable } from "inversify";
import {
CONTEXT_MENU_EXTERNAL_APPS_SERVICE,
type ContextMenuExternalApp,
type IContextMenuExternalApps,
} from "./identifiers";
import type {
ArchivedTaskAction,
ArchivedTaskContextMenuInput,
ArchivedTaskContextMenuResult,
BulkTaskAction,
BulkTaskContextMenuInput,
ConfirmDeleteArchivedTaskInput,
ConfirmDeleteArchivedTaskResult,
ConfirmDeleteTaskInput,
ConfirmDeleteTaskResult,
FileAction,
FileContextMenuInput,
FileContextMenuResult,
FolderAction,
FolderContextMenuInput,
FolderContextMenuResult,
SplitContextMenuResult,
SplitDirection,
TabAction,
TabContextMenuInput,
TabContextMenuResult,
TaskAction,
TaskContextMenuInput,
TaskContextMenuResult,
} from "./schemas";
import type {
ActionItemDef,
ConfirmOptions,
MenuItemDef,
SeparatorDef,
} from "./types";
@injectable()
export class ContextMenuService {
constructor(
@inject(CONTEXT_MENU_EXTERNAL_APPS_SERVICE)
private readonly externalApps: IContextMenuExternalApps,
@inject(DIALOG_SERVICE)
private readonly dialog: IDialog,
@inject(CONTEXT_MENU_SERVICE)
private readonly contextMenu: IContextMenu,
) {}
private async getExternalAppsData() {
const [apps, lastUsed] = await Promise.all([
this.externalApps.getDetectedApps(),
this.externalApps.getLastUsed(),
]);
return { apps, lastUsedAppId: lastUsed.lastUsedApp };
}
async confirmDeleteTask(
input: ConfirmDeleteTaskInput,
): Promise<ConfirmDeleteTaskResult> {
const confirmed = await this.confirm({
title: "Delete Task",
message: `Delete "${input.taskTitle}"?`,
detail: input.hasWorktree
? "This will permanently delete the task and its associated worktree."
: "This will permanently delete the task.",
confirmLabel: "Delete",
});
return { confirmed };
}
async confirmDeleteArchivedTask(
input: ConfirmDeleteArchivedTaskInput,
): Promise<ConfirmDeleteArchivedTaskResult> {
const confirmed = await this.confirm({
title: "Delete Archived Task",
message: `Delete "${input.taskTitle}"?`,
detail: "This will permanently delete the archived task.",
confirmLabel: "Delete",
});
return { confirmed };
}
async confirmDeleteWorktree({
worktreePath,
linkedTaskCount,
}: {
worktreePath: string;
linkedTaskCount: number;
}): Promise<{ confirmed: boolean }> {
const confirmed = await this.confirm({
title: "Delete Worktree",
message: `Delete worktree at ${worktreePath}?`,
detail:
linkedTaskCount > 0
? `This will remove ${linkedTaskCount} linked task${linkedTaskCount === 1 ? "" : "s"} and delete the worktree.`
: "This will delete the worktree from disk.",
confirmLabel: "Delete",
});
return { confirmed };
}
async showTaskContextMenu(
input: TaskContextMenuInput,
): Promise<TaskContextMenuResult> {
const {
worktreePath,
folderPath,
isPinned,
isSuspended,
canStop,
isInCommandCenter,
hasEmptyCommandCenterCell,
channels,
} = input;
const { apps, lastUsedAppId } = await this.getExternalAppsData();
const hasPath = worktreePath || folderPath;
const fileToItems: MenuItemDef<TaskAction>[] =
channels && channels.length > 0
? [
this.separator(),
{
type: "submenu",
label: "File to…",
items: channels.map((c) => ({
label: c.name,
action: {
type: "file-to-channel" as const,
channelId: c.id,
},
})),
},
]
: [];
return this.showMenu<TaskAction>([
this.item(isPinned ? "Unpin" : "Pin", { type: "pin" }),
this.item("Rename", { type: "rename" }),
...(canStop
? [this.separator(), this.item("Stop task", { type: "stop" as const })]
: []),
...(worktreePath
? [
this.separator(),
this.item(isSuspended ? "Unsuspend" : "Suspend", {
type: "suspend" as const,
}),
]
: []),
...(hasPath
? [
...(worktreePath ? [] : [this.separator()]),
...this.externalAppItems<TaskAction>(apps, lastUsedAppId),
]
: []),
...(!isInCommandCenter
? [
this.separator(),
this.item(
"Add to Command Center",
{ type: "add-to-command-center" as const },
{ enabled: hasEmptyCommandCenterCell ?? true },
),
]
: []),
...fileToItems,
this.separator(),
this.item("Archive", { type: "archive" }),
this.item(
"Archive prior tasks",
{ type: "archive-prior" },
{
confirm: {
title: "Archive Prior Tasks",
message: "Archive all tasks older than this one?",
detail:
"This will archive every task created before this one. You can unarchive them later.",
confirmLabel: "Archive",
},
},
),
// Confirmation is handled downstream by TaskDeletionService via
// confirmDeleteTask (worktree-aware wording), so no inline confirm here.
this.item("Delete", { type: "delete" }),
]);
}
async showBulkTaskContextMenu(
input: BulkTaskContextMenuInput,
): Promise<{ action: BulkTaskAction | null }> {
const { taskCount } = input;
const label = `Archive ${taskCount} tasks`;
return this.showMenu<BulkTaskAction>([
this.item(
label,
{ type: "archive" },
{
confirm: {
title: "Archive Tasks",
message: `Archive ${taskCount} tasks?`,
detail: "You can unarchive them later.",
confirmLabel: "Archive",
},
},
),
]);
}
async showArchivedTaskContextMenu(
input: ArchivedTaskContextMenuInput,
): Promise<ArchivedTaskContextMenuResult> {
return this.showMenu<ArchivedTaskAction>([
this.item("Unarchive", { type: "restore" }),
this.item(
"Delete",
{ type: "delete" },
{
confirm: {
title: "Delete Archived Task",
message: `Delete "${input.taskTitle}"?`,
detail: "This will permanently delete the archived task.",
confirmLabel: "Delete",
},
},
),
]);
}
async showFolderContextMenu(
input: FolderContextMenuInput,
): Promise<FolderContextMenuResult> {
const { folderName, folderPath } = input;
const { apps, lastUsedAppId } = await this.getExternalAppsData();
return this.showMenu<FolderAction>([
this.item(
"Remove folder",
{ type: "remove" },
{
confirm: {
title: "Remove Folder",
message: `Remove "${folderName}"?`,
detail:
"This will clean up any worktrees but keep your folder and tasks intact.",
confirmLabel: "Remove",
},
},
),
...(folderPath
? [
this.separator(),
...this.externalAppItems<FolderAction>(apps, lastUsedAppId),
]
: []),
]);
}
async showTabContextMenu(
input: TabContextMenuInput,
): Promise<TabContextMenuResult> {
const { canClose, filePath } = input;
const { apps, lastUsedAppId } = await this.getExternalAppsData();
return this.showMenu<TabAction>([
this.item(
"Close tab",
{ type: "close" },
{
accelerator: "CmdOrCtrl+W",
enabled: canClose,
},
),
this.item("Close other tabs", { type: "close-others" }),
this.item("Close tabs to the right", { type: "close-right" }),
...(filePath
? [
this.separator(),
...this.externalAppItems<TabAction>(apps, lastUsedAppId),
]
: []),
]);
}
async showSplitContextMenu(): Promise<SplitContextMenuResult> {
const result = await this.showMenu<SplitDirection>([
this.item("Split right", "right"),
this.item("Split left", "left"),
this.item("Split down", "down"),
this.item("Split up", "up"),
]);
return { direction: result.action };
}
async showFileContextMenu(
input: FileContextMenuInput,
): Promise<FileContextMenuResult> {
const { apps, lastUsedAppId } = await this.getExternalAppsData();
return this.showMenu<FileAction>([
...(input.showCollapseAll
? [
this.item<FileAction>("Collapse All", { type: "collapse-all" }),
this.separator(),
]
: []),
...this.externalAppItems<FileAction>(apps, lastUsedAppId),
]);
}
private externalAppItems<T>(
apps: ContextMenuExternalApp[],
lastUsedAppId?: string,
): MenuItemDef<T>[] {
if (apps.length === 0) {
return [this.disabled("No external apps detected")];
}
const lastUsedApp = apps.find((app) => app.id === lastUsedAppId) || apps[0];
const openIn = (appId: string): T =>
({ type: "external-app", action: { type: "open-in-app", appId } }) as T;
return [
this.item(`Open in ${lastUsedApp.name}`, openIn(lastUsedApp.id)),
{
type: "submenu",
label: "Open in",
items: apps.map((app) => ({
label: app.name,
icon: app.icon,
action: openIn(app.id),
})),
},
];
}
private item<T>(
label: string,
action: T,
options?: Partial<Omit<ActionItemDef<T>, "type" | "label" | "action">>,
): ActionItemDef<T> {
return { type: "item", label, action, ...options };
}
private separator(): SeparatorDef {
return { type: "separator" };
}
private disabled(label: string): MenuItemDef<never> {
return { type: "disabled", label };
}
private showMenu<T>(items: MenuItemDef<T>[]): Promise<{ action: T | null }> {
return new Promise((resolve) => {
let pendingConfirm = false;
const toContextMenuItem = (def: MenuItemDef<T>): ContextMenuItem => {
switch (def.type) {
case "separator":
return { separator: true };
case "disabled":
return { label: def.label, enabled: false, click: () => {} };
case "submenu":
return {
label: def.label,
submenu: def.items.map((sub) => ({
label: sub.label,
icon: sub.icon,
click: () => resolve({ action: sub.action }),
})),
click: () => {},
};
case "item": {
const confirmOptions = def.confirm;
const click = confirmOptions
? async () => {
pendingConfirm = true;
const confirmed = await this.confirm(confirmOptions);
resolve({ action: confirmed ? def.action : null });
}
: () => resolve({ action: def.action });
return {
label: def.label,
enabled: def.enabled,
accelerator: def.accelerator,
icon: def.icon,
click,
};
}
}
};
this.contextMenu.show(items.map(toContextMenuItem), {
onDismiss: () => {
if (!pendingConfirm) resolve({ action: null });
},
});
});
}
private async confirm(options: ConfirmOptions): Promise<boolean> {
const response = await this.dialog.confirm({
severity: "question",
title: options.title,
message: options.message,
detail: options.detail,
options: ["Cancel", options.confirmLabel],
defaultIndex: 1,
cancelIndex: 0,
});
return response === 1;
}
}