| 1 |
import db from "./db"; |
| 2 |
|
| 3 |
export const THEME = { |
| 4 |
DARK: "dark", |
| 5 |
LIGHT: "light", |
| 6 |
SYSTEM: "system", |
| 7 |
}; |
| 8 |
|
| 9 |
// How the date component is displayed. SYSTEM follows the browser's locale (i.e. the OS region/ |
| 10 |
// format settings, not the chosen UI translation language); ISO8601 forces YYYY-MM-DD; DMY and MDY |
| 11 |
// force the day/month order regardless of locale (e.g. for an English UI that defaults to US order). |
| 12 |
export const DATE_FORMAT = { |
| 13 |
SYSTEM: "system", |
| 14 |
ISO8601: "iso8601", |
| 15 |
DMY: "dmy", |
| 16 |
DMY_DOT: "dmy_dot", |
| 17 |
MDY: "mdy", |
| 18 |
}; |
| 19 |
|
| 20 |
// How the time component (clock) is displayed. Orthogonal to DATE_FORMAT and to the UI language -- |
| 21 |
// SYSTEM lets the browser locale decide, H12/H24 force a 12- or 24-hour clock. ISO 8601 dates are |
| 22 |
// always 24-hour, so this setting is ignored in that mode. |
| 23 |
export const TIME_FORMAT = { |
| 24 |
SYSTEM: "system", |
| 25 |
H12: "12h", |
| 26 |
H24: "24h", |
| 27 |
}; |
| 28 |
|
| 29 |
// Default values the getters return when a pref is unset; also used by PrefCache (PrefCache.jsx). |
| 30 |
export const PREF_DEFAULTS = { |
| 31 |
sound: "ding", |
| 32 |
minPriority: 1, |
| 33 |
deleteAfter: 604800, // one week |
| 34 |
theme: THEME.SYSTEM, |
| 35 |
dateFormat: DATE_FORMAT.SYSTEM, |
| 36 |
timeFormat: TIME_FORMAT.SYSTEM, |
| 37 |
webPushEnabled: false, |
| 38 |
}; |
| 39 |
|
| 40 |
// Guards against values the current build doesn't know about (e.g. a pref synced from a newer |
| 41 |
// client, or a renamed enum). An unknown value would leave the Settings <Select> with no matching |
| 42 |
// option, so we fall back to the default instead. |
| 43 |
const isKnownValue = (constants, value) => Object.values(constants).includes(value); |
| 44 |
|
| 45 |
export class Prefs { |
| 46 |
constructor(dbImpl) { |
| 47 |
this.db = dbImpl; |
| 48 |
} |
| 49 |
|
| 50 |
async setSound(sound) { |
| 51 |
this.db.prefs.put({ key: "sound", value: sound.toString() }); |
| 52 |
} |
| 53 |
|
| 54 |
async sound() { |
| 55 |
const sound = await this.db.prefs.get("sound"); |
| 56 |
return sound ? sound.value : PREF_DEFAULTS.sound; |
| 57 |
} |
| 58 |
|
| 59 |
async setMinPriority(minPriority) { |
| 60 |
this.db.prefs.put({ key: "minPriority", value: minPriority.toString() }); |
| 61 |
} |
| 62 |
|
| 63 |
async minPriority() { |
| 64 |
const minPriority = await this.db.prefs.get("minPriority"); |
| 65 |
return minPriority ? Number(minPriority.value) : PREF_DEFAULTS.minPriority; |
| 66 |
} |
| 67 |
|
| 68 |
async setDeleteAfter(deleteAfter) { |
| 69 |
await this.db.prefs.put({ key: "deleteAfter", value: deleteAfter.toString() }); |
| 70 |
} |
| 71 |
|
| 72 |
async deleteAfter() { |
| 73 |
const deleteAfter = await this.db.prefs.get("deleteAfter"); |
| 74 |
return deleteAfter ? Number(deleteAfter.value) : PREF_DEFAULTS.deleteAfter; |
| 75 |
} |
| 76 |
|
| 77 |
async webPushEnabled() { |
| 78 |
const webPushEnabled = await this.db.prefs.get("webPushEnabled"); |
| 79 |
return webPushEnabled?.value ?? PREF_DEFAULTS.webPushEnabled; |
| 80 |
} |
| 81 |
|
| 82 |
async setWebPushEnabled(enabled) { |
| 83 |
await this.db.prefs.put({ key: "webPushEnabled", value: enabled }); |
| 84 |
} |
| 85 |
|
| 86 |
async theme() { |
| 87 |
const theme = await this.db.prefs.get("theme"); |
| 88 |
return theme?.value ?? PREF_DEFAULTS.theme; |
| 89 |
} |
| 90 |
|
| 91 |
async setTheme(mode) { |
| 92 |
await this.db.prefs.put({ key: "theme", value: mode }); |
| 93 |
} |
| 94 |
|
| 95 |
async dateFormat() { |
| 96 |
const dateFormat = await this.db.prefs.get("dateFormat"); |
| 97 |
return isKnownValue(DATE_FORMAT, dateFormat?.value) ? dateFormat.value : PREF_DEFAULTS.dateFormat; |
| 98 |
} |
| 99 |
|
| 100 |
async setDateFormat(format) { |
| 101 |
await this.db.prefs.put({ key: "dateFormat", value: format }); |
| 102 |
} |
| 103 |
|
| 104 |
async timeFormat() { |
| 105 |
const timeFormat = await this.db.prefs.get("timeFormat"); |
| 106 |
return isKnownValue(TIME_FORMAT, timeFormat?.value) ? timeFormat.value : PREF_DEFAULTS.timeFormat; |
| 107 |
} |
| 108 |
|
| 109 |
async setTimeFormat(format) { |
| 110 |
await this.db.prefs.put({ key: "timeFormat", value: format }); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
const prefs = new Prefs(db()); |
| 115 |
export default prefs; |