Skip to content

Commit 2eb74be

Browse files
committed
feat(web): delete archived threads from the archive page
1 parent 0f4a130 commit 2eb74be

3 files changed

Lines changed: 120 additions & 120 deletions

File tree

‎apps/web/src/components/settings/ArchivedThreadsPanel.tsx‎

Lines changed: 106 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { settingsScopeEnvironmentLabels } from "./settingsScopeAxis";
22
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";
3+
import { ArchiveIcon, ArchiveX, Trash2Icon } from "lucide-react";
4+
import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
65
import { presentThreadShell } from "@t3tools/client-runtime/state/shell";
76
import {
7+
type AtomCommandResult,
88
isAtomCommandInterrupted,
99
settlePromise,
1010
squashAtomCommandFailure,
@@ -20,9 +20,20 @@ import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsL
2020
import { searchableSetting } from "./settingsSearch";
2121
import { ProjectFavicon } from "../ProjectFavicon";
2222

23+
import { useRef, useState } from "react";
24+
import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip";
25+
26+
const ARCHIVE_ACTIONS = [
27+
{ action: "unarchive", label: "Unarchive", Icon: ArchiveX },
28+
{ action: "delete", label: "Delete", Icon: Trash2Icon },
29+
] as const;
30+
type ArchiveAction = (typeof ARCHIVE_ACTIONS)[number]["action"];
31+
2332
export function ArchivedThreadsPanel() {
2433
const { scope, groups: projectGroups, environments } = useSettingsScope();
2534
const { unarchiveThread, confirmAndDeleteThread } = useThreadActions();
35+
const pendingActionsRef = useRef(new Set<string>());
36+
const [pendingKeys, setPendingKeys] = useState<ReadonlySet<string>>(() => new Set());
2637
const {
2738
snapshots: archivedSnapshots,
2839
error: archiveError,
@@ -122,52 +133,57 @@ export function ArchivedThreadsPanel() {
122133
return { groups: sortedGroups, hasMultipleEnvironments };
123134
})();
124135

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,
136+
type ArchivedThread = (typeof archivedGroups)[number]["threads"][number];
137+
138+
const reportFailure = (result: AtomCommandResult<unknown, unknown> | null, title: string) => {
139+
if (result?._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
140+
const error = squashAtomCommandFailure(result);
141+
toastManager.add(
142+
stackedThreadToast({
143+
type: "error",
144+
title,
145+
description: error instanceof Error ? error.message : "An error occurred.",
146+
}),
137147
);
148+
};
138149

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-
}
150+
const handleThreadAction = async (action: ArchiveAction, thread: ArchivedThread) => {
151+
const target = scopeThreadRef(thread.environmentId, thread.id);
152+
const key = scopedThreadKey(target);
153+
// Context menus can outlive their render, so checks and cleanup use the live set.
154+
const pending = pendingActionsRef.current;
155+
if (pending.has(key)) return;
156+
pending.add(key);
157+
setPendingKeys(new Set(pending));
158+
const result = await settlePromise(async () => {
159+
const result = await (action === "unarchive"
160+
? unarchiveThread(target)
161+
: confirmAndDeleteThread(target, thread.title));
162+
if (result._tag === "Success") refreshArchivedThreads();
163+
return result;
164+
}).finally(() => {
165+
pending.delete(key);
166+
setPendingKeys(new Set(pending));
167+
});
168+
reportFailure(result._tag === "Failure" ? result : result.value, `Failed to ${action} thread`);
169+
};
155170

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+
const handleContextMenu = async (thread: ArchivedThread, position: { x: number; y: number }) => {
172+
const pending = pendingActionsRef.current;
173+
if (pending.has(scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)))) return;
174+
const result = await settlePromise(async () => {
175+
const clicked = await readLocalApi()?.contextMenu.show(
176+
ARCHIVE_ACTIONS.map(({ action, label }) => ({
177+
id: action,
178+
label,
179+
destructive: action === "delete",
180+
})),
181+
position,
182+
);
183+
if (clicked === "unarchive" || clicked === "delete")
184+
await handleThreadAction(clicked, thread);
185+
});
186+
reportFailure(result, "Archived thread action failed");
171187
};
172188

