The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 3 | import { ThreadDetailsControl } from "./ThreadDetailsControl"; |
| 4 | import { useNavigate } from "@tanstack/react-router"; |
| 5 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 6 | import type { MessageResolver } from "~/i18n/messages"; |
| 7 | import { CalendarClockIcon, PencilIcon, PlayIcon, Settings2Icon } from "lucide-react"; |
| 8 | import { useState } from "react"; |
| 9 | import { |
| 10 | AuthOrchestrationOperateScope, |
| 11 | type EnvironmentId, |
| 12 | type ScheduledTask, |
| 13 | type ThreadId, |
| 14 | } from "@t3tools/contracts"; |
| 15 | import { |
| 16 | isAtomCommandInterrupted, |
| 17 | squashAtomCommandFailure, |
| 18 | } from "@t3tools/client-runtime/state/runtime"; |
| 19 | |
| 20 | import { ThreadDetailsSection } from "./ThreadDetailsSection"; |
| 21 | import { cn } from "../../lib/utils"; |
| 22 | import { ScheduledRelativeLabel } from "../settings/ScheduledTasksSettings"; |
| 23 | import { scheduleLabel } from "../settings/scheduledTasksSettings.logic"; |
| 24 | import { useEnvironmentQuery } from "../../state/query"; |
| 25 | import { serverEnvironment } from "../../state/server"; |
| 26 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 27 | |
| 28 | import { readEnvironmentScope } from "../../state/session"; |
| 29 | import { Switch } from "../ui/switch"; |
| 30 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 31 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 32 | import { |
| 33 | THREAD_DETAILS_PANEL_ICON_CLASS, |
| 34 | THREAD_DETAILS_PANEL_ROW_CONTENT_CLASS, |
| 35 | } from "./threadDetailsPanelStyles"; |
| 36 | |
| 37 | const STATUS_DOT_CLASS: Record<ScheduledTask["lastRunStatus"], string> = { |
| 38 | never: "bg-muted-foreground/40", |
| 39 | running: "bg-sky-500", |
| 40 | succeeded: "bg-emerald-500", |
| 41 | failed: "bg-destructive", |
| 42 | }; |
| 43 | |
| 44 | function taskScheduleLine( |
| 45 | task: ScheduledTask, |
| 46 | timeZone: string | undefined, |
| 47 | resolve: MessageResolver, |
| 48 | ) { |
| 49 | return ( |
| 50 | <> |
| 51 | {scheduleLabel(task.schedule, timeZone, task.nextRunAt, resolve)} |
| 52 | {task.blockedByPolicy !== undefined ? ( |
| 53 | ` · ${task.blockedByPolicy}` |
| 54 | ) : task.enabled && task.nextRunAt !== null ? ( |
| 55 | <> |
| 56 | {" · "} |
| 57 | <T> |
| 58 | next <ScheduledRelativeLabel value={task.nextRunAt} /> |
| 59 | </T> |
| 60 | </> |
| 61 | ) : task.enabled ? ( |
| 62 | "" |
| 63 | ) : ( |
| 64 | <> |
| 65 | {" · "} |
| 66 | <T context="automation state">paused</T> |
| 67 | </> |
| 68 | )} |
| 69 | </> |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | export function ThreadAutomationsPanel(props: { |
| 74 | readonly environmentId: EnvironmentId; |
| 75 | readonly threadId: ThreadId; |
| 76 | }) { |
| 77 | const canOperate = useAtomValue( |
| 78 | serverEnvironment.runScheduledTaskNow.permissionAtom(props.environmentId), |
| 79 | ); |
| 80 | const tasksQuery = useEnvironmentQuery( |
| 81 | serverEnvironment.scheduledTasksLive({ environmentId: props.environmentId, input: {} }), |
| 82 | useMessages(), |
| 83 | ); |
| 84 | const setTaskEnabled = useAtomCommand(serverEnvironment.setScheduledTaskEnabled, { |
| 85 | label: "thread automation toggle", |
| 86 | }); |
| 87 | const runTaskNow = useAtomCommand(serverEnvironment.runScheduledTaskNow, { |
| 88 | label: "thread automation run now", |
| 89 | }); |
| 90 | const navigate = useNavigate(); |
| 91 | const gt = useGT(); |
| 92 | const m = useMessages(); |
| 93 | const [busyTaskId, setBusyTaskId] = useState<string | null>(null); |
| 94 | |
| 95 | const boundTasks = (tasksQuery.data?.tasks ?? []).filter( |
| 96 | (task) => task.threadId === props.threadId, |
| 97 | ); |
| 98 | |
| 99 | if (tasksQuery.error === null && boundTasks.length === 0) return null; |
| 100 | |
| 101 | const reportFailure = (title: string, error: unknown) => { |
| 102 | toastManager.add( |
| 103 | stackedThreadToast({ |
| 104 | type: "error", |
| 105 | title, |
| 106 | description: thrownErrorMessage(error, m) ?? String(error), |
| 107 | }), |
| 108 | ); |
| 109 | }; |
| 110 | |
| 111 | const toggleEnabled = async (task: ScheduledTask, enabled: boolean) => { |
| 112 | if ( |
| 113 | busyTaskId !== null || |
| 114 | !readEnvironmentScope(props.environmentId, AuthOrchestrationOperateScope) |
| 115 | ) |
| 116 | return; |
| 117 | setBusyTaskId(task.id); |
| 118 | const result = await setTaskEnabled({ |
| 119 | environmentId: props.environmentId, |
| 120 | input: { id: task.id, enabled }, |
| 121 | }); |
| 122 | setBusyTaskId(null); |
| 123 | if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { |
| 124 | reportFailure(gt("Could not update automation"), squashAtomCommandFailure(result)); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | const runNow = async (task: ScheduledTask) => { |
| 129 | if ( |
| 130 | busyTaskId !== null || |
| 131 | !readEnvironmentScope(props.environmentId, AuthOrchestrationOperateScope) |
| 132 | ) |
| 133 | return; |
| 134 | setBusyTaskId(task.id); |
| 135 | const result = await runTaskNow({ |
| 136 | environmentId: props.environmentId, |
| 137 | input: { id: task.id }, |
| 138 | }); |
| 139 | setBusyTaskId(null); |
| 140 | if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { |
| 141 | reportFailure(gt("Could not run automation"), squashAtomCommandFailure(result)); |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | return ( |
| 146 | <ThreadDetailsSection |
| 147 | headingId="thread-details-automations-heading" |
| 148 | title={gt("Automations")} |
| 149 | data-thread-automations-panel |
| 150 | actions={ |
| 151 | <Tooltip> |
| 152 | <TooltipTrigger |
| 153 | render={ |
| 154 | <ThreadDetailsControl |
| 155 | size="icon-xs" |
| 156 | variant="ghost" |
| 157 | part="icon" |
| 158 | aria-label={gt("Manage scheduled tasks")} |
| 159 | onClick={() => |
| 160 | void navigate({ |
| 161 | to: "/settings/scheduled-tasks", |
| 162 | search: { environmentId: props.environmentId }, |
| 163 | }) |
| 164 | } |
| 165 | > |
| 166 | <Settings2Icon className="size-3.5" /> |
| 167 | </ThreadDetailsControl> |
| 168 | } |
| 169 | /> |
| 170 | <TooltipPopup> |
| 171 | <T>Manage scheduled tasks</T> |
| 172 | </TooltipPopup> |
| 173 | </Tooltip> |
| 174 | } |
| 175 | > |
| 176 | <ul className="m-0 max-h-[13.5rem] list-none overflow-y-auto p-0"> |
| 177 | {boundTasks.map((task) => ( |
| 178 | <li |
| 179 | key={task.id} |
| 180 | className={cn( |
| 181 | "group flex items-center rounded-lg py-1.5", |
| 182 | THREAD_DETAILS_PANEL_ROW_CONTENT_CLASS, |
| 183 | )} |
| 184 | > |
| 185 | <span className="relative inline-flex size-4 shrink-0 items-center justify-center"> |
| 186 | <CalendarClockIcon className={THREAD_DETAILS_PANEL_ICON_CLASS} /> |
| 187 | <span |
| 188 | className={cn( |
| 189 | "absolute -right-1 -top-1 size-1.5 rounded-full", |
| 190 | STATUS_DOT_CLASS[task.lastRunStatus], |
| 191 | )} |
| 192 | aria-hidden |
| 193 | /> |
| 194 | </span> |
| 195 | <Tooltip> |
| 196 | <TooltipTrigger render={<div className="min-w-0 flex-1" />}> |
| 197 | <span className="block truncate text-sm font-medium text-foreground/80"> |
| 198 | {task.title} |
| 199 | </span> |
| 200 | <p className="truncate text-2xs text-muted-foreground"> |
| 201 | {taskScheduleLine(task, tasksQuery.data?.timeZone, m)} |
| 202 | </p> |
| 203 | </TooltipTrigger> |
| 204 | <TooltipPopup side="left"> |
| 205 | <p className="font-medium">{task.title}</p> |
| 206 | <p className="text-muted-foreground"> |
| 207 | {taskScheduleLine(task, tasksQuery.data?.timeZone, m)} |
| 208 | </p> |
| 209 | </TooltipPopup> |
| 210 | </Tooltip> |
| 211 | <Tooltip> |
| 212 | <TooltipTrigger |
| 213 | render={ |
| 214 | <ThreadDetailsControl |
| 215 | size="icon-xs" |
| 216 | variant="ghost" |
| 217 | part="icon" |
| 218 | aria-label={gt("Edit {title}", { title: task.title })} |
| 219 | onClick={() => |
| 220 | void navigate({ |
| 221 | to: "/settings/scheduled-tasks", |
| 222 | search: { environmentId: props.environmentId, taskId: task.id }, |
| 223 | }) |
| 224 | } |
| 225 | > |
| 226 | <PencilIcon className="size-3.5" /> |
| 227 | </ThreadDetailsControl> |
| 228 | } |
| 229 | /> |
| 230 | <TooltipPopup> |
| 231 | <T>Edit automation</T> |
| 232 | </TooltipPopup> |
| 233 | </Tooltip> |
| 234 | {/* Webhook tasks need a request from their URL. */} |
| 235 | {task.schedule.type === "webhook" ? null : ( |
| 236 | <Tooltip> |
| 237 | <TooltipTrigger |
| 238 | render={ |
| 239 | <ThreadDetailsControl |
| 240 | size="icon-xs" |
| 241 | variant="ghost" |
| 242 | part="icon" |
| 243 | aria-label={gt("Run {title} now", { title: task.title })} |
| 244 | disabled={ |
| 245 | !canOperate || busyTaskId !== null || task.lastRunStatus === "running" |
| 246 | } |
| 247 | onClick={() => void runNow(task)} |
| 248 | > |
| 249 | <PlayIcon className="size-3.5" /> |
| 250 | </ThreadDetailsControl> |
| 251 | } |
| 252 | /> |
| 253 | <TooltipPopup> |
| 254 | <T>Run now</T> |
| 255 | </TooltipPopup> |
| 256 | </Tooltip> |
| 257 | )} |
| 258 | <Switch |
| 259 | checked={task.enabled} |
| 260 | disabled={!canOperate || busyTaskId !== null} |
| 261 | aria-label={ |
| 262 | task.enabled |
| 263 | ? gt("Pause {title}", { title: task.title }) |
| 264 | : gt("Resume {title}", { title: task.title }) |
| 265 | } |
| 266 | onCheckedChange={(enabled) => void toggleEnabled(task, enabled)} |
| 267 | /> |
| 268 | </li> |
| 269 | ))} |
| 270 | </ul> |
| 271 | {/* Kept last: the details card treats a section whose last child is empty as empty. */} |
| 272 | {tasksQuery.error !== null ? ( |
| 273 | <p className="px-2.5 py-1.5 text-2xs text-destructive"> |
| 274 | <T> |
| 275 | Could not load automations: <Var>{tasksQuery.error}</Var> |
| 276 | </T> |
| 277 | </p> |
| 278 | ) : null} |
| 279 | </ThreadDetailsSection> |
| 280 | ); |
| 281 | } |