"use client"; import { memo, useCallback, useMemo, useState } from "react"; import { IconDotsVertical, IconPlus, IconStar } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@kandev/ui/dropdown-menu"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@kandev/ui/alert-dialog"; import { AgentLogo } from "@/components/agent-logo"; import { useAppStore } from "@/components/state-provider"; import { useTaskSessions } from "@/hooks/use-task-sessions"; import { useSessionActions, isSessionStoppable, isSessionDeletable, isSessionResumable, } from "@/hooks/domains/session/use-session-actions"; import { NewSessionDialog } from "../new-session-dialog"; import { MobilePillButton } from "./mobile-pill-button"; import { MobilePickerSheet } from "./mobile-picker-sheet"; import { formatTaskSessionStateLabel } from "@/lib/ui/state-labels"; import type { TaskSession, TaskSessionState } from "@/lib/types/http"; import type { AgentProfileOption } from "@/lib/state/slices"; type SessionRow = { id: string; agentName: string | null; agentLabel: string; state: TaskSessionState | null; isPrimary: boolean; index: number; startedAt: string; }; function buildSessionRows( sessions: TaskSession[], agentProfiles: AgentProfileOption[], primarySessionId: string | null | undefined, ): SessionRow[] { const sorted = [...sessions].sort( (a, b) => new Date(a.started_at).getTime() - new Date(b.started_at).getTime(), ); return sorted.map((s, idx) => { const profile = agentProfiles.find((p) => p.id === s.agent_profile_id); const labelParts = profile?.label.split(" • ") ?? []; return { id: s.id, agentName: profile?.agent_name ?? null, agentLabel: labelParts[1] || labelParts[0] || profile?.label || "Agent", state: (s.state as TaskSessionState | undefined) ?? null, isPrimary: primarySessionId ? s.id === primarySessionId : !!s.is_primary, index: idx + 1, startedAt: s.started_at, }; }); } const STATE_TONE: Partial> = { RUNNING: "bg-emerald-500/15 text-emerald-600 dark:text-emerald-400", STARTING: "bg-emerald-500/15 text-emerald-600 dark:text-emerald-400", WAITING_FOR_INPUT: "bg-amber-500/15 text-amber-600 dark:text-amber-400", FAILED: "bg-destructive/15 text-destructive", }; function StateBadge({ state }: { state: TaskSessionState | null }) { if (!state) return null; const tone = STATE_TONE[state] ?? "bg-foreground/10 text-muted-foreground"; return ( {formatTaskSessionStateLabel(state)} ); } function SessionActionsMenu({ state, isPrimary, onSetPrimary, onStop, onResume, onAskDelete, }: { state: TaskSessionState | null; isPrimary: boolean; onSetPrimary: () => void; onStop: () => void; onResume: () => void; onAskDelete: () => void; }) { return ( e.stopPropagation()}> Set as Primary {state && isSessionStoppable(state) && ( Stop )} {state && isSessionResumable(state) && ( Resume )} {state && isSessionDeletable(state) && ( Delete )} ); } function DeleteSessionConfirmDialog({ open, onOpenChange, isPrimary, isOnlySession, onConfirm, }: { open: boolean; onOpenChange: (open: boolean) => void; isPrimary: boolean; isOnlySession: boolean; onConfirm: () => void; }) { return ( Delete session?

This will permanently delete the conversation history with this session.

{isPrimary && !isOnlySession && (

This is the primary session. Another session will be set as primary.

)} {isOnlySession && (

This is the only session for this task.

)}
Cancel { onOpenChange(false); onConfirm(); }} className="cursor-pointer bg-destructive text-destructive-foreground hover:bg-destructive/90" > Delete
); } function SessionRowItem({ row, taskId, isActive, totalSessions, onSelect, }: { row: SessionRow; taskId: string; isActive: boolean; totalSessions: number; onSelect: (sessionId: string) => void; }) { const [confirmDelete, setConfirmDelete] = useState(false); const actions = useSessionActions({ sessionId: row.id, taskId }); const isOnly = totalSessions === 1; const showBadges = totalSessions > 1; return ( <>
onSelect(row.id)} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); onSelect(row.id); } }} data-testid={`mobile-session-row-${row.id}`} className={`flex items-center gap-2 px-2 py-2 rounded-md cursor-pointer select-none ${ isActive ? "bg-accent" : "hover:bg-accent/50" }`} > {row.isPrimary && showBadges && ( )} {showBadges && ( {row.index} )} {row.agentName && } {row.agentLabel} void actions.setPrimary()} onStop={() => void actions.stop()} onResume={() => void actions.resume()} onAskDelete={() => setConfirmDelete(true)} />
void actions.remove()} /> ); } function useSessionRows(taskId: string | null) { const agentProfiles = useAppStore((s) => s.agentProfiles.items); const primarySessionId = useAppStore((s) => { if (!taskId) return null; const task = s.kanban.tasks.find((t: { id: string }) => t.id === taskId); return task?.primarySessionId ?? null; }); const { sessions, isLoading } = useTaskSessions(taskId); const rows = useMemo( () => buildSessionRows(sessions, agentProfiles, primarySessionId), [sessions, agentProfiles, primarySessionId], ); return { rows, isLoading, primarySessionId }; } const MobileSessionsList = memo(function MobileSessionsList({ taskId, onClose, }: { taskId: string | null; onClose: () => void; }) { const setActiveSession = useAppStore((s) => s.setActiveSession); const activeSessionId = useAppStore((s) => s.tasks.activeSessionId); const { rows, isLoading } = useSessionRows(taskId); const [launchOpen, setLaunchOpen] = useState(false); const handleSelect = useCallback( (sessionId: string) => { if (!taskId) return; setActiveSession(taskId, sessionId); onClose(); }, [taskId, setActiveSession, onClose], ); if (!taskId) { return (
No active task
); } return (
{rows.length} session{rows.length === 1 ? "" : "s"}
{isLoading && rows.length === 0 && (
Loading sessions…
)} {!isLoading && rows.length === 0 && (
No sessions yet. Launch one to get started.
)} {rows.map((row) => ( ))}
{launchOpen && ( )}
); }); function useActiveSessionPillLabel(taskId: string | null): { label: string; count: string | undefined; } { const activeSessionId = useAppStore((s) => s.tasks.activeSessionId); const { rows } = useSessionRows(taskId); const activeRow = rows.find((r) => r.id === activeSessionId); const total = rows.length; const idx = activeRow?.index; let count: string | undefined; if (total > 1 && idx) count = `${idx}/${total}`; else if (total > 1) count = `${total}`; return { label: activeRow?.agentLabel ?? "Session", count }; } export const MobileSessionsPicker = memo(function MobileSessionsPicker({ taskId, compact, fullWidth, }: { taskId: string | null; compact?: boolean; fullWidth?: boolean; }) { const [open, setOpen] = useState(false); const { label, count } = useActiveSessionPillLabel(taskId); if (!taskId) return null; return ( <> setOpen(true)} data-testid="mobile-sessions-pill" ariaLabel={`Active session: ${label}. Tap to switch.`} /> setOpen(false)} /> ); });