36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { createSignal, createEffect, onCleanup } from "solid-js"
|
|
import type { PublishedProfile } from "@welshman/util"
|
|
import { pubkey, deriveProfile, deriveProfileDisplay } from "@welshman/app"
|
|
import { useReadable } from "./lib/stores"
|
|
|
|
export function useActivePubkey(): () => string {
|
|
const pk = useReadable(pubkey)
|
|
return () => pk() ?? ""
|
|
}
|
|
|
|
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]
|
|
}
|
|
|
|
export function useProfile(pubkeyFn: () => string): () => PublishedProfile | undefined {
|
|
const [profile, setProfile] = createSignal<PublishedProfile | undefined>()
|
|
createEffect(() => {
|
|
const pk = pubkeyFn()
|
|
if (!pk) { setProfile(undefined); return }
|
|
onCleanup(deriveProfile(pk).subscribe(p => setProfile(() => p)))
|
|
})
|
|
return profile
|
|
}
|
|
|
|
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
|
|
}
|