Skip to content

Commit 3238bcb

Browse files
Simon Dobaclaude
andcommitted
refactor(web): read the markdown preference through useLocalStorage
The hook exists and does this better: it reads, writes, validates and keeps instances in sync, so the hand-rolled initializer and the try/catch around the write both go away. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent a533f3a commit 3238bcb

1 file changed

Lines changed: 6 additions & 16 deletions

File tree

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import ChatMarkdown from "~/components/ChatMarkdown";
2222
import { OpenInPicker } from "~/components/chat/OpenInPicker";
2323
import { useClientSettings } from "~/hooks/useSettings";
2424
import { useTheme } from "~/hooks/useTheme";
25-
import { getLocalStorageItem, setLocalStorageItem } from "~/hooks/useLocalStorage";
25+
import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "~/hooks/useLocalStorage";
2626
import { resolveDiffThemeName } from "~/lib/diffRendering";
2727
import { cn } from "~/lib/utils";
2828
import { isPreviewSupportedInRuntime } from "~/previewStateStore";
@@ -646,15 +646,6 @@ function initialExplorerOpen(): boolean {
646646
}
647647
}
648648

649-
function initialRenderMarkdown(): boolean {
650-
try {
651-
return getLocalStorageItem(RENDER_MARKDOWN_STORAGE_KEY, Schema.Boolean) ?? false;
652-
} catch (error) {
653-
console.error(error);
654-
return false;
655-
}
656-
}
657-
658649
export default function FilePreviewPanel({
659650
environmentId,
660651
cwd,
@@ -684,7 +675,11 @@ export default function FilePreviewPanel({
684675
const [explorerOpen, setExplorerOpen] = useState(initialExplorerOpen);
685676
// Reading markdown rendered is a preference, not a property of one file. Keeping
686677
// it on the panel meant a thread switch dropped it and forced source back.
687-
const [renderMarkdownPreferred, setRenderMarkdownPreferred] = useState(initialRenderMarkdown);
678+
const [renderMarkdownPreferred, setRenderMarkdownPreferred] = useLocalStorage(
679+
RENDER_MARKDOWN_STORAGE_KEY,
680+
false,
681+
Schema.Boolean,
682+
);
688683
// Paired with the path on purpose: each file surface counts its reveals from
689684
// one, so a bare id would let a dismissed reveal on one file swallow the first
690685
// reveal on the next.
@@ -811,11 +806,6 @@ export default function FilePreviewPanel({
811806
? { path: relativePath, requestId: revealRequestId }
812807
: null,
813808
);
814-
try {
815-
setLocalStorageItem(RENDER_MARKDOWN_STORAGE_KEY, pressed, Schema.Boolean);
816-
} catch (error) {
817-
console.error(error);
818-
}
819809
}}
820810
aria-label={renderMarkdown ? "Show markdown source" : "Show rendered markdown"}
821811
variant="ghost"

0 commit comments

Comments
 (0)