feat: fetch suggestions and scroll if required

This commit is contained in:
Shivam Mishra
2023-07-10 15:04:28 +05:30
parent 0ff3680fea
commit 5b1c89eabb
2 changed files with 49 additions and 1 deletions
@@ -66,6 +66,7 @@
/>
<conversation-label-suggestion
v-if="isEnterprise && isAIIntegrationEnabled"
:suggested-labels="labelSuggestions"
:chat-labels="currentChat.labels"
:conversation-id="currentChat.id"
/>
@@ -151,6 +152,7 @@ export default {
conversationPanel: null,
selectedTweetId: null,
isPopoutReplyBox: false,
labelSuggestions: [],
};
},
@@ -304,6 +306,7 @@ export default {
return;
}
this.fetchAllAttachmentsFromCurrentChat();
this.fetchSuggestions();
this.selectedTweetId = null;
},
},
@@ -316,7 +319,7 @@ export default {
mounted() {
this.addScrollListener();
this.fetchAllAttachmentsFromCurrentChat();
this.fetchIntegrationsIfRequired();
this.fetchSuggestions();
},
beforeDestroy() {
@@ -325,6 +328,18 @@ export default {
},
methods: {
async fetchSuggestions() {
// start empty
this.labelSuggestions = [];
await this.fetchIntegrationsIfRequired();
this.labelSuggestions = await this.fetchLabelSuggestions({
conversationId: this.currentChat.id,
});
this.$nextTick(() => {
this.scrollToBottom();
});
},
fetchAllAttachmentsFromCurrentChat() {
this.$store.dispatch('fetchAllAttachments', this.currentChat.id);
},
@@ -370,17 +385,24 @@ export default {
},
scrollToBottom() {
let relevantMessages = [];
let labelSuggestions = this.conversationPanel.querySelector(
'.label-suggestion'
);
// if there are unread messages, scroll to the first unread message
if (this.unreadMessageCount > 0) {
// capturing only the unread messages
relevantMessages = this.conversationPanel.querySelectorAll(
'.message--unread'
);
} else if (labelSuggestions) {
relevantMessages = [labelSuggestions];
} else {
// capturing last message from the messages list
relevantMessages = Array.from(
this.conversationPanel.querySelectorAll('.message--read')
).slice(-1);
}
this.conversationPanel.scrollTop = calculateScrollTop(
this.conversationPanel.scrollHeight,
this.$el.scrollHeight,
@@ -1,5 +1,6 @@
import { mapGetters } from 'vuex';
import { OPEN_AI_EVENTS } from '../helper/AnalyticsHelper/events';
import OpenAPI from '../api/integrations/openapi';
export default {
mounted() {
@@ -33,5 +34,30 @@ export default {
});
}
},
async fetchLabelSuggestions({ conversationId }) {
try {
const result = await OpenAPI.processEvent({
type: 'label_suggestion',
hookId: this.hookId,
conversationId: conversationId,
});
const {
data: { message: labels },
} = result;
return this.cleanLabels(labels);
} catch (error) {
return [];
}
},
cleanLabels(labels) {
return labels
.toLowerCase() // Set it to lowercase
.split(',') // split the string into an array
.filter(label => label.trim()) // remove any empty strings
.filter((label, index, self) => self.indexOf(label) === index) // remove any duplicates
.map(label => label.trim()); // trim the words
},
},
};