Skip to content

Commit

Permalink
🚑 fix(export): Export Issue with New Chat (danny-avila#2777)
Browse files Browse the repository at this point in the history
* 🚑 fix: re-fetch messages when exporting

* Revert "🚑 fix: re-fetch messages when exporting"

This reverts commit 693b86e.

* 🚑 fix: use the same logic to get export data as useChatHelper

* refactor(useExportConversation): use query cache to build messages tree on request

* chore: organize imports

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
  • Loading branch information
ohneda and danny-avila authored May 28, 2024
1 parent fed2be6 commit 3a36ba0
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions client/src/hooks/Conversations/useExportConversation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import download from 'downloadjs';
import { useCallback } from 'react';
import exportFromJSON from 'export-from-json';
import { useGetMessagesByConvoId } from 'librechat-data-provider/react-query';
import { useQueryClient } from '@tanstack/react-query';
import {
QueryKeys,
ContentTypes,
ToolCallTypes,
imageGenTools,
Expand All @@ -16,6 +18,7 @@ import type {
import useBuildMessageTree from '~/hooks/Messages/useBuildMessageTree';
import { useScreenshot } from '~/hooks/ScreenshotContext';
import { cleanupPreset, buildTree } from '~/utils';
import { useParams } from 'react-router-dom';

export default function useExportConversation({
conversation,
Expand All @@ -32,24 +35,25 @@ export default function useExportConversation({
exportBranches: boolean | 'indeterminate';
recursive: boolean | 'indeterminate';
}) {
const queryClient = useQueryClient();
const { captureScreenshot } = useScreenshot();
const buildMessageTree = useBuildMessageTree();
const { data: messagesTree = null } = useGetMessagesByConvoId(
conversation?.conversationId ?? '',
{
select: (data) => {
const dataTree = buildTree({ messages: data });
return dataTree?.length === 0 ? null : dataTree ?? null;
},
},
);

const { conversationId: paramId } = useParams();

const getMessageTree = useCallback(() => {
const queryParam = paramId === 'new' ? paramId : conversation?.conversationId ?? paramId ?? '';
const messages = queryClient.getQueryData<TMessage[]>([QueryKeys.messages, queryParam]) ?? [];
const dataTree = buildTree({ messages });
return dataTree?.length === 0 ? null : dataTree ?? null;
}, [paramId, conversation?.conversationId, queryClient]);

const getMessageText = (message: TMessage, format = 'text') => {
if (!message) {
return '';
}

const formatText = (sender, text) => {
const formatText = (sender: string, text: string) => {
if (format === 'text') {
return `>> ${sender}:\n${text}`;
}
Expand Down Expand Up @@ -149,7 +153,7 @@ export default function useExportConversation({
const messages = await buildMessageTree({
messageId: conversation?.conversationId,
message: null,
messages: messagesTree,
messages: getMessageTree(),
branches: !!exportBranches,
recursive: false,
});
Expand Down Expand Up @@ -224,7 +228,7 @@ export default function useExportConversation({
const messages = await buildMessageTree({
messageId: conversation?.conversationId,
message: null,
messages: messagesTree,
messages: getMessageTree(),
branches: false,
recursive: false,
});
Expand Down Expand Up @@ -280,7 +284,7 @@ export default function useExportConversation({
const messages = await buildMessageTree({
messageId: conversation?.conversationId,
message: null,
messages: messagesTree,
messages: getMessageTree(),
branches: false,
recursive: false,
});
Expand Down Expand Up @@ -332,7 +336,7 @@ export default function useExportConversation({
const messages = await buildMessageTree({
messageId: conversation?.conversationId,
message: null,
messages: messagesTree,
messages: getMessageTree(),
branches: !!exportBranches,
recursive: !!recursive,
});
Expand Down

0 comments on commit 3a36ba0

Please sign in to comment.