fix(webui): show route-aligned skeletons from first paint (#6303)
This commit is contained in:
1 parent
af68da3280
commit
5d886fbb20
43 files changed
+1592
-213
No files matched your search
@@ -8,9 +8,11 @@ const AUTOMATIONS_REFRESH_MS = 3000;
|
||||
|
||||
export function useSessionAutomationJobs(open: boolean, token: string, sessionKey: string) {
|
||||
const pageVisible = usePageVisibility();
|
||||
const [jobs, setJobs] = useState<SessionAutomationJob[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [loadFailed, setLoadFailed] = useState(false);
|
||||
const [snapshot, setSnapshot] = useState<{
|
||||
sessionKey: string;
|
||||
jobs: SessionAutomationJob[] | null;
|
||||
loadFailed: boolean;
|
||||
}>({ sessionKey, jobs: null, loadFailed: false });
|
||||
const [now, setNow] = useState(() => Date.now());
|
||||
const tokenRef = useRef(token);
|
||||
tokenRef.current = token;
|
||||
@@ -18,34 +20,28 @@ export function useSessionAutomationJobs(open: boolean, token: string, sessionKe
|
||||
useEffect(() => {
|
||||
if (!open || !pageVisible) return;
|
||||
let cancelled = false;
|
||||
let loadedOnce = false;
|
||||
let refreshing = false;
|
||||
setSnapshot((current) => current.sessionKey === sessionKey
|
||||
? { ...current, loadFailed: false }
|
||||
: { sessionKey, jobs: null, loadFailed: false });
|
||||
|
||||
const refresh = async (showLoading = false) => {
|
||||
const refresh = async () => {
|
||||
if (refreshing) return;
|
||||
refreshing = true;
|
||||
if (showLoading) {
|
||||
setLoading(true);
|
||||
setLoadFailed(false);
|
||||
setJobs([]);
|
||||
}
|
||||
try {
|
||||
const next = await fetchSessionAutomations(tokenRef.current, sessionKey);
|
||||
if (cancelled) return;
|
||||
setJobs(next.jobs);
|
||||
setLoadFailed(false);
|
||||
loadedOnce = true;
|
||||
if (!cancelled) setSnapshot({ sessionKey, jobs: next.jobs, loadFailed: false });
|
||||
} catch {
|
||||
if (!cancelled && !loadedOnce) setLoadFailed(true);
|
||||
if (!cancelled) setSnapshot((current) => current.jobs !== null
|
||||
? current : { sessionKey, jobs: null, loadFailed: true });
|
||||
} finally {
|
||||
refreshing = false;
|
||||
if (!cancelled && showLoading) setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
void refresh(true);
|
||||
const refreshId = window.setInterval(() => void refresh(false), AUTOMATIONS_REFRESH_MS);
|
||||
const refreshOnFocus = () => void refresh(false);
|
||||
void refresh();
|
||||
const refreshId = window.setInterval(() => void refresh(), AUTOMATIONS_REFRESH_MS);
|
||||
const refreshOnFocus = () => void refresh();
|
||||
window.addEventListener("focus", refreshOnFocus);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
@@ -61,5 +57,11 @@ export function useSessionAutomationJobs(open: boolean, token: string, sessionKe
|
||||
return () => window.clearInterval(tickId);
|
||||
}, [open, pageVisible]);
|
||||
|
||||
return { jobs, loading, loadFailed, now };
|
||||
const current = snapshot.sessionKey === sessionKey ? snapshot : null;
|
||||
return {
|
||||
jobs: current?.jobs ?? [],
|
||||
loading: open && current?.jobs == null && !current?.loadFailed,
|
||||
loadFailed: current?.loadFailed ?? false,
|
||||
now,
|
||||
};
|
||||
}
|
||||
@@ -210,6 +210,7 @@ function cachedHistoryState(
|
||||
export function useSessions(): {
|
||||
sessions: ChatSummary[];
|
||||
loading: boolean;
|
||||
initialLoading: boolean;
|
||||
error: string | null;
|
||||
refresh: () => Promise<void>;
|
||||
createChat: (
|
||||
@@ -225,6 +226,7 @@ export function useSessions(): {
|
||||
} {
|
||||
const { client, token } = useClient();
|
||||
const [sessions, setSessions] = useState<ChatSummary[]>(readReloadSessions);
|
||||
const [loadedOnce, setLoadedOnce] = useState(() => sessions.length > 0);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const tokenRef = useRef(token);
|
||||
@@ -247,6 +249,7 @@ export function useSessions(): {
|
||||
refreshPendingRef.current = false;
|
||||
try {
|
||||
const rows = await listSessions(tokenRef.current);
|
||||
setLoadedOnce(true);
|
||||
const serverKeys = new Set(rows.map((row) => row.key));
|
||||
setSessions((prev) => {
|
||||
const byKey = new Map(prev.map((row) => [row.key, row]));
|
||||
@@ -379,6 +382,7 @@ export function useSessions(): {
|
||||
return {
|
||||
sessions,
|
||||
loading,
|
||||
initialLoading: loading && !loadedOnce,
|
||||
error,
|
||||
refresh,
|
||||
createChat,
|
||||
|
||||
Reference in new issue
Block a user