-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a bare-bones settings-helper on top of localstorage to make it easy to read and write values that should be persisted, in a way that does not get in the way of other things running on localhost:3000 This fixes #48.
- Loading branch information
1 parent
b49f0e3
commit 58543da
Showing
2 changed files
with
27 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const defaultSettings = { | ||
volume: 1, | ||
}; | ||
|
||
type SettingKey = keyof typeof defaultSettings; | ||
|
||
function computeKey(key: SettingKey): string { | ||
return `renin-${key}`; | ||
} | ||
|
||
export function getSetting<T extends SettingKey>(key: T): typeof defaultSettings[T] { | ||
const raw = localStorage[computeKey(key)]; | ||
if (raw !== undefined) { | ||
return JSON.parse(raw); | ||
} | ||
return defaultSettings[key]; | ||
} | ||
|
||
export function setSetting<T extends SettingKey>(key: T, value: typeof defaultSettings[T]) { | ||
localStorage[computeKey(key)] = JSON.stringify(value); | ||
} |