refactor: abstract localstorage storing and retrieving (#252)

This commit is contained in:
Vojtech Simetka 2023-03-13 21:12:39 +01:00 committed by GitHub
parent c6611f2854
commit 40613b656d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 17 deletions

View File

@ -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()

View File

@ -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
}