Initial commit: AI chat assistant with workflow chat, workspace, and profile tabs
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
53
composables/useStorage.uts
Normal file
53
composables/useStorage.uts
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* useStorage — Typed persistence wrapper around uni.getStorageSync/setStorageSync
|
||||
*/
|
||||
|
||||
export function useStorage() {
|
||||
|
||||
/**
|
||||
* Load a value from storage with a fallback default.
|
||||
* Returns fallback if key doesn't exist or JSON parsing fails.
|
||||
*/
|
||||
function loadValue<T>(key: string, fallback: T): T {
|
||||
try {
|
||||
const raw = uni.getStorageSync(key)
|
||||
if (raw === '' || raw === undefined || raw === null) {
|
||||
return fallback
|
||||
}
|
||||
const parsed = JSON.parse(raw) as T
|
||||
return parsed !== null ? parsed : fallback
|
||||
} catch (_e) {
|
||||
console.warn(`[useStorage] Failed to load key "${key}", using fallback`)
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a value to storage as JSON.
|
||||
*/
|
||||
function saveValue<T>(key: string, value: T): void {
|
||||
try {
|
||||
const raw = JSON.stringify(value)
|
||||
uni.setStorageSync(key, raw)
|
||||
} catch (e) {
|
||||
console.error(`[useStorage] Failed to save key "${key}":`, e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a key from storage.
|
||||
*/
|
||||
function removeValue(key: string): void {
|
||||
try {
|
||||
uni.removeStorageSync(key)
|
||||
} catch (e) {
|
||||
console.error(`[useStorage] Failed to remove key "${key}":`, e)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
loadValue,
|
||||
saveValue,
|
||||
removeValue,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user