-
Notifications
You must be signed in to change notification settings - Fork 0
/
community-channel-query.ts
45 lines (42 loc) · 1.41 KB
/
community-channel-query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { ChatQueries } from "./queries";
import { Channel, useKeysQuery, useNostrFetchQuery } from "../nostr";
import { useGetCommunityChannelQuery } from "../api";
import { Kind } from "nostr-tools";
import { convertEvent } from "../nostr/utils/event-converter";
import { UseQueryResult } from "@tanstack/react-query";
import { useEffect } from "react";
/**
* Get the community's channel information
* @note it marked as any because select function changes return type
* @see {@link ../mutations/create-community-chat.ts}
*/
export function useCommunityChannelQuery(community?: {
name: string;
}): UseQueryResult<Channel | undefined> {
const { data: communityChannel, refetch: refetchCommunityChannel } =
useGetCommunityChannelQuery(community?.name);
const { hasKeys } = useKeysQuery();
useEffect(() => {
if (!communityChannel && community) {
refetchCommunityChannel();
}
}, [communityChannel, community]);
return useNostrFetchQuery<any>(
[ChatQueries.COMMUNITY_CHANNEL, community?.name],
[
{
kinds: [Kind.ChannelCreation],
ids: [communityChannel?.channel_id ?? ""],
},
],
(events) =>
events
.map((event) => convertEvent<Kind.ChannelCreation>(event))
.filter((channel) => !!channel) as Channel[],
{
initialData: [],
enabled: hasKeys && !!communityChannel?.channel_id,
select: (channels) => channels?.[0],
},
);
}