import { useId, useState, type ReactNode } from "react"; import type { Issue, RoutineDetail, RoutineTrigger } from "@paperclipai/shared"; import { ArrowLeft, CalendarClock, Check, ChevronDown, Copy, GitBranch, History, KeyRound, MessageSquare, MoreHorizontal, Pencil, Radar, ScrollText, Sparkles, Terminal, Trash2, Webhook, } from "lucide-react"; import { AgentAvatar, type AvatarAgent } from "@/components/AgentAvatar"; import { RunButton } from "@/components/AgentActionButtons"; import { MarkdownBody } from "@/components/MarkdownBody"; import { ScheduleEditor } from "@/components/ScheduleEditor"; import { TaskSummaryList } from "@/components/TaskSummaryList"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { ToggleSwitch } from "@/components/ui/toggle-switch"; import { deriveInitials } from "@/components/Identity"; import { copyTextToClipboard } from "@/lib/clipboard"; import { Link } from "@/lib/router"; import { cn } from "@/lib/utils"; import { ROUTINE_STATE_LABELS, WEBHOOK_HEALTH_LABELS, customRunRules, describeScheduleTrigger, formatNextRun, instructionsPreview, nextScheduledRun, routineAutomationState, routinePausedNotice, routineStatusDetail, webhookHealth, webhookTitle, } from "./routine-summary"; export type RoutinePerson = { id: string; name: string; avatarUrl?: string | null }; export type RoutinePageAgent = AvatarAgent & { id: string; name: string; urlKey?: string | null }; export type RoutinePageProject = { id: string; name: string; color?: string | null; urlKey?: string | null }; /** A responsibility this routine is under: linked itself, or through its project. */ export type RoutineResponsibility = { id: string; title: string; areaTitle?: string | null; via: "direct" | "project"; }; export type RoutineScheduleChange = { cronExpression: string; timezone: string }; export type RoutinePageProps = { routine: RoutineDetail; agent: RoutinePageAgent | null; project: RoutinePageProject | null; responsibleUser: RoutinePerson | null; /** Leave undefined while areas of responsibility are switched off; the field is then hidden. */ responsibilities?: readonly RoutineResponsibility[]; /** Tasks this routine created, newest first. Pass the pages loaded so far. */ history: readonly Issue[]; historyLoading?: boolean; /** Shown in place of the task list when History could not be loaded. */ historyError?: ReactNode; /** More tasks exist beyond the loaded pages. */ hasMoreHistory?: boolean; loadingMoreHistory?: boolean; onLoadMoreHistory?: () => void; /** Recent runs that made no task because an earlier run was still working. */ skippedRunCount?: number; /** Open comments on the instructions. */ instructionCommentCount?: number; statusPending?: boolean; onToggleActive: (active: boolean) => void; onEdit: () => void; onSaveSchedule: (triggerId: string, change: RoutineScheduleChange) => void; onRemoveSchedule: (triggerId: string) => void; onOpenWebhook: (trigger: RoutineTrigger) => void; /** Pauses or resumes one schedule, standing check or API trigger. Omit to hide Pause/Resume. */ onSetTriggerPaused?: (triggerId: string, paused: boolean) => void; /** Removes a standing check or API trigger. Omit to keep those rows read-only. */ onRemoveTrigger?: (trigger: RoutineTrigger) => void; /** Triggers removed from this page, offered back with Undo under Repeats. */ removedTriggers?: readonly RoutineTrigger[]; onRestoreTrigger?: (trigger: RoutineTrigger) => void; onOpenInstructionComments?: () => void; /** Show the whole instructions, e.g. while their comments are open. */ instructionsExpanded?: boolean; /** Wraps the rendered instructions, e.g. with the comment layer that anchors to them. */ renderInstructionsBody?: (body: ReactNode) => ReactNode; /** Router state for task links in History, so a task can lead back here. */ historyLinkState?: unknown; }; function Field({ label, children, action }: { label: string; children: ReactNode; action?: ReactNode }) { const headingId = useId(); return (

{label}

{action}
{children}
); } function PersonAvatar({ person }: { person: RoutinePerson }) { return ( ); } function localTimezone(): string { try { return Intl.DateTimeFormat().resolvedOptions().timeZone; } catch { return "UTC"; } } const rowButtonClass = "group -mx-2 flex min-w-0 items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm hover:bg-accent/50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"; function ScheduleRow({ trigger, routineActive, onSave, onRemove, onSetPaused, }: { trigger: RoutineTrigger; routineActive: boolean; onSave: (change: RoutineScheduleChange) => void; onRemove: () => void; onSetPaused?: (paused: boolean) => void; }) { const [open, setOpen] = useState(false); const [cron, setCron] = useState(trigger.cronExpression ?? "0 9 * * 1-5"); const [valid, setValid] = useState(true); const timezone = trigger.timezone ?? localTimezone(); const dimmed = !trigger.enabled || !routineActive; return ( { setOpen(next); if (next) setCron(trigger.cronExpression ?? "0 9 * * 1-5"); }} > {/* Wider when Pause/Resume joins Remove and Save, so the footer stays on one row. */}

Times are in {timezone}.

{onSetPaused ? ( ) : null}
); } function WebhookRow({ trigger, routineActive, onOpen, }: { trigger: RoutineTrigger; routineActive: boolean; onOpen: () => void; }) { const health = webhookHealth(trigger); const ignored = !routineActive && health !== "setup" && health !== "testing"; const Icon = trigger.signingMode === "github_hmac" ? GitBranch : Webhook; return ( ); } /** A standing check or API trigger: nothing to configure, but it can be paused or removed. */ function StaticTriggerRow({ icon: Icon, title, detail, description, trigger, routineActive, onSetPaused, onRemove, }: { icon: typeof Radar; title: string; detail: string; description: string; trigger: RoutineTrigger; routineActive: boolean; onSetPaused?: (paused: boolean) => void; onRemove?: () => void; }) { const [open, setOpen] = useState(false); const dimmed = !trigger.enabled || !routineActive; const content = ( <>