Files
DiscordClone/packages/platform-web/src/settings.js
Bryan1029384756 6813bb40dc
All checks were successful
Build and Release / build-and-release (push) Successful in 20m36s
1.1.00
2026-04-16 20:14:27 -05:00

52 lines
1.6 KiB
JavaScript

/**
* Web platform settings using localStorage.
* Returns Promises to match the Electron IPC-based API contract.
*/
const PREFIX = 'discord-clone-settings:';
// Browsers spell the quota error in a few different ways across vendors.
// Normalizing here so callers can `if (err.isQuotaError)` regardless.
function isQuotaError(e) {
if (!e) return false;
const name = e.name || '';
const code = e.code;
return (
name === 'QuotaExceededError' ||
name === 'NS_ERROR_DOM_QUOTA_REACHED' ||
code === 22 ||
code === 1014
);
}
export default {
get(key) {
try {
const raw = localStorage.getItem(PREFIX + key);
return Promise.resolve(raw !== null ? JSON.parse(raw) : undefined);
} catch {
// Corrupted JSON or blocked storage access — return undefined so
// the caller falls back to defaults. Worth recovering silently
// here because a single bad key shouldn't take down the app.
return Promise.resolve(undefined);
}
},
set(key, value) {
try {
localStorage.setItem(PREFIX + key, JSON.stringify(value));
return Promise.resolve();
} catch (e) {
if (isQuotaError(e)) {
// Reject so the caller can surface the quota error to the user —
// silently swallowing meant settings just "didn't save" with no
// warning. Decorate with a flag so callers can branch without
// sniffing error names themselves.
const err = new Error('Browser storage quota exceeded');
err.isQuotaError = true;
return Promise.reject(err);
}
return Promise.reject(e);
}
},
};