UNPKG

nostr-hooks

Version:
285 lines 11.1 kB
import { produce } from 'immer'; import { create } from 'zustand'; export const useNip29Store = create()((set) => ({ groups: {}, updateGroupMetadata: (subId, groupId, metadata) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { state.groups[subId][groupId].metadata = metadata; } else { state.groups[subId][groupId] = { metadata }; } } else { state.groups[subId] = { [groupId]: { metadata } }; } })); }, updateGroupAdmins: (subId, groupId, admins) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { state.groups[subId][groupId].admins = admins; } else { state.groups[subId][groupId] = { admins }; } } else { state.groups[subId] = { [groupId]: { admins } }; } })); }, updateGroupMembers: (subId, groupId, members) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { state.groups[subId][groupId].members = members; } else { state.groups[subId][groupId] = { members }; } } else { state.groups[subId] = { [groupId]: { members } }; } })); }, updateGroupRoles: (subId, groupId, roles) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { state.groups[subId][groupId].roles = roles; } else { state.groups[subId][groupId] = { roles }; } } else { state.groups[subId] = { [groupId]: { roles } }; } })); }, addGroupChat: (subId, groupId, chat) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { if (state.groups[subId][groupId].chats) { state.groups[subId][groupId].chats = [...state.groups[subId][groupId].chats, chat] .filter((thread, index, self) => self.findIndex((t) => t.id === thread.id) === index) .sort((a, b) => a.timestamp - b.timestamp); } else { state.groups[subId][groupId].chats = [chat]; } } else { state.groups[subId][groupId] = { chats: [chat] }; } } else { state.groups[subId] = { [groupId]: { chats: [chat] } }; } })); }, addGroupJoinRequest: (subId, groupId, joinRequest) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { if (state.groups[subId][groupId].joinRequests) { state.groups[subId][groupId].joinRequests = [ ...state.groups[subId][groupId].joinRequests, joinRequest, ] .filter((thread, index, self) => self.findIndex((t) => t.id === thread.id) === index) .sort((a, b) => a.timestamp - b.timestamp); } else { state.groups[subId][groupId].joinRequests = [joinRequest]; } } else { state.groups[subId][groupId] = { joinRequests: [joinRequest] }; } } else { state.groups[subId] = { [groupId]: { joinRequests: [joinRequest] } }; } })); }, addGroupLeaveRequest: (subId, groupId, leaveRequest) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { if (state.groups[subId][groupId].leaveRequests) { state.groups[subId][groupId].leaveRequests = [ ...state.groups[subId][groupId].leaveRequests, leaveRequest, ] .filter((thread, index, self) => self.findIndex((t) => t.id === thread.id) === index) .sort((a, b) => a.timestamp - b.timestamp); } else { state.groups[subId][groupId].leaveRequests = [leaveRequest]; } } else { state.groups[subId][groupId] = { leaveRequests: [leaveRequest] }; } } else { state.groups[subId] = { [groupId]: { leaveRequests: [leaveRequest] } }; } })); }, addGroupThread: (subId, groupId, thread) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { if (state.groups[subId][groupId].threads) { state.groups[subId][groupId].threads = [ ...state.groups[subId][groupId].threads, thread, ] .filter((thread, index, self) => self.findIndex((t) => t.id === thread.id) === index) .sort((a, b) => a.timestamp - b.timestamp); } else { state.groups[subId][groupId].threads = [thread]; } } else { state.groups[subId][groupId] = { threads: [thread] }; } } else { state.groups[subId] = { [groupId]: { threads: [thread] } }; } })); }, addGroupThreadComment: (subId, groupId, threadComment) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { if (state.groups[subId][groupId].threadComments) { state.groups[subId][groupId].threadComments = [ ...state.groups[subId][groupId].threadComments, threadComment, ] .filter((thread, index, self) => self.findIndex((t) => t.id === thread.id) === index) .sort((a, b) => a.timestamp - b.timestamp); } else { state.groups[subId][groupId].threadComments = [threadComment]; } } else { state.groups[subId][groupId] = { threadComments: [threadComment] }; } } else { state.groups[subId] = { [groupId]: { threadComments: [threadComment] } }; } })); }, addGroupReaction: (subId, groupId, reaction) => { if (!subId || !groupId) return; set(produce((state) => { if (state.groups[subId]) { if (state.groups[subId][groupId]) { if (state.groups[subId][groupId].reactions) { state.groups[subId][groupId].reactions = [ ...state.groups[subId][groupId].reactions, reaction, ] .filter((reaction, index, self) => self.findIndex((t) => t.id === reaction.id) === index) .sort((a, b) => a.timestamp - b.timestamp); } else { state.groups[subId][groupId].reactions = [reaction]; } } else { state.groups[subId][groupId] = { reactions: [reaction] }; } } else { state.groups[subId] = { [groupId]: { reactions: [reaction] } }; } })); }, removeGroupChat: (subId, groupId, chat) => { if (!subId || !groupId) return; set(produce((state) => { if (!state.groups[subId]) return; if (!state.groups[subId][groupId]) return; if (!state.groups[subId][groupId].chats) return; state.groups[subId][groupId].chats = state.groups[subId][groupId].chats.filter((c) => c.id !== chat.id); })); }, removeGroupThread: (subId, groupId, thread) => { if (!subId || !groupId) return; set(produce((state) => { if (!state.groups[subId]) return; if (!state.groups[subId][groupId]) return; if (!state.groups[subId][groupId].threads) return; state.groups[subId][groupId].threads = state.groups[subId][groupId].threads.filter((t) => t.id !== thread.id); })); }, removeGroupThreadComment: (subId, groupId, threadComment) => { if (!subId || !groupId) return; set(produce((state) => { if (!state.groups[subId]) return; if (!state.groups[subId][groupId]) return; if (!state.groups[subId][groupId].threadComments) return; state.groups[subId][groupId].threadComments = state.groups[subId][groupId].threadComments.filter((c) => c.id !== threadComment.id); })); }, removeGroupReaction: (subId, groupId, reaction) => { if (!subId || !groupId) return; set(produce((state) => { if (!state.groups[subId]) return; if (!state.groups[subId][groupId]) return; if (!state.groups[subId][groupId].reactions) return; state.groups[subId][groupId].reactions = state.groups[subId][groupId].reactions.filter((r) => r.id !== reaction.id); })); }, })); //# sourceMappingURL=index.js.map