fixes #118
This commit is contained in:
@@ -10,9 +10,11 @@ import { getConnectedDispatcherAPI } from "_querys/dispatcher";
|
|||||||
import { getConnectedAircraftsAPI } from "_querys/aircrafts";
|
import { getConnectedAircraftsAPI } from "_querys/aircrafts";
|
||||||
import { useDispatchConnectionStore } from "_store/dispatch/connectionStore";
|
import { useDispatchConnectionStore } from "_store/dispatch/connectionStore";
|
||||||
import { usePilotConnectionStore } from "_store/pilot/connectionStore";
|
import { usePilotConnectionStore } from "_store/pilot/connectionStore";
|
||||||
|
import { Trash } from "lucide-react";
|
||||||
|
|
||||||
export const Chat = () => {
|
export const Chat = () => {
|
||||||
const {
|
const {
|
||||||
|
removeChat,
|
||||||
setReportTabOpen,
|
setReportTabOpen,
|
||||||
chatOpen,
|
chatOpen,
|
||||||
setChatOpen,
|
setChatOpen,
|
||||||
@@ -50,13 +52,21 @@ export const Chat = () => {
|
|||||||
setOwnId(session.data?.user.id);
|
setOwnId(session.data?.user.id);
|
||||||
}, [session.data?.user.id, setOwnId]);
|
}, [session.data?.user.id, setOwnId]);
|
||||||
|
|
||||||
const filteredDispatcher = dispatcher?.filter((d) => d.userId !== session.data?.user.id);
|
const filteredDispatcher = dispatcher?.filter(
|
||||||
|
(d) => d.userId !== session.data?.user.id && !chats[d.userId],
|
||||||
|
);
|
||||||
const filteredAircrafts = aircrafts?.filter(
|
const filteredAircrafts = aircrafts?.filter(
|
||||||
(a) => a.userId !== session.data?.user.id && dispatcherConnected,
|
(a) => a.userId !== session.data?.user.id && dispatcherConnected && chats[a.userId],
|
||||||
);
|
);
|
||||||
|
|
||||||
const btnActive = pilotConnected || dispatcherConnected;
|
const btnActive = pilotConnected || dispatcherConnected;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!filteredDispatcher?.length && !filteredAircrafts?.length) {
|
||||||
|
setAddTabValue("default");
|
||||||
|
}
|
||||||
|
}, [filteredDispatcher, filteredAircrafts]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!btnActive) {
|
if (!btnActive) {
|
||||||
setChatOpen(false);
|
setChatOpen(false);
|
||||||
@@ -146,13 +156,17 @@ export const Chat = () => {
|
|||||||
<button
|
<button
|
||||||
className="btn btn-sm btn-soft btn-primary join-item"
|
className="btn btn-sm btn-soft btn-primary join-item"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
if (addTabValue === "default") return;
|
||||||
const aircraftUser = aircrafts?.find((a) => a.userId === addTabValue);
|
const aircraftUser = aircrafts?.find((a) => a.userId === addTabValue);
|
||||||
const dispatcherUser = dispatcher?.find((d) => d.userId === addTabValue);
|
const dispatcherUser = dispatcher?.find((d) => d.userId === addTabValue);
|
||||||
const user = aircraftUser || dispatcherUser;
|
const user = aircraftUser || dispatcherUser;
|
||||||
|
console.log("Adding chat for user:", addTabValue, user);
|
||||||
if (!user) return;
|
if (!user) return;
|
||||||
const role = "Station" in user ? user.Station.bosCallsignShort : user.zone;
|
const role = "Station" in user ? user.Station.bosCallsignShort : user.zone;
|
||||||
|
console.log("Adding chat for user:", addTabValue);
|
||||||
addChat(addTabValue, `${asPublicUser(user.publicUser).fullName} (${role})`);
|
addChat(addTabValue, `${asPublicUser(user.publicUser).fullName} (${role})`);
|
||||||
setSelectedChat(addTabValue);
|
setSelectedChat(addTabValue);
|
||||||
|
setAddTabValue("default");
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span className="text-xl">+</span>
|
<span className="text-xl">+</span>
|
||||||
@@ -164,7 +178,7 @@ export const Chat = () => {
|
|||||||
if (!chat) return null;
|
if (!chat) return null;
|
||||||
return (
|
return (
|
||||||
<Fragment key={userId}>
|
<Fragment key={userId}>
|
||||||
<a
|
<div
|
||||||
className={cn("indicator tab", selectedChat === userId && "tab-active")}
|
className={cn("indicator tab", selectedChat === userId && "tab-active")}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (selectedChat === userId) {
|
if (selectedChat === userId) {
|
||||||
@@ -176,7 +190,7 @@ export const Chat = () => {
|
|||||||
>
|
>
|
||||||
{chat.name}
|
{chat.name}
|
||||||
{chat.notification && <span className="indicator-item status status-info" />}
|
{chat.notification && <span className="indicator-item status status-info" />}
|
||||||
</a>
|
</div>
|
||||||
<div className="tab-content bg-base-100 border-base-300 max-h-[250px] overflow-y-auto p-6">
|
<div className="tab-content bg-base-100 border-base-300 max-h-[250px] overflow-y-auto p-6">
|
||||||
{/* So macht man kein overflow handeling, weiß ich. Aber es funktioniert... */}
|
{/* So macht man kein overflow handeling, weiß ich. Aber es funktioniert... */}
|
||||||
{chat.messages.map((chatMessage) => {
|
{chat.messages.map((chatMessage) => {
|
||||||
@@ -208,6 +222,16 @@ export const Chat = () => {
|
|||||||
)}
|
)}
|
||||||
{selectedChat && (
|
{selectedChat && (
|
||||||
<div className="join">
|
<div className="join">
|
||||||
|
<button
|
||||||
|
className="join-item btn btn-error btn-outline"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
removeChat(selectedChat);
|
||||||
|
}}
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
<Trash size={16} />
|
||||||
|
</button>
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<label className="input join-item w-full">
|
<label className="input join-item w-full">
|
||||||
<input
|
<input
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ interface ChatStore {
|
|||||||
sendMessage: (userId: string, message: string) => Promise<void>;
|
sendMessage: (userId: string, message: string) => Promise<void>;
|
||||||
addChat: (userId: string, name: string) => void;
|
addChat: (userId: string, name: string) => void;
|
||||||
addMessage: (userId: string, message: ChatMessage) => void;
|
addMessage: (userId: string, message: ChatMessage) => void;
|
||||||
|
removeChat: (userId: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useLeftMenuStore = create<ChatStore>((set, get) => ({
|
export const useLeftMenuStore = create<ChatStore>((set, get) => ({
|
||||||
@@ -37,6 +38,15 @@ export const useLeftMenuStore = create<ChatStore>((set, get) => ({
|
|||||||
setChatNotification(chatId, false); // Set notification to false when chat is selected
|
setChatNotification(chatId, false); // Set notification to false when chat is selected
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
removeChat: (userId: string) => {
|
||||||
|
const { chats, setSelectedChat, selectedChat } = get();
|
||||||
|
const newChats = { ...chats };
|
||||||
|
delete newChats[userId];
|
||||||
|
set({ chats: newChats });
|
||||||
|
if (selectedChat === userId) {
|
||||||
|
setSelectedChat(null);
|
||||||
|
}
|
||||||
|
},
|
||||||
setOwnId: (id: string) => set({ ownId: id }),
|
setOwnId: (id: string) => set({ ownId: id }),
|
||||||
chats: {},
|
chats: {},
|
||||||
sendMessage: (userId: string, message: string) => {
|
sendMessage: (userId: string, message: string) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user