update channel

This commit is contained in:
Ren Amamiya
2023-05-29 22:04:50 +07:00
parent 0492729e7e
commit c8f643198e
18 changed files with 340 additions and 467 deletions

View File

@@ -1,31 +0,0 @@
import { atom } from "jotai";
import { atomWithReset } from "jotai/utils";
// channel reply id
export const channelReplyAtom = atomWithReset({
id: null,
pubkey: null,
content: null,
});
// channel messages
export const channelMessagesAtom = atomWithReset([]);
export const sortedChannelMessagesAtom = atom((get) => {
const messages = get(channelMessagesAtom);
return messages.sort(
(x: { created_at: number }, y: { created_at: number }) =>
x.created_at - y.created_at,
);
});
// channel user list
export const channelMembersAtom = atom((get) => {
const messages = get(channelMessagesAtom);
const uniqueMembers = new Set(
messages.map((m: { pubkey: string }) => m.pubkey),
);
return uniqueMembers;
});
// channel message content
export const channelContentAtom = atomWithReset("");

View File

@@ -1,5 +1,6 @@
import { getChannels } from "@utils/storage";
import { create } from "zustand";
import { immer } from "zustand/middleware/immer";
export const useChannels = create((set) => ({
channels: [],
@@ -9,10 +10,31 @@ export const useChannels = create((set) => ({
},
}));
export const useChannelMessages = create((set) => ({
messages: [],
replyTo: null,
add: (message: any) => {
set((state: any) => ({ messages: [...state.messages, message] }));
},
}));
export const useChannelMessages = create(
immer((set) => ({
messages: [],
members: new Set(),
replyTo: { id: null, pubkey: null, content: null },
add: (message: any) => {
set((state: any) => ({ messages: [...state.messages, message] }));
},
openReply: (id: string, pubkey: string, content: string) => {
set(() => ({ replyTo: { id, pubkey, content } }));
},
closeReply: () => {
set(() => ({ replyTo: { id: null, pubkey: null, content: null } }));
},
hideMessage: (id: string) => {
set((state) => {
const target = state.messages.findIndex((m) => m.id === id);
state.messages[target]["hide"] = true;
});
},
muteUser: (pubkey: string) => {
set((state) => {
const target = state.messages.findIndex((m) => m.pubkey === pubkey);
state.messages[target]["mute"] = true;
});
},
})),
);