fix(chat): switch from webview to iframe for Control UI embedding
The <webview> tag had issues with event listener attachment and React ref handling, causing the loading overlay to stay forever (white screen). Switched to a standard <iframe> which is simpler and works reliably: - Uses iframe onLoad/onError instead of webview dom-ready events - Added 5s fallback timeout to dismiss loading overlay - The X-Frame-Options/CSP header overrides from session.webRequest allow the iframe to load the Control UI
This commit is contained in:
@@ -4,34 +4,16 @@
|
|||||||
* The Control UI handles all chat protocol details (sessions, streaming, etc.)
|
* The Control UI handles all chat protocol details (sessions, streaming, etc.)
|
||||||
* and is served by the Gateway at http://127.0.0.1:{port}/
|
* and is served by the Gateway at http://127.0.0.1:{port}/
|
||||||
*/
|
*/
|
||||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
import { useState, useEffect, useCallback } from 'react';
|
||||||
import { AlertCircle, RefreshCw, Loader2 } from 'lucide-react';
|
import { AlertCircle, RefreshCw, Loader2 } from 'lucide-react';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { useGatewayStore } from '@/stores/gateway';
|
import { useGatewayStore } from '@/stores/gateway';
|
||||||
|
|
||||||
// Custom CSS to inject into the Control UI to match ClawX theme
|
|
||||||
const CUSTOM_CSS = `
|
|
||||||
/* Hide the Control UI header/nav that we don't need */
|
|
||||||
.gateway-header, [data-testid="gateway-header"] {
|
|
||||||
display: none !important;
|
|
||||||
}
|
|
||||||
/* Remove top padding that the header would occupy */
|
|
||||||
body, #root {
|
|
||||||
background: transparent !important;
|
|
||||||
}
|
|
||||||
/* Ensure the chat area fills the frame */
|
|
||||||
.chat-container, [data-testid="chat-container"] {
|
|
||||||
height: 100vh !important;
|
|
||||||
max-height: 100vh !important;
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
export function Chat() {
|
export function Chat() {
|
||||||
const gatewayStatus = useGatewayStore((state) => state.status);
|
const gatewayStatus = useGatewayStore((state) => state.status);
|
||||||
const [controlUiUrl, setControlUiUrl] = useState<string | null>(null);
|
const [controlUiUrl, setControlUiUrl] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const webviewRef = useRef<HTMLWebViewElement>(null);
|
|
||||||
|
|
||||||
const isGatewayRunning = gatewayStatus.state === 'running';
|
const isGatewayRunning = gatewayStatus.state === 'running';
|
||||||
|
|
||||||
@@ -53,57 +35,43 @@ export function Chat() {
|
|||||||
setControlUiUrl(r.url);
|
setControlUiUrl(r.url);
|
||||||
} else {
|
} else {
|
||||||
setError(r.error || 'Failed to get Control UI URL');
|
setError(r.error || 'Failed to get Control UI URL');
|
||||||
|
setLoading(false);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err: unknown) => {
|
.catch((err: unknown) => {
|
||||||
setError(String(err));
|
setError(String(err));
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
});
|
});
|
||||||
}, [isGatewayRunning]);
|
}, [isGatewayRunning]);
|
||||||
|
|
||||||
// Inject custom CSS when webview loads
|
// Handle iframe load events
|
||||||
const handleWebviewReady = useCallback(() => {
|
const handleIframeLoad = useCallback(() => {
|
||||||
const webview = webviewRef.current as unknown as HTMLElement & {
|
setLoading(false);
|
||||||
addEventListener: (event: string, cb: (e: unknown) => void) => void;
|
setError(null);
|
||||||
insertCSS: (css: string) => Promise<string>;
|
}, []);
|
||||||
reload: () => void;
|
|
||||||
};
|
const handleIframeError = useCallback(() => {
|
||||||
if (!webview) return;
|
setError('Failed to load chat interface');
|
||||||
|
setLoading(false);
|
||||||
webview.addEventListener('dom-ready', () => {
|
|
||||||
// Inject custom CSS to match ClawX theme
|
|
||||||
webview.insertCSS(CUSTOM_CSS).catch((err: unknown) => {
|
|
||||||
console.warn('Failed to inject CSS:', err);
|
|
||||||
});
|
|
||||||
setLoading(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
webview.addEventListener('did-fail-load', (event: unknown) => {
|
|
||||||
const e = event as { errorCode: number; errorDescription: string };
|
|
||||||
if (e.errorCode !== -3) { // -3 is ERR_ABORTED, ignore it
|
|
||||||
setError(`Failed to load: ${e.errorDescription}`);
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Set up webview event listeners
|
|
||||||
useEffect(() => {
|
|
||||||
if (controlUiUrl && webviewRef.current) {
|
|
||||||
handleWebviewReady();
|
|
||||||
}
|
|
||||||
}, [controlUiUrl, handleWebviewReady]);
|
|
||||||
|
|
||||||
const handleReload = useCallback(() => {
|
const handleReload = useCallback(() => {
|
||||||
const webview = webviewRef.current as unknown as HTMLElement & { reload: () => void };
|
setLoading(true);
|
||||||
if (webview) {
|
setError(null);
|
||||||
setLoading(true);
|
// Force re-mount the iframe by clearing and resetting URL
|
||||||
setError(null);
|
const url = controlUiUrl;
|
||||||
webview.reload();
|
setControlUiUrl(null);
|
||||||
}
|
setTimeout(() => setControlUiUrl(url), 100);
|
||||||
}, []);
|
}, [controlUiUrl]);
|
||||||
|
|
||||||
|
// Auto-hide loading after a timeout (fallback)
|
||||||
|
useEffect(() => {
|
||||||
|
if (!loading || !controlUiUrl) return;
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
setLoading(false);
|
||||||
|
}, 5000);
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}, [loading, controlUiUrl]);
|
||||||
|
|
||||||
// Gateway not running state
|
// Gateway not running state
|
||||||
if (!isGatewayRunning) {
|
if (!isGatewayRunning) {
|
||||||
@@ -132,7 +100,7 @@ export function Chat() {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Error state */}
|
{/* Error state */}
|
||||||
{error && (
|
{error && !loading && (
|
||||||
<div className="absolute inset-0 z-10 flex flex-col items-center justify-center bg-background p-8 text-center">
|
<div className="absolute inset-0 z-10 flex flex-col items-center justify-center bg-background p-8 text-center">
|
||||||
<AlertCircle className="h-12 w-12 text-red-500 mb-4" />
|
<AlertCircle className="h-12 w-12 text-red-500 mb-4" />
|
||||||
<h2 className="text-xl font-semibold mb-2">Connection Error</h2>
|
<h2 className="text-xl font-semibold mb-2">Connection Error</h2>
|
||||||
@@ -144,19 +112,19 @@ export function Chat() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Embedded Control UI */}
|
{/* Embedded Control UI via iframe */}
|
||||||
{controlUiUrl && (
|
{controlUiUrl && (
|
||||||
<webview
|
<iframe
|
||||||
ref={webviewRef as unknown as React.RefObject<HTMLElement>}
|
|
||||||
src={controlUiUrl}
|
src={controlUiUrl}
|
||||||
className="flex-1 w-full h-full border-0"
|
onLoad={handleIframeLoad}
|
||||||
// @ts-expect-error webview attributes not in React types
|
onError={handleIframeError}
|
||||||
allowpopups="true"
|
className="flex-1 w-full border-0"
|
||||||
style={{
|
style={{
|
||||||
display: error ? 'none' : 'flex',
|
display: error && !loading ? 'none' : 'block',
|
||||||
flex: 1,
|
height: '100%',
|
||||||
minHeight: 0,
|
|
||||||
}}
|
}}
|
||||||
|
title="ClawX Chat"
|
||||||
|
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-modals"
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user