Files
kandev/apps/web/hooks/domains/threads/use-deck-thread.ts
T
Jérémy Coatelen 538813de02 feat(web): add Threads view for live agent conversations (#3112)
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
2026-09-03 08:07:16 +00:00

39 lines
1.5 KiB
TypeScript

"use client";
import { useAppStore } from "@/components/state-provider";
import { findTaskInSnapshots } from "@/lib/kanban/find-task";
import { isThreadTaskEligible, resolveTaskPendingAction } from "@/lib/threads/active-threads";
/**
* Whether this session can be opened in the Threads deck.
*
* Reads the already-loaded task sessions rather than fetching. A settled
* sibling can be selected when its task still has a live primary column, but
* an inactive task still hides the affordance because Threads has no column.
*/
export function useIsDeckThread(taskId: string | null, sessionId: string | null): boolean {
return useAppStore((state) => {
if (!taskId || !sessionId) return false;
const session = state.taskSessionsByTask.itemsByTaskId[taskId]?.find(
(candidate) => candidate.id === sessionId,
);
if (!session) return false;
const taskSessions = state.taskSessionsByTask.itemsByTaskId[taskId] ?? [];
const task = findTaskInSnapshots(taskId, state.kanbanMulti.snapshots, state.kanban.tasks);
const primarySession =
taskSessions.find((candidate) => candidate.is_primary) ??
(session.is_primary ? session : null);
return isThreadTaskEligible({
taskState: task?.state ?? null,
reviewStatus: task?.reviewStatus ?? null,
taskPendingAction: task ? resolveTaskPendingAction(task) : null,
primarySession: primarySession
? {
state: primarySession.state,
pendingAction: primarySession.pending_action,
}
: null,
});
});
}