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

projectRemoval.ts

90 lines3.7 KB

1import { isListedThreadLineage } from "@t3tools/contracts";
2​
3import type { EnvironmentProject, EnvironmentThreadShell } from "./models.ts";
4import { mFallback, msg, plural, part } from "gt-i18n";
5import type { M } from "gt-i18n";
6​
7export function planProjectRemoval<
8 Thread extends Pick<EnvironmentThreadShell, "environmentId" | "projectId" | "lineage">,
9>(
10 options: {
11 readonly members: ReadonlyArray<
12 Pick<EnvironmentProject, "id" | "environmentId" | "title" | "workspaceRoot"> & {
13 readonly environmentLabel?: string | null;
14 }
15 >;
16 readonly groupMemberCount: number;
17 readonly hasOtherMembers: boolean;
18 readonly displayName: string;
19 readonly threads: ReadonlyArray<Thread>;
20 /** The members' threads this client does not hold: older settled threads its shell left out. */
21 readonly unloadedThreadCount?: number;
22 },
23 resolve: M = mFallback,
24) {
25 const { members, groupMemberCount, hasOtherMembers, displayName, threads } = options;
26 const memberKeys = new Set(members.map((member) => `${member.environmentId}:${member.id}`));
27 const projectThreads = threads.filter((thread) =>
28 memberKeys.has(`${thread.environmentId}:${thread.projectId}`),
29 );
30 // Side chats and subagents go with their parent but never show in thread lists.
31 const threadCount =
32 projectThreads.filter((thread) => isListedThreadLineage(thread.lineage)).length +
33 (options.unloadedThreadCount ?? 0);
34 const isWholeGroup = members.length === groupMemberCount;
35 const targetKind = hasOtherMembers || !isWholeGroup ? "checkout" : "project";
36 const singleMember = members.length === 1 ? members[0]! : null;
37 const targetLabel = singleMember?.title ?? displayName;
38 const confirmationMessage = [
39 threadCount > 0
40 ? targetKind === "checkout"
41 ? resolve(
42 msg`${plural(
43 { count: threadCount },
44 {
45 one: part`Remove checkout "${{ label: targetLabel }}" and delete its ${{ count: threadCount }} thread?`,
46 other: part`Remove checkout "${{ label: targetLabel }}" and delete its ${{ count: threadCount }} threads?`,
47 },
48 )}`,
49 )
50 : resolve(
51 msg`${plural(
52 { count: threadCount },
53 {
54 one: part`Remove project "${{ label: targetLabel }}" and delete its ${{ count: threadCount }} thread?`,
55 other: part`Remove project "${{ label: targetLabel }}" and delete its ${{ count: threadCount }} threads?`,
56 },
57 )}`,
58 )
59 : targetKind === "checkout"
60 ? resolve(msg`Remove checkout "${{ label: targetLabel }}"?`)
61 : resolve(msg`Remove project "${{ label: targetLabel }}"?`),
62 ...(singleMember
63 ? [
64 resolve(msg`Path: ${{ path: singleMember.workspaceRoot }}`),
65 ...(singleMember.environmentLabel
66 ? [resolve(msg`Environment: ${{ environment: singleMember.environmentLabel }}`)]
67 : []),
68 ]
69 : [resolve(msg`This removes ${{ count: members.length }} grouped project entries.`)]),
70 ...(threadCount > 0
71 ? [
72 resolve(
73 msg(
74 "Those threads and any archived threads are deleted from T3 Code. Provider sessions are kept.",
75 ),
76 ),
77 ]
78 : [
79 resolve(
80 msg("Any archived threads are deleted from T3 Code. Provider sessions are kept."),
81 ),
82 ]),
83 isWholeGroup && !hasOtherMembers
84 ? resolve(msg("This removes only the project entries, not the files on disk."))
85 : resolve(msg("Other entries in this grouped project are unaffected.")),
86 resolve(msg("This action cannot be undone.")),
87 ].join("\n");
88​
89 return { projectThreads, isWholeGroup, targetKind, confirmationMessage };