Fix some ui bugs
This commit is contained in:
+19
-32
@@ -1,12 +1,11 @@
|
||||
import { createSignal, createEffect, onCleanup } from "solid-js"
|
||||
import type { TrustedEvent } from "@welshman/util"
|
||||
import type { RepositoryUpdate } from "@welshman/net"
|
||||
import { account } from "./store"
|
||||
import { repository } from "./nostr"
|
||||
import { parseRelayList, parseOutboxRelays } from "./lib/relays"
|
||||
import type { PublishedProfile } from "@welshman/util"
|
||||
import { pubkey, deriveProfile, deriveProfileDisplay } from "@welshman/app"
|
||||
import { useReadable } from "./lib/stores"
|
||||
|
||||
export function useActivePubkey(): () => string {
|
||||
return () => account()?.pubkey ?? ""
|
||||
const pk = useReadable(pubkey)
|
||||
return () => pk() ?? ""
|
||||
}
|
||||
|
||||
export function useAutoThreshold(members: () => string[]): [() => number, (n: number) => void] {
|
||||
@@ -15,34 +14,22 @@ export function useAutoThreshold(members: () => string[]): [() => number, (n: nu
|
||||
return [threshold, setThreshold]
|
||||
}
|
||||
|
||||
function useReplaceable(kind: number, pubkey: () => string): () => TrustedEvent | undefined {
|
||||
const [event, setEvent] = createSignal<TrustedEvent | undefined>()
|
||||
export function useProfile(pubkeyFn: () => string): () => PublishedProfile | undefined {
|
||||
const [profile, setProfile] = createSignal<PublishedProfile | undefined>()
|
||||
createEffect(() => {
|
||||
const pk = pubkey()
|
||||
if (!pk) { setEvent(undefined); return }
|
||||
const query = () => setEvent(repository.getEvent(`${kind}:${pk}:`))
|
||||
const onUpdate = (update: RepositoryUpdate) => {
|
||||
if (update.added.some(e => e.kind === kind && e.pubkey === pk) || update.removed.size > 0) {
|
||||
query()
|
||||
}
|
||||
}
|
||||
query()
|
||||
repository.on("update", onUpdate)
|
||||
onCleanup(() => repository.off("update", onUpdate))
|
||||
const pk = pubkeyFn()
|
||||
if (!pk) { setProfile(undefined); return }
|
||||
onCleanup(deriveProfile(pk).subscribe(p => setProfile(() => p)))
|
||||
})
|
||||
return event
|
||||
return profile
|
||||
}
|
||||
|
||||
export function useProfileEvent(pubkey: () => string): () => TrustedEvent | undefined {
|
||||
return useReplaceable(0, pubkey)
|
||||
}
|
||||
|
||||
export function useMessagingRelays(pubkey: () => string): () => string[] {
|
||||
const event = useReplaceable(10050, pubkey)
|
||||
return () => { const e = event(); return e ? parseRelayList(e) : [] }
|
||||
}
|
||||
|
||||
export function useOutboxRelays(pubkey: () => string): () => string[] {
|
||||
const event = useReplaceable(10002, pubkey)
|
||||
return () => { const e = event(); return e ? parseOutboxRelays(e) : [] }
|
||||
export function useProfileDisplay(pubkeyFn: () => string): () => string {
|
||||
const [name, setName] = createSignal("")
|
||||
createEffect(() => {
|
||||
const pk = pubkeyFn()
|
||||
if (!pk) { setName(""); return }
|
||||
onCleanup(deriveProfileDisplay(pk).subscribe(n => setName(() => n)))
|
||||
})
|
||||
return name
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user