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:
1 parent
db804ef94a
commit
30e43aec82
109 files changed
+7874
-3459
No files matched your search
@@ -1,46 +1,11 @@
|
||||
import { useRef, useState } from "react";
|
||||
import {
|
||||
CalendarClock,
|
||||
ChevronRight,
|
||||
CircleAlert,
|
||||
RefreshCcw,
|
||||
} from "lucide-react";
|
||||
import type { TFunction } from "i18next";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useCallback, useState } from "react";
|
||||
|
||||
import {
|
||||
AutomationDeleteDialog,
|
||||
AutomationDetailDialog,
|
||||
AutomationEditDialog,
|
||||
} from "@/components/settings/system/AutomationsSettings";
|
||||
import type { AutomationAction } from "@/components/settings/system/AutomationsSettings";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { formControlFocusClassName } from "@/components/ui/form-control";
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover";
|
||||
import { SessionInfoPopoverView } from "@/components/thread/SessionInfoPopoverView";
|
||||
import { useSessionAutomationJobs } from "@/hooks/useSessionAutomationJobs";
|
||||
import { currentLocale } from "@/i18n";
|
||||
import { runAutomationAction, updateAutomation } from "@/lib/api";
|
||||
import type { WebUIMutationTransport } from "@/lib/api";
|
||||
import { fmtDateTime } from "@/lib/format-time";
|
||||
import type {
|
||||
AutomationUpdatePayload,
|
||||
SessionAutomationJob,
|
||||
} from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const RELATIVE_THRESHOLDS: [number, Intl.RelativeTimeFormatUnit][] = [
|
||||
[60, "second"],
|
||||
[60, "minute"],
|
||||
[24, "hour"],
|
||||
[7, "day"],
|
||||
[4.345, "week"],
|
||||
[12, "month"],
|
||||
[Number.POSITIVE_INFINITY, "year"],
|
||||
];
|
||||
import type { AutomationUpdatePayload, SessionAutomationJob } from "@/lib/types";
|
||||
|
||||
interface SessionInfoPopoverProps {
|
||||
client: WebUIMutationTransport;
|
||||
@@ -50,286 +15,12 @@ interface SessionInfoPopoverProps {
|
||||
}
|
||||
|
||||
export function SessionInfoPopover({ client, sessionKey, token, title }: SessionInfoPopoverProps) {
|
||||
const { t } = useTranslation("common");
|
||||
const [open, setOpen] = useState(false);
|
||||
const [inspectedJob, setInspectedJob] = useState<SessionAutomationJob | null>(null);
|
||||
const [detailOpen, setDetailOpen] = useState(false);
|
||||
const [actionKey, setActionKey] = useState<string | null>(null);
|
||||
const [actionError, setActionError] = useState<string | null>(null);
|
||||
const [pendingEdit, setPendingEdit] = useState<SessionAutomationJob | null>(null);
|
||||
const [pendingDelete, setPendingDelete] = useState<SessionAutomationJob | null>(null);
|
||||
const triggerRef = useRef<HTMLButtonElement | null>(null);
|
||||
const afterDetailClose = useRef<(() => void) | null>(null);
|
||||
const surfacesOpen = open || detailOpen || Boolean(pendingEdit) || Boolean(pendingDelete);
|
||||
const { jobs, loading, loadFailed, now } = useSessionAutomationJobs(
|
||||
surfacesOpen,
|
||||
token,
|
||||
sessionKey,
|
||||
);
|
||||
const selectedJob = inspectedJob;
|
||||
|
||||
const inspectJob = (job: SessionAutomationJob) => {
|
||||
setActionError(null);
|
||||
setInspectedJob(job);
|
||||
setOpen(false);
|
||||
setDetailOpen(true);
|
||||
};
|
||||
const handOffDetail = (next: () => void) => {
|
||||
afterDetailClose.current = next;
|
||||
setDetailOpen(false);
|
||||
};
|
||||
const updateInspectedJob = (nextJobs: SessionAutomationJob[], id: string) => {
|
||||
const nextJob = nextJobs.find((job) => job.id === id);
|
||||
if (nextJob) setInspectedJob(nextJob);
|
||||
};
|
||||
const handleAction = async (action: AutomationAction, job: SessionAutomationJob) => {
|
||||
const key = `${action}:${job.id}`;
|
||||
setActionKey(key);
|
||||
setActionError(null);
|
||||
try {
|
||||
const payload = await runAutomationAction(client, action, job.id);
|
||||
if (action === "delete") {
|
||||
setPendingDelete(null);
|
||||
setInspectedJob(null);
|
||||
} else {
|
||||
updateInspectedJob(payload.jobs, job.id);
|
||||
}
|
||||
} catch (error) {
|
||||
setActionError(error instanceof Error ? error.message : String(error));
|
||||
} finally {
|
||||
setActionKey(null);
|
||||
}
|
||||
};
|
||||
const handleEdit = async (job: SessionAutomationJob, values: AutomationUpdatePayload) => {
|
||||
const key = `update:${job.id}`;
|
||||
setActionKey(key);
|
||||
setActionError(null);
|
||||
try {
|
||||
const payload = await updateAutomation(client, job.id, values);
|
||||
updateInspectedJob(payload.jobs, job.id);
|
||||
setPendingEdit(null);
|
||||
} catch (error) {
|
||||
setActionError(error instanceof Error ? error.message : String(error));
|
||||
} finally {
|
||||
setActionKey(null);
|
||||
}
|
||||
};
|
||||
const automationContent = loading ? (
|
||||
<div className="mx-2 flex items-center gap-2 rounded-floating bg-muted/45 px-3 py-3 text-[12.5px] text-muted-foreground">
|
||||
<RefreshCcw className="h-3.5 w-3.5 animate-spin" />
|
||||
{t("thread.sessionInfo.loading")}
|
||||
</div>
|
||||
) : loadFailed ? (
|
||||
<div className="mx-2 flex items-center gap-2 rounded-floating bg-destructive/10 px-3 py-3 text-[12.5px] text-destructive">
|
||||
<CircleAlert className="h-3.5 w-3.5" />
|
||||
{t("thread.sessionInfo.loadFailed")}
|
||||
</div>
|
||||
) : jobs.length ? (
|
||||
<div className="space-y-1 px-2">
|
||||
{jobs.map((job) => (
|
||||
<AutomationRow key={job.id} job={job} now={now} onSelect={inspectJob} />
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="mx-2 rounded-floating bg-muted/35 px-3 py-3 text-[12.5px] leading-relaxed text-muted-foreground">
|
||||
{t("thread.sessionInfo.empty")}
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
ref={triggerRef}
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
aria-label={t("thread.header.sessionInfo")}
|
||||
className={cn(
|
||||
"host-no-drag h-8 w-8 rounded-control text-muted-foreground/85",
|
||||
"hover:text-foreground",
|
||||
)}
|
||||
>
|
||||
<CalendarClock className="h-4 w-4 stroke-[1.75]" />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
align="end"
|
||||
sideOffset={8}
|
||||
className="w-[min(23rem,calc(100vw-1.5rem))] overflow-hidden p-0"
|
||||
onCloseAutoFocus={(event) => {
|
||||
if (detailOpen) event.preventDefault();
|
||||
}}
|
||||
>
|
||||
<div className="flex max-h-[min(var(--radix-popover-content-available-height),32rem)] flex-col">
|
||||
<div className="px-4 pb-3 pt-3.5">
|
||||
<div className="min-w-0">
|
||||
<div className="text-[12px] font-normal text-muted-foreground/75">
|
||||
{t("thread.sessionInfo.title")}
|
||||
</div>
|
||||
<div className="mt-0.5 truncate text-[14px] font-medium text-foreground">
|
||||
{title || t("thread.sessionInfo.untitled")}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-3 flex items-center justify-between gap-3 border-t border-border/45 pt-3">
|
||||
<span className="truncate text-[13px] font-medium text-foreground">
|
||||
{t("thread.sessionInfo.automations")}
|
||||
</span>
|
||||
<span className="rounded-full bg-muted/70 px-2 py-0.5 text-[11px] tabular-nums text-muted-foreground">
|
||||
{t("thread.sessionInfo.count", { count: jobs.length })}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="min-h-0 flex-1 overflow-y-auto overscroll-contain pb-2">
|
||||
{automationContent}
|
||||
</div>
|
||||
|
||||
<div className="border-t border-border/45 p-2">
|
||||
<Button asChild variant="ghost" size="sm" className="w-full justify-between px-2 text-[12px] text-muted-foreground">
|
||||
<a href="#/automations">
|
||||
{t("thread.sessionInfo.openCalendar")}
|
||||
<ChevronRight className="h-3.5 w-3.5" aria-hidden />
|
||||
</a>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
|
||||
<AutomationDetailDialog
|
||||
job={selectedJob}
|
||||
open={detailOpen}
|
||||
locale={currentLocale()}
|
||||
actionKey={actionKey}
|
||||
error={actionError}
|
||||
onOpenChange={setDetailOpen}
|
||||
onAction={handleAction}
|
||||
onRequestEdit={(job) => handOffDetail(() => setPendingEdit(job))}
|
||||
onRequestDelete={(job) => handOffDetail(() => setPendingDelete(job))}
|
||||
onCloseAutoFocus={(event) => {
|
||||
event.preventDefault();
|
||||
if (detailOpen) return;
|
||||
setInspectedJob(null);
|
||||
triggerRef.current?.focus({ preventScroll: true });
|
||||
const next = afterDetailClose.current;
|
||||
afterDetailClose.current = null;
|
||||
next?.();
|
||||
}}
|
||||
/>
|
||||
<AutomationEditDialog
|
||||
job={pendingEdit}
|
||||
saving={actionKey === `update:${pendingEdit?.id ?? ""}`}
|
||||
onOpenChange={(nextOpen) => {
|
||||
if (!nextOpen) setPendingEdit(null);
|
||||
}}
|
||||
onCancel={(job) => {
|
||||
setInspectedJob(job);
|
||||
setDetailOpen(true);
|
||||
}}
|
||||
onSave={handleEdit}
|
||||
/>
|
||||
<AutomationDeleteDialog
|
||||
job={pendingDelete}
|
||||
deleting={actionKey === `delete:${pendingDelete?.id ?? ""}`}
|
||||
onOpenChange={(nextOpen) => {
|
||||
if (!nextOpen) setPendingDelete(null);
|
||||
}}
|
||||
onConfirm={(job) => handleAction("delete", job)}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function AutomationRow({
|
||||
job,
|
||||
now,
|
||||
onSelect,
|
||||
}: {
|
||||
job: SessionAutomationJob;
|
||||
now: number;
|
||||
onSelect: (job: SessionAutomationJob) => void;
|
||||
}) {
|
||||
const { t } = useTranslation("common");
|
||||
const nextRun = formatNextRun(job, t, now);
|
||||
const needsAttention = job.state.last_status === "error" || Boolean(job.state.last_error);
|
||||
const summary = needsAttention
|
||||
? t("settings.automations.filters.failed", { defaultValue: "Needs attention" })
|
||||
: nextRun.label;
|
||||
const statusClass = job.enabled
|
||||
? needsAttention
|
||||
? "bg-destructive"
|
||||
: "bg-emerald-500"
|
||||
: "bg-muted-foreground/35";
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
aria-haspopup="dialog"
|
||||
aria-label={`${job.name || job.id}, ${summary}`}
|
||||
onClick={() => onSelect(job)}
|
||||
className={cn(
|
||||
"grid min-h-12 w-full grid-cols-[0.375rem_minmax(0,1fr)_1rem] items-center gap-2.5 rounded-control px-3 py-2 text-left",
|
||||
"transition-colors duration-150 settings-hover",
|
||||
formControlFocusClassName,
|
||||
)}
|
||||
>
|
||||
<span className={cn("h-1.5 w-1.5 shrink-0 rounded-full", statusClass)} aria-hidden />
|
||||
<span className="min-w-0">
|
||||
<span className="block truncate text-[13px] font-medium leading-5 text-foreground">
|
||||
{job.name || job.id}
|
||||
</span>
|
||||
<span
|
||||
className={cn(
|
||||
"block truncate text-[11.5px] leading-4 text-muted-foreground",
|
||||
needsAttention && "text-destructive",
|
||||
)}
|
||||
title={needsAttention ? summary : nextRun.title}
|
||||
>
|
||||
{summary}
|
||||
</span>
|
||||
</span>
|
||||
<ChevronRight className="h-4 w-4 text-muted-foreground/65" aria-hidden />
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
function formatNextRun(job: SessionAutomationJob, t: TFunction, now: number) {
|
||||
const locale = currentLocale();
|
||||
if (!job.enabled) {
|
||||
return { label: t("thread.sessionInfo.next.disabled"), title: "" };
|
||||
}
|
||||
if (job.state.pending) {
|
||||
return { label: t("thread.sessionInfo.next.pending"), title: "" };
|
||||
}
|
||||
if (isLocalTriggerAutomation(job)) {
|
||||
return { label: t("thread.sessionInfo.next.local"), title: "" };
|
||||
}
|
||||
const next = job.state.next_run_at_ms;
|
||||
if (!next) {
|
||||
return { label: t("thread.sessionInfo.next.none"), title: "" };
|
||||
}
|
||||
return {
|
||||
label: t("thread.sessionInfo.next.label", { time: relativeTimeFrom(next, now, locale) }),
|
||||
title: fmtDateTime(next, locale),
|
||||
};
|
||||
}
|
||||
|
||||
function isLocalTriggerAutomation(job: SessionAutomationJob): boolean {
|
||||
return job.kind === "local_trigger"
|
||||
|| job.payload.kind === "local_trigger"
|
||||
|| job.schedule.kind === "local";
|
||||
}
|
||||
|
||||
function relativeTimeFrom(value: number, now: number, locale: string): string {
|
||||
let delta = (value - now) / 1000;
|
||||
const formatter = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
||||
for (const [step, unit] of RELATIVE_THRESHOLDS) {
|
||||
if (Math.abs(delta) < step) {
|
||||
return formatter.format(Math.round(delta), unit);
|
||||
}
|
||||
delta /= step;
|
||||
}
|
||||
return formatter.format(Math.round(delta), "year");
|
||||
const [surfacesOpen, setSurfacesOpen] = useState(false);
|
||||
const { jobs, loading, loadFailed, now } = useSessionAutomationJobs(surfacesOpen, token, sessionKey);
|
||||
const act = useCallback(async (action: AutomationAction, job: SessionAutomationJob) =>
|
||||
(await runAutomationAction(client, action, job.id)).jobs, [client]);
|
||||
const edit = useCallback(async (job: SessionAutomationJob, values: AutomationUpdatePayload) =>
|
||||
(await updateAutomation(client, job.id, values)).jobs, [client]);
|
||||
return <SessionInfoPopoverView title={title} jobs={jobs} loading={loading} loadFailed={loadFailed} now={now}
|
||||
onAction={act} onEdit={edit} onSurfacesOpenChange={setSurfacesOpen} calendarHref="#/automations" />;
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import {
|
||||
CalendarClock,
|
||||
ChevronRight,
|
||||
CircleAlert,
|
||||
RefreshCcw,
|
||||
} from "lucide-react";
|
||||
import type { TFunction } from "i18next";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import {
|
||||
AutomationDeleteDialog,
|
||||
AutomationDetailDialog,
|
||||
AutomationEditDialog,
|
||||
} from "@/components/settings/system/AutomationsSettings";
|
||||
import type { AutomationAction } from "@/components/settings/system/AutomationsSettings";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { formControlFocusClassName } from "@/components/ui/form-control";
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover";
|
||||
import { currentLocale } from "@/i18n";
|
||||
import { fmtDateTime } from "@/lib/format-time";
|
||||
import type {
|
||||
AutomationUpdatePayload,
|
||||
SessionAutomationJob,
|
||||
} from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const RELATIVE_THRESHOLDS: [number, Intl.RelativeTimeFormatUnit][] = [
|
||||
[60, "second"],
|
||||
[60, "minute"],
|
||||
[24, "hour"],
|
||||
[7, "day"],
|
||||
[4.345, "week"],
|
||||
[12, "month"],
|
||||
[Number.POSITIVE_INFINITY, "year"],
|
||||
];
|
||||
|
||||
export function SessionInfoPopoverView({ title, jobs, loading, loadFailed, now, onAction, onEdit,
|
||||
onSurfacesOpenChange, calendarHref, onOpenCalendar }: {
|
||||
title: string;
|
||||
jobs: SessionAutomationJob[];
|
||||
loading: boolean;
|
||||
loadFailed: boolean;
|
||||
now: number;
|
||||
onAction: (action: AutomationAction, job: SessionAutomationJob) => Promise<SessionAutomationJob[]>;
|
||||
onEdit: (job: SessionAutomationJob, values: AutomationUpdatePayload) => Promise<SessionAutomationJob[]>;
|
||||
onSurfacesOpenChange?: (open: boolean) => void;
|
||||
calendarHref?: string;
|
||||
onOpenCalendar?: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation("common");
|
||||
const [open, setOpen] = useState(false);
|
||||
const [inspectedJob, setInspectedJob] = useState<SessionAutomationJob | null>(null);
|
||||
const [detailOpen, setDetailOpen] = useState(false);
|
||||
const [actionKey, setActionKey] = useState<string | null>(null);
|
||||
const [actionError, setActionError] = useState<string | null>(null);
|
||||
const [pendingEdit, setPendingEdit] = useState<SessionAutomationJob | null>(null);
|
||||
const [pendingDelete, setPendingDelete] = useState<SessionAutomationJob | null>(null);
|
||||
const triggerRef = useRef<HTMLButtonElement | null>(null);
|
||||
const afterDetailClose = useRef<(() => void) | null>(null);
|
||||
const surfacesOpen = open || detailOpen || Boolean(pendingEdit) || Boolean(pendingDelete);
|
||||
useEffect(() => { onSurfacesOpenChange?.(surfacesOpen); }, [onSurfacesOpenChange, surfacesOpen]);
|
||||
const selectedJob = inspectedJob;
|
||||
|
||||
const inspectJob = (job: SessionAutomationJob) => {
|
||||
setActionError(null);
|
||||
setInspectedJob(job);
|
||||
setOpen(false);
|
||||
setDetailOpen(true);
|
||||
};
|
||||
const handOffDetail = (next: () => void) => {
|
||||
afterDetailClose.current = next;
|
||||
setDetailOpen(false);
|
||||
};
|
||||
const updateInspectedJob = (nextJobs: SessionAutomationJob[], id: string) => {
|
||||
const nextJob = nextJobs.find((job) => job.id === id);
|
||||
if (nextJob) setInspectedJob(nextJob);
|
||||
};
|
||||
const handleAction = async (action: AutomationAction, job: SessionAutomationJob) => {
|
||||
const key = `${action}:${job.id}`;
|
||||
setActionKey(key);
|
||||
setActionError(null);
|
||||
try {
|
||||
const nextJobs = await onAction(action, job);
|
||||
if (action === "delete") {
|
||||
setPendingDelete(null);
|
||||
setInspectedJob(null);
|
||||
} else {
|
||||
updateInspectedJob(nextJobs, job.id);
|
||||
}
|
||||
} catch (error) {
|
||||
setActionError(error instanceof Error ? error.message : String(error));
|
||||
} finally {
|
||||
setActionKey(null);
|
||||
}
|
||||
};
|
||||
const handleEdit = async (job: SessionAutomationJob, values: AutomationUpdatePayload) => {
|
||||
const key = `update:${job.id}`;
|
||||
setActionKey(key);
|
||||
setActionError(null);
|
||||
try {
|
||||
const nextJobs = await onEdit(job, values);
|
||||
updateInspectedJob(nextJobs, job.id);
|
||||
setPendingEdit(null);
|
||||
} catch (error) {
|
||||
setActionError(error instanceof Error ? error.message : String(error));
|
||||
} finally {
|
||||
setActionKey(null);
|
||||
}
|
||||
};
|
||||
const automationContent = loading ? (
|
||||
<div className="mx-2 flex items-center gap-2 rounded-floating bg-muted/45 px-3 py-3 text-[12.5px] text-muted-foreground">
|
||||
<RefreshCcw className="h-3.5 w-3.5 animate-spin" />
|
||||
{t("thread.sessionInfo.loading")}
|
||||
</div>
|
||||
) : loadFailed ? (
|
||||
<div className="mx-2 flex items-center gap-2 rounded-floating bg-destructive/10 px-3 py-3 text-[12.5px] text-destructive">
|
||||
<CircleAlert className="h-3.5 w-3.5" />
|
||||
{t("thread.sessionInfo.loadFailed")}
|
||||
</div>
|
||||
) : jobs.length ? (
|
||||
<div className="space-y-1 px-2">
|
||||
{jobs.map((job) => (
|
||||
<AutomationRow key={job.id} job={job} now={now} onSelect={inspectJob} />
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="mx-2 rounded-floating bg-muted/35 px-3 py-3 text-[12.5px] leading-relaxed text-muted-foreground">
|
||||
{t("thread.sessionInfo.empty")}
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
ref={triggerRef}
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
aria-label={t("thread.header.sessionInfo")}
|
||||
className={cn(
|
||||
"host-no-drag h-8 w-8 rounded-control text-muted-foreground/85",
|
||||
"hover:text-foreground",
|
||||
)}
|
||||
>
|
||||
<CalendarClock className="h-4 w-4 stroke-[1.75]" />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
align="end"
|
||||
sideOffset={8}
|
||||
className="w-[min(23rem,calc(100vw-1.5rem))] overflow-hidden p-0"
|
||||
onCloseAutoFocus={(event) => {
|
||||
if (detailOpen) event.preventDefault();
|
||||
}}
|
||||
>
|
||||
<div className="flex max-h-[min(var(--radix-popover-content-available-height),32rem)] flex-col">
|
||||
<div className="px-4 pb-3 pt-3.5">
|
||||
<div className="min-w-0">
|
||||
<div className="text-[12px] font-normal text-muted-foreground/75">
|
||||
{t("thread.sessionInfo.title")}
|
||||
</div>
|
||||
<div className="mt-0.5 truncate text-[14px] font-medium text-foreground">
|
||||
{title || t("thread.sessionInfo.untitled")}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-3 flex items-center justify-between gap-3 border-t border-border/45 pt-3">
|
||||
<span className="truncate text-[13px] font-medium text-foreground">
|
||||
{t("thread.sessionInfo.automations")}
|
||||
</span>
|
||||
<span className="rounded-full bg-muted/70 px-2 py-0.5 text-[11px] tabular-nums text-muted-foreground">
|
||||
{t("thread.sessionInfo.count", { count: jobs.length })}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="min-h-0 flex-1 overflow-y-auto overscroll-contain pb-2">
|
||||
{automationContent}
|
||||
</div>
|
||||
|
||||
<div className="border-t border-border/45 p-2">
|
||||
<Button asChild={!!calendarHref} variant="ghost" size="sm" onClick={onOpenCalendar}
|
||||
className="w-full justify-between px-2 text-[12px] text-muted-foreground">
|
||||
{calendarHref ? <a href={calendarHref}>
|
||||
{t("thread.sessionInfo.openCalendar")}
|
||||
<ChevronRight className="h-3.5 w-3.5" aria-hidden />
|
||||
</a> : <>{t("thread.sessionInfo.openCalendar")}<ChevronRight className="h-3.5 w-3.5" aria-hidden /></>}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
|
||||
<AutomationDetailDialog
|
||||
job={selectedJob}
|
||||
open={detailOpen}
|
||||
locale={currentLocale()}
|
||||
actionKey={actionKey}
|
||||
error={actionError}
|
||||
onOpenChange={setDetailOpen}
|
||||
onAction={handleAction}
|
||||
onRequestEdit={(job) => handOffDetail(() => setPendingEdit(job))}
|
||||
onRequestDelete={(job) => handOffDetail(() => setPendingDelete(job))}
|
||||
onCloseAutoFocus={(event) => {
|
||||
event.preventDefault();
|
||||
if (detailOpen) return;
|
||||
setInspectedJob(null);
|
||||
triggerRef.current?.focus({ preventScroll: true });
|
||||
const next = afterDetailClose.current;
|
||||
afterDetailClose.current = null;
|
||||
next?.();
|
||||
}}
|
||||
/>
|
||||
<AutomationEditDialog
|
||||
job={pendingEdit}
|
||||
saving={actionKey === `update:${pendingEdit?.id ?? ""}`}
|
||||
onOpenChange={(nextOpen) => {
|
||||
if (!nextOpen) setPendingEdit(null);
|
||||
}}
|
||||
onCancel={(job) => {
|
||||
setInspectedJob(job);
|
||||
setDetailOpen(true);
|
||||
}}
|
||||
onSave={handleEdit}
|
||||
/>
|
||||
<AutomationDeleteDialog
|
||||
job={pendingDelete}
|
||||
deleting={actionKey === `delete:${pendingDelete?.id ?? ""}`}
|
||||
onOpenChange={(nextOpen) => {
|
||||
if (!nextOpen) setPendingDelete(null);
|
||||
}}
|
||||
onConfirm={(job) => handleAction("delete", job)}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function AutomationRow({
|
||||
job,
|
||||
now,
|
||||
onSelect,
|
||||
}: {
|
||||
job: SessionAutomationJob;
|
||||
now: number;
|
||||
onSelect: (job: SessionAutomationJob) => void;
|
||||
}) {
|
||||
const { t } = useTranslation("common");
|
||||
const nextRun = formatNextRun(job, t, now);
|
||||
const needsAttention = job.state.last_status === "error" || Boolean(job.state.last_error);
|
||||
const summary = needsAttention
|
||||
? t("settings.automations.filters.failed", { defaultValue: "Needs attention" })
|
||||
: nextRun.label;
|
||||
const statusClass = job.enabled
|
||||
? needsAttention
|
||||
? "bg-destructive"
|
||||
: "bg-emerald-500"
|
||||
: "bg-muted-foreground/35";
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
aria-haspopup="dialog"
|
||||
aria-label={`${job.name || job.id}, ${summary}`}
|
||||
onClick={() => onSelect(job)}
|
||||
className={cn(
|
||||
"grid min-h-12 w-full grid-cols-[0.375rem_minmax(0,1fr)_1rem] items-center gap-2.5 rounded-control px-3 py-2 text-left",
|
||||
"transition-colors duration-150 settings-hover",
|
||||
formControlFocusClassName,
|
||||
)}
|
||||
>
|
||||
<span className={cn("h-1.5 w-1.5 shrink-0 rounded-full", statusClass)} aria-hidden />
|
||||
<span className="min-w-0">
|
||||
<span className="block truncate text-[13px] font-medium leading-5 text-foreground">
|
||||
{job.name || job.id}
|
||||
</span>
|
||||
<span
|
||||
className={cn(
|
||||
"block truncate text-[11.5px] leading-4 text-muted-foreground",
|
||||
needsAttention && "text-destructive",
|
||||
)}
|
||||
title={needsAttention ? summary : nextRun.title}
|
||||
>
|
||||
{summary}
|
||||
</span>
|
||||
</span>
|
||||
<ChevronRight className="h-4 w-4 text-muted-foreground/65" aria-hidden />
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
function formatNextRun(job: SessionAutomationJob, t: TFunction, now: number) {
|
||||
const locale = currentLocale();
|
||||
if (!job.enabled) {
|
||||
return { label: t("thread.sessionInfo.next.disabled"), title: "" };
|
||||
}
|
||||
if (job.state.pending) {
|
||||
return { label: t("thread.sessionInfo.next.pending"), title: "" };
|
||||
}
|
||||
if (isLocalTriggerAutomation(job)) {
|
||||
return { label: t("thread.sessionInfo.next.local"), title: "" };
|
||||
}
|
||||
const next = job.state.next_run_at_ms;
|
||||
if (!next) {
|
||||
return { label: t("thread.sessionInfo.next.none"), title: "" };
|
||||
}
|
||||
return {
|
||||
label: t("thread.sessionInfo.next.label", { time: relativeTimeFrom(next, now, locale) }),
|
||||
title: fmtDateTime(next, locale),
|
||||
};
|
||||
}
|
||||
|
||||
function isLocalTriggerAutomation(job: SessionAutomationJob): boolean {
|
||||
return job.kind === "local_trigger"
|
||||
|| job.payload.kind === "local_trigger"
|
||||
|| job.schedule.kind === "local";
|
||||
}
|
||||
|
||||
function relativeTimeFrom(value: number, now: number, locale: string): string {
|
||||
let delta = (value - now) / 1000;
|
||||
const formatter = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
||||
for (const [step, unit] of RELATIVE_THRESHOLDS) {
|
||||
if (Math.abs(delta) < step) {
|
||||
return formatter.format(Math.round(delta), unit);
|
||||
}
|
||||
delta /= step;
|
||||
}
|
||||
return formatter.format(Math.round(delta), "year");
|
||||
}
|
||||
@@ -1,14 +1,6 @@
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { LoaderCircle } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { ThreadMessages } from "@/components/thread/ThreadMessages";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { usePageVisibility } from "@/hooks/usePageVisibility";
|
||||
import { fetchSubagentThread } from "@/lib/api";
|
||||
import { projectWebuiThreadMessages } from "@/lib/thread-display-projection";
|
||||
import { projectThreadEvents } from "@/lib/thread-event-projection";
|
||||
import type { SubagentTaskSnapshot, UIMessage, WebuiThreadPersistedPayload } from "@/lib/types";
|
||||
import { SubagentThreadView } from "@/components/thread/SubagentThreadView";
|
||||
import { useSubagentThread } from "@/components/thread/useSubagentThread";
|
||||
import type { SubagentTaskSnapshot } from "@/lib/types";
|
||||
|
||||
interface SubagentThreadProps {
|
||||
token: string;
|
||||
@@ -19,58 +11,7 @@ interface SubagentThreadProps {
|
||||
}
|
||||
|
||||
export function SubagentThread({ token, sessionKey, task, historyEnabled, isStreaming }: SubagentThreadProps) {
|
||||
const { t } = useTranslation();
|
||||
const pageVisible = usePageVisibility();
|
||||
const [thread, setThread] = useState<WebuiThreadPersistedPayload | null>(null);
|
||||
const [failed, setFailed] = useState(false);
|
||||
const refreshRef = useRef<(() => void) | null>(null);
|
||||
const taskId = task.task_id;
|
||||
|
||||
useEffect(() => {
|
||||
if (!historyEnabled || !pageVisible) return;
|
||||
let cancelled = false;
|
||||
let refreshing = false;
|
||||
let pending = false;
|
||||
const controller = new AbortController();
|
||||
const refresh = async () => {
|
||||
if (refreshing) { pending = true; return; }
|
||||
refreshing = true;
|
||||
try {
|
||||
const payload = await fetchSubagentThread(token, sessionKey, taskId, controller.signal);
|
||||
if (!cancelled) { setThread(payload); setFailed(false); }
|
||||
} catch {
|
||||
if (!cancelled) setFailed(true);
|
||||
} finally {
|
||||
refreshing = false;
|
||||
if (pending && !cancelled) { pending = false; void refresh(); }
|
||||
}
|
||||
};
|
||||
refreshRef.current = () => { void refresh(); };
|
||||
return () => { cancelled = true; refreshRef.current = null; controller.abort(); };
|
||||
}, [historyEnabled, pageVisible, sessionKey, taskId, token]);
|
||||
|
||||
// Task revisions already arrive through the owning chat's event stream.
|
||||
useEffect(() => {
|
||||
refreshRef.current?.();
|
||||
}, [historyEnabled, pageVisible, sessionKey, taskId, task.revision, token]);
|
||||
|
||||
const messages = useMemo(() => {
|
||||
if (historyEnabled) return thread ? projectWebuiThreadMessages(projectThreadEvents(thread.events)) : [];
|
||||
// Protocol 1 hosts without history still expose a task description and result.
|
||||
const summary: UIMessage[] = [{ id: `${taskId}-description`, role: "user", content: task.task_description, createdAt: task.created_at * 1000 }];
|
||||
if (task.result) summary.push({ id: `${taskId}-result`, role: "assistant", content: task.result,
|
||||
createdAt: (task.completed_at ?? task.created_at) * 1000 });
|
||||
return summary;
|
||||
}, [historyEnabled, taskId, task.task_description, task.created_at, task.completed_at, task.result, thread]);
|
||||
|
||||
return <>
|
||||
{failed ? <div role="alert" className="mb-4 flex items-center gap-2 px-5 text-sm text-destructive">
|
||||
<span>{t("thread.history.error")}</span>
|
||||
<Button type="button" variant="ghost" size="sm" onClick={() => refreshRef.current?.()}>{t("thread.history.retry")}</Button>
|
||||
</div> : historyEnabled && thread === null ? <p role="status" className="flex items-center gap-2 px-5 text-sm text-muted-foreground">
|
||||
<LoaderCircle className="h-3.5 w-3.5 animate-spin motion-reduce:animate-none" aria-hidden />{t("chat.loading")}
|
||||
</p> : null}
|
||||
<ThreadMessages messages={messages} isStreaming={isStreaming} activeTurnId={thread?.active_turn_id}
|
||||
runStartedAt={task.created_at} />
|
||||
</>;
|
||||
const { messages, loading, failed, activeTurnId, retry } = useSubagentThread({ token, sessionKey, task, historyEnabled });
|
||||
return <SubagentThreadView messages={messages} loading={loading} failed={failed} isStreaming={isStreaming}
|
||||
activeTurnId={activeTurnId} runStartedAt={task.created_at} onRetry={retry} />;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { LoaderCircle } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { ThreadMessages } from "@/components/thread/ThreadMessages";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import type { UIMessage } from "@/lib/types";
|
||||
|
||||
export function SubagentThreadView({ messages, loading, failed, isStreaming, activeTurnId, runStartedAt, onRetry }: {
|
||||
messages: UIMessage[];
|
||||
loading: boolean;
|
||||
failed: boolean;
|
||||
isStreaming: boolean;
|
||||
activeTurnId?: string | null;
|
||||
runStartedAt: number;
|
||||
onRetry: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
return <>
|
||||
{failed ? <div role="alert" className="mb-4 flex items-center gap-2 px-5 text-sm text-destructive">
|
||||
<span>{t("thread.history.error")}</span>
|
||||
<Button type="button" variant="ghost" size="sm" onClick={onRetry}>{t("thread.history.retry")}</Button>
|
||||
</div> : loading ? <p role="status" className="flex items-center gap-2 px-5 text-sm text-muted-foreground">
|
||||
<LoaderCircle className="h-3.5 w-3.5 animate-spin motion-reduce:animate-none" aria-hidden />{t("chat.loading")}
|
||||
</p> : null}
|
||||
<ThreadMessages messages={messages} isStreaming={isStreaming} activeTurnId={activeTurnId} runStartedAt={runStartedAt} />
|
||||
</>;
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import type { HTMLAttributes, ReactElement } from "react";
|
||||
import { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
|
||||
import { ArrowLeft, ArrowRight, Check, ChevronRight, FolderSearch, History, Search, Star } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { ToggleButton } from "@/components/settings/ToggleButton";
|
||||
import { ToggleButton } from "@/components/ui/toggle-button";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { floatingItemClassName, floatingItemFocusClassName } from "@/components/ui/floating-surface";
|
||||
import { HoverHint } from "@/components/ui/tooltip";
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
|
||||
import { usePageVisibility } from "@/hooks/usePageVisibility";
|
||||
import { fetchSubagentThread } from "@/lib/api";
|
||||
import { projectWebuiThreadMessages } from "@/lib/thread-display-projection";
|
||||
import { projectThreadEvents } from "@/lib/thread-event-projection";
|
||||
import type { SubagentTaskSnapshot, UIMessage, WebuiThreadPersistedPayload } from "@/lib/types";
|
||||
|
||||
export function useSubagentThread({ token, sessionKey, task, historyEnabled }: {
|
||||
token: string;
|
||||
sessionKey: string;
|
||||
task: SubagentTaskSnapshot;
|
||||
historyEnabled: boolean;
|
||||
}) {
|
||||
const pageVisible = usePageVisibility();
|
||||
const [thread, setThread] = useState<WebuiThreadPersistedPayload | null>(null);
|
||||
const [failed, setFailed] = useState(false);
|
||||
const refreshRef = useRef<(() => void) | null>(null);
|
||||
const taskId = task.task_id;
|
||||
|
||||
useEffect(() => {
|
||||
if (!historyEnabled || !pageVisible) return;
|
||||
let cancelled = false;
|
||||
let refreshing = false;
|
||||
let pending = false;
|
||||
const controller = new AbortController();
|
||||
const refresh = async () => {
|
||||
if (refreshing) { pending = true; return; }
|
||||
refreshing = true;
|
||||
try {
|
||||
const payload = await fetchSubagentThread(token, sessionKey, taskId, controller.signal);
|
||||
if (!cancelled) { setThread(payload); setFailed(false); }
|
||||
} catch {
|
||||
if (!cancelled) setFailed(true);
|
||||
} finally {
|
||||
refreshing = false;
|
||||
if (pending && !cancelled) { pending = false; void refresh(); }
|
||||
}
|
||||
};
|
||||
refreshRef.current = () => { void refresh(); };
|
||||
return () => { cancelled = true; refreshRef.current = null; controller.abort(); };
|
||||
}, [historyEnabled, pageVisible, sessionKey, taskId, token]);
|
||||
|
||||
// Task revisions already arrive through the owning chat's event stream.
|
||||
useEffect(() => {
|
||||
refreshRef.current?.();
|
||||
}, [historyEnabled, pageVisible, sessionKey, taskId, task.revision, token]);
|
||||
|
||||
const messages = useMemo(() => {
|
||||
if (historyEnabled) return thread ? projectWebuiThreadMessages(projectThreadEvents(thread.events)) : [];
|
||||
// Protocol 1 hosts without history still expose a task description and result.
|
||||
const summary: UIMessage[] = [{ id: `${taskId}-description`, role: "user", content: task.task_description, createdAt: task.created_at * 1000 }];
|
||||
if (task.result) summary.push({ id: `${taskId}-result`, role: "assistant", content: task.result,
|
||||
createdAt: (task.completed_at ?? task.created_at) * 1000 });
|
||||
return summary;
|
||||
}, [historyEnabled, taskId, task.task_description, task.created_at, task.completed_at, task.result, thread]);
|
||||
|
||||
const retry = useCallback(() => refreshRef.current?.(), []);
|
||||
return { messages, failed, loading: historyEnabled && thread === null, activeTurnId: thread?.active_turn_id, retry };
|
||||
}
|
||||
Reference in new issue
Block a user