Skip to content

Commit

Permalink
Add an alert when a user overfills local storage one way or anotehr
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter York committed Mar 28, 2024
1 parent 6f905bb commit f541a3f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 49 deletions.
3 changes: 2 additions & 1 deletion src/lib/browse/SchemeCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { map } from "stores";
import type { SchemeData } from "types";
import { schemesGj } from "./stores";
import { setLocalStorageItem } from "lib/common";
export let scheme: SchemeData;
Expand All @@ -30,7 +31,7 @@
// Assuming the schema is always v1
// Put the file in local storage, so it'll be loaded from the next page
window.localStorage.setItem(filename, JSON.stringify(gj));
setLocalStorageItem(filename, JSON.stringify(gj));
window.open(
`scheme.html?authority=${scheme.browse?.authority_or_region}`,
"_blank",
Expand Down
1 change: 1 addition & 0 deletions src/lib/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export { default as StreetViewHelp } from "./StreetViewHelp.svelte";
export { default as StreetViewTool } from "./StreetViewTool.svelte";
export { default as WarningIcon } from "./WarningIcon.svelte";
export { default as ZoomOutMap } from "./ZoomOutMap.svelte";
export * from "./storage";

export async function getAuthoritiesGeoJson(): Promise<AuthorityBoundaries> {
let resp = await fetch(authoritiesUrl);
Expand Down
18 changes: 18 additions & 0 deletions src/lib/common/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let storageQuotaExceeded = false;
const storageQuotaErrorMessage = "Unable to save because the local storage quota has been exceeded: you may need to clear out your web browser's local storage for this app, or run in private mode before you can save again. Specific error here: ";

export function setLocalStorageItem(name: string, content: string) {
try {
window.localStorage.setItem(name, content);
} catch (error: any) {
const isStorageQuotaError = error.stack && error.stack.includes("exceeded the quota.") && error.stack.includes("at setLocalStorageItem");
if (!storageQuotaExceeded && isStorageQuotaError) {
window.alert(storageQuotaErrorMessage + error);
storageQuotaExceeded = true;
} else if(isStorageQuotaError) {
console.log(`StorageQuotaExceeded again: ${error}`);
} else {
console.log(`Unexpected error when saving locally: ${error}`);
}
}
};
8 changes: 4 additions & 4 deletions src/lib/critical_entry/Form.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { Modal } from "lib/common";
import { Modal, setLocalStorageItem } from "lib/common";
import {
ButtonGroup,
DefaultButton,
Expand All @@ -21,9 +21,9 @@
let schemeReference = window.localStorage.getItem("schemeReference") ?? "";
let currentDesignStage =
window.localStorage.getItem("currentDesignStage") ?? "";
$: window.localStorage.setItem("inspector", inspector);
$: window.localStorage.setItem("schemeReference", schemeReference);
$: window.localStorage.setItem("currentDesignStage", currentDesignStage);
$: setLocalStorageItem("inspector", inspector);
$: setLocalStorageItem("schemeReference", schemeReference);
$: setLocalStorageItem("currentDesignStage", currentDesignStage);
let criticalIssueType = "";
let locationDescription = "";
Expand Down
4 changes: 2 additions & 2 deletions src/lib/sidebar/FileManagement.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { Modal } from "lib/common";
import { Modal, setLocalStorageItem } from "lib/common";
import {
gjSchemeCollection,
hideSchemes,
Expand Down Expand Up @@ -61,7 +61,7 @@
$: {
if (loaded && $gjSchemeCollection) {
console.log(`GJ changed, saving to local storage`);
window.localStorage.setItem(filename, JSON.stringify(geojsonToSave()));
setLocalStorageItem(filename, JSON.stringify(geojsonToSave()));
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/pages/ChooseArea.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
LoggedIn,
MapLibreMap,
Popup,
setLocalStorageItem,
} from "lib/common";
import About from "lib/sidebar/About.svelte";
import { schema as schemaStore } from "stores";
Expand Down Expand Up @@ -84,7 +85,7 @@
}
// Put the file in local storage, so it'll be loaded from the next page
window.localStorage.setItem(filename, JSON.stringify(gj));
setLocalStorageItem(filename, JSON.stringify(gj));
window.location.href = `scheme.html?authority=${gj.authority}&schema=${schema}`;
} catch (err) {
pageErrorMessage = `Couldn't load scheme from a file: ${err}`;
Expand Down
3 changes: 2 additions & 1 deletion src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { Map } from "maplibre-gl";
import { writable, type Writable } from "svelte/store";
import { isStreetViewImagery, type Schema, type UserSettings } from "./types";
import { setLocalStorageItem } from "lib/common"

// Note this must be set before gjSchemeCollection in lib/draw/stores.ts
export const schema: Writable<Schema> = writable(defaultSchema());
Expand All @@ -17,7 +18,7 @@ export const mapStyle: Writable<string> = writable("dataviz");
export const userSettings: Writable<UserSettings> =
writable(loadUserSettings());
userSettings.subscribe((value) =>
window.localStorage.setItem("userSettings", JSON.stringify(value)),
setLocalStorageItem("userSettings", JSON.stringify(value)),
);

function loadUserSettings(): UserSettings {
Expand Down
40 changes: 0 additions & 40 deletions tests/storage.spec.ts

This file was deleted.

0 comments on commit f541a3f

Please sign in to comment.