Files
openchamber/packages/ui/src/components/update/OpenCodeUpdateToast.tsx
T
Bohdan Triapitsyn 2031e3b4a8 Decouple bundled UI from runtime API and add remote instance tooling (#1228)
Add a packaged-client runtime boundary so the shared UI can talk to local,
desktop, remote, and VS Code runtimes through the right transport instead of
assuming one same-origin web server.

Centralize OpenChamber-owned API access behind RuntimeAPIs, runtimeFetch, and
runtime URL helpers, while keeping official OpenCode traffic on the SDK path.
Support runtime switching, remote host selection, desktop client credentials,
and headless connection links for pairing packaged clients with remote
OpenChamber servers.

Harden the new auth model by moving long-lived client tokens out of browser
URLs, introducing short-lived scoped URL tokens for browser-owned transports,
restricting URL-token access to explicit readable/realtime routes, and making
client-token management session-scoped or self-scoped as appropriate.

Update browser-owned assets and preview proxy flows to work with the split
runtime model, including authenticated project icons, preview token propagation,
CSP-safe preview bridge injection, and preview proxy auth that survives
short-lived URL-token expiry.

Tighten Electron security boundaries for packaged clients by gating privileged
preload state to trusted origins and requiring explicit confirmation before
connect deep-links import or switch remote runtimes.

Also refresh agent guidance and project skills so future runtime/API, auth,
preview, UI, CLI, settings, locale, and drag-to-reorder work follows the new
architecture.
2026-06-02 00:43:05 +03:00

167 lines
6.2 KiB
TypeScript

import * as React from 'react';
import { Icon } from '@/components/icon/Icon';
import { toast } from '@/components/ui/toast';
import { reloadOpenCodeConfiguration } from '@/stores/useAgentsStore';
import { useUIStore } from '@/stores/useUIStore';
import { useI18n } from '@/lib/i18n';
import { runtimeFetch } from '@/lib/runtime-fetch';
import { getSafeStorage } from '@/stores/utils/safeStorage';
import {
resolveOpenCodeUpdateVersion,
resolveOpenCodeUpgradeStatusVersion,
shouldShowOpenCodeUpdateToast,
type OpenCodeUpgradeStatusLike,
} from './openCodeUpdateDedup';
const UPDATE_TOAST_ID = 'opencode-update-available';
const UPGRADE_TOAST_ID = 'opencode-upgrade-progress';
const INITIAL_CHECK_DELAY_MS = 5_000;
const CHECK_RETRY_DELAYS_MS = [10_000, 60_000];
const UPDATE_TOAST_DISMISSED_VERSION_KEY = 'opencode-update-toast-dismissed-version';
export const OpenCodeUpdateToast: React.FC = () => {
const { t } = useI18n();
const showOpenCodeUpdateNotifications = useUIStore((state) => state.showOpenCodeUpdateNotifications);
const seenVersionsRef = React.useRef(new Set<string>());
const upgradingRef = React.useRef(false);
React.useEffect(() => {
if (!showOpenCodeUpdateNotifications) {
toast.dismiss(UPDATE_TOAST_ID);
}
}, [showOpenCodeUpdateNotifications]);
const reloadOpenCode = React.useCallback(() => {
toast.dismiss(UPGRADE_TOAST_ID);
void reloadOpenCodeConfiguration({
message: t('opencodeUpdate.toast.reload.message'),
mode: 'projects',
scopes: ['all'],
});
}, [t]);
const runUpgrade = React.useCallback(async () => {
if (upgradingRef.current) return;
upgradingRef.current = true;
toast.dismiss(UPDATE_TOAST_ID);
toast.message(t('opencodeUpdate.toast.upgrading.title'), {
id: UPGRADE_TOAST_ID,
description: t('opencodeUpdate.toast.upgrading.description'),
duration: Infinity,
icon: <Icon name="refresh" className="h-4 w-4 animate-spin text-muted-foreground" />,
});
try {
const response = await runtimeFetch('/api/opencode/upgrade', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({}),
});
const payload = await response.json().catch(() => null) as null | { success?: boolean; version?: string; error?: string };
if (!response.ok || payload?.success === false) {
throw new Error(payload?.error || response.statusText || t('opencodeUpdate.toast.failed.description'));
}
toast.success(t('opencodeUpdate.toast.updated.title'), {
id: UPGRADE_TOAST_ID,
description: payload?.version
? t('opencodeUpdate.toast.updated.descriptionWithVersion', { version: payload.version })
: t('opencodeUpdate.toast.updated.description'),
duration: Infinity,
icon: <Icon name="check" className="h-4 w-4 text-[var(--status-success)]" />,
action: {
label: t('opencodeUpdate.toast.actions.reload'),
onClick: reloadOpenCode,
},
});
} catch (error) {
toast.error(t('opencodeUpdate.toast.failed.title'), {
id: UPGRADE_TOAST_ID,
description: error instanceof Error ? error.message : t('opencodeUpdate.toast.failed.description'),
duration: Infinity,
});
} finally {
upgradingRef.current = false;
}
}, [reloadOpenCode, t]);
React.useEffect(() => {
const showUpdateAvailableToast = (version: string) => {
// Upstream setting wins over our dedup logic: if user disabled
// OpenCode update notifications, dismiss any active toast and bail
// before consulting dedup state.
if (!useUIStore.getState().showOpenCodeUpdateNotifications) {
toast.dismiss(UPDATE_TOAST_ID);
return;
}
const decision = shouldShowOpenCodeUpdateToast({
version,
dismissedVersion: getSafeStorage().getItem(UPDATE_TOAST_DISMISSED_VERSION_KEY),
seenVersions: seenVersionsRef.current,
});
if (!decision) {
return;
}
seenVersionsRef.current.add(version);
toast.info(t('opencodeUpdate.toast.available.title'), {
id: UPDATE_TOAST_ID,
description: t('opencodeUpdate.toast.available.description', { version }),
duration: Infinity,
action: {
label: t('opencodeUpdate.toast.actions.update'),
onClick: runUpgrade,
},
cancel: {
label: t('opencodeUpdate.toast.actions.dismiss'),
onClick: () => {
getSafeStorage().setItem(UPDATE_TOAST_DISMISSED_VERSION_KEY, version);
toast.dismiss(UPDATE_TOAST_ID);
},
},
});
};
const onUpdateAvailable = (event: Event) => {
const version = resolveOpenCodeUpdateVersion((event as CustomEvent<unknown>).detail);
showUpdateAvailableToast(version);
};
let cancelled = false;
const timeoutIds: Array<ReturnType<typeof setTimeout>> = [];
const checkForUpdate = async (attempt: number) => {
try {
const response = await runtimeFetch('/api/opencode/upgrade-status', { headers: { Accept: 'application/json' } });
if (!response.ok) throw new Error(response.statusText || 'OpenCode upgrade status check failed');
const status = await response.json().catch(() => null) as OpenCodeUpgradeStatusLike | null;
const version = resolveOpenCodeUpgradeStatusVersion(status);
if (!cancelled && version) {
showUpdateAvailableToast(version);
}
} catch {
const delay = CHECK_RETRY_DELAYS_MS[attempt];
if (!cancelled && delay !== undefined) {
timeoutIds.push(setTimeout(() => { void checkForUpdate(attempt + 1); }, delay));
}
}
};
if (showOpenCodeUpdateNotifications) {
timeoutIds.push(setTimeout(() => { void checkForUpdate(0); }, INITIAL_CHECK_DELAY_MS));
}
window.addEventListener('openchamber:opencode-update-available', onUpdateAvailable);
return () => {
cancelled = true;
for (const timeoutId of timeoutIds) clearTimeout(timeoutId);
window.removeEventListener('openchamber:opencode-update-available', onUpdateAvailable);
};
}, [runUpgrade, showOpenCodeUpdateNotifications, t]);
return null;
};