From 300d8e182317e44fe83376044e543058ad087c42 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Dec 2022 12:55:36 -0600 Subject: fix menu --- src/web/src/components/Chat/ChatMenu.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/Chat/ChatMenu.js b/src/web/src/components/Chat/ChatMenu.js index 971646f4..3dd91763 100644 --- a/src/web/src/components/Chat/ChatMenu.js +++ b/src/web/src/components/Chat/ChatMenu.js @@ -8,12 +8,6 @@ import SendMessageModal from './SendMessageModal'; const ChatMenu = ({ conversations, active, onConversationChange, ...rest }) => { const names = Object.keys(conversations); - - const unread = Object.entries(conversations).reduce((acc, [name, messages]) => { - acc[name] = messages.filter(message => !message.acknowledged); - return acc; - }, {}); - const isActive = (name) => active === name; return ( @@ -28,8 +22,8 @@ const ChatMenu = ({ conversations, active, onConversationChange, ...rest }) => { > {name} - {(unread[name] || []).length === 0 ? '' : - + {conversations[name].hasUnAcknowledgedMessages && + } )} -- cgit v1.2.3 From 9176bbb14ee35e4bc12c5ff947fcf8bdbf2bb090 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Dec 2022 15:08:36 -0600 Subject: add get() --- src/web/src/lib/chat.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/web') diff --git a/src/web/src/lib/chat.js b/src/web/src/lib/chat.js index 2b31bcd8..31edcc8b 100644 --- a/src/web/src/lib/chat.js +++ b/src/web/src/lib/chat.js @@ -4,6 +4,10 @@ export const getAll = async () => { return (await api.get('/conversations')).data; }; +export const get = async ({ username }) => { + return (await api.get(`/conversations/${encodeURIComponent(username)}`)).data; +}; + export const acknowledge = ({ username }) => { return api.put(`/conversations/${encodeURIComponent(username)}`); }; -- cgit v1.2.3 From 47edf3ca8bca0ddb0b298091bac856d508ecc25b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Dec 2022 15:08:51 -0600 Subject: pass state to Chat --- src/web/src/components/App.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index df65bc20..70d154af 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -334,8 +334,12 @@ class App extends Component { )} /> this.withTokenCheck()}/> - this.withTokenCheck()}/> - this.withTokenCheck()}/> + + this.withTokenCheck()} + /> + + this.withTokenCheck()} + /> this.withTokenCheck()}/> -- cgit v1.2.3 From f8dc73ac2fb26a9f37cfea899e47e2fbc68a7d6c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Dec 2022 15:09:05 -0600 Subject: fix all the broken stuff --- src/web/src/components/Chat/Chat.js | 74 +++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 24 deletions(-) (limited to 'src/web') diff --git a/src/web/src/components/Chat/Chat.js b/src/web/src/components/Chat/Chat.js index 12840e4a..e9ff0be9 100644 --- a/src/web/src/components/Chat/Chat.js +++ b/src/web/src/components/Chat/Chat.js @@ -43,41 +43,55 @@ class Chat extends Component { }; fetchConversations = async () => { - const { active } = this.state; + // fetch all of the active conversations (but no messages) let conversations = await chat.getAll(); - - const unAckedActiveMessages = (conversations[active] || []) - .filter(message => !message.acknowledged); - - if (unAckedActiveMessages.length > 0) { - await this.acknowledgeMessages(active, { force: true }); + + // turn into a map, keyed by username + // if there are no active conversations, set to an empty map + if (conversations.length === 0) { + conversations = {}; + } else { + conversations = conversations.reduce((map, current) => { + map[current.username] = current; + return map; + }, {}); + } + + const { active } = this.state; + const activeConversation = conversations[active]; + + // check to see if the active chat is still active + // this will happen whenever a chat is closed/removed + if (activeConversation) { + console.log('active?', activeConversation); + // *before* fetching messages, ack any unacked messages + // for the active chat + if (activeConversation.hasUnAcknowledgedMessages === true) { + await this.acknowledgeMessages(active); + } + conversations = { - ...conversations, - [active]: conversations[active].map(message => ({...message, acknowledged: true })), + ...conversations, + [active]: await chat.get({ username: active }), }; } this.setState({ conversations }, () => { + // if a chat isn't active or the active chat is closed, + // select the first conversation, if there is one if (!this.state.conversations[this.state.active]) { this.selectConversation(this.getFirstConversation()); - } else { - this.acknowledgeMessages(this.state.active); } }); }; - acknowledgeMessages = async (username, { force = false } = {}) => { - if (!username) return; - - const unAckedMessages = (this.state.conversations[username] || []) - .filter(message => !message.acknowledged); - - if (!force && unAckedMessages.length === 0) return; - + acknowledgeMessages = async (username) => { + if (!username || username === '') return; await chat.acknowledge({ username }); }; sendMessage = async (username, message) => { + if (!username || !message || username === '') return; await chat.send({ username, message }); }; @@ -91,6 +105,10 @@ class Chat extends Component { await this.sendMessage(active, message); this.messageRef.current.value = ''; + + // force a refresh to append the message + // we could probably do this in the browser but we can be lazy + this.fetchConversations(); }; validInput = () => @@ -118,11 +136,17 @@ class Chat extends Component { active: username, loading: true, }, async () => { - const { active } = this.state; + const { active, conversations } = this.state; sessionStorage.setItem(activeChatKey, active); - this.setState({ loading: false }, () => { + this.setState({ + loading: false, + conversations: active === '' ? conversations : { + ...conversations, + [active]: await chat.get({ username: active }), + }, + }, () => { try { this.listRef.current.lastChild.scrollIntoView(); } catch { @@ -146,7 +170,8 @@ class Chat extends Component { render = () => { const { conversations = [], active, loading } = this.state; - const messages = conversations[active] || []; + const messages = conversations[active]?.messages || []; + const { user } = this.props.state; return (
@@ -183,10 +208,11 @@ class Chat extends Component { {messages.map((message, index) => {this.formatTimestamp(message.timestamp)} - {message.username}: + + {message.direction === 'Out' ? user.username : message.username}: {message.message} )} -- cgit v1.2.3