-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-community-chat.ts
66 lines (61 loc) · 2.14 KB
/
create-community-chat.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
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useNostrPublishMutation } from "../nostr";
import { Kind } from "nostr-tools";
import { KindOfCommunity } from "../types";
import { useAddChannelToEcency } from "../api";
import { ChatQueries } from "../queries";
import { convertEvent } from "../nostr/utils/event-converter";
/**
* A custom React Query hook for creating a chat channel within a community.
* This hook allows you to create a chat channel associated with a specific community.
* @note only ecency official account able to create a community channel
*/
export function useCreateCommunityChat(community: KindOfCommunity) {
const queryClient = useQueryClient();
const { mutateAsync: createChannel } = useNostrPublishMutation(
["chats/nostr-create-channel"],
Kind.ChannelCreation,
() => {},
{},
);
const { mutateAsync: addToEcency } = useAddChannelToEcency();
return useMutation({
mutationKey: ["chats/create-community-chat"],
mutationFn: async () => {
console.debug(
"[ns-query] Attempting to create a channel for",
community.name,
);
// Step 1: Create a chat channel using the `createChannel` mutation.
const data = await createChannel({
eventMetadata: {
name: community.title,
about: "Ecency app community channel",
communityName: community.name,
picture: "",
},
tags: [],
});
console.debug("[ns-query] Created a channel for", community.name, data);
// Step 2: Add channel to Ecency
const ecencyChannel = await addToEcency({
channel_id: data.id,
meta: {},
username: community.name,
});
return [convertEvent<Kind.ChannelCreation>(data), ecencyChannel] as const;
},
onSuccess: async ([channel, ecencyChannel]) => {
if (channel) {
queryClient.setQueryData(
[ChatQueries.COMMUNITY_CHANNEL, channel.communityName],
channel,
);
queryClient.setQueryData(
["private-api", "get-community-channel", channel.communityName],
ecencyChannel,
);
}
},
});
}