-
Notifications
You must be signed in to change notification settings - Fork 0
/
joined-channels-query.ts
70 lines (64 loc) · 2.07 KB
/
joined-channels-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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {
Channel,
useKeysQuery,
useNostrFetchQuery,
useNostrGetUserProfileQuery,
} from "../nostr";
import { ChatQueries } from "./queries";
import { Kind } from "nostr-tools";
import { convertEvent } from "../nostr/utils/event-converter";
import { useContext } from "react";
import { ChatContext } from "../chat-context-provider";
/**
* This query represents real joined communities list in Nostr
* because joined channel list could contain computed values
*/
export function useOriginalJoinedChannelsQuery() {
const { activeUsername } = useContext(ChatContext);
const { publicKey } = useKeysQuery();
const { data: activeUserNostrProfiles } =
useNostrGetUserProfileQuery(publicKey);
return useNostrFetchQuery(
[ChatQueries.ORIGINAL_JOINED_CHANNELS, activeUsername],
activeUserNostrProfiles?.[0]?.joinedChannels?.map((id) => ({
kinds: [Kind.ChannelCreation],
ids: [id],
})) ?? [],
(events) =>
events
.map((event) => convertEvent<Kind.ChannelCreation>(event))
.filter((channel) => !!channel) as Channel[],
{
enabled: (activeUserNostrProfiles?.[0]?.joinedChannels?.length ?? 0) > 0,
refetchOnMount: false,
},
);
}
export function useJoinedChannelsQuery() {
const { activeUsername } = useContext(ChatContext);
const { publicKey } = useKeysQuery();
const { data: activeUserNostrProfiles } =
useNostrGetUserProfileQuery(publicKey);
return useNostrFetchQuery(
[ChatQueries.JOINED_CHANNELS, activeUsername],
activeUserNostrProfiles?.[0]?.joinedChannels?.map((id) => ({
kinds: [Kind.ChannelCreation],
ids: [id],
})) ?? [],
(events) =>
events
.map((event) => convertEvent<Kind.ChannelCreation>(event))
.filter((channel) => !!channel)
.reduce<Channel[]>(
(acc, channel) =>
acc.every((ac) => ac.id !== channel?.id)
? [...acc, channel as Channel]
: acc,
[],
),
{
enabled: (activeUserNostrProfiles?.[0]?.joinedChannels?.length ?? 0) > 0,
refetchOnMount: false,
},
);
}