ghost-dao-interface/src/hooks/ghost/useApplauseDetails.js
2025-12-15 15:58:37 +03:00

47 lines
1.9 KiB
JavaScript

import useSWRSubscription from "swr/subscription"
import { getDynamicBuilder, getLookupFn } from "@polkadot-api/metadata-builders"
import { distinct, filter, map, mergeMap } from "rxjs"
import { fromHex } from "@polkadot-api/utils";
import { useUnstableProvider } from "./UnstableProvider"
import { useMetadata } from "./MetadataProvider"
export const useApplauseDetails = ({ currentSession, argsHash }) => {
const { chainHead$, chainId } = useUnstableProvider()
const metadata = useMetadata()
const { data: applauseDetails } = useSWRSubscription(
chainHead$ && argsHash && currentSession && chainId && metadata
? ["applauseDetails", chainHead$, argsHash, currentSession, chainId, metadata]
: null,
([_, chainHead$, argsHash, currentSession, chainId, metadata], { next }) => {
const { finalized$, storage$ } = chainHead$
const subscription = finalized$.pipe(
filter(Boolean),
mergeMap((blockInfo) => {
const builder = getDynamicBuilder(getLookupFn(metadata))
const applauseDetails = builder.buildStorage("GhostSlowClaps", "ApplauseDetails")
return storage$(blockInfo?.hash, "value", () =>
applauseDetails?.keys.enc(
currentSession,
{ asBytes: () => fromHex(argsHash) },
)
).pipe(
filter(Boolean),
distinct(),
map((value) => applauseDetails?.value.dec(value))
)
}),
)
.subscribe({
next(applauseDetails) {
next(null, applauseDetails)
},
error: next,
})
return () => subscription.unsubscribe()
}
)
return applauseDetails
}