Files
kandev/apps/web/hooks/use-task-archive-confirm.ts
T
João Salavisa ac520a8cdc feat(web): archive the current task from the command palette (#2346)
Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 11:29:24 +02:00

58 lines
2.3 KiB
TypeScript

import { useCallback, useState } from "react";
import { useTranslation } from "react-i18next";
import { useAppStoreApi } from "@/components/state-provider";
import { useArchiveAndSwitchTask } from "@/hooks/use-task-actions";
import { useToast } from "@/components/toast-provider";
import { findTaskInSnapshots } from "@/lib/kanban/find-task";
export type ArchiveConfirmTarget = { title: string; executorType?: string | null };
/**
* Archive flow shared by every task-scoped archive entry point (the terminal-state
* PR banners, the command palette). Archiving always goes through the same
* preference-aware confirmation dialog: `requestArchive` only resolves the target
* task, and `TaskArchiveConfirmDialog` decides whether to prompt or bypass.
* Only failures toast; on success the archive-and-switch flow moves the user to
* the next task.
*/
export function useTaskArchiveConfirm(taskId: string | null) {
const { t } = useTranslation();
const store = useAppStoreApi();
const archiveAndSwitch = useArchiveAndSwitchTask();
const { toast } = useToast();
const [target, setTarget] = useState<ArchiveConfirmTarget | null>(null);
const [isPending, setIsPending] = useState(false);
const requestArchive = useCallback(() => {
if (!taskId) return;
const state = store.getState();
const task = findTaskInSnapshots(taskId, state.kanbanMulti.snapshots, state.kanban.tasks);
setTarget({
title: task?.title ?? t("task:thisTask"),
executorType: task?.primaryExecutorType,
});
}, [store, t, taskId]);
const closeConfirm = useCallback(() => setTarget(null), []);
const confirmArchive = useCallback(
async ({ cascade }: { cascade: boolean }) => {
if (!taskId) return;
setIsPending(true);
try {
await archiveAndSwitch(taskId, { cascade });
} catch (error) {
// The toast is deliberately generic; keep the rejection reason in the
// console so a failed archive is diagnosable from a user's devtools.
console.error("Failed to archive task:", error);
toast({ description: t("task:failedToArchiveTask"), variant: "error" });
} finally {
setIsPending(false);
}
},
[archiveAndSwitch, t, taskId, toast],
);
return { target, requestArchive, closeConfirm, confirmArchive, isPending };
}