Situation Board WIP #28
This commit is contained in:
@@ -4,6 +4,8 @@ import { dispatchSocket } from "dispatch/socket";
|
||||
import { pilotSocket } from "pilot/socket";
|
||||
|
||||
interface ChatStore {
|
||||
situationTabOpen: boolean;
|
||||
setSituationTabOpen: (open: boolean) => void;
|
||||
reportTabOpen: boolean;
|
||||
setReportTabOpen: (open: boolean) => void;
|
||||
ownId: null | string;
|
||||
@@ -12,10 +14,7 @@ interface ChatStore {
|
||||
setChatOpen: (open: boolean) => void;
|
||||
setSelectedChat: (chatId: string | null) => void;
|
||||
setOwnId: (id: string) => void;
|
||||
chats: Record<
|
||||
string,
|
||||
{ name: string; notification: boolean; messages: ChatMessage[] }
|
||||
>;
|
||||
chats: Record<string, { name: string; notification: boolean; messages: ChatMessage[] }>;
|
||||
setChatNotification: (userId: string, notification: boolean) => void;
|
||||
sendMessage: (userId: string, message: string) => Promise<void>;
|
||||
addChat: (userId: string, name: string) => void;
|
||||
@@ -23,6 +22,8 @@ interface ChatStore {
|
||||
}
|
||||
|
||||
export const useLeftMenuStore = create<ChatStore>((set, get) => ({
|
||||
situationTabOpen: false,
|
||||
setSituationTabOpen: (open: boolean) => set({ situationTabOpen: open }),
|
||||
reportTabOpen: false,
|
||||
setReportTabOpen: (open: boolean) => set({ reportTabOpen: open }),
|
||||
ownId: null,
|
||||
@@ -34,17 +35,13 @@ export const useLeftMenuStore = create<ChatStore>((set, get) => ({
|
||||
chats: {},
|
||||
sendMessage: (userId: string, message: string) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
dispatchSocket.emit(
|
||||
"send-message",
|
||||
{ userId, message },
|
||||
({ error }: { error?: string }) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
},
|
||||
);
|
||||
dispatchSocket.emit("send-message", { userId, message }, ({ error }: { error?: string }) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
addChat: (userId, name) => {
|
||||
@@ -87,8 +84,7 @@ export const useLeftMenuStore = create<ChatStore>((set, get) => ({
|
||||
[userId]: {
|
||||
...user,
|
||||
name: isSender ? message.receiverName : message.senderName,
|
||||
notification:
|
||||
!isSender && (state.selectedChat !== userId || !state.chatOpen),
|
||||
notification: !isSender && (state.selectedChat !== userId || !state.chatOpen),
|
||||
messages: [...user.messages, message], // Neuen Zustand erzeugen
|
||||
},
|
||||
},
|
||||
@@ -106,12 +102,9 @@ dispatchSocket.on(
|
||||
store.addMessage(userId, message);
|
||||
},
|
||||
);
|
||||
pilotSocket.on(
|
||||
"chat-message",
|
||||
({ userId, message }: { userId: string; message: ChatMessage }) => {
|
||||
const store = useLeftMenuStore.getState();
|
||||
console.log("chat-message", userId, message);
|
||||
// Update the chat store with the new message
|
||||
store.addMessage(userId, message);
|
||||
},
|
||||
);
|
||||
pilotSocket.on("chat-message", ({ userId, message }: { userId: string; message: ChatMessage }) => {
|
||||
const store = useLeftMenuStore.getState();
|
||||
console.log("chat-message", userId, message);
|
||||
// Update the chat store with the new message
|
||||
store.addMessage(userId, message);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user