.
This commit is contained in:
@@ -1,19 +1,8 @@
|
||||
import type { QueryClient } from "@tanstack/react-query";
|
||||
import { create } from "zustand";
|
||||
import {
|
||||
messageSchema,
|
||||
type ChannelId,
|
||||
type Message,
|
||||
type MessageId,
|
||||
type ServerId,
|
||||
} from "~/lib/api/types";
|
||||
import { messageSchema, type ChannelId, type Message, type MessageId, type ServerId } from "~/lib/api/types";
|
||||
import { GatewayClient } from "~/lib/websocket/gateway/client";
|
||||
import {
|
||||
ConnectionState,
|
||||
EventType,
|
||||
type EventData,
|
||||
type VoiceServerUpdateEvent,
|
||||
} from "~/lib/websocket/gateway/types";
|
||||
import { ConnectionState, EventType, type EventData, type VoiceServerUpdateEvent } from "~/lib/websocket/gateway/types";
|
||||
import { useChannelsVoiceStateStore } from "./channels-voice-state";
|
||||
import { usePrivateChannelsStore } from "./private-channels-store";
|
||||
import { useServerChannelsStore } from "./server-channels-store";
|
||||
@@ -23,10 +12,7 @@ import { useUsersStore } from "./users-store";
|
||||
const GATEWAY_URL = "ws://localhost:12345/gateway/ws";
|
||||
|
||||
const HANDLERS = {
|
||||
[EventType.ADD_SERVER]: (
|
||||
self: GatewayState,
|
||||
data: Extract<EventData, { type: EventType.ADD_SERVER }>["data"],
|
||||
) => {
|
||||
[EventType.ADD_SERVER]: (self: GatewayState, data: Extract<EventData, { type: EventType.ADD_SERVER }>["data"]) => {
|
||||
useServerListStore.getState().addServer(data.server);
|
||||
},
|
||||
|
||||
@@ -59,31 +45,20 @@ const HANDLERS = {
|
||||
|
||||
[EventType.ADD_SERVER_CHANNEL]: (
|
||||
self: GatewayState,
|
||||
data: Extract<
|
||||
EventData,
|
||||
{ type: EventType.ADD_SERVER_CHANNEL }
|
||||
>["data"],
|
||||
data: Extract<EventData, { type: EventType.ADD_SERVER_CHANNEL }>["data"],
|
||||
) => {
|
||||
useServerChannelsStore.getState().addChannel(data.channel);
|
||||
},
|
||||
|
||||
[EventType.REMOVE_SERVER_CHANNEL]: (
|
||||
self: GatewayState,
|
||||
data: Extract<
|
||||
EventData,
|
||||
{ type: EventType.REMOVE_SERVER_CHANNEL }
|
||||
>["data"],
|
||||
data: Extract<EventData, { type: EventType.REMOVE_SERVER_CHANNEL }>["data"],
|
||||
) => {
|
||||
useServerChannelsStore
|
||||
.getState()
|
||||
.removeChannel(data.serverId, data.channelId);
|
||||
useServerChannelsStore.getState().removeChannel(data.serverId, data.channelId);
|
||||
useChannelsVoiceStateStore.getState().removeChannel(data.serverId);
|
||||
},
|
||||
|
||||
[EventType.ADD_USER]: (
|
||||
self: GatewayState,
|
||||
data: Extract<EventData, { type: EventType.ADD_USER }>["data"],
|
||||
) => {
|
||||
[EventType.ADD_USER]: (self: GatewayState, data: Extract<EventData, { type: EventType.ADD_USER }>["data"]) => {
|
||||
useUsersStore.getState().addUser(data.user);
|
||||
},
|
||||
|
||||
@@ -103,10 +78,7 @@ const HANDLERS = {
|
||||
|
||||
[EventType.REMOVE_SERVER_MEMBER]: (
|
||||
self: GatewayState,
|
||||
data: Extract<
|
||||
EventData,
|
||||
{ type: EventType.REMOVE_SERVER_MEMBER }
|
||||
>["data"],
|
||||
data: Extract<EventData, { type: EventType.REMOVE_SERVER_MEMBER }>["data"],
|
||||
) => {
|
||||
useUsersStore.getState().removeUser(data.userId);
|
||||
},
|
||||
@@ -123,15 +95,9 @@ const HANDLERS = {
|
||||
(oldData: { pages: Message[][]; pageParams: MessageId[] }) => {
|
||||
return {
|
||||
pages: oldData?.pages
|
||||
? [
|
||||
[message, ...oldData.pages[0]],
|
||||
...oldData.pages.slice(1),
|
||||
]
|
||||
? [[message, ...oldData.pages[0]], ...oldData.pages.slice(1)]
|
||||
: [[message]],
|
||||
pageParams: oldData?.pageParams ?? [
|
||||
undefined,
|
||||
message.id,
|
||||
],
|
||||
pageParams: oldData?.pageParams ?? [undefined, message.id],
|
||||
};
|
||||
},
|
||||
);
|
||||
@@ -143,43 +109,28 @@ const HANDLERS = {
|
||||
data: Extract<EventData, { type: EventType.REMOVE_MESSAGE }>["data"],
|
||||
) => {
|
||||
if (self.queryClient) {
|
||||
self.queryClient.setQueryData(
|
||||
["messages", data.channelId],
|
||||
(oldData: any) => {
|
||||
if (!oldData) return [];
|
||||
return oldData.filter(
|
||||
(message: any) => message.id !== data.messageId,
|
||||
);
|
||||
},
|
||||
);
|
||||
self.queryClient.setQueryData(["messages", data.channelId], (oldData: any) => {
|
||||
if (!oldData) return [];
|
||||
return oldData.filter((message: any) => message.id !== data.messageId);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
[EventType.VOICE_CHANNEL_CONNECTED]: (
|
||||
self: GatewayState,
|
||||
data: Extract<
|
||||
EventData,
|
||||
{ type: EventType.VOICE_CHANNEL_CONNECTED }
|
||||
>["data"],
|
||||
data: Extract<EventData, { type: EventType.VOICE_CHANNEL_CONNECTED }>["data"],
|
||||
) => {
|
||||
useChannelsVoiceStateStore
|
||||
.getState()
|
||||
.addUser(data.channelId, data.userId, {
|
||||
deaf: false,
|
||||
muted: false,
|
||||
});
|
||||
useChannelsVoiceStateStore.getState().addUser(data.channelId, data.userId, {
|
||||
deaf: false,
|
||||
muted: false,
|
||||
});
|
||||
},
|
||||
|
||||
[EventType.VOICE_CHANNEL_DISCONNECTED]: (
|
||||
self: GatewayState,
|
||||
data: Extract<
|
||||
EventData,
|
||||
{ type: EventType.VOICE_CHANNEL_DISCONNECTED }
|
||||
>["data"],
|
||||
data: Extract<EventData, { type: EventType.VOICE_CHANNEL_DISCONNECTED }>["data"],
|
||||
) => {
|
||||
useChannelsVoiceStateStore
|
||||
.getState()
|
||||
.removeUser(data.channelId, data.userId);
|
||||
useChannelsVoiceStateStore.getState().removeUser(data.channelId, data.userId);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -195,19 +146,13 @@ interface GatewayState {
|
||||
|
||||
updateVoiceState: (serverId: ServerId, channelId: ChannelId) => void;
|
||||
requestVoiceStates: (serverId: ServerId) => void;
|
||||
onVoiceServerUpdate: (
|
||||
handler: (
|
||||
event: VoiceServerUpdateEvent["data"],
|
||||
) => void | Promise<void>,
|
||||
) => () => void;
|
||||
onVoiceServerUpdate: (handler: (event: VoiceServerUpdateEvent["data"]) => void | Promise<void>) => () => void;
|
||||
}
|
||||
|
||||
export const useGatewayStore = create<GatewayState>()((set, get) => {
|
||||
const client = new GatewayClient(GATEWAY_URL);
|
||||
|
||||
const voiceHandlers = new Set<
|
||||
(event: VoiceServerUpdateEvent["data"]) => void
|
||||
>();
|
||||
const voiceHandlers = new Set<(event: VoiceServerUpdateEvent["data"]) => void>();
|
||||
|
||||
client.onEvent(EventType.VOICE_SERVER_UPDATE, (event) => {
|
||||
voiceHandlers.forEach((handler) => handler(event));
|
||||
|
||||
Reference in New Issue
Block a user