fix(preview): resolve ReferenceError by hoisting isForceStarting state declaration

This commit is contained in:
2026-06-12 12:28:22 -07:00
parent 2a2962a098
commit 69e8086018

View File

@@ -155,6 +155,8 @@ export default function PreviewTab() {
const refreshKey = usePreviewToolbarStore((s) => s.refreshKey);
const currentPath = usePreviewToolbarStore((s) => s.currentPath);
const [isForceStarting, setIsForceStarting] = useState(false);
// When the user clicks the manual refresh button in the toolbar, we don't
// just want to reload the iframe — we also want to trigger the same ghost/zombie
// check as the initial mount, in case the server died while they were looking at it.
@@ -163,9 +165,13 @@ export default function PreviewTab() {
if (refreshKey === prevRefreshKeyRef.current) return;
prevRefreshKeyRef.current = refreshKey;
// Reset the ensure called flag so the ensure effect (below) fires again.
// We only reset the ensure flag if we aren't currently waiting for a forced start.
// If they hit refresh while it's already booting, don't break the state machine.
if (!isForceStarting) {
ensureCalledRef.current = false;
}, [refreshKey]);
setEnsureStatus("idle");
}
}, [refreshKey, isForceStarting]);
useLayoutEffect(() => {
if (!primaryRunning?.url) {
@@ -184,29 +190,26 @@ export default function PreviewTab() {
bridge.registerPreviewIframe(iframeDomRef.current, iframeSrc);
}, [bridge, iframeSrc]);
const [isForceStarting, setIsForceStarting] = useState(false);
// Determine which empty state to show.
const emptyContent = (() => {
if (loading && !anatomy) return <InitialLoader />;
if (inFlightApp) return <BuildingState app={inFlightApp} />;
if (failedApp) return <FailedState app={failedApp} />;
// Dev server is in the process of booting (either picked up from anatomy
// or we just fired the ensure endpoint and are waiting for the DB row).
if (primaryStarting) {
// If isForceStarting is true, we know we clicked the manual start button
// and are waiting for the DB to reflect 'starting'.
if (primaryStarting || ensureStatus === "starting" || isForceStarting) {
return (
<WarmingUpState
startedAt={primaryStarting.startedAt}
startedAt={primaryStarting?.startedAt}
fallbackUrl={fallbackUrl}
port={selectedPort}
/>
);
}
if (
ensureStatus === "calling" ||
ensureStatus === "starting" ||
isForceStarting
) {
if (ensureStatus === "calling") {
return (
<WarmingUpState
startedAt={undefined}
@@ -225,7 +228,11 @@ export default function PreviewTab() {
{
method: "POST",
},
).catch(() => setIsForceStarting(false));
)
.then((res) => {
if (!res.ok) throw new Error("Failed to start");
})
.catch(() => setIsForceStarting(false));
}}
/>
);