Skip to content

Commit 1e2c33f

Browse files
refactor(web): the archived threads settings page lives in its own file
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6a6c84f commit 1e2c33f

3 files changed

Lines changed: 289 additions & 282 deletions

File tree

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
import { settingsScopeEnvironmentLabels } from "./settingsScopeAxis";
2+
import { Spinner } from "~/components/ui/spinner";
3+
import { ArchiveIcon, ArchiveX } from "lucide-react";
4+
import { type ScopedThreadRef } from "@t3tools/contracts";
5+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
6+
import { presentThreadShell } from "@t3tools/client-runtime/state/shell";
7+
import {
8+
isAtomCommandInterrupted,
9+
settlePromise,
10+
squashAtomCommandFailure,
11+
} from "@t3tools/client-runtime/state/runtime";
12+
import { useSettingsScope } from "./SettingsScopeContext";
13+
import { useThreadActions } from "../../hooks/useThreadActions";
14+
import { readLocalApi } from "../../localApi";
15+
import { useArchivedThreadSnapshots } from "../../lib/archivedThreadsState";
16+
import { RelativeTime } from "../RelativeTime";
17+
import { Button } from "../ui/button";
18+
import { stackedThreadToast, toastManager } from "../ui/toast";
19+
import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout";
20+
import { searchableSetting } from "./settingsSearch";
21+
import { ProjectFavicon } from "../ProjectFavicon";
22+
23+
export function ArchivedThreadsPanel() {
24+
const { scope, groups: projectGroups, environments } = useSettingsScope();
25+
const { unarchiveThread, confirmAndDeleteThread } = useThreadActions();
26+
const {
27+
snapshots: archivedSnapshots,
28+
error: archiveError,
29+
isLoading: isLoadingArchive,
30+
refresh: refreshArchivedThreads,
31+
} = useArchivedThreadSnapshots(scope.environmentIds);
32+
33+
const { groups: archivedGroups, hasMultipleEnvironments } = (() => {
34+
const selectedProjectKeys =
35+
scope.kind === "project" || scope.kind === "checkout"
36+
? new Set(scope.members.map((member) => `${member.environmentId}:${member.id}`))
37+
: null;
38+
const projectsByEnvironmentAndId = new Map(
39+
archivedSnapshots.flatMap(({ environmentId, snapshot }) =>
40+
snapshot.projects
41+
.filter(
42+
(project) =>
43+
selectedProjectKeys === null ||
44+
selectedProjectKeys.has(`${environmentId}:${project.id}`),
45+
)
46+
.map(
47+
(project) => [`${environmentId}:${project.id}`, { ...project, environmentId }] as const,
48+
),
49+
),
50+
);
51+
const environmentLabels = new Map(
52+
environments.map((environment) => {
53+
const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments);
54+
return [environment.environmentId, secondary ? `${primary} (${secondary})` : primary];
55+
}),
56+
);
57+
const threads = archivedSnapshots.flatMap(({ environmentId, snapshot }) =>
58+
snapshot.threads.map((thread) => ({
59+
...presentThreadShell(environmentId, thread),
60+
environmentLabel: environmentLabels.get(environmentId) ?? environmentId,
61+
})),
62+
);
63+
const hasMultipleEnvironments =
64+
new Set(
65+
threads
66+
.filter((thread) =>
67+
projectsByEnvironmentAndId.has(`${thread.environmentId}:${thread.projectId}`),
68+
)
69+
.map((thread) => thread.environmentId),
70+
).size > 1;
71+
const logicalGroupsByProjectKey = new Map(
72+
projectGroups.flatMap((group) =>
73+
group.memberProjectRefs.map(
74+
(member) => [`${member.environmentId}:${member.projectId}`, group] as const,
75+
),
76+
),
77+
);
78+
79+
const archivedProjects = Array.from(projectsByEnvironmentAndId.values());
80+
const groups = new Map<
81+
string,
82+
{
83+
readonly project: (typeof archivedProjects)[number];
84+
readonly title: string;
85+
readonly threads: Array<(typeof threads)[number]>;
86+
}
87+
>();
88+
for (const project of archivedProjects) {
89+
const projectThreads: Array<(typeof threads)[number]> = [];
90+
for (const thread of threads) {
91+
if (thread.projectId === project.id && thread.environmentId === project.environmentId) {
92+
projectThreads.push(thread);
93+
}
94+
}
95+
if (projectThreads.length > 0) {
96+
const physicalProjectKey = `${project.environmentId}:${project.id}` as const;
97+
const logicalGroup = hasMultipleEnvironments
98+
? logicalGroupsByProjectKey.get(physicalProjectKey)
99+
: undefined;
100+
const groupKey = logicalGroup?.projectKey ?? physicalProjectKey;
101+
const group = groups.get(groupKey);
102+
if (group) {
103+
group.threads.push(...projectThreads);
104+
} else {
105+
groups.set(groupKey, {
106+
project,
107+
title: logicalGroup?.displayName ?? project.title,
108+
threads: projectThreads,
109+
});
110+
}
111+
}
112+
}
113+
const sortedGroups = Array.from(groups, ([key, group]) => ({
114+
...group,
115+
key,
116+
threads: group.threads.toSorted((left, right) => {
117+
const leftKey = left.archivedAt ?? left.createdAt;
118+
const rightKey = right.archivedAt ?? right.createdAt;
119+
return rightKey.localeCompare(leftKey) || right.id.localeCompare(left.id);
120+
}),
121+
}));
122+
return { groups: sortedGroups, hasMultipleEnvironments };
123+
})();
124+
125+
const handleArchivedThreadContextMenu = async (
126+
threadRef: ScopedThreadRef,
127+
position: { x: number; y: number },
128+
) => {
129+
const api = readLocalApi();
130+
if (!api) return;
131+
const clicked = await api.contextMenu.show(
132+
[
133+
{ id: "unarchive", label: "Unarchive" },
134+
{ id: "delete", label: "Delete", destructive: true },
135+
],
136+
position,
137+
);
138+
139+
if (clicked === "unarchive") {
140+
const result = await unarchiveThread(threadRef);
141+
if (result._tag === "Success") {
142+
refreshArchivedThreads();
143+
} else if (!isAtomCommandInterrupted(result)) {
144+
const error = squashAtomCommandFailure(result);
145+
toastManager.add(
146+
stackedThreadToast({
147+
type: "error",
148+
title: "Failed to unarchive thread",
149+
description: error instanceof Error ? error.message : "An error occurred.",
150+
}),
151+
);
152+
}
153+
return;
154+
}
155+
156+
if (clicked === "delete") {
157+
const result = await confirmAndDeleteThread(threadRef);
158+
if (result._tag === "Success") {
159+
refreshArchivedThreads();
160+
} else if (!isAtomCommandInterrupted(result)) {
161+
const error = squashAtomCommandFailure(result);
162+
toastManager.add(
163+
stackedThreadToast({
164+
type: "error",
165+
title: "Failed to delete thread",
166+
description: error instanceof Error ? error.message : "An error occurred.",
167+
}),
168+
);
169+
}
170+
}
171+
};
172+
173+
return (
174+
<SettingsPageContainer>
175+
{archivedGroups.length === 0 ? (
176+
<SettingsSection
177+
id={isLoadingArchive ? undefined : searchableSetting("archive").id}
178+
title={searchableSetting("archive").title}
179+
>
180+
<SettingsRow
181+
title={
182+
<span className="inline-flex items-center gap-2">
183+
{isLoadingArchive ? (
184+
<Spinner size="sm" tone="muted" />
185+
) : (
186+
<ArchiveIcon className="size-3.5 text-muted-foreground" />
187+
)}
188+
{isLoadingArchive
189+
? "Loading archived threads"
190+
: archiveError
191+
? "Could not load archived threads"
192+
: "No archived threads"}
193+
</span>
194+
}
195+
description={
196+
isLoadingArchive
197+
? "Checking connected environments."
198+
: (archiveError ?? "Archived threads will appear here.")
199+
}
200+
/>
201+
</SettingsSection>
202+
) : (
203+
archivedGroups.map(({ key, project, title, threads: projectThreads }, index) => (
204+
<SettingsSection
205+
key={key}
206+
id={index === 0 ? searchableSetting("archive").id : undefined}
207+
title={title}
208+
icon={<ProjectFavicon project={project} />}
209+
>
210+
{projectThreads.map((thread) => (
211+
<SettingsRow
212+
key={`${thread.environmentId}:${thread.id}`}
213+
onContextMenu={(event) => {
214+
event.preventDefault();
215+
void (async () => {
216+
const result = await settlePromise(() =>
217+
handleArchivedThreadContextMenu(
218+
scopeThreadRef(thread.environmentId, thread.id),
219+
{
220+
x: event.clientX,
221+
y: event.clientY,
222+
},
223+
),
224+
);
225+
if (result._tag === "Failure") {
226+
const error = squashAtomCommandFailure(result);
227+
toastManager.add(
228+
stackedThreadToast({
229+
type: "error",
230+
title: "Archived thread action failed",
231+
description:
232+
error instanceof Error ? error.message : "An error occurred.",
233+
}),
234+
);
235+
}
236+
})();
237+
}}
238+
title={thread.title}
239+
description={
240+
<span className={hasMultipleEnvironments ? "block truncate" : undefined}>
241+
{hasMultipleEnvironments ? `${thread.environmentLabel} · ` : null}
242+
Archived <RelativeTime timestamp={thread.archivedAt ?? thread.createdAt} />
243+
{" \u00b7 Created "}
244+
<RelativeTime timestamp={thread.createdAt} />
245+
</span>
246+
}
247+
control={
248+
<Button
249+
type="button"
250+
variant="outline"
251+
size="sm"
252+
className="shrink-0"
253+
onClick={() => {
254+
void (async () => {
255+
const result = await unarchiveThread(
256+
scopeThreadRef(thread.environmentId, thread.id),
257+
);
258+
if (result._tag === "Success") {
259+
refreshArchivedThreads();
260+
return;
261+
}
262+
if (!isAtomCommandInterrupted(result)) {
263+
const error = squashAtomCommandFailure(result);
264+
toastManager.add(
265+
stackedThreadToast({
266+
type: "error",
267+
title: "Failed to unarchive thread",
268+
description:
269+
error instanceof Error ? error.message : "An error occurred.",
270+
}),
271+
);
272+
}
273+
})();
274+
}}
275+
>
276+
<ArchiveX className="size-3.5" />
277+
<span>Unarchive</span>
278+
</Button>
279+
}
280+
/>
281+
))}
282+
</SettingsSection>
283+
))
284+
)}
285+
</SettingsPageContainer>
286+
);
287+
}

0 commit comments

Comments
 (0)