/**
* Design §9.5's record, rendered so a screenshot is a diagnosis.
*
* On a device there is no console to open, so this panel is the whole of the
* evidence. It leads with the failure when there is one — **which stage boot
* reached and which phase threw** — because that pair is what tells a reader
* which of design §9.4's five layers let the failure through, and everything
* else is context behind it.
*
* Dev-only by default, per §9.5. `show` overrides that, which is what the
* device runs at 2.7/2.8 use to read the panel out of a release build.
*/
import { SizableText, YStack } from "tamagui";
import type { ReactNode } from "react";
import { bootStages } from "../dom/boot";
import type { GraphDiagnostics as GraphDiagnosticsRecord } from "./types";
export interface GraphDiagnosticsProps {
readonly diagnostics: GraphDiagnosticsRecord;
/** Defaults to dev-only. Pass `true` to read the panel in a release build. */
readonly show?: boolean;
}
/** `script → module → …`, with the stage reached marked. */
function stageTrail(stage: GraphDiagnosticsRecord["stage"]): string {
if (!stage) return `${bootStages.join(" → ")} (none reached)`;
const index = bootStages.indexOf(stage);
return bootStages.map((name, i) => (i === index ? `[${name}]` : name)).join(" → ");
}
function Row({ label, value }: { label: string; value: string }) {
return (
{label}: {value}
);
}
/**
* The failure banner.
*
* Named separately because it is the part that has to survive a screenshot at
* phone resolution: a reader must get the stage and the phase without zooming.
*/
export function GraphBootFailure({
diagnostics,
}: {
diagnostics: GraphDiagnosticsRecord;
}): ReactNode {
const { lastError, stage } = diagnostics;
if (!lastError) return null;
return (
boot failed at stage {stage ?? "none"} · phase {lastError.phase}
{lastError.message}
{stageTrail(stage)}
);
}
export function GraphDiagnostics({ diagnostics, show }: GraphDiagnosticsProps): ReactNode {
const visible = show ?? process.env.NODE_ENV !== "production";
if (!visible) return null;
const {
stage,
bootMs,
renderer,
xyflowVersion,
layoutRunner,
faces,
faceDelivery,
nodesMounted,
nodesInDocument,
culling,
renderMode,
scheme,
} = diagnostics;
// The gap between the two counts is the thing a reader has to be told, not
// left to work out: with culling on, a 12-node document paints ten bodies at
// the default viewport, and a panel that printed one number for both made
// "renders 12 nodes" impossible to read off a screenshot.
const culled = nodesInDocument - nodesMounted;
return (
0
? `${nodesMounted} — ${culled} outside the viewport, culling is on`
: String(nodesMounted)
}
/>
{faceDelivery.length === 0 ? (
) : (
faceDelivery.map((face) => (
))
)}
{faces.length === 0 ? (
) : (
faces.map((probe) => (
face: {!probe.conclusive ? "INCONCLUSIVE" : probe.matched ? "MATCHED" : "MISS"} ·{" "}
{probe.widthStack.toFixed(2)}px vs {probe.widthFirstCorrupted.toFixed(2)}px ·{" "}
{probe.stack}
))
)}
);
}