Files
fusion/packages/dashboard/app/context/ShellContext.tsx
Fusion 2b7b92229c feat(FN-3400): add native shell connection handoff, plugin management CLI/l
This merge adds a complete plugin management system (FN-3565) with CLI commands, a loader, runner, and dashboard routes, along with project-scoped auth storage (FN-3544), native shell connection support for mobile (FN-3400) spanning onboarding, connection manager, and remote desktop handoff, and ref

Fusion-Task-Id: FN-3400
2026-05-06 05:29:56 -07:00

65 lines
1.9 KiB
TypeScript

import { createContext, useContext, useEffect, useMemo, useState, type PropsWithChildren } from "react";
import type { FusionShellApi, ShellConnectionState } from "../types/native-shell";
export interface ShellContextValue {
shellApi: FusionShellApi | null;
state: ShellConnectionState;
ready: boolean;
openConnectionManagerSignal: number;
}
const DEFAULT_STATE: ShellConnectionState = {
host: "web",
activeProfileId: null,
profiles: [],
};
const ShellContext = createContext<ShellContextValue>({
shellApi: null,
state: DEFAULT_STATE,
ready: true,
openConnectionManagerSignal: 0,
});
export function ShellProvider({ children }: PropsWithChildren) {
const shellApi = useMemo(() => (typeof window !== "undefined" ? window.fusionShell ?? null : null), []);
const [state, setState] = useState<ShellConnectionState>(DEFAULT_STATE);
const [ready, setReady] = useState(!shellApi);
const [openConnectionManagerSignal, setOpenConnectionManagerSignal] = useState(0);
useEffect(() => {
if (!shellApi) {
return;
}
let cancelled = false;
void shellApi.getState().then((value) => {
if (!cancelled) {
setState(value);
setReady(true);
}
});
const unsubscribe = shellApi.subscribe((nextState) => {
setState(nextState);
});
const handleOpenConnectionManager = () => {
setOpenConnectionManagerSignal((value) => value + 1);
};
window.addEventListener("shell:open-connection-manager", handleOpenConnectionManager);
return () => {
cancelled = true;
unsubscribe();
window.removeEventListener("shell:open-connection-manager", handleOpenConnectionManager);
};
}, [shellApi]);
return <ShellContext.Provider value={{ shellApi, state, ready, openConnectionManagerSignal }}>{children}</ShellContext.Provider>;
}
export function useShellContext(): ShellContextValue {
return useContext(ShellContext);
}