mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 08:20:14 +01:00
🐞 fix: Prevent Type Error in Successful Bookmark Deletion (#9014)
This commit is contained in:
parent
4799593e1a
commit
9dbf153489
1 changed files with 39 additions and 25 deletions
|
|
@ -388,43 +388,57 @@ export const useDeleteTagInConversations = () => {
|
|||
QueryKeys.allConversations,
|
||||
]);
|
||||
|
||||
// If there is no conversations cache yet, nothing to update
|
||||
if (!data || !Array.isArray(data.pages) || data.pages.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const conversationIdsWithTag: string[] = [];
|
||||
|
||||
// Remove deleted tag from conversations
|
||||
const newData = JSON.parse(JSON.stringify(data)) as InfiniteData<ConversationListResponse>;
|
||||
for (let pageIndex = 0; pageIndex < newData.pages.length; pageIndex++) {
|
||||
const page = newData.pages[pageIndex];
|
||||
page.conversations = page.conversations.map((conversation) => {
|
||||
if (
|
||||
conversation.conversationId &&
|
||||
'tags' in conversation &&
|
||||
Array.isArray(conversation.tags) &&
|
||||
conversation.tags.includes(deletedTag)
|
||||
) {
|
||||
conversationIdsWithTag.push(conversation.conversationId);
|
||||
conversation.tags = conversation.tags.filter((tag: string) => tag !== deletedTag);
|
||||
}
|
||||
return conversation;
|
||||
});
|
||||
}
|
||||
// Create an updated copy of the infinite query data without mutating the cache directly
|
||||
const updatedData: InfiniteData<ConversationListResponse> = {
|
||||
pageParams: Array.isArray(data.pageParams) ? [...data.pageParams] : [],
|
||||
pages: data.pages.map((page) => ({
|
||||
...page,
|
||||
conversations: page.conversations.map((conversation) => {
|
||||
if (
|
||||
conversation.conversationId &&
|
||||
'tags' in conversation &&
|
||||
Array.isArray((conversation as unknown as { tags?: string[] }).tags) &&
|
||||
(conversation as unknown as { tags: string[] }).tags.includes(deletedTag)
|
||||
) {
|
||||
conversationIdsWithTag.push(conversation.conversationId);
|
||||
return {
|
||||
...conversation,
|
||||
tags: (conversation as unknown as { tags: string[] }).tags.filter(
|
||||
(tag: string) => tag !== deletedTag,
|
||||
),
|
||||
} as t.TConversation;
|
||||
}
|
||||
return conversation as t.TConversation;
|
||||
}),
|
||||
})),
|
||||
};
|
||||
|
||||
queryClient.setQueryData<InfiniteData<ConversationListResponse>>(
|
||||
[QueryKeys.allConversations],
|
||||
newData,
|
||||
updatedData,
|
||||
);
|
||||
|
||||
// Remove the deleted tag from the cache of each conversation
|
||||
// Remove the deleted tag from the cache of each individual conversation
|
||||
for (let i = 0; i < conversationIdsWithTag.length; i++) {
|
||||
const conversationId = conversationIdsWithTag[i];
|
||||
const conversationData = queryClient.getQueryData<t.TConversation>([
|
||||
QueryKeys.conversation,
|
||||
conversationId,
|
||||
]);
|
||||
if (conversationData && 'tags' in conversationData && Array.isArray(conversationData.tags)) {
|
||||
conversationData.tags = conversationData.tags.filter((tag: string) => tag !== deletedTag);
|
||||
queryClient.setQueryData<t.TConversation>(
|
||||
[QueryKeys.conversation, conversationId],
|
||||
conversationData,
|
||||
);
|
||||
if (conversationData && Array.isArray((conversationData as { tags?: string[] }).tags)) {
|
||||
queryClient.setQueryData<t.TConversation>([QueryKeys.conversation, conversationId], {
|
||||
...conversationData,
|
||||
tags: (conversationData as { tags: string[] }).tags.filter(
|
||||
(tag: string) => tag !== deletedTag,
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue