Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
/**
* Shared bootstrap for every webview entry: theme sync, host handshake,
* loading state, error boundary, and re-init when the host pushes new params
* into an already-open panel.
*/
import { Component, StrictMode, useEffect, useState, type ReactNode } from "react";
import { createRoot } from "react-dom/client";
import type {
InitMessage,
PanelViewId,
RepoInit,
ViewParams,
WebviewViewId,
} from "../../../src/shared/webviewMessages";
import { createHost, type WebviewHost } from "./rpc";
import { ApiError, OrbLoader, ORB_STATE } from "@repowise-dev/ui/shared";
import { PanelChrome } from "./chrome";
import { initTheme, setThemePreference } from "./theme";
/** Everything a view component receives. */
export interface ViewProps<V extends WebviewViewId> {
host: WebviewHost;
repo: RepoInit;
params: ViewParams[V];
/** Increments when the index moves; refetch effects key on it. */
refreshToken: number;
}
export function mountView<V extends WebviewViewId>(
view: V,
App: React.ComponentType<ViewProps<V>>,
): void {
document.documentElement.dataset.view = view;
initTheme();
const host = createHost();
// Registered before render so the persisted preference is applied by the
// time the view component first mounts (init subscribers fire in order).
host.onInit((msg) => setThemePreference(msg.theme ?? "auto"));
host.onThemeChanged((theme) => setThemePreference(theme));
const container = document.getElementById("root");
if (!container) throw new Error("Webview root element missing");
createRoot(container).render(
<StrictMode>
<Bootstrap view={view} host={host} App={App} />
</StrictMode>,
);
host.ready();
}
interface BootstrapProps<V extends WebviewViewId> {
view: V;
host: WebviewHost;
App: React.ComponentType<ViewProps<V>>;
}
function Bootstrap<V extends WebviewViewId>({ view, host, App }: BootstrapProps<V>): ReactNode {
const [init, setInit] = useState<InitMessage | null>(null);
const [initSeq, setInitSeq] = useState(0);
const [repo, setRepo] = useState<RepoInit | null>(null);
const [refreshToken, setRefreshToken] = useState(0);
useEffect(() => {
const offInit = host.onInit((msg) => {
setInit(msg);
setRepo(msg.repo);
// A repeat init (same panel, new params) resets the view subtree.
setInitSeq((n) => n + 1);
});
const offRefresh = host.onRefresh((msg) => {
setRepo(msg.repo);
setRefreshToken((n) => n + 1);
});
return () => {
offInit();
offRefresh();
};
}, [host]);
if (!init || !repo) {
return (
<div className="flex h-screen items-center justify-center">
<OrbLoader state={ORB_STATE.page} size={64} label="Loading Repowise" />
</div>
);
}
const content = (
<ErrorBoundary key={initSeq}>
<App
host={host}
repo={repo}
params={init.params as ViewParams[V]}
refreshToken={refreshToken}
/>
</ErrorBoundary>
);
// The sidebar Home view is the launcher itself; it gets no panel chrome.
// Every editor-tab panel is wrapped so it has a way back to Home and a
// sibling switcher, with its own content scrolling under the sticky header.
if (view === "home") return content;
return (
<div className="flex h-screen flex-col">
<PanelChrome view={view as PanelViewId} host={host} />
<div className="min-h-0 flex-1 overflow-auto">{content}</div>
</div>
);
}
interface ErrorBoundaryState {
error: Error | null;
}
class ErrorBoundary extends Component<{ children: ReactNode }, ErrorBoundaryState> {
state: ErrorBoundaryState = { error: null };
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
return { error };
}
render(): ReactNode {
if (this.state.error) {
return (
<ApiError
className="m-6"
title="This view hit an error."
message={this.state.error.message}
onRetry={() => this.setState({ error: null })}
/>
);
}
return this.props.children;
}
}