Skip to content

Commit c8a04fa

Browse files
committed
impr: notify the user if custom text saving failed due to local storage being full
1 parent 9bbcf40 commit c8a04fa

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

frontend/src/ts/modals/save-custom-text.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ function save(): boolean {
4848
return false;
4949
}
5050

51-
CustomText.setCustomText(name, state.textToSave, checkbox);
52-
CustomTextState.setCustomTextName(name, checkbox);
53-
Notifications.add("Custom text saved", 1);
54-
return true;
51+
const saved = CustomText.setCustomText(name, state.textToSave, checkbox);
52+
if (saved) {
53+
CustomTextState.setCustomTextName(name, checkbox);
54+
Notifications.add("Custom text saved", 1);
55+
return true;
56+
} else {
57+
Notifications.add("Error saving custom text", -1);
58+
return false;
59+
}
5560
}
5661

5762
function updateIndicatorAndButton(): void {

frontend/src/ts/test/custom-text.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export function setCustomText(
168168
name: string,
169169
text: string | string[],
170170
long = false
171-
): void {
171+
): boolean {
172172
if (long) {
173173
const customText = getLocalStorageLong();
174174

@@ -188,7 +188,7 @@ export function setCustomText(
188188
textByName.text = text.join(" ");
189189
}
190190

191-
setLocalStorageLong(customText);
191+
return setLocalStorageLong(customText);
192192
} else {
193193
const customText = getLocalStorage();
194194

@@ -198,7 +198,7 @@ export function setCustomText(
198198
customText[name] = text.join(" ");
199199
}
200200

201-
setLocalStorage(customText);
201+
return setLocalStorage(customText);
202202
}
203203
}
204204

@@ -242,12 +242,12 @@ function getLocalStorageLong(): CustomTextLongObject {
242242
return customTextLongLS.get();
243243
}
244244

245-
function setLocalStorage(data: CustomTextObject): void {
246-
customTextLS.set(data);
245+
function setLocalStorage(data: CustomTextObject): boolean {
246+
return customTextLS.set(data);
247247
}
248248

249-
function setLocalStorageLong(data: CustomTextLongObject): void {
250-
customTextLongLS.set(data);
249+
function setLocalStorageLong(data: CustomTextLongObject): boolean {
250+
return customTextLongLS.set(data);
251251
}
252252

253253
export function getCustomTextNames(long = false): string[] {

frontend/src/ts/utils/local-storage-with-schema.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { ZodError, ZodIssue } from "zod";
1+
import { ZodIssue } from "zod";
22
import { deepClone } from "./misc";
3+
import { isZodError } from "@monkeytype/util/zod";
4+
import * as Notifications from "../elements/notifications";
35

46
export class LocalStorageWithSchema<T> {
57
private key: string;
@@ -77,11 +79,23 @@ export class LocalStorageWithSchema<T> {
7779
window.localStorage.setItem(this.key, JSON.stringify(parsed));
7880
return true;
7981
} catch (e) {
80-
console.error(
81-
`Failed to set ${this.key} in localStorage`,
82-
data,
83-
(e as ZodError).issues
84-
);
82+
let message = "Unknown error occurred";
83+
84+
if (isZodError(e)) {
85+
message = e.issues
86+
.map((i) => (i.message ? i.message : JSON.stringify(i)))
87+
.join(", ");
88+
} else {
89+
if ((e as Error).message.includes("exceeded the quota")) {
90+
message =
91+
"Local storage is full. Please clear some space and try again.";
92+
}
93+
}
94+
95+
const msg = `Failed to set ${this.key} in localStorage: ${message}`;
96+
console.error(msg);
97+
Notifications.add(msg, -1);
98+
8599
return false;
86100
}
87101
}

0 commit comments

Comments
 (0)