Switch away from nonboard for nostr login

This commit is contained in:
Jon Staab
2026-02-26 08:57:47 -08:00
parent 4e4d5907bf
commit 58d3036d31
7 changed files with 368 additions and 46 deletions
+43
View File
@@ -1,9 +1,52 @@
import { EventStore } from "applesauce-core"
import { RelayPool } from "applesauce-relay"
import { AccountManager } from "applesauce-accounts"
import type { IAccount, SerializedAccount } from "applesauce-accounts"
import { registerCommonAccountTypes } from "applesauce-accounts/accounts"
import { NostrConnectSigner } from "applesauce-signers"
export const API_URL = import.meta.env.VITE_API_URL
export const eventStore = new EventStore()
export const pool = new RelayPool()
export const accounts = new AccountManager()
const ACCOUNTS_STORAGE_KEY = "caravel.accounts"
const ACTIVE_ACCOUNT_STORAGE_KEY = "caravel.activeAccount"
registerCommonAccountTypes(accounts)
NostrConnectSigner.subscriptionMethod = pool.subscription.bind(pool)
NostrConnectSigner.publishMethod = pool.publish.bind(pool)
export function restoreAccounts() {
const raw = localStorage.getItem(ACCOUNTS_STORAGE_KEY)
if (raw) {
try {
const saved = JSON.parse(raw) as SerializedAccount[]
accounts.fromJSON(saved, true)
} catch (error) {
console.warn("Failed to restore accounts", error)
}
}
const activeId = localStorage.getItem(ACTIVE_ACCOUNT_STORAGE_KEY)
if (activeId && accounts.getAccount(activeId)) {
accounts.setActive(activeId)
}
}
export function activateAccount(account: IAccount) {
accounts.addAccount(account)
accounts.setActive(account)
persistAccounts()
}
export function persistAccounts() {
localStorage.setItem(ACCOUNTS_STORAGE_KEY, JSON.stringify(accounts.toJSON(true)))
const active = accounts.getActive()
if (active) {
localStorage.setItem(ACTIVE_ACCOUNT_STORAGE_KEY, active.id)
} else {
localStorage.removeItem(ACTIVE_ACCOUNT_STORAGE_KEY)
}
}