2024-03-15 15:09:50 +00:00
|
|
|
import { useAuthorization } from '@/composables'
|
|
|
|
import { get as baseGet, remove as baseRemove, set as baseSet } from 'local-storage'
|
|
|
|
|
|
|
|
export const useLocalStorage = (namespaced = true) => {
|
|
|
|
let namespace = ''
|
|
|
|
|
|
|
|
if (namespaced) {
|
|
|
|
const { currentUser } = useAuthorization()
|
|
|
|
namespace = `${currentUser.value.id}::`
|
|
|
|
}
|
|
|
|
|
|
|
|
const get = <T> (key: string, defaultValue: T | null = null): T | null => {
|
|
|
|
const value = baseGet<T>(namespace + key)
|
|
|
|
|
|
|
|
return value === null ? defaultValue : value
|
|
|
|
}
|
|
|
|
|
|
|
|
const set = (key: string, value: any) => baseSet(namespace + key, value)
|
|
|
|
const remove = (key: string) => baseRemove(namespace + key)
|
|
|
|
|
|
|
|
return {
|
|
|
|
get,
|
|
|
|
set,
|
2024-10-13 17:37:01 +00:00
|
|
|
remove,
|
2024-03-15 15:09:50 +00:00
|
|
|
}
|
|
|
|
}
|