fix: TypeError cannot read properties of undefined (reading 'click') (#10067)

Fixes https://linear.app/chatwoot/issue/CW-3535/typeerror-cannot-read-properties-of-undefined-reading-click
This commit is contained in:
Sivin Varghese
2024-09-04 11:34:08 +05:30
committed by GitHub
parent a3732c8f51
commit 8a2f652b94

View File

@@ -117,35 +117,41 @@ export default {
lastConversationIndex, lastConversationIndex,
}; };
}; };
const handlePreviousConversation = () => { const handleConversationNavigation = direction => {
const { allConversations, activeConversationIndex } =
getKeyboardListenerParams();
if (activeConversationIndex === -1) {
allConversations[0].click();
}
if (activeConversationIndex >= 1) {
allConversations[activeConversationIndex - 1].click();
}
};
const handleNextConversation = () => {
const { const {
allConversations, allConversations,
activeConversationIndex, activeConversationIndex,
lastConversationIndex, lastConversationIndex,
} = getKeyboardListenerParams(); } = getKeyboardListenerParams();
if (activeConversationIndex === -1) {
allConversations[lastConversationIndex].click(); // Determine the new index based on the direction
} else if (activeConversationIndex < lastConversationIndex) { const newIndex =
allConversations[activeConversationIndex + 1].click(); direction === 'previous'
? activeConversationIndex - 1
: activeConversationIndex + 1;
// Check if the new index is within the valid range
if (
allConversations.length > 0 &&
newIndex >= 0 &&
newIndex <= lastConversationIndex
) {
// Click the conversation at the new index
allConversations[newIndex].click();
} else if (allConversations.length > 0) {
// If the new index is out of range, click the first or last conversation based on the direction
const fallbackIndex =
direction === 'previous' ? 0 : lastConversationIndex;
allConversations[fallbackIndex].click();
} }
}; };
const keyboardEvents = { const keyboardEvents = {
'Alt+KeyJ': { 'Alt+KeyJ': {
action: () => handlePreviousConversation(), action: () => handleConversationNavigation('previous'),
allowOnFocusedInput: true, allowOnFocusedInput: true,
}, },
'Alt+KeyK': { 'Alt+KeyK': {
action: () => handleNextConversation(), action: () => handleConversationNavigation('next'),
allowOnFocusedInput: true, allowOnFocusedInput: true,
}, },
}; };