fix(webui): make browser notification failures actionable

This commit is contained in:
Xubin Ren authored and chengyongru committed 2026-10-20 13:45:04 +08:00
1 parent 909b277e7a
commit 7b2af031c0
30 files changed
+919 -96

No files matched your search

@@ -11,6 +11,7 @@ import { HostNavigationContext, HostSwitcher, RemoteHostMenu, type HostPicker }
import { useSidebarHostBridge } from "./useSidebarHostBridge";
import { readPairReturn, subscribePairReturn } from "@/lib/remote-pair-return";
import { RemoteContext, type HostConnectionState } from "./remote-context";
import { NOTIFICATION_OPEN_EVENT } from "@/lib/browser-notifications";
/** Keep host views alive; navigation belongs in each view's sidebar, not a second header. */
export function RemoteInstances({ children }: { children: ReactNode }) {
@@ -34,7 +35,16 @@ export function RemoteInstances({ children }: { children: ReactNode }) {
if (lastLocalFocus.current?.isConnected) lastLocalFocus.current.focus({ preventScroll: true });
};
const message = error ? t(`remote.errors.${error}`, { defaultValue: t("remote.errors.unknown") }) : "";
const bridge = useSidebarHostBridge(frames, selected?.id, frameNodes, restoreLocalFocus, { pendingName: pending?.name, error: message }, managing, () => changeManagement(false));
const bridge = useSidebarHostBridge(frames, selected?.id, frameNodes, restoreLocalFocus, { pendingName: pending?.name, error: message }, managing, () => changeManagement(false), (id) => {
if (hosts.reveal(id)) changeManagement(false);
});
useEffect(() => {
const revealLocal = () => {
if (window.parent === window) { returnLocal(); setManaging(false); }
};
window.addEventListener(NOTIFICATION_OPEN_EVENT, revealLocal);
return () => window.removeEventListener(NOTIFICATION_OPEN_EVENT, revealLocal);
}, [returnLocal]);
useEffect(() => {
if (!managing || !selected || !bridge.surface?.theme) return;
// Management is drawn by the local shell, beside this host's sidebar.
@@ -58,6 +58,13 @@ export function useHostSessions() {
publish();
}, [publish]);
const local = useCallback(() => { cancel(); select(null); }, [cancel, select]);
// A notification may reveal its existing view, never reconnect a host.
const reveal = useCallback((id: string) => {
const frame = cache.current.get(id);
if (!frame?.loaded || frame.offline) return false;
cancel(); select(frame.connection);
return true;
}, [cancel, select]);
const applyDirectory = useCallback((next: RemoteDirectory) => {
setDirectory(next); setDirectoryError(false);
// Names are metadata, not a new host session. Keep iframe URLs, drafts,
@@ -263,5 +270,5 @@ export function useHostSessions() {
};
}, [applyDirectory, client, getToken, publish]);
return { directory, directoryError, refresh, rename, selected, recentIds, frames, pending,
error: failure?.code || "", errorId: failure?.id, clearError, connect, cancel, local, disconnect, remove, loaded };
error: failure?.code || "", errorId: failure?.id, clearError, connect, cancel, local, reveal, disconnect, remove, loaded };
}
@@ -1,11 +1,12 @@
import { useCallback, useEffect, useRef, useState, type RefObject } from "react";
import { HOST_BRIDGE, bridgeMessage, isLocalShellOrigin, readEmbeddedHost, readHostAnchor, type EmbeddedHost, type HostAnchor } from "./host-bridge";
import type { HostFrame } from "./useHostSessions";
import { NOTIFICATION_OPEN_EVENT } from "@/lib/browser-notifications";
interface Peer { source: Window; origin: string; nonce: string }
export function useSidebarHostBridge(frames: HostFrame[], selectedId: string | undefined,
nodes: RefObject<Map<string, HTMLIFrameElement>>, restoreLocalFocus: () => void,
activity: { pendingName?: string; error?: string }, managing = false, onLeaveManagement?: () => void) {
activity: { pendingName?: string; error?: string }, managing = false, onLeaveManagement?: () => void, onReveal?: (id: string) => void) {
const [embedded, setEmbedded] = useState<EmbeddedHost | null>(null);
const [readyIds, setReadyIds] = useState<string[]>([]);
const [anchor, setAnchor] = useState<HostAnchor | null>(null);
@@ -13,8 +14,8 @@ export function useSidebarHostBridge(frames: HostFrame[], selectedId: string | u
const [hostedManagement, setHostedManagement] = useState(false);
const parent = useRef<Peer | null>(null);
const peers = useRef(new Map<string, Peer>());
const latest = useRef({ frames, selectedId, restoreLocalFocus, activity, managing, onLeaveManagement });
latest.current = { frames, selectedId, restoreLocalFocus, activity, managing, onLeaveManagement };
const latest = useRef({ frames, selectedId, restoreLocalFocus, activity, managing, onLeaveManagement, onReveal });
latest.current = { frames, selectedId, restoreLocalFocus, activity, managing, onLeaveManagement, onReveal };
const initialize = useCallback((id: string) => {
const frame = latest.current.frames.find((item) => item.connection.id === id);
const source = nodes.current?.get(id)?.contentWindow;
@@ -70,13 +71,25 @@ export function useSidebarHostBridge(frames: HostFrame[], selectedId: string | u
if (rect && rect.left >= 0 && rect.top >= 0) setSurfaces((previous) => ({ ...previous, [id]: { ...rect, theme } }));
} else if (message.type === "leave-management" && latest.current.selectedId === id) {
latest.current.onLeaveManagement?.();
} else if (message.type === "reveal-notification" && frame.loaded && !frame.offline) {
// Source/origin/nonce above select only this already-open view. No
// requested host id, destination URL, or connection command is accepted.
latest.current.onReveal?.(id);
}
// No select/connect/disconnect action is accepted from an embedded page.
};
window.addEventListener("message", receive);
const revealNotification = () => {
const peer = parent.current;
if (peer) window.parent.postMessage({ channel: HOST_BRIDGE, type: "reveal-notification", nonce: peer.nonce }, peer.origin);
};
window.addEventListener(NOTIFICATION_OPEN_EVENT, revealNotification);
// A public capability probe carries no identity, settings or credentials.
if (window.parent !== window) window.parent.postMessage({ channel: HOST_BRIDGE, type: "hello" }, "*");
return () => window.removeEventListener("message", receive);
return () => {
window.removeEventListener("message", receive);
window.removeEventListener(NOTIFICATION_OPEN_EVENT, revealNotification);
};
}, [initialize, nodes]);
useEffect(() => { setAnchor(null); }, [selectedId]);
const selectedName = frames.find((frame) => frame.connection.id === selectedId)?.connection.name;