completed basic Chat
This commit is contained in:
@@ -8,16 +8,22 @@ import {
|
|||||||
getDispatcher,
|
getDispatcher,
|
||||||
} from "(dispatch)/_components/navbar/_components/action";
|
} from "(dispatch)/_components/navbar/_components/action";
|
||||||
import { cn } from "helpers/cn";
|
import { cn } from "helpers/cn";
|
||||||
import { socket } from "(dispatch)/socket";
|
|
||||||
import { ChatMessage } from "@repo/db";
|
|
||||||
|
|
||||||
export const Chat = () => {
|
export const Chat = () => {
|
||||||
const { sendMessage, addChat, chats, addMessage, setOwnId } = useChatStore();
|
const {
|
||||||
|
chatOpen,
|
||||||
|
setChatOpen,
|
||||||
|
sendMessage,
|
||||||
|
addChat,
|
||||||
|
chats,
|
||||||
|
setOwnId,
|
||||||
|
selectedChat,
|
||||||
|
setSelectedChat,
|
||||||
|
setChatNotification,
|
||||||
|
} = useChatStore();
|
||||||
const [sending, setSending] = useState(false);
|
const [sending, setSending] = useState(false);
|
||||||
const session = useSession();
|
const session = useSession();
|
||||||
const [dropdownOpen, setDropdownOpen] = useState(false);
|
|
||||||
const [addTabValue, setAddTabValue] = useState<string>("");
|
const [addTabValue, setAddTabValue] = useState<string>("");
|
||||||
const [selectedTab, setSelectedTab] = useState<string | null>(null);
|
|
||||||
const [message, setMessage] = useState<string>("");
|
const [message, setMessage] = useState<string>("");
|
||||||
const [dispatcher, setDispatcher] = useState<Dispatcher[] | null>(null);
|
const [dispatcher, setDispatcher] = useState<Dispatcher[] | null>(null);
|
||||||
const timeout = useRef<NodeJS.Timeout | null>(null);
|
const timeout = useRef<NodeJS.Timeout | null>(null);
|
||||||
@@ -30,39 +36,54 @@ export const Chat = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchDispatcher = async () => {
|
const fetchDispatcher = async () => {
|
||||||
const data = await getDispatcher();
|
const data = await getDispatcher();
|
||||||
setDispatcher(data);
|
if (data) {
|
||||||
setAddTabValue(data[0]?.userId || "");
|
const filteredDispatcher = data.filter((user) => {
|
||||||
|
return (
|
||||||
|
user.userId !== session.data?.user.id &&
|
||||||
|
!Object.keys(chats).includes(user.userId)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
setDispatcher(filteredDispatcher);
|
||||||
|
}
|
||||||
|
if (!addTabValue && data[0]) setAddTabValue(data[0].userId);
|
||||||
};
|
};
|
||||||
|
|
||||||
timeout.current = setInterval(() => {
|
timeout.current = setInterval(() => {
|
||||||
fetchDispatcher();
|
fetchDispatcher();
|
||||||
}, 10000);
|
}, 1000);
|
||||||
fetchDispatcher();
|
fetchDispatcher();
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (timeout.current) {
|
if (timeout.current) {
|
||||||
clearInterval(timeout.current);
|
clearInterval(timeout.current);
|
||||||
|
timeout.current = null;
|
||||||
|
console.log("cleared");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, []);
|
}, [addTabValue, chats]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn("dropdown dropdown-center", chatOpen && "dropdown-open")}
|
||||||
"dropdown dropdown-center",
|
|
||||||
dropdownOpen && "dropdown-open",
|
|
||||||
)}
|
|
||||||
>
|
>
|
||||||
<button
|
<div className="indicator">
|
||||||
className="btn btn-soft btn-sm btn-primary"
|
{Object.values(chats).some((c) => c.notification) && (
|
||||||
onClick={() => {
|
<span className="indicator-item status status-info"></span>
|
||||||
console.log("clicked");
|
)}
|
||||||
setDropdownOpen((prev) => !prev);
|
<button
|
||||||
}}
|
className="btn btn-soft btn-sm btn-primary"
|
||||||
>
|
onClick={() => {
|
||||||
<ChatBubbleIcon className="w-4 h-4" />
|
console.log("clicked");
|
||||||
</button>
|
setChatOpen(!chatOpen);
|
||||||
{dropdownOpen && (
|
if (selectedChat) {
|
||||||
|
setChatNotification(selectedChat, false);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ChatBubbleIcon className="w-4 h-4" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{chatOpen && (
|
||||||
<div
|
<div
|
||||||
tabIndex={0}
|
tabIndex={0}
|
||||||
className="dropdown-content card bg-base-200 w-150 shadow-md z-[1100]"
|
className="dropdown-content card bg-base-200 w-150 shadow-md z-[1100]"
|
||||||
@@ -74,9 +95,9 @@ export const Chat = () => {
|
|||||||
value={addTabValue}
|
value={addTabValue}
|
||||||
onChange={(e) => setAddTabValue(e.target.value)}
|
onChange={(e) => setAddTabValue(e.target.value)}
|
||||||
>
|
>
|
||||||
<option key={"default"} disabled={true}>
|
{!dispatcher?.length && (
|
||||||
Wähle einen Chatpartner...
|
<option disabled={true}>Keine Chatpartner gefunden</option>
|
||||||
</option>
|
)}
|
||||||
{dispatcher?.map((user) => (
|
{dispatcher?.map((user) => (
|
||||||
<option key={user.userId} value={user.userId}>
|
<option key={user.userId} value={user.userId}>
|
||||||
{user.name}
|
{user.name}
|
||||||
@@ -91,7 +112,7 @@ export const Chat = () => {
|
|||||||
);
|
);
|
||||||
if (!user) return;
|
if (!user) return;
|
||||||
addChat(addTabValue, user.name);
|
addChat(addTabValue, user.name);
|
||||||
// setSelectedTab(addTabValue);
|
setSelectedChat(addTabValue);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span className="text-xl">+</span>
|
<span className="text-xl">+</span>
|
||||||
@@ -108,11 +129,14 @@ export const Chat = () => {
|
|||||||
name="my_tabs_3"
|
name="my_tabs_3"
|
||||||
className="tab"
|
className="tab"
|
||||||
aria-label={`<${chat.name}>`}
|
aria-label={`<${chat.name}>`}
|
||||||
checked={selectedTab === userId}
|
checked={selectedChat === userId}
|
||||||
|
onClick={() => {
|
||||||
|
setChatNotification(userId, false);
|
||||||
|
}}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
if (e.target.checked) {
|
if (e.target.checked) {
|
||||||
// Handle tab change
|
// Handle tab change
|
||||||
setSelectedTab(userId);
|
setSelectedChat(userId);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@@ -164,10 +188,10 @@ export const Chat = () => {
|
|||||||
className="btn btn-soft join-item"
|
className="btn btn-soft join-item"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setSending(true);
|
|
||||||
if (message.length < 1) return;
|
if (message.length < 1) return;
|
||||||
if (!selectedTab) return;
|
if (!selectedChat) return;
|
||||||
sendMessage(selectedTab, message)
|
setSending(true);
|
||||||
|
sendMessage(selectedChat, message)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
setMessage("");
|
setMessage("");
|
||||||
setSending(false);
|
setSending(false);
|
||||||
|
|||||||
@@ -4,8 +4,16 @@ import { socket } from "(dispatch)/socket";
|
|||||||
|
|
||||||
interface ChatStore {
|
interface ChatStore {
|
||||||
ownId: null | string;
|
ownId: null | string;
|
||||||
|
selectedChat: string | null;
|
||||||
|
chatOpen: boolean;
|
||||||
|
setChatOpen: (open: boolean) => void;
|
||||||
|
setSelectedChat: (chatId: string | null) => void;
|
||||||
setOwnId: (id: string) => void;
|
setOwnId: (id: string) => void;
|
||||||
chats: Record<string, { name: string; messages: ChatMessage[] }>;
|
chats: Record<
|
||||||
|
string,
|
||||||
|
{ name: string; notification: boolean; messages: ChatMessage[] }
|
||||||
|
>;
|
||||||
|
setChatNotification: (userId: string, notification: boolean) => void;
|
||||||
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;
|
||||||
@@ -13,11 +21,14 @@ interface ChatStore {
|
|||||||
|
|
||||||
export const useChatStore = create<ChatStore>((set, get) => ({
|
export const useChatStore = create<ChatStore>((set, get) => ({
|
||||||
ownId: null,
|
ownId: null,
|
||||||
|
chatOpen: false,
|
||||||
|
selectedChat: null,
|
||||||
|
setChatOpen: (open: boolean) => set({ chatOpen: open }),
|
||||||
|
setSelectedChat: (chatId: string | null) => set({ selectedChat: chatId }),
|
||||||
setOwnId: (id: string) => set({ ownId: id }),
|
setOwnId: (id: string) => set({ ownId: id }),
|
||||||
chats: {},
|
chats: {},
|
||||||
sendMessage: (userId: string, message: string) => {
|
sendMessage: (userId: string, message: string) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
console.log("sendMessage", userId, message);
|
|
||||||
socket.emit(
|
socket.emit(
|
||||||
"send-message",
|
"send-message",
|
||||||
{ userId, message },
|
{ userId, message },
|
||||||
@@ -35,22 +46,44 @@ export const useChatStore = create<ChatStore>((set, get) => ({
|
|||||||
set((state) => ({
|
set((state) => ({
|
||||||
chats: {
|
chats: {
|
||||||
...state.chats, // Bestehende Chats beibehalten
|
...state.chats, // Bestehende Chats beibehalten
|
||||||
[userId]: { name, messages: [] }, // Neuen Chat hinzufügen
|
[userId]: { name, notification: false, messages: [] }, // Neuen Chat hinzufügen
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
addMessage: (userId: string, message: ChatMessage) => {
|
setChatNotification: (userId, notification) => {
|
||||||
console.log("addMessage", userId, message);
|
const chat = get().chats[userId];
|
||||||
|
if (!chat) return;
|
||||||
|
console.log("setChatNotification", userId, notification);
|
||||||
set((state) => {
|
set((state) => {
|
||||||
const user = state.chats[userId] || { name: userId, messages: [] };
|
return {
|
||||||
|
chats: {
|
||||||
|
...state.chats,
|
||||||
|
[userId]: {
|
||||||
|
...chat,
|
||||||
|
notification,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
},
|
||||||
|
addMessage: (userId: string, message: ChatMessage) => {
|
||||||
|
set((state) => {
|
||||||
|
const user = state.chats[userId] || {
|
||||||
|
name: userId,
|
||||||
|
messages: [],
|
||||||
|
notification: false,
|
||||||
|
};
|
||||||
const isSender = message.senderId === state.ownId;
|
const isSender = message.senderId === state.ownId;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
selectedChat: state.selectedChat ? state.selectedChat : userId,
|
||||||
chats: {
|
chats: {
|
||||||
...state.chats,
|
...state.chats,
|
||||||
[userId]: {
|
[userId]: {
|
||||||
...user,
|
...user,
|
||||||
name: isSender ? message.receiverName : message.senderName,
|
name: isSender ? message.receiverName : message.senderName,
|
||||||
|
notification:
|
||||||
|
!isSender && (state.selectedChat !== userId || !state.chatOpen),
|
||||||
messages: [...user.messages, message], // Neuen Zustand erzeugen
|
messages: [...user.messages, message], // Neuen Zustand erzeugen
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user