Skip to content

Commit df323f8

Browse files
Sync diff renderer theme with app theme changes
- Pass explicit Pierre light/dark theme into diff render options - Re-key file diffs on resolved theme so views rerender on theme switch - Stabilize `useTheme` snapshots to propagate system theme updates reliably
1 parent 041acf1 commit df323f8

3 files changed

Lines changed: 61 additions & 8 deletions

File tree

‎apps/web/src/components/DiffPanel.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import { useStore } from "../store";
1616
import { ToggleGroup, Toggle } from "./ui/toggle-group";
1717

1818
type DiffRenderMode = "stacked" | "split";
19-
type DiffThemeType = "light" | "dark" | "system";
19+
type DiffThemeType = "light" | "dark";
20+
21+
function resolveDiffThemeName(theme: "light" | "dark") {
22+
return theme === "dark" ? "pierre-dark" : "pierre-light";
23+
}
2024

2125
type RenderablePatch =
2226
| {
@@ -472,13 +476,15 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
472476
{renderableFiles.map((fileDiff) => {
473477
const filePath = resolveFileDiffPath(fileDiff);
474478
const fileKey = buildFileDiffRenderKey(fileDiff);
479+
const themedFileKey = `${fileKey}:${resolvedTheme}`;
475480
return (
476-
<div key={fileKey} data-diff-file-path={filePath} className="rounded-md">
481+
<div key={themedFileKey} data-diff-file-path={filePath} className="rounded-md">
477482
<FileDiff
478483
fileDiff={fileDiff}
479484
options={{
480485
diffStyle: diffRenderMode === "split" ? "split" : "unified",
481486
lineDiffType: "none",
487+
theme: resolveDiffThemeName(resolvedTheme),
482488
themeType: resolvedTheme as DiffThemeType,
483489
}}
484490
/>

‎apps/web/src/components/DiffWorkerPoolProvider.tsx‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
1-
import { WorkerPoolContextProvider } from "@pierre/diffs/react";
1+
import { WorkerPoolContextProvider, useWorkerPool } from "@pierre/diffs/react";
22
import DiffsWorker from "@pierre/diffs/worker/worker.js?worker";
3-
import { useMemo, type ReactNode } from "react";
3+
import { useEffect, useMemo, type ReactNode } from "react";
4+
import { useTheme } from "../hooks/useTheme";
5+
6+
function resolveDiffThemeName(theme: "light" | "dark") {
7+
return theme === "dark" ? "pierre-dark" : "pierre-light";
8+
}
9+
10+
function DiffWorkerThemeSync({ themeName }: { themeName: "pierre-light" | "pierre-dark" }) {
11+
const workerPool = useWorkerPool();
12+
13+
useEffect(() => {
14+
if (!workerPool) {
15+
return;
16+
}
17+
18+
const current = workerPool.getDiffRenderOptions();
19+
if (current.theme === themeName) {
20+
return;
21+
}
22+
23+
void workerPool
24+
.setRenderOptions({
25+
...current,
26+
theme: themeName,
27+
})
28+
.catch(() => undefined);
29+
}, [themeName, workerPool]);
30+
31+
return null;
32+
}
433

534
export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) {
35+
const { resolvedTheme } = useTheme();
36+
const diffThemeName = resolveDiffThemeName(resolvedTheme);
637
const workerPoolSize = useMemo(() => {
738
const cores =
839
typeof navigator === "undefined" ? 4 : Math.max(1, navigator.hardwareConcurrency || 4);
@@ -17,9 +48,11 @@ export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) {
1748
totalASTLRUCacheSize: 240,
1849
}}
1950
highlighterOptions={{
51+
theme: diffThemeName,
2052
tokenizeMaxLineLength: 1_000,
2153
}}
2254
>
55+
<DiffWorkerThemeSync themeName={diffThemeName} />
2356
{children}
2457
</WorkerPoolContextProvider>
2558
);

‎apps/web/src/hooks/useTheme.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { useCallback, useEffect, useSyncExternalStore } from "react";
22

33
type Theme = "light" | "dark" | "system";
4+
type ThemeSnapshot = {
5+
theme: Theme;
6+
systemDark: boolean;
7+
};
48

59
const STORAGE_KEY = "t3code:theme";
610
const MEDIA_QUERY = "(prefers-color-scheme: dark)";
711

812
let listeners: Array<() => void> = [];
13+
let lastSnapshot: ThemeSnapshot | null = null;
914
function emitChange() {
1015
for (const listener of listeners) listener();
1116
}
@@ -39,8 +44,16 @@ function applyTheme(theme: Theme, suppressTransitions = false) {
3944
// Apply immediately on module load to prevent flash
4045
applyTheme(getStored());
4146

42-
function getSnapshot(): Theme {
43-
return getStored();
47+
function getSnapshot(): ThemeSnapshot {
48+
const theme = getStored();
49+
const systemDark = theme === "system" ? getSystemDark() : false;
50+
51+
if (lastSnapshot && lastSnapshot.theme === theme && lastSnapshot.systemDark === systemDark) {
52+
return lastSnapshot;
53+
}
54+
55+
lastSnapshot = { theme, systemDark };
56+
return lastSnapshot;
4457
}
4558

4659
function subscribe(listener: () => void): () => void {
@@ -71,10 +84,11 @@ function subscribe(listener: () => void): () => void {
7184
}
7285

7386
export function useTheme() {
74-
const theme = useSyncExternalStore(subscribe, getSnapshot);
87+
const snapshot = useSyncExternalStore(subscribe, getSnapshot);
88+
const theme = snapshot.theme;
7589

7690
const resolvedTheme: "light" | "dark" =
77-
theme === "system" ? (getSystemDark() ? "dark" : "light") : theme;
91+
theme === "system" ? (snapshot.systemDark ? "dark" : "light") : theme;
7892

7993
const setTheme = useCallback((next: Theme) => {
8094
localStorage.setItem(STORAGE_KEY, next);

0 commit comments

Comments
 (0)