Skip to content
TypeScript

perf(web): the app sidebar layout compiles with the React Compiler (#20569)

Julius Marminge8e6ace616660
diff --git a/apps/web/src/components/AppSidebarLayout.tsx b/apps/web/src/components/AppSidebarLayout.tsx
index 10dd078347a..2d8c503fd9e 100644
--- a/apps/web/src/components/AppSidebarLayout.tsx
+++ b/apps/web/src/components/AppSidebarLayout.tsx
@@ -68,6 +68,44 @@ function readViewportWidth(): number {
return window.innerWidth;
}
+// getWindowFullscreenState is a synchronous IPC round trip, so the snapshot is cached
+// and only re-read when a subscription starts.
+let windowFullscreenSnapshot: boolean | null = null;
+
+function macosDesktopBridge() {
+ if (!isElectron || !isMacPlatform(navigator.platform)) return null;
+ return window.desktopBridge ?? null;
+}
+
+function subscribeToWindowFullscreen(onChange: () => void): () => void {
+ const bridge = macosDesktopBridge();
+ if (
+ typeof bridge?.getWindowFullscreenState !== "function" ||
+ typeof bridge.onWindowFullscreenStateChange !== "function"
+ ) {
+ return () => {};
+ }
+ const unsubscribe = bridge.onWindowFullscreenStateChange((fullscreen) => {
+ windowFullscreenSnapshot = fullscreen;
+ onChange();
+ });
+ // The window may have changed state between the first render and this subscription.
+ windowFullscreenSnapshot = bridge.getWindowFullscreenState();
+ return () => {
+ unsubscribe();
+ windowFullscreenSnapshot = null;
+ };
+}
+
+function readWindowFullscreen(): boolean {
+ if (windowFullscreenSnapshot === null) {
+ const getWindowFullscreenState = macosDesktopBridge()?.getWindowFullscreenState;
+ windowFullscreenSnapshot =
+ typeof getWindowFullscreenState === "function" ? getWindowFullscreenState() : false;
+ }
+ return windowFullscreenSnapshot;
+}
+
function readInitialThreadSidebarWidth(): number {
try {
return resolveInitialThreadSidebarWidth(
@@ -245,12 +283,10 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
}
setSidebarWidth(resolveInitialThreadSidebarWidth(null, viewportWidth));
};
- const [isWindowFullscreen, setIsWindowFullscreen] = useState(() => {
- const getWindowFullscreenState = window.desktopBridge?.getWindowFullscreenState;
- return isMacosDesktop && typeof getWindowFullscreenState === "function"
- ? getWindowFullscreenState()
- : false;
- });
+ const isWindowFullscreen = useSyncExternalStore(
+ subscribeToWindowFullscreen,
+ readWindowFullscreen,
+ );
const windowControlsInsetStyle = (
isMacosDesktop && !isWindowFullscreen
? { "--workspace-controls-left": MACOS_TRAFFIC_LIGHTS_LEFT_INSET }
@@ -262,23 +298,6 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
...windowControlsInsetStyle,
} as CSSProperties;
- useEffect(() => {
- if (!isMacosDesktop) return;
- const bridge = window.desktopBridge;
- if (!bridge) return;
- const { getWindowFullscreenState, onWindowFullscreenStateChange } = bridge;
- if (
- typeof getWindowFullscreenState !== "function" ||
- typeof onWindowFullscreenStateChange !== "function"
- ) {
- return;
- }
-
- const unsubscribe = onWindowFullscreenStateChange(setIsWindowFullscreen);
- setIsWindowFullscreen(getWindowFullscreenState());
- return unsubscribe;
- }, [isMacosDesktop]);
-
useEffect(() => {
const onMenuAction = window.desktopBridge?.onMenuAction;
if (typeof onMenuAction !== "function") {