This commit is contained in:
Ren Amamiya
2023-06-09 18:26:24 +07:00
parent 0ba9877785
commit 61ab719a2f
7 changed files with 75 additions and 163 deletions

View File

@@ -1,9 +1,5 @@
import { RelayContext } from "@shared/relayProvider";
import { READONLY_RELAYS } from "@stores/constants";
import { getChannel, updateChannelMetadata } from "@utils/storage";
import { useContext } from "react";
import useSWR, { useSWRConfig } from "swr";
import useSWRSubscription from "swr/subscription";
@@ -18,7 +14,7 @@ const fetcher = async ([, id]) => {
};
export function useChannelProfile(id: string, channelPubkey: string) {
const pool: any = useContext(RelayContext);
const ndk = useContext(RelayContext);
const { mutate } = useSWRConfig();
const { data, isLoading } = useSWR(["channel-metadata", id], fetcher);
@@ -27,30 +23,26 @@ export function useChannelProfile(id: string, channelPubkey: string) {
!isLoading && data ? ["channel-metadata", id] : null,
([, key]) => {
// subscribe to channel
const unsubscribe = pool.subscribe(
[
{
"#e": [key],
authors: [channelPubkey],
kinds: [41],
},
],
READONLY_RELAYS,
(event: { content: string }) => {
// update in local database
updateChannelMetadata(key, event.content);
// revaildate
mutate(["channel-metadata", key]);
},
undefined,
undefined,
const sub = ndk.subscribe(
{
unsubscribeOnEose: true,
"#e": [key],
authors: [channelPubkey],
kinds: [41],
},
{
closeOnEose: true,
},
);
sub.addListener("event", (event: { content: string }) => {
// update in local database
updateChannelMetadata(key, event.content);
// revaildate
mutate(["channel-metadata", key]);
});
return () => {
unsubscribe();
sub.stop();
};
},
);