From 939c0d8784b01ee7100ad4a9d03452531ed47714 Mon Sep 17 00:00:00 2001 From: MeowcaTheoRange Date: Thu, 9 May 2024 17:20:16 -0500 Subject: [PATCH] Message indicators --- src/app/chat/page.tsx | 31 ++++++++++++++++++++++++++-- src/layout/ChatLayout/ChatLayout.tsx | 13 +++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/app/chat/page.tsx b/src/app/chat/page.tsx index 4113bd7..5dbd6dc 100644 --- a/src/app/chat/page.tsx +++ b/src/app/chat/page.tsx @@ -91,13 +91,18 @@ export default function Home() { const [ whoami, _setWhoAmI ] = useState<{[key:string]:any}>({}); const whoamiRef = useRef(whoami); const setWhoAmI = (x:any) => {whoamiRef.current = x;_setWhoAmI(x);}; - const [ channel, setChannel ] = useState(""); + const [ channel, _setChannel ] = useState(""); + const channelRef = useRef(channel); + const setChannel = (x:any) => {channelRef.current = x;_setChannel(x);}; const [ channels, setChannels ] = useState<{ name: string, action: () => void, readable: boolean, writable: boolean }[]>([]); + const [ channelIndicators, _setChannelIndicators ] = useState<{[key:string]:boolean}>({}); + const channelIndicatorsRef = useRef(channelIndicators); + const setChannelIndicators = (x:any) => {channelIndicatorsRef.current = x;_setChannelIndicators(x);}; const [ users, _setUsers ] = useState<{ id: string, username: string, @@ -278,6 +283,23 @@ export default function Home() { if (index < 0) return; setMessages(messagesRef.current.toSpliced(index, 1)); } + + function addChannelIndicator(channel:string) { + const CIs = { + ...channelIndicatorsRef.current + }; + CIs[channel] = true; + setChannelIndicators(CIs); + } + + function removeChannelIndicator(channel:string) { + const CIs = { + ...channelIndicatorsRef.current + }; + delete CIs[channel]; + setChannelIndicators(CIs); + } + function messagesChatRes({ type, spec, data }:Res) { unsetError(); if (type.includes("ERR")) { @@ -309,7 +331,10 @@ export default function Home() { socket.off("RES_CHAT_MESSAGE"); socket.on("RES_CHAT_MESSAGE", ({ message }) => { - addMessage(message); + if (message.channel == channelRef.current) + addMessage(message); + else + addChannelIndicator(message.channel); }); socket.off("RES_CHAT_RMMESSAGE"); @@ -352,6 +377,7 @@ export default function Home() { } else { prevscrtop.current = 0; setChannel(data.channel); + removeChannelIndicator(data.channel); socket.emit('SIG_CHAT_USERS', {}, usersChatRes); socket.emit('SIG_CHAT_MESSAGES', {}, messagesChatRes); } @@ -400,6 +426,7 @@ export default function Home() { subtitle={error} props={{ channels, + channelIndicators, users, currentChannel: channel, connected, diff --git a/src/layout/ChatLayout/ChatLayout.tsx b/src/layout/ChatLayout/ChatLayout.tsx index ec3f046..acd3b7e 100644 --- a/src/layout/ChatLayout/ChatLayout.tsx +++ b/src/layout/ChatLayout/ChatLayout.tsx @@ -26,6 +26,7 @@ export function ChatLayout({ readable: boolean, writable: boolean }[], + channelIndicators: {[key:string]:boolean}, users: { id: string, username: string, @@ -66,7 +67,17 @@ export function ChatLayout({ title={`#${channel.name}`} >
- tag + + + arrow_right + + + + tag + {channel.name}
))}