fix: store draft personas and favorites locally (#366)

This commit is contained in:
Vojtech Simetka 2023-04-03 19:42:25 +02:00 committed by GitHub
parent a6349834e0
commit d81981ebd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@ import { connectWallet } from '$lib/services'
import { chats, type Chat, type Message } from '$lib/stores/chat'
import { personas, type DraftPersona, type Persona } from '$lib/stores/persona'
import { profile } from '$lib/stores/profile'
import { saveToLocalStorage } from '$lib/utils'
import { getFromLocalStorage, saveToLocalStorage } from '$lib/utils'
import type { Signer } from 'ethers'
import { create } from 'ipfs-http-client'
import {
@ -163,8 +163,9 @@ export class Firebase implements Adapter {
return new Promise((resolve) => {
personas.update(({ favorite, ...store }) => {
const favoriteNew: string[] = [...favorite, groupId]
const { address } = get(profile)
saveToLocalStorage('favorite', favoriteNew)
if (address) saveToLocalStorage(`${address}-firebase-favorite`, favoriteNew)
resolve()
return { ...store, favorite: favoriteNew }
@ -175,8 +176,9 @@ export class Firebase implements Adapter {
return new Promise((resolve) => {
personas.update(({ favorite, ...store }) => {
const favoriteNew: string[] = favorite.filter((s) => s !== groupId)
const { address } = get(profile)
saveToLocalStorage('favorite', favoriteNew)
if (address) saveToLocalStorage(`${address}-firebase-favorite`, favoriteNew)
resolve()
return { ...store, favorite: favoriteNew }
@ -263,7 +265,7 @@ export class Firebase implements Adapter {
personas.update(({ draft, ...state }) => {
const newDraft = draft.filter((d) => d !== draftPersona)
saveToLocalStorage('drafts', newDraft)
saveToLocalStorage(`${address}-firebase-drafts`, newDraft)
return { ...state, draft: newDraft }
})
@ -274,6 +276,9 @@ export class Firebase implements Adapter {
async signIn(): Promise<void> {
const signer = await connectWallet()
const address = await signer.getAddress()
const draftPersonas = getFromLocalStorage(`${address}-firebase-drafts`, [])
const favoritePersonas = getFromLocalStorage(`${address}-firebase-favorite`, [])
personas.update((state) => ({ ...state, drafts: draftPersonas, favorite: favoritePersonas }))
const user = doc(db, `users/${address}`)
setDoc(user, { address, lastSignIn: Date.now() }, { merge: true })