Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

projectRemoval.ts

52 lines2.3 KB

1import type { EnvironmentProject, EnvironmentThreadShell } from "./models.ts";
2​
3export function planProjectRemoval<
4 Thread extends Pick<EnvironmentThreadShell, "environmentId" | "projectId">,
5>(options: {
6 readonly members: ReadonlyArray<
7 Pick<EnvironmentProject, "id" | "environmentId" | "title" | "workspaceRoot"> & {
8 readonly environmentLabel?: string | null;
9 }
10 >;
11 readonly groupMemberCount: number;
12 readonly hasOtherMembers: boolean;
13 readonly displayName: string;
14 readonly threads: ReadonlyArray<Thread>;
15 /** The members' threads this client does not hold: older settled threads its shell left out. */
16 readonly unloadedThreadCount?: number;
17}) {
18 const { members, groupMemberCount, hasOtherMembers, displayName, threads } = options;
19 const memberKeys = new Set(members.map((member) => `${member.environmentId}:${member.id}`));
20 const projectThreads = threads.filter((thread) =>
21 memberKeys.has(`${thread.environmentId}:${thread.projectId}`),
22 );
23 const threadCount = projectThreads.length + (options.unloadedThreadCount ?? 0);
24 const isWholeGroup = members.length === groupMemberCount;
25 const targetKind = hasOtherMembers || !isWholeGroup ? "checkout" : "project";
26 const singleMember = members.length === 1 ? members[0]! : null;
27 const targetLabel = singleMember?.title ?? displayName;
28 const confirmationMessage = [
29 threadCount > 0
30 ? `Remove ${targetKind} "${targetLabel}" and delete its ${threadCount} thread${threadCount === 1 ? "" : "s"}?`
31 : `Remove ${targetKind} "${targetLabel}"?`,
32 ...(singleMember
33 ? [
34 `Path: ${singleMember.workspaceRoot}`,
35 ...(singleMember.environmentLabel
36 ? [`Environment: ${singleMember.environmentLabel}`]
37 : []),
38 ]
39 : [`This removes ${members.length} grouped project entries.`]),
40 ...(threadCount > 0
41 ? [
42 "Those threads and any archived threads are deleted from T3 Code. Provider sessions are kept.",
43 ]
44 : ["Any archived threads are deleted from T3 Code. Provider sessions are kept."]),
45 isWholeGroup && !hasOtherMembers
46 ? "This removes only the project entries, not the files on disk."
47 : "Other entries in this grouped project are unaffected.",
48 "This action cannot be undone.",
49 ].join("\n");
50​
51 return { projectThreads, isWholeGroup, targetKind, confirmationMessage };