Skip to content

Commit 5c91426

Browse files
voltcrashclaude
andcommitted
feat(web): unarchive or delete all archived threads at once
Add Unarchive all and Delete all to the Archive settings page, both for the whole current scope and for each project group. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent a727d1d commit 5c91426

3 files changed

Lines changed: 247 additions & 3 deletions

File tree

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

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import { SettingsGroup } from "./SettingsGroup";
22
import { Spinner } from "~/components/ui/spinner";
33
import { NotificationSettings } from "./NotificationSettings";
4-
import { ArchiveIcon, ArchiveX, CheckIcon, ChevronRightIcon, SettingsIcon } from "lucide-react";
4+
import {
5+
ArchiveIcon,
6+
ArchiveX,
7+
CheckIcon,
8+
ChevronRightIcon,
9+
EllipsisIcon,
10+
SettingsIcon,
11+
Trash2Icon,
12+
} from "lucide-react";
513
import { Link, useNavigate } from "@tanstack/react-router";
614
import type { CSSProperties, ReactNode } from "react";
715
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
816
import {
917
type BackgroundActivityProfile,
1018
type DesktopUpdateChannel,
19+
type EnvironmentId,
1120
ProviderDriverKind,
1221
type ProviderInstanceId,
1322
type ScopedThreadRef,
1423
type SidebarProjectGroupingMode,
24+
type ThreadId,
1525
} from "@t3tools/contracts";
1626
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
1727
import {
@@ -107,6 +117,7 @@ import {
107117
AlertDialogTitle,
108118
} from "../ui/alert-dialog";
109119
import { Button } from "../ui/button";
120+
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
110121
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
111122
import {
112123
Dialog,
@@ -3281,7 +3292,9 @@ export function GeneralSettingsPanel() {
32813292

32823293
export function ArchivedThreadsPanel() {
32833294
const { scope } = useSettingsScope();
3284-
const { unarchiveThread, confirmAndDeleteThread } = useThreadActions();
3295+
const { unarchiveThread, unarchiveThreads, confirmAndDeleteThread, deleteArchivedThreads } =
3296+
useThreadActions();
3297+
const [isBulkActionPending, setIsBulkActionPending] = useState(false);
32853298
const {
32863299
snapshots: archivedSnapshots,
32873300
error: archiveError,
@@ -3388,8 +3401,98 @@ export function ArchivedThreadsPanel() {
33883401
[confirmAndDeleteThread, refreshArchivedThreads, unarchiveThread],
33893402
);
33903403

3404+
const archivedThreadCount = useMemo(
3405+
() => archivedGroups.reduce((count, group) => count + group.threads.length, 0),
3406+
[archivedGroups],
3407+
);
3408+
3409+
/** Unarchives or deletes every listed thread; `scopeLabel` names the set in the delete prompt. */
3410+
const handleBulkArchivedThreadAction = useCallback(
3411+
async (
3412+
action: "unarchive" | "delete",
3413+
threads: ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly id: ThreadId }>,
3414+
scopeLabel: string,
3415+
) => {
3416+
if (threads.length === 0) return;
3417+
if (action === "delete") {
3418+
const api = readLocalApi();
3419+
if (!api) return;
3420+
// Always confirm: one click here can erase many threads at once.
3421+
const confirmed = await settlePromise(() =>
3422+
api.dialogs.confirm(
3423+
[
3424+
`Delete ${threads.length} archived thread${threads.length === 1 ? "" : "s"}${scopeLabel}?`,
3425+
"This permanently clears conversation history for these threads.",
3426+
].join("\n"),
3427+
{ variant: "destructive" },
3428+
),
3429+
);
3430+
if (confirmed._tag === "Failure" || !confirmed.value) return;
3431+
}
3432+
setIsBulkActionPending(true);
3433+
const targets = threads.map((thread) => scopeThreadRef(thread.environmentId, thread.id));
3434+
const failure = await (action === "unarchive"
3435+
? unarchiveThreads(targets)
3436+
: deleteArchivedThreads(targets));
3437+
setIsBulkActionPending(false);
3438+
if (failure !== null) {
3439+
const error = squashAtomCommandFailure(failure);
3440+
toastManager.add(
3441+
stackedThreadToast({
3442+
type: "error",
3443+
title:
3444+
action === "unarchive" ? "Failed to unarchive threads" : "Failed to delete threads",
3445+
description: error instanceof Error ? error.message : "An error occurred.",
3446+
}),
3447+
);
3448+
}
3449+
},
3450+
[deleteArchivedThreads, unarchiveThreads],
3451+
);
3452+
33913453
return (
33923454
<SettingsPageContainer>
3455+
{archivedGroups.length > 0 ? (
3456+
<div className="flex flex-wrap items-center justify-between gap-x-4 gap-y-2 px-3 sm:px-4">
3457+
<p className="text-sm text-muted-foreground">
3458+
{archivedThreadCount} archived thread{archivedThreadCount === 1 ? "" : "s"}
3459+
</p>
3460+
<div className="flex items-center gap-1.5">
3461+
<Button
3462+
type="button"
3463+
variant="outline"
3464+
size="xs"
3465+
disabled={isBulkActionPending}
3466+
onClick={() => {
3467+
void handleBulkArchivedThreadAction(
3468+
"unarchive",
3469+
archivedGroups.flatMap((group) => group.threads),
3470+
"",
3471+
);
3472+
}}
3473+
>
3474+
<ArchiveX className="size-3.5" />
3475+
<span>Unarchive all</span>
3476+
</Button>
3477+
<Button
3478+
type="button"
3479+
variant="destructive-outline"
3480+
size="xs"
3481+
disabled={isBulkActionPending}
3482+
onClick={() => {
3483+
void handleBulkArchivedThreadAction(
3484+
"delete",
3485+
archivedGroups.flatMap((group) => group.threads),
3486+
archivedGroups.length === 1 ? ` in ${archivedGroups[0]!.project.title}` : "",
3487+
);
3488+
}}
3489+
>
3490+
<Trash2Icon className="size-3.5" />
3491+
<span>Delete all</span>
3492+
</Button>
3493+
</div>
3494+
</div>
3495+
) : null}
33933496
{archivedGroups.length === 0 ? (
33943497
<SettingsSection
33953498
id={isLoadingArchive ? undefined : searchableSetting("archive").id}
@@ -3424,6 +3527,46 @@ export function ArchivedThreadsPanel() {
34243527
id={index === 0 ? searchableSetting("archive").id : undefined}
34253528
title={project.title}
34263529
icon={<ProjectFavicon project={project} />}
3530+
headerAction={
3531+
<Menu>
3532+
<MenuTrigger
3533+
render={
3534+
<Button
3535+
type="button"
3536+
variant="ghost-muted"
3537+
size="icon-xs"
3538+
disabled={isBulkActionPending}
3539+
aria-label={`Archived thread actions for ${project.title}`}
3540+
/>
3541+
}
3542+
>
3543+
<EllipsisIcon className="size-3.5" />
3544+
</MenuTrigger>
3545+
<MenuPopup align="end">
3546+
<MenuItem
3547+
onClick={() => {
3548+
void handleBulkArchivedThreadAction("unarchive", projectThreads, "");
3549+
}}
3550+
>
3551+
<ArchiveX />
3552+
Unarchive all in project
3553+
</MenuItem>
3554+
<MenuItem
3555+
variant="destructive"
3556+
onClick={() => {
3557+
void handleBulkArchivedThreadAction(
3558+
"delete",
3559+
projectThreads,
3560+
` in ${project.title}`,
3561+
);
3562+
}}
3563+
>
3564+
<Trash2Icon />
3565+
Delete all in project
3566+
</MenuItem>
3567+
</MenuPopup>
3568+
</Menu>
3569+
}
34273570
>
34283571
{projectThreads.map((thread) => (
34293572
<SettingsRow

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,57 @@
1+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
12
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3+
import * as Cause from "effect/Cause";
4+
import { AsyncResult } from "effect/unstable/reactivity";
25
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
36

47
import {
58
navigateAfterThreadDeletion,
69
requestThreadUnpinConfirmation,
10+
runArchivedThreadBatch,
711
ThreadArchiveBlockedError,
812
} from "./useThreadActions";
913
import { toastManager } from "../components/ui/toast";
14+
import { refreshArchivedThreadsForEnvironment } from "../lib/archivedThreadsState";
15+
16+
vi.mock("../lib/archivedThreadsState", () => ({
17+
refreshArchivedThreadsForEnvironment: vi.fn(),
18+
}));
19+
20+
describe("runArchivedThreadBatch", () => {
21+
afterEach(() => vi.clearAllMocks());
22+
23+
const local = EnvironmentId.make("environment-local");
24+
const remote = EnvironmentId.make("environment-remote");
25+
const targets = [
26+
scopeThreadRef(local, ThreadId.make("thread-1")),
27+
scopeThreadRef(local, ThreadId.make("thread-2")),
28+
scopeThreadRef(remote, ThreadId.make("thread-3")),
29+
];
30+
31+
it("keeps going after a failure and refreshes each environment once", async () => {
32+
const firstFailure = AsyncResult.failure(Cause.fail(new Error("first")));
33+
const results = [
34+
firstFailure,
35+
AsyncResult.failure(Cause.fail(new Error("second"))),
36+
AsyncResult.success(undefined),
37+
];
38+
const run = vi.fn(async () => results.shift()!);
39+
40+
await expect(runArchivedThreadBatch(targets, run)).resolves.toBe(firstFailure);
41+
42+
expect(run).toHaveBeenCalledTimes(3);
43+
expect(vi.mocked(refreshArchivedThreadsForEnvironment).mock.calls).toEqual([[local], [remote]]);
44+
});
45+
46+
it("stops at an interruption without reporting it", async () => {
47+
const run = vi.fn(async () => AsyncResult.failure(Cause.interrupt()));
48+
49+
await expect(runArchivedThreadBatch(targets, run)).resolves.toBeNull();
50+
51+
expect(run).toHaveBeenCalledOnce();
52+
expect(refreshArchivedThreadsForEnvironment).toHaveBeenCalledTimes(2);
53+
});
54+
});
1055

1156
describe("navigateAfterThreadDeletion", () => {
1257
afterEach(() => vi.restoreAllMocks());

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import {
44
scopeThreadRef,
55
scopedThreadKey,
66
} from "@t3tools/client-runtime/environment";
7-
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
7+
import {
8+
type AtomCommandResult,
9+
isAtomCommandInterrupted,
10+
settlePromise,
11+
squashAtomCommandFailure,
12+
} from "@t3tools/client-runtime/state/runtime";
813
import { canSnooze, threadWokeAt } from "@t3tools/client-runtime/state/thread-settled";
914
import { EnvironmentId, type ScopedThreadRef, ThreadId } from "@t3tools/contracts";
1015
import { resolveWorktreeCleanup } from "@t3tools/shared/projectSettings";
@@ -190,6 +195,28 @@ export async function navigateAfterThreadDeletion(navigate: () => Promise<void>)
190195
}
191196
}
192197

198+
/**
199+
* Runs one command per archived thread in order, then refreshes each touched
200+
* environment's archive once instead of once per thread. Stops at the first
201+
* interruption and reports the first failure.
202+
*/
203+
export async function runArchivedThreadBatch(
204+
targets: readonly ScopedThreadRef[],
205+
run: (target: ScopedThreadRef) => Promise<AtomCommandResult<unknown, unknown>>,
206+
) {
207+
let firstFailure: AsyncResult.Failure<unknown, unknown> | null = null;
208+
for (const target of targets) {
209+
const result = await run(target);
210+
if (result._tag === "Success") continue;
211+
if (isAtomCommandInterrupted(result)) break;
212+
firstFailure ??= result;
213+
}
214+
for (const environmentId of new Set(targets.map((target) => target.environmentId))) {
215+
refreshArchivedThreadsForEnvironment(environmentId);
216+
}
217+
return firstFailure;
218+
}
219+
193220
export function useThreadActions() {
194221
const closeTerminal = useAtomCommand(terminalEnvironment.close);
195222
const archiveThreadMutation = useAtomCommand(threadEnvironment.archive, {
@@ -292,6 +319,31 @@ export function useThreadActions() {
292319
[router, unarchiveThreadMutation],
293320
);
294321

322+
const unarchiveThreads = useCallback(
323+
(targets: readonly ScopedThreadRef[]) =>
324+
runArchivedThreadBatch(targets, (target) => {
325+
ThreadUndo.invalidate("archive", scopedThreadKey(target));
326+
return unarchiveThreadMutation({
327+
environmentId: target.environmentId,
328+
input: { threadId: target.threadId },
329+
});
330+
}),
331+
[unarchiveThreadMutation],
332+
);
333+
334+
// Archived threads have no live session, worktree prompt, or route to leave;
335+
// the server's deletion reactor stops leftover sessions and terminals.
336+
const deleteArchivedThreads = useCallback(
337+
(targets: readonly ScopedThreadRef[]) =>
338+
runArchivedThreadBatch(targets, (target) =>
339+
deleteThreadMutation({
340+
environmentId: target.environmentId,
341+
input: { threadId: target.threadId },
342+
}),
343+
),
344+
[deleteThreadMutation],
345+
);
346+
295347
const archiveThread = useCallback(
296348
async (target: ScopedThreadRef, opts: { onArchived?: () => void } = {}) => {
297349
const resolved = resolveThreadTarget(target);
@@ -901,7 +953,9 @@ export function useThreadActions() {
901953
() => ({
902954
archiveThread,
903955
unarchiveThread,
956+
unarchiveThreads,
904957
deleteThread,
958+
deleteArchivedThreads,
905959
confirmAndDeleteThread,
906960
settleThread,
907961
unsettleThread,
@@ -918,6 +972,7 @@ export function useThreadActions() {
918972
archiveThread,
919973
confirmAndDeleteThread,
920974
confirmAndUnpinThread,
975+
deleteArchivedThreads,
921976
deleteThread,
922977
pinThread,
923978
reorderPinnedThread,
@@ -926,6 +981,7 @@ export function useThreadActions() {
926981
settleThread,
927982
snoozeThread,
928983
unarchiveThread,
984+
unarchiveThreads,
929985
unpinThread,
930986
unsettleThread,
931987
unsnoozeThread,

0 commit comments

Comments
 (0)