Clean up login page
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { createEffect, createSignal, onCleanup } from "solid-js"
|
||||
import { EventStore } from "applesauce-core"
|
||||
import { getProfilePicture } from "applesauce-core/helpers/profile"
|
||||
import { RelayPool } from "applesauce-relay"
|
||||
import { AccountManager } from "applesauce-accounts"
|
||||
import type { IAccount, SerializedAccount } from "applesauce-accounts"
|
||||
@@ -8,6 +10,8 @@ import { NostrConnectSigner } from "applesauce-signers"
|
||||
export const API_URL = import.meta.env.VITE_API_URL
|
||||
export const PLATFORM_NAME = import.meta.env.VITE_PLATFORM_NAME || "Caravel"
|
||||
|
||||
const PROFILE_RELAYS = ["wss://purplepag.es", "wss://relay.damus.io", "wss://nos.lol"]
|
||||
|
||||
export const eventStore = new EventStore()
|
||||
export const pool = new RelayPool()
|
||||
export const accounts = new AccountManager()
|
||||
@@ -51,3 +55,40 @@ export function persistAccounts() {
|
||||
localStorage.removeItem(ACTIVE_ACCOUNT_STORAGE_KEY)
|
||||
}
|
||||
}
|
||||
|
||||
export function useActiveAccount() {
|
||||
const [account, setAccount] = createSignal(accounts.active)
|
||||
const sub = accounts.active$.subscribe(setAccount)
|
||||
onCleanup(() => sub.unsubscribe())
|
||||
return account
|
||||
}
|
||||
|
||||
export function useProfilePicture(pubkey: () => string | undefined) {
|
||||
const [picture, setPicture] = createSignal<string | undefined>()
|
||||
|
||||
createEffect(() => {
|
||||
const pk = pubkey()
|
||||
|
||||
if (!pk) {
|
||||
setPicture(undefined)
|
||||
return
|
||||
}
|
||||
|
||||
// Subscribe to profile changes in the event store
|
||||
const profileSub = eventStore.profile(pk).subscribe(profile => {
|
||||
setPicture(getProfilePicture(profile))
|
||||
})
|
||||
|
||||
// Fetch the kind 0 from relays and add to the event store
|
||||
const reqSub = pool.request(PROFILE_RELAYS, { kinds: [0], authors: [pk] }).subscribe(event => {
|
||||
eventStore.add(event)
|
||||
})
|
||||
|
||||
onCleanup(() => {
|
||||
profileSub.unsubscribe()
|
||||
reqSub.unsubscribe()
|
||||
})
|
||||
})
|
||||
|
||||
return picture
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user