"use client"; import { useMemo, useRef, useState, type ReactNode } from "react"; import { IconColumns } from "@tabler/icons-react"; import type { ActiveThread } from "@/lib/threads/active-threads"; import { useTranslation } from "react-i18next"; import { ThreadColumn } from "./thread-column"; import { useThreadColumnActivation } from "./use-thread-column-activation"; import { useThreadFocusRequest } from "./use-thread-focus-request"; import { useResponsiveBreakpoint } from "@/hooks/use-responsive-breakpoint"; import { MobileThreadPicker } from "./mobile-thread-picker"; import { ThreadTaskActionsProvider } from "./thread-task-actions"; import { useThreadSelectionRecovery } from "./use-thread-selection-recovery"; type ThreadsBoardProps = { threads: ActiveThread[]; isLoading?: boolean; /** Column a deep link asked for; scrolled into view and ringed on arrival. */ focusedTaskId?: string | null; /** * URL-driven callers must pass a stable request identity across target absence. * Omission keys dismissal to the resolved task ID for legacy callers, so a * temporary null target resets dismissal rather than retaining URL semantics. */ focusRequestKey?: string | null; /** Session a task-detail link asked the target column to select. */ focusedSessionId?: string | null; /** Removes a target session query after the target column proves it invalid. */ onInvalidRequestedSession?: (taskId: string, sessionId: string) => void; onOpenTask: (taskId: string) => void; /** Composes the page header with the viewport's active phone thread. */ renderHeader?: (activeMobileTaskId: string | null) => ReactNode; }; function ThreadsPlaceholder({ testId, children }: { testId: string; children: React.ReactNode }) { return (
{children}
); } function ThreadsEmptyState() { const { t } = useTranslation(); return ( ); } function ThreadsLoadingState() { const { t } = useTranslation(); return (

{t("threads:loading")}

); } function focusThreadPicker(event: Event, column: Element | undefined) { const trigger = column?.querySelector('[data-testid="thread-picker-trigger"]'); if (!trigger) return; event.preventDefault(); trigger.focus({ preventScroll: true }); } /** * The deck: every live agent conversation as its own column, scrolled * horizontally. Columns keep the order the selector gave them, so a thread the * reader is following does not jump while they read it. */ export function ThreadsBoard({ threads, isLoading = false, focusedTaskId = null, focusRequestKey = focusedTaskId, focusedSessionId = null, onInvalidRequestedSession, onOpenTask, renderHeader, }: ThreadsBoardProps) { const { markedTaskId, activationTaskId, retire } = useThreadFocusRequest( focusedTaskId, focusRequestKey, ); const { isMobile } = useResponsiveBreakpoint(); const [pickerOpen, setPickerOpen] = useState(false); if (!isMobile && pickerOpen) setPickerOpen(false); const returnFocusTaskId = useRef(null); const orderedIds = useMemo(() => threads.map((thread) => thread.taskId), [threads]); const { boardRef, registerColumn, preloadTaskIds, detailTaskIds, mobileTaskId } = useThreadColumnActivation(orderedIds, activationTaskId); const rememberThread = useThreadSelectionRecovery(orderedIds, boardRef, isMobile); function taskColumn(taskId: string | null) { return Array.from(boardRef.current?.children ?? []).find( (element) => element.getAttribute("data-thread-column-id") === taskId, ); } function selectThread(taskId: string) { returnFocusTaskId.current = taskId; taskColumn(taskId)?.scrollIntoView({ inline: "start", block: "nearest", behavior: "instant" }); setPickerOpen(false); } function restorePickerFocus(event: Event) { const column = taskColumn(returnFocusTaskId.current) ?? taskColumn(mobileTaskId) ?? taskColumn(orderedIds[0] ?? null); focusThreadPicker(event, column); } return (
{renderHeader?.(mobileTaskId)} {threads.length === 0 ? (
{isLoading ? : }
) : (
{ retire(); rememberThread(event); }} onFocusCapture={(event) => { retire(); rememberThread(event); }} className="flex min-h-0 min-w-0 w-full flex-1 snap-x snap-mandatory overflow-x-auto overflow-y-hidden overscroll-x-contain md:gap-3 md:p-3 md:snap-none" > {threads.map((thread) => ( { returnFocusTaskId.current = thread.taskId; setPickerOpen(true); }, } : undefined } isFocused={thread.taskId === markedTaskId} requestedSessionId={thread.taskId === focusedTaskId ? focusedSessionId : null} isPreloaded={preloadTaskIds.has(thread.taskId)} isDetailActive={detailTaskIds.has(thread.taskId)} onInvalidRequestedSession={onInvalidRequestedSession} onColumnRef={registerColumn} onOpenTask={onOpenTask} /> ))}
)} {isMobile && threads.length > 0 && ( )}
); }