feat: migrate some more stuff to svelte-5, mainly app settings
Some checks failed
Deploy to GitHub Pages / build_site (push) Failing after 4s
Some checks failed
Deploy to GitHub Pages / build_site (push) Failing after 4s
This commit is contained in:
24
app/src/lib/helpers/deepMerge.ts
Normal file
24
app/src/lib/helpers/deepMerge.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export function isObject(item: Record<string, unknown> | unknown): item is Record<string, unknown> {
|
||||
return (typeof item === 'object' && !Array.isArray(item));
|
||||
}
|
||||
|
||||
type Object = Record<string, unknown>;
|
||||
|
||||
export function mergeDeep<T extends Object>(target: T, ...sources: Object[]): T {
|
||||
if (!sources.length) return target;
|
||||
const source = sources.shift();
|
||||
|
||||
if (source === undefined) return target;
|
||||
if (isObject(target) && isObject(source)) {
|
||||
for (const key in source) {
|
||||
if (isObject(source[key])) {
|
||||
if (!target[key]) Object.assign(target, { [key]: {} });
|
||||
if (isObject(target[key])) mergeDeep(target[key], source[key]);
|
||||
} else {
|
||||
Object.assign(target, { [key]: source[key] });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mergeDeep(target, ...sources);
|
||||
}
|
||||
11
app/src/lib/helpers/localState.svelte.ts
Normal file
11
app/src/lib/helpers/localState.svelte.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export function localState<T>(key: string, defaultValue: T): T {
|
||||
const stored = localStorage.getItem(key)
|
||||
const state = $state(stored ? JSON.parse(stored) : defaultValue)
|
||||
$effect.root(() => {
|
||||
$effect(() => {
|
||||
const value = $state.snapshot(state);
|
||||
localStorage.setItem(key, JSON.stringify(value));
|
||||
});
|
||||
});
|
||||
return state;
|
||||
}
|
||||
Reference in New Issue
Block a user