mirror of
https://github.com/acid-info/Kurate.git
synced 2025-01-12 17:04:07 +00:00
refactor: abstract localstorage storing and retrieving (#252)
This commit is contained in:
parent
c6611f2854
commit
40613b656d
@ -1,7 +1,6 @@
|
||||
import { browser } from '$app/environment'
|
||||
import { chats, type Chat } from '$lib/stores/chat'
|
||||
import { personas, type DraftPersona, type Persona } from '$lib/stores/persona'
|
||||
import { sleep } from '$lib/utils'
|
||||
import { getFromLocalStorage, saveToLocalStorage, sleep } from '$lib/utils'
|
||||
import type { Adapter } from '.'
|
||||
|
||||
export class ZkitterAdapter implements Adapter {
|
||||
@ -65,9 +64,8 @@ export class ZkitterAdapter implements Adapter {
|
||||
all.set('4', geoPolitics)
|
||||
all.set('5', controversy)
|
||||
|
||||
const draft = browser && localStorage ? JSON.parse(localStorage.getItem('drafts') ?? '[]') : []
|
||||
const favorite =
|
||||
browser && localStorage ? JSON.parse(localStorage.getItem('favorite') ?? '[]') : []
|
||||
const draft = getFromLocalStorage('drafts', [])
|
||||
const favorite = getFromLocalStorage('favorite', [])
|
||||
|
||||
personas.set({ all, draft, favorite, loading: false })
|
||||
|
||||
@ -178,9 +176,7 @@ export class ZkitterAdapter implements Adapter {
|
||||
personas.update(({ favorite, ...store }) => {
|
||||
const favoriteNew = [...favorite, groupId]
|
||||
|
||||
if (browser && localStorage) {
|
||||
localStorage.setItem('favorite', JSON.stringify(favoriteNew))
|
||||
}
|
||||
saveToLocalStorage('favorite', favoriteNew)
|
||||
|
||||
resolve()
|
||||
return { ...store, favorite: favoriteNew }
|
||||
@ -192,9 +188,7 @@ export class ZkitterAdapter implements Adapter {
|
||||
personas.update(({ favorite, ...store }) => {
|
||||
const favoriteNew = favorite.filter((s) => s !== groupId)
|
||||
|
||||
if (browser && localStorage) {
|
||||
localStorage.setItem('favorite', JSON.stringify(favoriteNew))
|
||||
}
|
||||
saveToLocalStorage('favorite', favoriteNew)
|
||||
|
||||
resolve()
|
||||
return { ...store, favorite: favoriteNew }
|
||||
@ -206,9 +200,7 @@ export class ZkitterAdapter implements Adapter {
|
||||
personas.update(({ draft, ...state }) => {
|
||||
const newDraft = [...draft, draftPersona]
|
||||
|
||||
if (browser && localStorage) {
|
||||
localStorage.setItem('drafts', JSON.stringify(newDraft))
|
||||
}
|
||||
saveToLocalStorage('drafts', newDraft)
|
||||
|
||||
resolve(newDraft.length - 1)
|
||||
|
||||
@ -221,9 +213,7 @@ export class ZkitterAdapter implements Adapter {
|
||||
personas.update(({ draft, ...state }) => {
|
||||
draft[index] = draftPersona
|
||||
|
||||
if (browser && localStorage) {
|
||||
localStorage.setItem('drafts', JSON.stringify(draft))
|
||||
}
|
||||
saveToLocalStorage('drafts', draft)
|
||||
|
||||
resolve()
|
||||
|
||||
|
@ -1,3 +1,7 @@
|
||||
import { browser } from '$app/environment'
|
||||
|
||||
type JSONdecoded = string | number | boolean | object | Array<JSONdecoded>
|
||||
|
||||
/**
|
||||
* Sleep for N miliseconds
|
||||
*
|
||||
@ -6,3 +10,18 @@
|
||||
export async function sleep(ms: number): Promise<void> {
|
||||
return new Promise<void>((resolve) => setTimeout(() => resolve(), ms))
|
||||
}
|
||||
|
||||
export async function saveToLocalStorage<T extends JSONdecoded>(key: string, data: T) {
|
||||
if (browser && localStorage) {
|
||||
localStorage.setItem(key, JSON.stringify(data))
|
||||
}
|
||||
}
|
||||
|
||||
export function getFromLocalStorage<T extends JSONdecoded>(key: string, defaultValue: T): T {
|
||||
if (browser && localStorage) {
|
||||
const data = localStorage.getItem(key)
|
||||
return data ? JSON.parse(data) : defaultValue
|
||||
}
|
||||
|
||||
return defaultValue
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user