Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions apps/web/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,83 @@ import { isElectron } from "./env";
import { getRouter } from "./router";
import { APP_DISPLAY_NAME } from "./branding";

const APP_ZOOM_STORAGE_KEY = "t3code:app-zoom";
const APP_ZOOM_INSTALL_FLAG = "__t3codeAppZoomInstalled";
const DEFAULT_ZOOM_LEVEL = 1;
const MIN_ZOOM_LEVEL = 0.7;
const MAX_ZOOM_LEVEL = 1.8;
const ZOOM_STEP = 0.1;

const roundZoomLevel = (value: number) => Math.round(value * 10) / 10;

const clampZoomLevel = (value: number) =>
roundZoomLevel(Math.min(MAX_ZOOM_LEVEL, Math.max(MIN_ZOOM_LEVEL, value)));

const readStoredZoomLevel = () => {
const storedValue = window.localStorage.getItem(APP_ZOOM_STORAGE_KEY);
const parsedValue = Number.parseFloat(storedValue ?? "");

if (!Number.isFinite(parsedValue)) {
return DEFAULT_ZOOM_LEVEL;
}

return clampZoomLevel(parsedValue);
};
Comment on lines +25 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟑 Medium src/main.tsx:25

readStoredZoomLevel directly accesses window.localStorage.getItem() without exception handling. When the user has disabled cookies or the page runs in a strict iframe sandbox, this throws a SecurityError that propagates uncaught to the top level, halting main.tsx before ReactDOM.createRoot and leaving users with a blank white screen. Consider wrapping the localStorage access in a try-catch that returns DEFAULT_ZOOM_LEVEL on any exception.

+const readStoredZoomLevel = () => {
+  try {
+    const storedValue = window.localStorage.getItem(APP_ZOOM_STORAGE_KEY);
+    const parsedValue = Number.parseFloat(storedValue ?? "");
+
+    if (!Number.isFinite(parsedValue)) {
+      return DEFAULT_ZOOM_LEVEL;
+    }
+
+    return clampZoomLevel(parsedValue);
+  } catch {
+    return DEFAULT_ZOOM_LEVEL;
+  }
+};
πŸš€ Reply "fix it for me" or copy this AI Prompt for your agent:
In file apps/web/src/main.tsx around lines 25-34:

`readStoredZoomLevel` directly accesses `window.localStorage.getItem()` without exception handling. When the user has disabled cookies or the page runs in a strict iframe sandbox, this throws a `SecurityError` that propagates uncaught to the top level, halting `main.tsx` before `ReactDOM.createRoot` and leaving users with a blank white screen. Consider wrapping the `localStorage` access in a try-catch that returns `DEFAULT_ZOOM_LEVEL` on any exception.

Evidence trail:
apps/web/src/main.tsx lines 25-33: `readStoredZoomLevel` function with direct `window.localStorage.getItem()` call and no try-catch. Line 59: `applyZoomLevel(readStoredZoomLevel())` inside `installAppZoomControls`. Lines 87-90: `installAppZoomControls()` called at line 88, `ReactDOM.createRoot()` at line 90. The exception path is: localStorage.getItem (line 26) β†’ readStoredZoomLevel β†’ installAppZoomControls (line 88) β†’ uncaught β†’ script halts before line 90.


const applyZoomLevel = (value: number) => {
const normalizedValue = clampZoomLevel(value);
document.documentElement.style.zoom = String(normalizedValue);
window.localStorage.setItem(APP_ZOOM_STORAGE_KEY, String(normalizedValue));
};

const shouldHandleZoomShortcut = (event: KeyboardEvent) => {
const isModPressed = event.ctrlKey || event.metaKey;

if (!isModPressed || event.altKey) {
return false;
}

return ["+", "=", "-", "_", "0", "NumpadAdd", "NumpadSubtract", "Numpad0"].includes(event.key);
};

const installAppZoomControls = () => {
if ((window as Window & { [APP_ZOOM_INSTALL_FLAG]?: boolean })[APP_ZOOM_INSTALL_FLAG]) {
return;
}

(window as Window & { [APP_ZOOM_INSTALL_FLAG]?: boolean })[APP_ZOOM_INSTALL_FLAG] = true;

applyZoomLevel(readStoredZoomLevel());

window.addEventListener("keydown", (event) => {
if (!shouldHandleZoomShortcut(event)) {
return;
}

event.preventDefault();

const currentZoomLevel = readStoredZoomLevel();

if (event.key === "0" || event.key === "Numpad0") {
applyZoomLevel(DEFAULT_ZOOM_LEVEL);
return;
}

if (event.key === "+" || event.key === "=" || event.key === "NumpadAdd") {
applyZoomLevel(currentZoomLevel + ZOOM_STEP);
return;
}

applyZoomLevel(currentZoomLevel - ZOOM_STEP);
});
};

const history = isElectron ? createHashHistory() : createBrowserHistory();

const router = getRouter(history);

document.title = APP_DISPLAY_NAME;
installAppZoomControls();

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
Expand Down