perf(webui): defer non-chat startup interfaces

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-24 22:15:50 +08:00
1 parent ae7fed88e2
commit 4bd1b74c6b
20 files changed
+710 -70

No files matched your search

@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from "react";
import { lazy, Suspense, useEffect, useRef, useState } from "react";
import {
CalendarClock,
ChevronRight,
@@ -7,12 +7,9 @@ import {
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 { SurfaceErrorBoundary } from "@/components/SurfaceErrorBoundary";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { SkeletonStatus } from "@/components/settings/shared/SkeletonStatus";
import { formControlFocusClassName } from "@/components/ui/form-control";
@@ -29,6 +26,19 @@ import type {
} from "@/lib/types";
import { cn } from "@/lib/utils";
const AutomationDetailDialog = lazy(async () => {
const module = await import("@/components/settings/system/AutomationsSettings");
return { default: module.AutomationDetailDialog };
});
const AutomationEditDialog = lazy(async () => {
const module = await import("@/components/settings/system/AutomationsSettings");
return { default: module.AutomationEditDialog };
});
const AutomationDeleteDialog = lazy(async () => {
const module = await import("@/components/settings/system/AutomationsSettings");
return { default: module.AutomationDeleteDialog };
});
const RELATIVE_THRESHOLDS: [number, Intl.RelativeTimeFormatUnit][] = [
[60, "second"],
[60, "minute"],
@@ -55,6 +65,8 @@ export function SessionInfoPopoverView({ title, jobs, loading, loadFailed, now,
const { t } = useTranslation("common");
const [open, setOpen] = useState(false);
const [inspectedJob, setInspectedJob] = useState<SessionAutomationJob | null>(null);
// Keep requested dialogs mounted through their close and focus-restoration lifecycle.
const [dialogsRequested, setDialogsRequested] = useState(false);
const [detailOpen, setDetailOpen] = useState(false);
const [actionKey, setActionKey] = useState<string | null>(null);
const [actionError, setActionError] = useState<string | null>(null);
@@ -67,6 +79,7 @@ export function SessionInfoPopoverView({ title, jobs, loading, loadFailed, now,
const selectedJob = inspectedJob;
const inspectJob = (job: SessionAutomationJob) => {
setDialogsRequested(true);
setActionError(null);
setInspectedJob(job);
setOpen(false);
@@ -76,6 +89,15 @@ export function SessionInfoPopoverView({ title, jobs, loading, loadFailed, now,
afterDetailClose.current = next;
setDetailOpen(false);
};
const handleDetailCloseAutoFocus = (event: Event) => {
event.preventDefault();
if (detailOpen) return;
setInspectedJob(null);
triggerRef.current?.focus({ preventScroll: true });
const next = afterDetailClose.current;
afterDetailClose.current = null;
next?.();
};
const updateInspectedJob = (nextJobs: SessionAutomationJob[], id: string) => {
const nextJob = nextJobs.find((job) => job.id === id);
if (nextJob) setInspectedJob(nextJob);
@@ -112,6 +134,18 @@ export function SessionInfoPopoverView({ title, jobs, loading, loadFailed, now,
setActionKey(null);
}
};
const detailPlaceholder = (failed: boolean) => (
<Dialog open={detailOpen} onOpenChange={setDetailOpen}>
<DialogContent aria-describedby={undefined} className="max-w-[520px]" onCloseAutoFocus={handleDetailCloseAutoFocus}>
<DialogHeader><DialogTitle>{selectedJob?.name || selectedJob?.id}</DialogTitle></DialogHeader>
{failed ? <p role="alert" className="text-sm text-destructive">{t("thread.sessionInfo.loadFailed")}</p> : (
<SkeletonStatus label={t("thread.sessionInfo.loading")} className="space-y-3">
<div className="h-24 rounded-control bg-muted/35" />
</SkeletonStatus>
)}
</DialogContent>
</Dialog>
);
const automationContent = loading && jobs.length === 0 ? (
<SkeletonStatus label={t("thread.sessionInfo.loading")} className="space-y-1 px-2">
{[0, 1, 2].map((index) => <div key={index} data-session-job-skeleton className="grid min-h-12 grid-cols-[0.375rem_minmax(0,1fr)_1rem] items-center gap-2.5 px-3 py-2">
@@ -203,46 +237,42 @@ export function SessionInfoPopoverView({ title, jobs, loading, loadFailed, now,
</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)}
/>
{dialogsRequested ? <SurfaceErrorBoundary fallback={detailPlaceholder(true)}>
<Suspense fallback={detailPlaceholder(false)}>
<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={handleDetailCloseAutoFocus}
/>
<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)}
/>
</Suspense>
</SurfaceErrorBoundary> : null}
</>
);
}