okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221merge f-split-ui4 a7cd2b38e into f-split-int7c4b3d743

main
3.2 KiB45 linesraw
1import { tr, localeTag } from '../lib/i18n';
2import { Icon } from './ui';
3import { Markdown } from './Markdown';
4import { ErrorNotice } from './ui';
5import type { Task } from '../lib/tasks';
6
7const FREQUENCY: Record<string, string> = { hourly: 'Every hour', daily: 'Every day', weekdays: 'Weekdays', weekly: 'Every week' };
8const WEEKDAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
9const MODE: Record<string, string> = { plan: 'Look only, change nothing', manual: 'Stop and tell me before changing anything', bypass: 'Bypass permissions' };
10
11const when = (iso?: string) => (iso ? new Date(iso).toLocaleString(localeTag(), { dateStyle: 'medium', timeStyle: 'short' }) : '');
12
13/** The schedule in one line: "Weekdays · 08:00", "Every week · Friday · 16:00". */
14export function scheduleText(t: Task): string {
15  const base = tr(FREQUENCY[t.frequency] ?? t.frequency);
16  if (t.frequency === 'hourly') return base;
17  return [base, t.frequency === 'weekly' ? tr(WEEKDAYS[t.weekday] ?? '') : '', t.at].filter(Boolean).join(' · ');
18}
19
20export type TaskState = 'running' | 'active' | 'paused';
21export const taskState = (t: Task): TaskState => (t.status === 'running' ? 'running' : t.enabled ? 'active' : 'paused');
22
23/**
24 * An opened task, skimmable: when it runs, what it may do, what it did last, then the instructions and the last answer.
25 * The title, the state and the actions (pause or resume, delete, Edit, Run now) are in the header the screen draws.
26 */
27export function TaskDetail({ task }: { task: Task }) {
28  const state = taskState(task);
29  const result = task.status === 'running' ? { label: 'Running…', tone: '' }
30    : task.error ? { label: 'Last run failed', tone: 'is-bad' }
31      : task.lastRunAt || task.output ? { label: 'Done', tone: 'is-good' } : { label: 'Not run yet', tone: '' };
32  return <div className="task-detail">
33    <h2 className="task-name">{task.title}</h2>
34    <section className="st-card task-facts" aria-label={tr("Task summary")}>
35      <div><span className="label">{tr("Schedule")}</span><strong><Icon name="clock" size={16}/>{scheduleText(task)}</strong>
36        <span className="small muted">{tr(state === 'paused' ? 'Paused' : task.nextRunAt ? tr("Next: {0}", [when(task.nextRunAt)]) : 'Calculating the next run…')}</span></div>
37      <div><span className="label">{tr("What it may do")}</span><strong><Icon name="shield" size={16}/>{tr(MODE[task.mode ?? 'plan'])}</strong></div>
38      <div><span className="label">{tr("Last result")}</span><strong className={result.tone}>{result.tone === 'is-good' ? <Icon name="check" size={16}/> : null}{tr(result.label)}</strong>
39        <span className="small muted">{when(task.lastRunAt)}</span></div>
40    </section>
41    <section className="st-card"><h2 className="label">{tr("What it does")}</h2><p className="task-instructions">{task.prompt}</p></section>
42    <section className="st-card"><h2 className="label">{tr("Last answer")}{task.demo ? tr(' · Example') : ''}</h2>
43      {task.error ? <ErrorNotice message={task.error} next={tr("Try running this task again.")}/> : task.output ? <div className="task-answer"><Markdown text={task.output}/></div> : <p className="small muted">{tr("Run this task to see its first result here.")}</p>}</section>
44  </div>;
45}