-
Notifications
You must be signed in to change notification settings - Fork 0
/
left-community-channels-query.ts
44 lines (39 loc) · 1.18 KB
/
left-community-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
import { useNostrFetchQuery } from "../nostr";
import { findTagValue, isSha256 } from "../nostr/utils";
import { ChatQueries } from "./queries";
import { convertEvent } from "../nostr/utils/event-converter";
import { Kind } from "nostr-tools";
import { useContext } from "react";
import { ChatContext } from "../chat-context-provider";
/**
* Fetch custom kind of event which stores the left channels
*/
export function useLeftCommunityChannelsQuery() {
const { activeUsername } = useContext(ChatContext);
return useNostrFetchQuery<string[]>(
[ChatQueries.LEFT_CHANNELS, activeUsername],
[30078 as Kind],
(events) => {
if (events.length === 0) {
return [];
}
const leftChannelsEvent = events
.filter(
(x) =>
x.kind == 30078 && findTagValue(x, "d") === "left-channel-list",
)
.sort((a, b) => b.created_at - a.created_at)[0];
if (!leftChannelsEvent) {
return [];
}
const content = convertEvent<30078>(leftChannelsEvent);
if (Array.isArray(content) && content.every((x) => isSha256(x))) {
return content;
}
return [];
},
{
initialData: [],
},
);
}