diff --git a/src/components/HotelContext.jsx b/src/components/HotelContext.jsx index 8029644..18c29f1 100644 --- a/src/components/HotelContext.jsx +++ b/src/components/HotelContext.jsx @@ -15,9 +15,10 @@ export function useHotel() { export function HotelProvider({ accessToken, children, resClientId}) { const [rooms, setRooms] = useState([]); const [conversations, setConversations] = useState([]); + const [messagesByConvId, setMessagesByConvId] = useState({}); const [users, setUsers] = useState([]); - const [usersById, setUsersById] = useState([]); + const [usersById, setUsersById] = useState({}); const [clientId, setClientId] = useState(resClientId|| null); const tokens = JSON.parse(accessToken); @@ -72,6 +73,16 @@ export function HotelProvider({ accessToken, children, resClientId}) { } + function appendMessage(msg) { + setMessagesByConvId(prev => ({ + ...prev, + [msg.conv_id]: [ + ...(prev[msg.conv_id] ?? []), + msg, + ], + })); +} + async function fetchConversations() { const res = await fetch(`${API_BASE}/chat/get_conv`, { method: "POST", @@ -105,7 +116,16 @@ export function HotelProvider({ accessToken, children, resClientId}) { body: JSON.stringify(payload), }); - return res.json(); + const messages = await res.json(); + + setMessagesByConvId(prev => ({ + ...prev, + [conv_id]: messages, + + })); + + return messages; + //return res.json } @@ -178,6 +198,11 @@ export function HotelProvider({ accessToken, children, resClientId}) { } + async function preloadMessages(conversations) { + for (const conv of conversations) { + await fetchMessages({ conv_id: conv.id }); + } +} // INVENTORY @@ -255,6 +280,11 @@ export function HotelProvider({ accessToken, children, resClientId}) { break; } + case "chat_message": { + appendMessage(msg); + break; + } + default: console.warn("Unhandled WS message bruuuuh:", msg); } @@ -274,19 +304,30 @@ export function HotelProvider({ accessToken, children, resClientId}) { fetchRooms(), fetchConversations(), fetchHotelUsers(), + ]); - + setClientId(resClientId); setRooms(roomsData); setConversations(convData); - //console.log("USERS =",users) setUsers(usersData); + + + } load(); + + //console.log("USERS 2 =",usersById) }, []); + useEffect(() => { + if (conversations.length === 0) return; + + preloadMessages(conversations); +}, [conversations]); + useEffect(() => { //if (!accessTokenSingle || !clientId) return; @@ -327,6 +368,7 @@ export function HotelProvider({ accessToken, children, resClientId}) { value={{ rooms, conversations, + messagesByConvId, users, usersById, clientId, diff --git a/src/components/widget/chatWidget.jsx b/src/components/widget/chatWidget.jsx index 22a01cf..433da1f 100644 --- a/src/components/widget/chatWidget.jsx +++ b/src/components/widget/chatWidget.jsx @@ -20,7 +20,7 @@ export default function ChatWidget({convlist}) { const [activeConvId, setActiveConvId] = useState(null); const [showAddUsers, setShowAddUsers] = useState(false); const [showCreateConv, setShowCreateConv] = useState(false); - + //FIXME: does this make multiple re render ? const handleOpenConv = async (conv_id) => { setActiveConvId(conv_id); }; @@ -207,44 +207,36 @@ function ConvCard({id, title, onOpenConv}) { ) } -function MessagesBox({ conv_id, fetchMessages, usersById, clientId }) { +function MessagesBox({ conv_id }) { + const { messagesByConvId, usersById, clientId } = useHotel(); + const messages = messagesByConvId[conv_id] ?? []; - const [messages, setMessages] = useState([]); - - useEffect(() => { - async function load() { - const msg = await fetchMessages({ conv_id }); - setMessages(msg); - } - load(); - }, [conv_id]); + console.log(messagesByConvId); + console.log(usersById); - if (!messages || messages.length === 0) { - return
No messages
- } - //console.log("FETCH RESULT =", messages); - return ( - -
- {messages - .slice() // don’t mutate state - .sort((a, b) => new Date(a.sent_at) - new Date(b.sent_at)) - .map(msg => { - const isMine = msg.sender_id === clientId + if (messages.length === 0) { + return
No messages
; + } - return( -
-

+ return ( +

+ {messages + .slice() + .sort((a, b) => new Date(a.sent_at) - new Date(b.sent_at)) + .map(msg => { + const isMine = msg.sender_id === clientId; + return ( +
+

Sender:{" "} {usersById[msg.sender_id] ?? "Unknown user"} -

-

{msg.content}

- {msg.sent_at} -
- ) - })} -
- - ); +

+ +

{msg.content}

+
+ ); + })} +
+ ); } \ No newline at end of file