import type { AgentAppearance } from "@paperclipai/shared"; import { AgentAvatar } from "@/components/AgentAvatar"; import type { MouseEvent, ReactNode } from "react"; import { MoreHorizontal, Play, LoaderCircle } from "lucide-react"; import { Link } from "@/lib/router"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Identity } from "./Identity"; import type { CompanyUserProfile } from "@/lib/company-members"; import { routineColumnGrid, type RoutineColumn } from "./RoutineTable"; import { ToggleSwitch } from "@/components/ui/toggle-switch"; export type RoutineListProjectSummary = { name: string; color?: string | null; }; export type RoutineListAgentSummary = { id?: string; appearance?: AgentAppearance | null; name: string; icon?: string | null; }; export type RoutineListRowItem = { id: string; title: string; status: string; projectId: string | null; assigneeAgentId: string | null; responsibleUserId?: string | null; lastRun?: { triggeredAt?: Date | string | null; status?: string | null; } | null; }; export function formatLastRunTimestamp(value: Date | string | null | undefined) { if (!value) return "Never"; return new Date(value).toLocaleString(); } export function formatRoutineRunStatus(value: string | null | undefined) { if (!value) return null; return value.replaceAll("_", " "); } export function nextRoutineStatus(currentStatus: string, enabled: boolean) { if (currentStatus === "archived" && enabled) return "active"; return enabled ? "active" : "paused"; } type RoutineRowProps = { routine: TRoutine; userById?: Map; projectById: Map; agentById: Map; runningRoutineId: string | null; statusMutationRoutineId: string | null; href: string; configureLabel?: string; managedByLabel?: string | null; secondaryDetails?: ReactNode; runNowButton?: boolean; disableRunNow?: boolean; disableToggle?: boolean; hideArchiveAction?: boolean; /** Render a bottom divider between consecutive rows. Off when the group is its own card. */ divider?: boolean; selected?: boolean; selectMode?: boolean; extraMenuItems?: ReactNode; onSelectChange?: (routine: TRoutine, selected: boolean) => void; onRunNow: (routine: TRoutine) => void; onToggleEnabled: (routine: TRoutine, enabled: boolean) => void; onToggleArchived?: (routine: TRoutine) => void; }; function openRoutineFromRow(event: MouseEvent) { if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; if ((event.target as Element).closest("a, button, input, [role='switch'], [role='checkbox']")) return; event.currentTarget.querySelector("[data-routine-link]")?.click(); } function routineRowParts({ routine, userById, projectById, agentById, runningRoutineId, statusMutationRoutineId, href, configureLabel = "Edit", managedByLabel, secondaryDetails, runNowButton = false, disableRunNow = false, disableToggle = false, hideArchiveAction = false, selected = false, extraMenuItems, onSelectChange, onRunNow, onToggleEnabled, onToggleArchived, }: RoutineRowProps) { const enabled = routine.status === "active"; const isArchived = routine.status === "archived"; const isStatusPending = statusMutationRoutineId === routine.id; const project = routine.projectId ? projectById.get(routine.projectId) ?? null : null; const agent = routine.assigneeAgentId ? agentById.get(routine.assigneeAgentId) ?? null : null; const isDraft = !isArchived && !routine.assigneeAgentId; const runDisabled = runningRoutineId === routine.id || isArchived || disableRunNow; const user = routine.responsibleUserId ? userById?.get(routine.responsibleUserId) : null; const projectCell = {routine.projectId ? project?.name ?? "Unknown project" : "No project"}; const agentCell = {routine.assigneeAgentId ? : null}{routine.assigneeAgentId ? agent?.name ?? "Unknown agent" : "No default agent"}; const userCell = user ? : {routine.responsibleUserId ? "Unavailable user" : "No responsible user"}; const lastRunCell = {formatLastRunTimestamp(routine.lastRun?.triggeredAt)}{routine.lastRun ? ` · ${formatRoutineRunStatus(routine.lastRun.status)}` : ""}; const enabledCell =
{ event.preventDefault(); event.stopPropagation(); }}> onToggleEnabled(routine, enabled)} disabled={isStatusPending || isArchived || disableToggle} aria-label={enabled ? `Disable ${routine.title}` : `Enable ${routine.title}`} />{isArchived ? "Archived" : isDraft ? "Draft" : enabled ? "On" : "Off"}
; const cellByColumn = { project: projectCell, assignee: agentCell, responsibleUser: userCell, lastRun: lastRunCell, enabled: enabledCell }; const title = (
{routine.title} {(isArchived || routine.status === "paused" || isDraft) ? ( {isArchived ? "archived" : isDraft ? "draft" : "paused"} ) : null} {managedByLabel ? ( {managedByLabel} ) : null}
{secondaryDetails ? (
{secondaryDetails}
) : null}
); const selection = (
{ event.stopPropagation(); }} > onSelectChange?.(routine, event.target.checked)} />
); const actions = (
{ event.preventDefault(); event.stopPropagation(); }}> {runNowButton ? ( ) : null} {configureLabel} onRunNow(routine)} > {runningRoutineId === routine.id ? "Running..." : "Run now"} {extraMenuItems ? ( <> {extraMenuItems} ) : null} onToggleEnabled(routine, enabled)} disabled={isStatusPending || isArchived || disableToggle} > {enabled ? "Pause" : "Enable"} {!hideArchiveAction && onToggleArchived ? ( onToggleArchived(routine)} disabled={isStatusPending} > {routine.status === "archived" ? "Restore" : "Archive"} ) : null}
); return { title, selection, actions, cellByColumn }; } export function RoutineTableRow(props: RoutineRowProps & { columns: RoutineColumn[]; userById: Map }) { const { title, selection, actions, cellByColumn } = routineRowParts(props); return
{props.selectMode ?
{selection}
: null}
{title}
{props.columns.map(column =>
{cellByColumn[column]}
)}
{actions}
; } export function RoutineListRow(props: RoutineRowProps) { const { title, selection, actions, cellByColumn } = routineRowParts(props); return
{props.selectMode ? selection : null}
{title}
{cellByColumn.project}{cellByColumn.assignee}{props.userById ? cellByColumn.responsibleUser : null}{cellByColumn.lastRun}
{cellByColumn.enabled}{actions}
; }