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

@@ -0,0 +1,16 @@
import { Component, type ReactNode } from "react";
export class SurfaceErrorBoundary extends Component<
{ children: ReactNode; fallback: ReactNode },
{ failed: boolean }
> {
state = { failed: false };
static getDerivedStateFromError() {
return { failed: true };
}
render() {
return this.state.failed ? this.props.fallback : this.props.children;
}
}
@@ -0,0 +1,30 @@
import { lazy, Suspense, type ComponentProps } from "react";
import { useTranslation } from "react-i18next";
import { StartupShell } from "@/components/StartupShell";
import { SurfaceErrorBoundary } from "@/components/SurfaceErrorBoundary";
import { Button } from "@/components/ui/button";
const RemoteConnectionsPage = lazy(async () => {
const module = await import("./RemoteConnectionsPage");
return { default: module.RemoteConnectionsPage };
});
export function LazyRemoteConnectionsPage(props: ComponentProps<typeof RemoteConnectionsPage>) {
const { t } = useTranslation();
return <SurfaceErrorBoundary fallback={
<div className="flex min-h-0 flex-1 flex-col items-center justify-center gap-3 bg-background p-6 text-center">
<p role="alert" className="max-w-sm text-sm text-destructive">{t("app.loading.failed")}</p>
<Button variant="ghost" onClick={props.onBackToChat}>{t("remote.back")}</Button>
</div>
}>
<Suspense fallback={
<div className="relative min-h-0 flex-1">
<StartupShell embedded surface="remote" sidebarExpanded={props.mainNavigationExpanded ?? false} />
<Button variant="ghost" className={`touch-target absolute left-4 z-10 ${props.hostChromeInset ? "top-20" : "top-4"}`}
onClick={props.onBackToChat}>{t("remote.back")}</Button>
</div>
}>
<RemoteConnectionsPage {...props} />
</Suspense>
</SurfaceErrorBoundary>;
}
@@ -4,7 +4,7 @@ import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/button";
import { groupRemoteProfiles, isCompatibilityError, needsRemoteSetup } from "@/lib/remote-instances";
import { HostCompatibilityDialog } from "./HostCompatibilityDialog";
import { RemoteConnectionsPage } from "./RemoteConnectionsPage";
import { LazyRemoteConnectionsPage } from "./LazyRemoteConnectionsPage";
import { ThemeProvider } from "@/hooks/useTheme";
import { useHostSessions } from "./useHostSessions";
import { HostNavigationContext, HostSwitcher, RemoteHostMenu, type HostPicker } from "./HostSwitcher";
@@ -174,7 +174,7 @@ export function RemoteInstances({ children }: { children: ReactNode }) {
className="absolute bottom-0 right-0 flex min-h-0 flex-col bg-background"
style={{ left: offline ? 0 : Math.min(bridge.surface?.left || 0, Math.max(0, window.innerWidth - 280)), top: offline ? 0 : Math.min(bridge.surface?.top || 0, Math.max(0, window.innerHeight - 280)) }}>
<ThemeProvider theme={bridge.surface?.theme || "light"}>
<RemoteConnectionsPage onBackToChat={() => changeManagement(false)} mainNavigationExpanded={!!bridge.surface?.left} />
<LazyRemoteConnectionsPage onBackToChat={() => changeManagement(false)} mainNavigationExpanded={!!bridge.surface?.left} />
</ThemeProvider>
</main>}
{!managing && selected && (offline || !activeFrame?.loaded) && <div role="region" aria-label={t(offline ? recoveryTitle : "remote.opening")} className="absolute inset-0 overflow-y-auto bg-background p-6 text-center"><div className="flex min-h-full flex-col items-center justify-center gap-3">
@@ -1,5 +1,4 @@
import { useEffect, useState, type ReactNode } from "react";
import QRCode from "qrcode";
import { Check, CircleAlert, Clock, Loader2, RotateCcw, X } from "lucide-react";
import { useTranslation } from "react-i18next";
@@ -77,17 +76,20 @@ export function ChannelQrConnectFlowView({
: undefined;
useEffect(() => {
if (!showQrCode || !connect?.qr_url) {
const qrUrl = connect?.qr_url;
if (!showQrCode || !qrUrl) {
setQrDataUrl("");
return;
}
let cancelled = false;
void QRCode.toDataURL(connect.qr_url, {
width: 184,
margin: 1,
color: { dark: "#111827", light: "#ffffff" },
})
.then((url) => {
void import("qrcode")
.then(async ({ default: QRCode }) => {
if (cancelled) return;
const url = await QRCode.toDataURL(qrUrl, {
width: 184,
margin: 1,
color: { dark: "#111827", light: "#ffffff" },
});
if (!cancelled) setQrDataUrl(url);
})
.catch(() => {
@@ -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}
</>
);
}