refactor(webui): separate presentation views and expand component gallery (#6179)

* refactor(webui): organize shared components with an internal gallery

* refactor(webui): decouple views and expand component gallery

* fix(docker): include channel sources in WebUI build

* refactor(webui): cover all channel interfaces in gallery

* docs(webui): remove manual paragraph wrapping
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-14 00:11:00 +08:00
1 parent db804ef94a
commit 30e43aec82
109 files changed
+7874 -3459

No files matched your search

@@ -1,7 +1,7 @@
import { CircleAlert, Loader2, X } from "lucide-react";
import { useId, useLayoutEffect, useMemo, useRef, useState } from "react";
import { useId, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from "react";
import { AutomationRunDialog, type AutomationRunRecord } from "@/components/settings/system/AutomationRunDialog";
import type { AutomationRunRecord } from "@/components/settings/system/AutomationRunDialogView";
import { Button } from "@/components/ui/button";
import { formControlFocusClassName } from "@/components/ui/form-control";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
@@ -29,13 +29,22 @@ type CalendarCopy = {
system: string;
};
export interface AutomationCalendarRunProps {
job: SessionAutomationJob;
run: AutomationRunRecord;
locale: string;
open: boolean;
onOpenChange: (open: boolean) => void;
onCloseAutoFocus: (event: Event) => void;
}
interface AutomationCalendarProps {
token?: string;
jobs: SessionAutomationJob[];
month: Date;
locale: string;
copy: CalendarCopy;
onInspect: (job: SessionAutomationJob, trigger: HTMLElement) => void;
renderRunDialog: (props: AutomationCalendarRunProps) => ReactNode;
}
function startOfDay(date: Date): Date {
@@ -217,7 +226,7 @@ function CalendarOverflowEntries({ entries, dayLabel, locale, copy, onInspect }:
);
}
export function AutomationCalendar({ token = "", jobs, month, locale, copy, onInspect }: AutomationCalendarProps) {
export function AutomationCalendar({ jobs, month, locale, copy, onInspect, renderRunDialog }: AutomationCalendarProps) {
const calendarRef = useRef<HTMLElement | null>(null);
const runTrigger = useRef<HTMLElement | null>(null);
const [selectedRun, setSelectedRun] = useState<{ job: SessionAutomationJob; run: AutomationRunRecord } | null>(null);
@@ -349,18 +358,17 @@ export function AutomationCalendar({ token = "", jobs, month, locale, copy, onIn
)}
</div> : null}
{selectedRun ? <AutomationRunDialog
token={token}
job={selectedRun.job} run={selectedRun.run} locale={locale} open={runOpen}
onOpenChange={setRunOpen}
onCloseAutoFocus={(event) => {
{selectedRun ? renderRunDialog({
job: selectedRun.job, run: selectedRun.run, locale, open: runOpen,
onOpenChange: setRunOpen,
onCloseAutoFocus: (event) => {
event.preventDefault();
if (runOpen) return;
setSelectedRun(null);
const target = runTrigger.current?.isConnected ? runTrigger.current : calendarRef.current;
target?.focus({ preventScroll: true });
}}
/> : null}
},
}) : null}
</section>
);
}
@@ -1,193 +1,32 @@
import { useEffect, useRef, useState, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { ChannelLogo } from "@/components/settings/channels/ChannelIdentity";
import { channelUiOwner, channelUiPresentation } from "@/channel-plugins/registry";
import { channelTranslator } from "@/channel-plugins/i18n";
import { Button } from "@/components/ui/button";
import { DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
import { useEffect, useState, type ReactNode } from "react";
import { AutomationChatBindingView, type AutomationChatNames, type ChangeAutomationChat } from "./AutomationChatBindingView";
import { fetchAutomationChats } from "@/lib/api";
import { readLocalPreferences } from "@/lib/local-preferences";
import type { AutomationChat, AutomationChatsPayload, AutomationChatUpdate, NanobotFeatureInfo, SessionAutomationJob } from "@/lib/types";
import type { AutomationChatsPayload, SessionAutomationJob } from "@/lib/types";
export type ChangeAutomationChat = (job: SessionAutomationJob, values: AutomationChatUpdate) => Promise<void>;
export type AutomationChatNames = ReadonlyMap<string, { title: string; handle: string }>;
function ChatIdentity({ chat, title }: { chat: AutomationChat; title: string }) {
const { t } = useTranslation();
const owner = channelUiOwner(chat.channel);
const presentation = channelUiPresentation(chat.channel);
const platform = chat.channel === "websocket" ? t("settings.automations.chat.web")
: channelTranslator(t, owner)("displayName", presentation?.displayName ?? chat.channel);
const feature: NanobotFeatureInfo = {
name: chat.channel, display_name: platform, type: "channel", enabled: true,
installed: true, ready: true, status: "ready", install_supported: false, requires_restart: false,
};
return <span className="flex min-w-0 items-center gap-2.5 text-start">
<span aria-hidden className="relative h-6 w-6 shrink-0">
{chat.channel === "websocket" ? <img src="/brand/nanobot_mark.svg" alt="" className="h-6 w-6" />
: <span className="absolute left-0 top-0 origin-top-left scale-75"><ChannelLogo feature={feature} showBrandLogos={readLocalPreferences().brandLogos} /></span>}
</span>
<span className="flex min-w-0 flex-col gap-0.5">
<span className="break-words text-sm leading-5 [overflow-wrap:anywhere]">{title}</span>
<span className="text-xs leading-4 text-muted-foreground">{platform}</span>
</span>
</span>;
}
export type { AutomationChatNames, ChangeAutomationChat } from "./AutomationChatBindingView";
export function AutomationChatBinding({ job, token, chatNames, onSave, children }: {
job: SessionAutomationJob; token: string; onSave: ChangeAutomationChat;
chatNames?: AutomationChatNames;
children: (picker: ReactNode) => ReactNode;
}) {
const { t } = useTranslation();
const tx = (key: string, values?: Record<string, string>) => t(`settings.automations.chat.${key}`, values);
const [data, setData] = useState<AutomationChatsPayload | null>(null);
const [loadError, setLoadError] = useState(false);
const [reload, setReload] = useState(0);
const [draft, setDraft] = useState<{ target: AutomationChat; previous: AutomationChat; revision: string; message: string; editingMessage?: boolean } | null>(null);
const [saving, setSaving] = useState(false);
const [error, setError] = useState("");
const [saved, setSaved] = useState<{ target: AutomationChat; previous: AutomationChat } | null>(null);
const heading = useRef<HTMLHeadingElement>(null);
const trigger = useRef<HTMLButtonElement>(null);
const returning = useRef(false);
useEffect(() => {
const controller = new AbortController();
setLoadError(false);
void fetchAutomationChats(token, job.id, controller.signal).then(value => {
if (!controller.signal.aborted) {
setData(value);
setSaved(previous => previous?.target.id === value.current?.id ? previous : null);
}
if (!controller.signal.aborted) setData(value);
}).catch(() => { if (!controller.signal.aborted) setLoadError(true); });
return () => controller.abort();
}, [token, job.id, job.chat_binding_revision, reload]);
const reviewing = draft !== null;
const locked = saving || Boolean(job.state.pending);
const pickerDisabled = locked || !data?.chats.length || loadError || (!reviewing && data.revision !== job.chat_binding_revision);
useEffect(() => {
if (reviewing) heading.current?.focus();
}, [reviewing]);
useEffect(() => {
if (!reviewing && !pickerDisabled && returning.current) {
trigger.current?.focus(); returning.current = false;
}
}, [reviewing, pickerDisabled]);
const back = () => { returning.current = true; setDraft(null); };
const current = data?.current;
const fallback: AutomationChat = { id: "current", title: job.origin?.title || tx("current"), channel: job.origin?.channel || "websocket" };
const baseTitle = (chat: AutomationChat) => chatNames?.get(chat.id)?.title ?? chat.title;
const counts = new Map<string, number>();
for (const chat of data?.chats ?? []) {
const title = baseTitle(chat);
counts.set(title, (counts.get(title) ?? 0) + 1);
}
const chatTitle = (chat: AutomationChat) => {
const title = baseTitle(chat);
const name = chatNames?.get(chat.id);
return name && (counts.get(title) ?? 0) > 1 ? `${title} · @${name.handle}` : title;
};
const identity = (chat: AutomationChat) => <ChatIdentity chat={chat} title={chatTitle(chat)} />;
const targetUnavailable = Boolean(draft && data && !data.chats.some(chat => chat.id === draft.target.id && !chat.unavailable));
const picker = (review: boolean) => <Select value={review ? draft?.target.id : current?.id ?? "current"}
disabled={pickerDisabled} onValueChange={id => {
const target = data?.chats.find(chat => chat.id === id);
if (!target || target.unavailable || id === current?.id) return;
setError("");
setDraft(previous => previous ? { ...previous, target } : {
target, previous: current ?? fallback,
revision: job.chat_binding_revision!, message: job.payload.message,
});
}}>
<SelectTrigger ref={review ? undefined : trigger} aria-label={tx(review ? "new" : "label")}
aria-busy={!loadError && (!data || data.revision !== job.chat_binding_revision)}
className="h-auto min-h-14 w-full py-2 disabled:opacity-100 disabled:[&>svg]:opacity-40 [&>span:first-child]:min-w-0 [&>span:first-child]:flex-1 [&>span:first-child]:text-start">
<SelectValue>{identity((review ? draft?.target : current) ?? fallback)}</SelectValue>
</SelectTrigger>
<SelectContent>
{!current ? <SelectItem value="current" disabled>{identity(fallback)}</SelectItem> : null}
{data?.chats.map(chat => <SelectItem key={chat.id} value={chat.id} textValue={`${chatTitle(chat)} ${chat.channel}`}
disabled={chat.unavailable || (review && chat.id === current?.id)}
className="h-auto min-h-11 py-2 [&>span:first-child]:min-w-0 [&>span:first-child]:max-w-[calc(100vw-5rem)] [&>span:first-child]:w-full">{identity(chat)}</SelectItem>)}
</SelectContent>
</Select>;
const save = async () => {
if (!draft || locked || targetUnavailable || !draft.message.trim()) return;
setSaving(true); setError("");
try {
await onSave(job, { target_id: draft.target.id, revision: draft.revision, message: draft.message });
setData(previous => previous ? { ...previous, current: draft.target } : previous);
setSaved({ target: draft.target, previous: draft.previous }); back(); setReload(value => value + 1);
} catch (cause) {
const reasons: Record<string, string> = {
automation_chat_conflict: "conflict", automation_chat_busy: "schedulerBusy",
automation_chat_unavailable: "unavailable",
};
setError(tx(cause instanceof Error ? reasons[cause.message] ?? "failed" : "failed"));
} finally { setSaving(false); }
};
if (!draft) return children(<div className="space-y-2">
<p className="text-[13px] font-medium">{tx("label")}</p>
{picker(false)}
<div className="flex flex-wrap items-baseline gap-x-3 gap-y-1">
<p role={saved ? "status" : undefined} className="text-[12px] leading-5 text-muted-foreground">
{job.state.pending ? tx("busy") : saved ? tx("saved", { chat: chatTitle(saved.target) }) : tx("hint")}
</p>
{saved && current?.id === saved.target.id && data?.chats.some(chat => chat.id === saved.previous.id && !chat.unavailable) ? (
<Button variant="link" size="sm" className="h-11 justify-start whitespace-normal p-0 text-start text-[12px] sm:h-auto" disabled={pickerDisabled}
onClick={() => {
setError("");
setDraft({ target: saved.previous, previous: current, revision: data.revision, message: job.payload.message });
}}>{tx("changeBack", { chat: chatTitle(saved.previous) })}</Button>
) : null}
</div>
{loadError ? <Button variant="link" size="sm" onClick={() => setReload(value => value + 1)}>{tx("retry")}</Button> : null}
{data && data.chats.length <= (current ? 1 : 0) ? <p className="text-[12px] leading-5 text-muted-foreground">{tx("available")}</p> : null}
</div>);
return <>
<DialogHeader className="shrink-0 px-6 pb-4 pr-12 pt-5 text-left">
<DialogTitle ref={heading} tabIndex={-1} className="break-words text-lg font-medium leading-snug tracking-normal outline-none">{tx("change")}</DialogTitle>
</DialogHeader>
<div className="min-h-0 space-y-4 overflow-y-auto overscroll-contain px-6 [text-wrap:pretty]">
<div className="space-y-3">
<div role="group" aria-label={tx("previous")} className="flex min-w-0 items-center gap-3 pb-1 text-muted-foreground">
<span className="shrink-0 text-[12px]">{tx("previous")}</span>
{identity(draft.previous)}
</div>
<div className="space-y-2">
<p className="text-[13px] font-medium">{tx("new")}</p>
{picker(true)}
</div>
</div>
<div className="space-y-1">
<p className="text-[13px] leading-5">{tx("effect")}</p>
<p className="text-[12px] leading-5 text-muted-foreground">{tx("history")}</p>
</div>
<div className="space-y-2 rounded-2xl bg-muted/50 px-4 pb-4 pt-2">
<div className="flex items-center justify-between gap-3">
<p className="text-[12px] text-muted-foreground">{tx("message")}</p>
{!draft.editingMessage ? <Button variant="link" size="sm" disabled={saving}
className="h-11 shrink-0 p-0 text-[12px] sm:h-9"
onClick={() => setDraft({ ...draft, editingMessage: true })}>{tx("editInstructions")}</Button> : null}
</div>
{draft.editingMessage ? <>
<Textarea autoFocus aria-label={tx("message")} rows={3} value={draft.message} disabled={saving}
onChange={event => setDraft({ ...draft, message: event.target.value })}
className="min-h-20 resize-y text-base leading-6 sm:text-[13px] sm:leading-5" />
<p className="text-[12px] leading-5 text-muted-foreground">{tx("editInstructionsHint")}</p>
</> : <p className="whitespace-pre-wrap break-words text-[13px] leading-5 [overflow-wrap:anywhere]">{draft.message}</p>}
</div>
<p className="text-[12px] leading-5 text-muted-foreground">{tx("review")}</p>
{error || targetUnavailable ? <p role="alert" className="text-[12px] leading-5 text-destructive">{error || tx("unavailable")}</p> : null}
</div>
<DialogFooter className="mt-4 shrink-0 flex-row justify-end gap-2 border-t border-border/45 px-6 py-3">
<Button variant="ghost" size="sm" disabled={saving} className="h-auto min-h-11 min-w-0 whitespace-normal font-normal text-muted-foreground sm:min-h-9" onClick={back}>{t("settings.automations.cancel")}</Button>
<Button size="sm" className="grid h-auto min-h-11 min-w-0 whitespace-normal sm:min-h-9" disabled={locked || targetUnavailable || !draft.message.trim()} aria-busy={saving} onClick={() => void save()}>
<span className="col-start-1 row-start-1" aria-hidden={saving || undefined} style={saving ? { visibility: "hidden" } : undefined}>{tx(draft.editingMessage ? "saveAndChange" : "confirm")}</span>
<span className="col-start-1 row-start-1" aria-hidden={!saving || undefined} style={!saving ? { visibility: "hidden" } : undefined}>{tx("saving")}</span>
</Button>
</DialogFooter>
</>;
return <AutomationChatBindingView job={job} data={data} loadError={loadError}
chatNames={chatNames} showBrandLogos={readLocalPreferences().brandLogos} onSave={onSave}
onReload={() => setReload(value => value + 1)} onSaved={target => {
setData(previous => previous ? { ...previous, current: target } : previous);
setReload(value => value + 1);
}}>{children}</AutomationChatBindingView>;
}
@@ -0,0 +1,186 @@
import { useEffect, useRef, useState, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { ChannelLogo } from "@/components/settings/channels/ChannelIdentity";
import { channelUiOwner, channelUiPresentation } from "@/channel-plugins/registry";
import { channelTranslator } from "@/channel-plugins/i18n";
import { Button } from "@/components/ui/button";
import { DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
import type { AutomationChat, AutomationChatsPayload, AutomationChatUpdate, NanobotFeatureInfo, SessionAutomationJob } from "@/lib/types";
export type ChangeAutomationChat = (job: SessionAutomationJob, values: AutomationChatUpdate) => Promise<void>;
export type AutomationChatNames = ReadonlyMap<string, { title: string; handle: string }>;
function ChatIdentity({ chat, title, showBrandLogos }: { chat: AutomationChat; title: string; showBrandLogos: boolean }) {
const { t } = useTranslation();
const owner = channelUiOwner(chat.channel);
const presentation = channelUiPresentation(chat.channel);
const platform = chat.channel === "websocket" ? t("settings.automations.chat.web")
: channelTranslator(t, owner)("displayName", presentation?.displayName ?? chat.channel);
const feature: NanobotFeatureInfo = {
name: chat.channel, display_name: platform, type: "channel", enabled: true,
installed: true, ready: true, status: "ready", install_supported: false, requires_restart: false,
};
return <span className="flex min-w-0 items-center gap-2.5 text-start">
<span aria-hidden className="relative h-6 w-6 shrink-0">
{chat.channel === "websocket" ? <img src="/brand/nanobot_mark.svg" alt="" className="h-6 w-6" />
: <span className="absolute left-0 top-0 origin-top-left scale-75"><ChannelLogo feature={feature} showBrandLogos={showBrandLogos} /></span>}
</span>
<span className="flex min-w-0 flex-col gap-0.5">
<span className="break-words text-sm leading-5 [overflow-wrap:anywhere]">{title}</span>
<span className="text-xs leading-4 text-muted-foreground">{platform}</span>
</span>
</span>;
}
export function AutomationChatBindingView({ job, data, loadError, chatNames, showBrandLogos, onSave, onSaved, onReload, children }: {
job: SessionAutomationJob;
data: AutomationChatsPayload | null;
loadError: boolean;
chatNames?: AutomationChatNames;
showBrandLogos: boolean;
onSave: ChangeAutomationChat;
onSaved: (target: AutomationChat) => void;
onReload: () => void;
children: (picker: ReactNode) => ReactNode;
}) {
const { t } = useTranslation();
const tx = (key: string, values?: Record<string, string>) => t(`settings.automations.chat.${key}`, values);
const [draft, setDraft] = useState<{ target: AutomationChat; previous: AutomationChat; revision: string; message: string; editingMessage?: boolean } | null>(null);
const [saving, setSaving] = useState(false);
const [error, setError] = useState("");
const [saved, setSaved] = useState<{ target: AutomationChat; previous: AutomationChat } | null>(null);
const heading = useRef<HTMLHeadingElement>(null);
const trigger = useRef<HTMLButtonElement>(null);
const returning = useRef(false);
useEffect(() => {
if (data) setSaved(previous => previous?.target.id === data.current?.id ? previous : null);
}, [data]);
const reviewing = draft !== null;
const locked = saving || Boolean(job.state.pending);
const pickerDisabled = locked || !data?.chats.length || loadError || (!reviewing && data.revision !== job.chat_binding_revision);
useEffect(() => {
if (reviewing) heading.current?.focus();
}, [reviewing]);
useEffect(() => {
if (!reviewing && !pickerDisabled && returning.current) {
trigger.current?.focus(); returning.current = false;
}
}, [reviewing, pickerDisabled]);
const back = () => { returning.current = true; setDraft(null); };
const current = data?.current;
const fallback: AutomationChat = { id: "current", title: job.origin?.title || tx("current"), channel: job.origin?.channel || "websocket" };
const baseTitle = (chat: AutomationChat) => chatNames?.get(chat.id)?.title ?? chat.title;
const counts = new Map<string, number>();
for (const chat of data?.chats ?? []) {
const title = baseTitle(chat);
counts.set(title, (counts.get(title) ?? 0) + 1);
}
const chatTitle = (chat: AutomationChat) => {
const title = baseTitle(chat);
const name = chatNames?.get(chat.id);
return name && (counts.get(title) ?? 0) > 1 ? `${title} · @${name.handle}` : title;
};
const identity = (chat: AutomationChat) => <ChatIdentity chat={chat} title={chatTitle(chat)} showBrandLogos={showBrandLogos} />;
const targetUnavailable = Boolean(draft && data && !data.chats.some(chat => chat.id === draft.target.id && !chat.unavailable));
const picker = (review: boolean) => <Select value={review ? draft?.target.id : current?.id ?? "current"}
disabled={pickerDisabled} onValueChange={id => {
const target = data?.chats.find(chat => chat.id === id);
if (!target || target.unavailable || id === current?.id) return;
setError("");
setDraft(previous => previous ? { ...previous, target } : {
target, previous: current ?? fallback,
revision: job.chat_binding_revision!, message: job.payload.message,
});
}}>
<SelectTrigger ref={review ? undefined : trigger} aria-label={tx(review ? "new" : "label")}
aria-busy={!loadError && (!data || data.revision !== job.chat_binding_revision)}
className="h-auto min-h-14 w-full py-2 disabled:opacity-100 disabled:[&>svg]:opacity-40 [&>span:first-child]:min-w-0 [&>span:first-child]:flex-1 [&>span:first-child]:text-start">
<SelectValue>{identity((review ? draft?.target : current) ?? fallback)}</SelectValue>
</SelectTrigger>
<SelectContent>
{!current ? <SelectItem value="current" disabled>{identity(fallback)}</SelectItem> : null}
{data?.chats.map(chat => <SelectItem key={chat.id} value={chat.id} textValue={`${chatTitle(chat)} ${chat.channel}`}
disabled={chat.unavailable || (review && chat.id === current?.id)}
className="h-auto min-h-11 py-2 [&>span:first-child]:min-w-0 [&>span:first-child]:max-w-[calc(100vw-5rem)] [&>span:first-child]:w-full">{identity(chat)}</SelectItem>)}
</SelectContent>
</Select>;
const save = async () => {
if (!draft || locked || targetUnavailable || !draft.message.trim()) return;
setSaving(true); setError("");
try {
await onSave(job, { target_id: draft.target.id, revision: draft.revision, message: draft.message });
onSaved(draft.target);
setSaved({ target: draft.target, previous: draft.previous }); back();
} catch (cause) {
const reasons: Record<string, string> = {
automation_chat_conflict: "conflict", automation_chat_busy: "schedulerBusy",
automation_chat_unavailable: "unavailable",
};
setError(tx(cause instanceof Error ? reasons[cause.message] ?? "failed" : "failed"));
} finally { setSaving(false); }
};
if (!draft) return children(<div className="space-y-2">
<p className="text-[13px] font-medium">{tx("label")}</p>
{picker(false)}
<div className="flex flex-wrap items-baseline gap-x-3 gap-y-1">
<p role={saved ? "status" : undefined} className="text-[12px] leading-5 text-muted-foreground">
{job.state.pending ? tx("busy") : saved ? tx("saved", { chat: chatTitle(saved.target) }) : tx("hint")}
</p>
{saved && current?.id === saved.target.id && data?.chats.some(chat => chat.id === saved.previous.id && !chat.unavailable) ? (
<Button variant="link" size="sm" className="h-11 justify-start whitespace-normal p-0 text-start text-[12px] sm:h-auto" disabled={pickerDisabled}
onClick={() => {
setError("");
setDraft({ target: saved.previous, previous: current, revision: data.revision, message: job.payload.message });
}}>{tx("changeBack", { chat: chatTitle(saved.previous) })}</Button>
) : null}
</div>
{loadError ? <Button variant="link" size="sm" onClick={onReload}>{tx("retry")}</Button> : null}
{data && data.chats.length <= (current ? 1 : 0) ? <p className="text-[12px] leading-5 text-muted-foreground">{tx("available")}</p> : null}
</div>);
return <>
<DialogHeader className="shrink-0 px-6 pb-4 pr-12 pt-5 text-left">
<DialogTitle ref={heading} tabIndex={-1} className="break-words text-lg font-medium leading-snug tracking-normal outline-none">{tx("change")}</DialogTitle>
</DialogHeader>
<div className="min-h-0 space-y-4 overflow-y-auto overscroll-contain px-6 [text-wrap:pretty]">
<div className="space-y-3">
<div role="group" aria-label={tx("previous")} className="flex min-w-0 items-center gap-3 pb-1 text-muted-foreground">
<span className="shrink-0 text-[12px]">{tx("previous")}</span>
{identity(draft.previous)}
</div>
<div className="space-y-2">
<p className="text-[13px] font-medium">{tx("new")}</p>
{picker(true)}
</div>
</div>
<div className="space-y-1">
<p className="text-[13px] leading-5">{tx("effect")}</p>
<p className="text-[12px] leading-5 text-muted-foreground">{tx("history")}</p>
</div>
<div className="space-y-2 rounded-2xl bg-muted/50 px-4 pb-4 pt-2">
<div className="flex items-center justify-between gap-3">
<p className="text-[12px] text-muted-foreground">{tx("message")}</p>
{!draft.editingMessage ? <Button variant="link" size="sm" disabled={saving}
className="h-11 shrink-0 p-0 text-[12px] sm:h-9"
onClick={() => setDraft({ ...draft, editingMessage: true })}>{tx("editInstructions")}</Button> : null}
</div>
{draft.editingMessage ? <>
<Textarea autoFocus aria-label={tx("message")} rows={3} value={draft.message} disabled={saving}
onChange={event => setDraft({ ...draft, message: event.target.value })}
className="min-h-20 resize-y text-base leading-6 sm:text-[13px] sm:leading-5" />
<p className="text-[12px] leading-5 text-muted-foreground">{tx("editInstructionsHint")}</p>
</> : <p className="whitespace-pre-wrap break-words text-[13px] leading-5 [overflow-wrap:anywhere]">{draft.message}</p>}
</div>
<p className="text-[12px] leading-5 text-muted-foreground">{tx("review")}</p>
{error || targetUnavailable ? <p role="alert" className="text-[12px] leading-5 text-destructive">{error || tx("unavailable")}</p> : null}
</div>
<DialogFooter className="mt-4 shrink-0 flex-row justify-end gap-2 border-t border-border/45 px-6 py-3">
<Button variant="ghost" size="sm" disabled={saving} className="h-auto min-h-11 min-w-0 whitespace-normal font-normal text-muted-foreground sm:min-h-9" onClick={back}>{t("settings.automations.cancel")}</Button>
<Button size="sm" className="grid h-auto min-h-11 min-w-0 whitespace-normal sm:min-h-9" disabled={locked || targetUnavailable || !draft.message.trim()} aria-busy={saving} onClick={() => void save()}>
<span className="col-start-1 row-start-1" aria-hidden={saving || undefined} style={saving ? { visibility: "hidden" } : undefined}>{tx(draft.editingMessage ? "saveAndChange" : "confirm")}</span>
<span className="col-start-1 row-start-1" aria-hidden={!saving || undefined} style={!saving ? { visibility: "hidden" } : undefined}>{tx("saving")}</span>
</Button>
</DialogFooter>
</>;
}
@@ -1,15 +1,10 @@
import { CircleAlert } from "lucide-react";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { MarkdownText } from "@/components/MarkdownText";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import type { SessionAutomationJob } from "@/lib/types";
import { AutomationRunDialogView, type AutomationRunRecord } from "./AutomationRunDialogView";
import { fetchAutomationRunResult } from "@/lib/api";
import { cn } from "@/lib/utils";
import type { SessionAutomationJob } from "@/lib/types";
export type AutomationRunRecord = NonNullable<SessionAutomationJob["state"]["run_history"]>[number];
export type { AutomationRunRecord } from "./AutomationRunDialogView";
export function AutomationRunDialog({ token = "", job, run, locale, open, onOpenChange, onCloseAutoFocus }: {
token?: string;
@@ -20,7 +15,6 @@ export function AutomationRunDialog({ token = "", job, run, locale, open, onOpen
onOpenChange: (open: boolean) => void;
onCloseAutoFocus: (event: Event) => void;
}) {
const { t } = useTranslation();
const [attempt, setAttempt] = useState(0);
const kind = job.kind === "local_trigger" ? "local_trigger" : "cron";
const resultKey = JSON.stringify([token, kind, job.id, run.run_at_ms, attempt]);
@@ -49,79 +43,8 @@ export function AutomationRunDialog({ token = "", job, run, locale, open, onOpen
return () => controller.abort();
}, [job.id, kind, open, resultKey, run.run_at_ms, token]);
const currentResult = result?.key === resultKey ? result : null;
const failed = run.status === "error";
const status = run.status === "ok"
? t("settings.automations.status.completed")
: failed ? t("settings.automations.status.failed")
: run.status === "skipped" ? t("settings.automations.skipped")
: t("settings.automations.calendar.recorded");
const duration = run.duration_ms;
const chatKey = run.webui_session_key !== undefined ? run.webui_session_key
: job.origin?.channel === "websocket" ? job.origin.session_key : null;
const chatHref = chatKey ? `#/chat/${encodeURIComponent(chatKey)}` : null;
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent
{...(!open ? { inert: "", "aria-hidden": true } : {})}
className="flex max-h-[calc(100dvh-2rem)] max-w-[520px] flex-col gap-0 p-0 text-sm font-normal leading-normal text-foreground"
onCloseAutoFocus={onCloseAutoFocus}
>
<DialogHeader className="shrink-0 px-6 pb-4 pr-14 pt-5 text-left">
<DialogTitle className="min-w-0 break-words text-lg font-medium leading-snug tracking-normal text-balance">
{job.name || job.id}
</DialogTitle>
<DialogDescription className="text-sm font-normal text-muted-foreground">
<time dateTime={new Date(run.run_at_ms).toISOString()}>
{new Intl.DateTimeFormat(locale, { dateStyle: "medium", timeStyle: "short" }).format(run.run_at_ms)}
</time>
</DialogDescription>
</DialogHeader>
<div className="min-h-0 space-y-4 overflow-y-auto overscroll-contain px-6 pb-6">
<dl className="space-y-3">
<div className="flex items-start justify-between gap-4">
<dt className="text-muted-foreground">{t("settings.automations.runDetails.result")}</dt>
<dd className={cn("flex items-center gap-1.5 text-end", failed && "text-destructive")}>
{failed ? <CircleAlert className="h-3.5 w-3.5 shrink-0" strokeWidth={1.5} aria-hidden /> : null}
{status}
</dd>
</div>
{duration != null && Number.isFinite(duration) && duration >= 0 ? (
<div className="flex items-start justify-between gap-4">
<dt className="text-muted-foreground">{t("settings.automations.runDetails.duration")}</dt>
<dd className="text-end tabular-nums">{new Intl.NumberFormat(locale, {
style: "unit", unit: "second", unitDisplay: "short",
maximumFractionDigits: duration < 1000 ? 3 : 1,
}).format(duration / 1000)}</dd>
</div>
) : null}
</dl>
{run.error ? <p className={cn("whitespace-pre-wrap break-words [overflow-wrap:anywhere]", failed && "text-destructive")}>{run.error}</p> : null}
{!currentResult || currentResult.status === "loading" ? (
<p role="status" className="text-muted-foreground">{t("settings.automations.runDetails.loading")}</p>
) : currentResult.status === "error" ? (
<div className="flex flex-wrap items-center justify-between gap-2">
<p role="alert" className="text-muted-foreground">{t("settings.automations.runDetails.loadError")}</p>
<Button variant="ghost" size="sm" onClick={() => setAttempt((value) => value + 1)}>
{t("settings.automations.runDetails.retry")}
</Button>
</div>
) : currentResult.response?.trim() ? (
<MarkdownText className="min-w-0 text-sm [overflow-wrap:anywhere]">{currentResult.response}</MarkdownText>
) : (
<p className="text-muted-foreground">{t(currentResult.response === null
? "settings.automations.runDetails.noOutput"
: "settings.automations.runDetails.emptyOutput")}</p>
)}
</div>
{chatHref ? (
<div className="flex shrink-0 justify-end border-t border-border/45 px-6 py-3">
<Button asChild variant="ghost" size="sm" className="font-normal text-muted-foreground">
<a href={chatHref}>{t("settings.automations.emptyAction")}</a>
</Button>
</div>
) : null}
</DialogContent>
</Dialog>
);
return <AutomationRunDialogView job={job} run={run} locale={locale} open={open}
result={currentResult ?? { status: "loading", response: null }}
onRetry={() => setAttempt((value) => value + 1)}
onOpenChange={onOpenChange} onCloseAutoFocus={onCloseAutoFocus} />;
}
@@ -0,0 +1,102 @@
import { CircleAlert } from "lucide-react";
import { useTranslation } from "react-i18next";
import { MarkdownText } from "@/components/MarkdownText";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import type { SessionAutomationJob } from "@/lib/types";
import { cn } from "@/lib/utils";
export type AutomationRunRecord = NonNullable<SessionAutomationJob["state"]["run_history"]>[number];
export type AutomationRunResult = {
status: "loading" | "loaded" | "error";
response: string | null;
};
export function AutomationRunDialogView({ job, run, locale, open, result, onRetry, onOpenChange, onCloseAutoFocus }: {
job: SessionAutomationJob;
run: AutomationRunRecord;
locale: string;
open: boolean;
result: AutomationRunResult;
onRetry: () => void;
onOpenChange: (open: boolean) => void;
onCloseAutoFocus: (event: Event) => void;
}) {
const { t } = useTranslation();
const failed = run.status === "error";
const status = run.status === "ok"
? t("settings.automations.status.completed")
: failed ? t("settings.automations.status.failed")
: run.status === "skipped" ? t("settings.automations.skipped")
: t("settings.automations.calendar.recorded");
const duration = run.duration_ms;
const chatKey = run.webui_session_key !== undefined ? run.webui_session_key
: job.origin?.channel === "websocket" ? job.origin.session_key : null;
const chatHref = chatKey ? `#/chat/${encodeURIComponent(chatKey)}` : null;
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent
{...(!open ? { inert: "", "aria-hidden": true } : {})}
className="flex max-h-[calc(100dvh-2rem)] max-w-[520px] flex-col gap-0 p-0 text-sm font-normal leading-normal text-foreground"
onCloseAutoFocus={onCloseAutoFocus}
>
<DialogHeader className="shrink-0 px-6 pb-4 pr-14 pt-5 text-left">
<DialogTitle className="min-w-0 break-words text-lg font-medium leading-snug tracking-normal text-balance">
{job.name || job.id}
</DialogTitle>
<DialogDescription className="text-sm font-normal text-muted-foreground">
<time dateTime={new Date(run.run_at_ms).toISOString()}>
{new Intl.DateTimeFormat(locale, { dateStyle: "medium", timeStyle: "short" }).format(run.run_at_ms)}
</time>
</DialogDescription>
</DialogHeader>
<div className="min-h-0 space-y-4 overflow-y-auto overscroll-contain px-6 pb-6">
<dl className="space-y-3">
<div className="flex items-start justify-between gap-4">
<dt className="text-muted-foreground">{t("settings.automations.runDetails.result")}</dt>
<dd className={cn("flex items-center gap-1.5 text-end", failed && "text-destructive")}>
{failed ? <CircleAlert className="h-3.5 w-3.5 shrink-0" strokeWidth={1.5} aria-hidden /> : null}
{status}
</dd>
</div>
{duration != null && Number.isFinite(duration) && duration >= 0 ? (
<div className="flex items-start justify-between gap-4">
<dt className="text-muted-foreground">{t("settings.automations.runDetails.duration")}</dt>
<dd className="text-end tabular-nums">{new Intl.NumberFormat(locale, {
style: "unit", unit: "second", unitDisplay: "short",
maximumFractionDigits: duration < 1000 ? 3 : 1,
}).format(duration / 1000)}</dd>
</div>
) : null}
</dl>
{run.error ? <p className={cn("whitespace-pre-wrap break-words [overflow-wrap:anywhere]", failed && "text-destructive")}>{run.error}</p> : null}
{result.status === "loading" ? (
<p role="status" className="text-muted-foreground">{t("settings.automations.runDetails.loading")}</p>
) : result.status === "error" ? (
<div className="flex flex-wrap items-center justify-between gap-2">
<p role="alert" className="text-muted-foreground">{t("settings.automations.runDetails.loadError")}</p>
<Button variant="ghost" size="sm" onClick={onRetry}>
{t("settings.automations.runDetails.retry")}
</Button>
</div>
) : result.response?.trim() ? (
<MarkdownText className="min-w-0 text-sm [overflow-wrap:anywhere]">{result.response}</MarkdownText>
) : (
<p className="text-muted-foreground">{t(result.response === null
? "settings.automations.runDetails.noOutput"
: "settings.automations.runDetails.emptyOutput")}</p>
)}
</div>
{chatHref ? (
<div className="flex shrink-0 justify-end border-t border-border/45 px-6 py-3">
<Button asChild variant="ghost" size="sm" className="font-normal text-muted-foreground">
<a href={chatHref}>{t("settings.automations.emptyAction")}</a>
</Button>
</div>
) : null}
</DialogContent>
</Dialog>
);
}
@@ -15,6 +15,7 @@ import { useTranslation } from "react-i18next";
import { channelTranslator } from "@/channel-plugins/i18n";
import { channelUiOwner, channelUiPresentation } from "@/channel-plugins/registry";
import { AutomationCalendar } from "@/components/settings/system/AutomationCalendar";
import { AutomationRunDialog } from "@/components/settings/system/AutomationRunDialog";
import { AutomationChatBinding, type AutomationChatNames, type ChangeAutomationChat } from "@/components/settings/system/AutomationChatBinding";
import { AutomationRunAtPicker, parseAutomationRunAt } from "@/components/settings/system/AutomationRunAtPicker";
import {
@@ -349,7 +350,7 @@ export function AutomationsSettings({
</section>
) : (
<AutomationCalendar
token={token}
renderRunDialog={(props) => <AutomationRunDialog token={token} {...props} />}
jobs={filtered}
month={month}
locale={locale}
@@ -1,7 +1,7 @@
import { useEffect, useRef, useState, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { ToggleButton } from "@/components/settings/ToggleButton";
import { ToggleButton } from "@/components/ui/toggle-button";
import { SettingsGroup, SettingsRow, SettingsSectionTitle, RestartRequiredNotice, RestartSettingsFooter } from "@/components/settings/shared/SettingsControls";
import { ProviderPicker } from "@/components/settings/shared/ModelControls";
import { TimezonePicker } from "@/components/settings/shared/TimezonePicker";