Files
hyperframes/packages/studio/src/main.tsx
T
Miguel Ángel 9f4d9e64b2 feat(studio): a light and dark theme toggle in the header that remembers the choice (#4839)
* feat(studio): a light and dark theme toggle in the header that remembers the choice

The sun and moon button flips Studio between Graphite light and dark with a
circular reveal, saves the choice, and follows the system until one is made.

* fix(studio): studio opens light the first time, then keeps the saved theme

* fix(studio): drop the theme field's comment, the theme owner states the rule

* fix(studio): the theme toggle shows only in studio's own app and follows the theme on screen

A host that embeds StudioApp gets no toggle; the button reads the document's theme, the reveal drops the default crossfade, and reduced motion skips it.

* fix(studio): the theme reveal leaves other view transitions alone; one default for the toggle

The crossfade rule applies only while the toggle's own transition runs, StudioApp passes the header's single default through, and the reveal's reduced-motion and keyboard paths are tested.

* fix(studio): a second theme click mid-reveal keeps the crossfade off

Only the latest reveal clears the document marker; tests pin the marker while a reveal runs and the circle's radius.

* refactor(studio): the theme toggle shares its icon props and reads as plain functions

Simplify pass: one icon props object for the sun and moon, no useCallback around a handler nothing memoises, cannotReveal named for what it answers, the test reuses the theme reader.

* fix(studio): studio's own app turns the theme toggle on through context; tests parse index.html

App.tsx stays at main's size: main.tsx provides ShowThemeToggle and the header reads it, default off for hosts. The boot-script tests read index.html through DOMParser instead of an HTML regex.
2026-10-01 08:44:57 -07:00

139 lines
4.7 KiB
TypeScript

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { IconContext } from "@phosphor-icons/react";
import { StudioApp } from "./App";
import { ShowThemeToggle } from "./components/ThemeToggle";
import { StudioErrorBoundary } from "./components/StudioErrorBoundary";
import { readIconTokens } from "./styles/iconTokens";
import { trackStudioEvent } from "./utils/studioTelemetry";
import { prefetchPreviewForHash } from "./utils/previewPrefetch";
import "./styles/studio.css";
prefetchPreviewForHash(window.location.hash);
window.addEventListener("hashchange", () => prefetchPreviewForHash(window.location.hash));
trackStudioEvent("session_start");
function errorProps(value: unknown): {
error_message: string;
error_name: string | null;
stack_trace: string | null;
} {
if (value instanceof Error) {
return {
error_message: value.message,
error_name: value.name,
stack_trace: value.stack?.slice(0, 4000) ?? null,
};
}
return { error_message: String(value), error_name: null, stack_trace: null };
}
// fallow-ignore-next-line complexity
function isCompositionAssetError(msg: string, name: string | null): boolean {
if (msg.includes("Error fetching") && (msg.includes("404") || msg.includes("Not Found")))
return true;
if (name === "EncodingError" || msg.includes("unsupported or unrecognizable format")) return true;
if (msg.includes("MEDIA_ERR_SRC_NOT_SUPPORTED")) return true;
return false;
}
const ERROR_CAP = 50;
let errorCount = 0;
let rejectionCount = 0;
let errorCapSent = false;
let rejectionCapSent = false;
window.addEventListener("error", (event) => {
if (event.message?.includes("ResizeObserver loop")) {
event.stopImmediatePropagation();
event.preventDefault();
return;
}
errorCount++;
if (errorCount > ERROR_CAP) {
if (!errorCapSent) {
errorCapSent = true;
trackStudioEvent("error_cap_reached", { count: errorCount });
}
return;
}
trackStudioEvent("unhandled_error", {
...errorProps(event.error),
error_message: event.message,
filename: event.filename,
lineno: event.lineno,
colno: event.colno,
});
});
let filteredAssetErrorCount = 0;
let filteredAbortCount = 0;
// Bounded so a hostile extension cannot grow it; a novel abort message still surfaces once.
const seenAbortMessages = new Set<string>();
const MAX_SEEN_ABORT_MESSAGES = 20;
// fallow-ignore-next-line complexity
window.addEventListener("unhandledrejection", (event) => {
const props = errorProps(event.reason);
if (isCompositionAssetError(props.error_message, props.error_name)) {
filteredAssetErrorCount++;
if (filteredAssetErrorCount === 1 || filteredAssetErrorCount % 100 === 0) {
trackStudioEvent("composition_asset_error_filtered", {
error_message: props.error_message.slice(0, 200),
error_name: props.error_name,
total_filtered: filteredAssetErrorCount,
});
}
return;
}
// An AbortError rejection is a cancellation we asked for, not a failure. Our own
// fetch chains handle it; the unhandled copies come from browser extensions that
// wrap window.fetch and derive a promise from each request without a rejection
// handler, so they reject with our abort reason whenever a thumbnail lease is
// released mid-fetch. Filtered before the cap so a scroll burst cannot exhaust
// ERROR_CAP and silence the session's real rejections. Sampled per distinct
// message, so an abort we have not seen before (e.g. our own "Aborted") is
// recorded once instead of drowning in the extension flood.
if (props.error_name === "AbortError") {
filteredAbortCount++;
const message = props.error_message.slice(0, 200);
const novel =
!seenAbortMessages.has(message) && seenAbortMessages.size < MAX_SEEN_ABORT_MESSAGES;
if (novel) seenAbortMessages.add(message);
if (novel || filteredAbortCount % 100 === 0) {
trackStudioEvent("abort_rejection_filtered", {
error_message: message,
total_filtered: filteredAbortCount,
});
}
return;
}
rejectionCount++;
if (rejectionCount > ERROR_CAP) {
if (!rejectionCapSent) {
rejectionCapSent = true;
trackStudioEvent("rejection_cap_reached", { count: rejectionCount });
}
return;
}
trackStudioEvent("unhandled_promise_rejection", props);
});
createRoot(document.getElementById("root")!).render(
<StrictMode>
{/* One icon size and weight for the whole app, taken from the theme file.
Icons that pass their own size or weight still win. */}
<IconContext.Provider value={readIconTokens()}>
<StudioErrorBoundary>
<ShowThemeToggle.Provider value>
<StudioApp />
</ShowThemeToggle.Provider>
</StudioErrorBoundary>
</IconContext.Provider>
</StrictMode>,
);