173189
return (
@@ -207,78 +223,50 @@ export function ArchivedThreadsPanel() {
207223
title={title}
208224
icon={<ProjectFavicon project={project} />}
209225
>
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-
))}
226+
{projectThreads.map((thread) => {
227+
const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
228+
return (
229+
<SettingsRow
230+
key={`${thread.environmentId}:${thread.id}`}
231+
onContextMenu={(event) => {
232+
event.preventDefault();
233+
void handleContextMenu(thread, { x: event.clientX, y: event.clientY });
234+
}}
235+
title={thread.title}
236+
description={
237+
<span className={hasMultipleEnvironments ? "block truncate" : undefined}>
238+
{hasMultipleEnvironments ? `${thread.environmentLabel} · ` : null}
239+
Archived <RelativeTime timestamp={thread.archivedAt ?? thread.createdAt} />
240+
{" \u00b7 Created "}
241+
<RelativeTime timestamp={thread.createdAt} />
242+
</span>
243+
}
244+
control={
245+
<div className="flex shrink-0 items-center gap-2">
246+
{ARCHIVE_ACTIONS.map(({ action, label, Icon }) => (
247+
<Tooltip key={action}>
248+
<TooltipTrigger
249+
render={
250+
<Button
251+
type="button"
252+
variant={action === "delete" ? "destructive-outline" : "outline"}
253+
size="icon-sm"
254+
aria-label={`${label} ${thread.title}`}
255+
disabled={pendingKeys.has(threadKey)}
256+
onClick={() => void handleThreadAction(action, thread)}
257+
/>
258+
}
259+
>
260+
<Icon className="size-3.5" />
261+
</TooltipTrigger>
262+
<TooltipPopup>{label}</TooltipPopup>
263+
</Tooltip>
264+
))}
265+
</div>
266+
}
267+
/>
268+
);
269+
})}
282270
</SettingsSection>
283271
))
284272
)}

‎apps/web/src/hooks/useThreadActions.permissions.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,18 @@ describe("thread action permissions", () => {
307307
},
308308
);
309309

310+
it("names an archived thread in its delete confirmation and preserves it on cancel", async () => {
311+
state.threads = [];
312+
state.scopes.get(secondary)!.add(AuthOrchestrationOperateScope);
313+
state.confirm.mockResolvedValue(false);
314+
await useThreadActions().confirmAndDeleteThread(target, "Archived release notes");
315+
expect(state.confirm).toHaveBeenCalledWith(
316+
expect.stringContaining('Delete thread "Archived release notes"?'),
317+
{ variant: "destructive" },
318+
);
319+
expect(state.requests).toEqual([]);
320+
});
321+
310322
it("deletes an archived thread only with its own environment's grant", async () => {
311323
state.threads = [];
312324
seedThreadPanelState(target);

‎apps/web/src/hooks/useThreadActions.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,14 +921,14 @@ export function useThreadActions() {
921921
(result) => result._tag === "Success",
922922
);
923923

924-
const confirmAndDeleteThread = async (target: ScopedThreadRef) => {
924+
const confirmAndDeleteThread = async (target: ScopedThreadRef, knownTitle?: string) => {
925925
const permissionFailure = threadOperationFailure(target);
926926
if (permissionFailure) return permissionFailure;
927927
const localApi = readLocalApi();
928928
const resolved = resolveThreadTarget(target);
929929

930930
if (confirmThreadDelete && localApi) {
931-
const title = resolved?.thread.title ?? "this thread";
931+
const title = resolved?.thread.title ?? knownTitle ?? "this thread";
932932
const confirmationResult = await settlePromise(() =>
933933
localApi.dialogs.confirm(
934934
[

0 commit comments

Comments
 (0)