Skip to content

Commit 64d2c59

Browse files
committed
Slightly improve persistent option validation
1 parent 64ded6d commit 64d2c59

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

web/src/lib/global-options.svelte.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,20 @@ export class GlobalOptions {
9898
}
9999

100100
private deserialize(serialized: string) {
101-
const jsonObject = JSON.parse(serialized);
102-
if (jsonObject.syntaxHighlighting !== undefined) {
101+
try {
102+
const jsonObject = JSON.parse(serialized);
103+
this.loadFrom(jsonObject);
104+
} catch {
105+
// Ignore invalid options
106+
}
107+
}
108+
109+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
110+
private loadFrom(jsonObject: any) {
111+
if (jsonObject === undefined || jsonObject === null) {
112+
return;
113+
}
114+
if (typeof jsonObject.syntaxHighlighting === "boolean") {
103115
this.syntaxHighlighting = jsonObject.syntaxHighlighting;
104116
}
105117
if (jsonObject.syntaxHighlightingThemeLight !== undefined) {
@@ -112,13 +124,13 @@ export class GlobalOptions {
112124
} else {
113125
this.syntaxHighlightingThemeDark = DEFAULT_THEME_DARK;
114126
}
115-
if (jsonObject.omitPatchHeaderOnlyHunks !== undefined) {
127+
if (typeof jsonObject.omitPatchHeaderOnlyHunks === "boolean") {
116128
this.omitPatchHeaderOnlyHunks = jsonObject.omitPatchHeaderOnlyHunks;
117129
}
118-
if (jsonObject.wordDiff !== undefined) {
130+
if (typeof jsonObject.wordDiff === "boolean") {
119131
this.wordDiffs = jsonObject.wordDiff;
120132
}
121-
if (jsonObject.lineWrap !== undefined) {
133+
if (typeof jsonObject.lineWrap === "boolean") {
122134
this.lineWrap = jsonObject.lineWrap;
123135
}
124136
if (jsonObject.sidebarLocation !== undefined) {

web/src/lib/layout.svelte.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class LayoutState {
3131
return undefined;
3232
});
3333

34-
constructor(state: PersistentLayoutState | null) {
35-
this.lastSidebarWidth = state?.sidebarWidth;
34+
constructor(persistentState: PersistentLayoutState | null) {
35+
this.loadFrom(persistentState);
3636

3737
// Maintain sidebar size when resizing window
3838
watch.pre(
@@ -47,6 +47,17 @@ export class LayoutState {
4747
);
4848
}
4949

50+
private loadFrom(persistentState: PersistentLayoutState | null) {
51+
if (persistentState === null) {
52+
return;
53+
}
54+
55+
const sidebarWidth = persistentState.sidebarWidth;
56+
if (Number.isFinite(sidebarWidth) && sidebarWidth >= 0 && sidebarWidth <= 100) {
57+
this.lastSidebarWidth = sidebarWidth;
58+
}
59+
}
60+
5061
toggleSidebar() {
5162
this.sidebarCollapsed = !this.sidebarCollapsed;
5263
}
@@ -85,7 +96,7 @@ export class LayoutState {
8596
a way to preset a size in px (it generally works in proportions only)
8697
8798
this means there may be a shift on hydration when a new window uses an old cookie
88-
99+
89100
see GH:svecosystem/paneforge/issues/91
90101
*/
91102
this.lastSidebarWidth = size;

web/src/routes/+page.server.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import type { PageServerLoad } from "./$types";
33

44
export const load: PageServerLoad = async ({ cookies }) => {
55
const cookie = cookies.get(ROOT_LAYOUT_KEY);
6-
const parsed: PersistentLayoutState | null = cookie ? JSON.parse(cookie) : null;
6+
let parsed: PersistentLayoutState | null = null;
7+
if (cookie) {
8+
try {
9+
parsed = JSON.parse(cookie);
10+
} catch {
11+
// Ignore invalid cookie
12+
}
13+
}
714
return {
815
rootLayout: parsed,
916
};

0 commit comments

Comments
 (0)