Try to make applesauce work
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { createSignal, createEffect, onCleanup } from "solid-js"
|
||||
import type { NostrEvent } from "applesauce-core/helpers/event"
|
||||
import { account } from "./store"
|
||||
import { eventStore } from "./nostr"
|
||||
import { parseRelayList, parseOutboxRelays } from "./lib/relays"
|
||||
|
||||
export function useActivePubkey(): () => string {
|
||||
return () => account()?.pubkey ?? ""
|
||||
}
|
||||
|
||||
export function useAutoThreshold(members: () => string[]): [() => number, (n: number) => void] {
|
||||
const [threshold, setThreshold] = createSignal(1)
|
||||
createEffect(() => setThreshold(Math.max(1, Math.floor(members().length * 2 / 3))))
|
||||
return [threshold, setThreshold]
|
||||
}
|
||||
|
||||
function useReplaceable(kind: number, pubkey: () => string): () => NostrEvent | undefined {
|
||||
const [event, setEvent] = createSignal<NostrEvent | undefined>()
|
||||
createEffect(() => {
|
||||
const pk = pubkey()
|
||||
if (!pk) { setEvent(undefined); return }
|
||||
const sub = eventStore.replaceable(kind, pk).subscribe(e => setEvent(e ?? undefined))
|
||||
onCleanup(() => sub.unsubscribe())
|
||||
})
|
||||
return event
|
||||
}
|
||||
|
||||
export function useProfileEvent(pubkey: () => string): () => NostrEvent | 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) : [] }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user