-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Exporting all conversations in one go #2701
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { useState } from 'react'; | ||
import { ArrowRightFromLine } from 'lucide-react'; | ||
import { useExportConversationsMutation } from '~/data-provider'; | ||
import { useLocalize } from '~/hooks'; | ||
import { useToastContext } from '~/Providers'; | ||
|
||
function ExportConversations() { | ||
const localize = useLocalize(); | ||
const { showToast } = useToastContext(); | ||
const [, setErrors] = useState<string[]>([]); | ||
const setError = (error: string) => setErrors((prevErrors) => [...prevErrors, error]); | ||
|
||
const exportFile = useExportConversationsMutation({ | ||
onSuccess: (data) => { | ||
console.log('Exporting started', data); | ||
showToast({ message: localize('com_ui_export_conversation_success') }); | ||
downloadConversationsJsonFile(data); | ||
}, | ||
onError: (error) => { | ||
console.error('Error: ', error); | ||
setError( | ||
(error as { response: { data: { message?: string } } }).response.data.message ?? | ||
'An error occurred while exporting the file.', | ||
); | ||
showToast({ message: localize('com_ui_export_conversation_error'), status: 'error' }); | ||
}, | ||
}); | ||
|
||
const startExport = () => { | ||
const formdata = new FormData(); | ||
exportFile.mutate(formdata); | ||
}; | ||
|
||
const downloadConversationsJsonFile = (data) => { | ||
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this seems not optimal. We kind of downloading potentially big file then storing it in memory. Can't we download it directly? |
||
const url = URL.createObjectURL(blob); | ||
|
||
const link = document.createElement('a'); | ||
link.href = url; | ||
link.download = 'conversations.json'; | ||
document.body.appendChild(link); | ||
link.click(); | ||
document.body.removeChild(link); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="flex items-center justify-between"> | ||
<span>{localize('com_nav_export_all_conversations')}</span> | ||
<button | ||
onClick={startExport} | ||
className="flex h-auto cursor-pointer items-center rounded bg-transparent px-2 py-1 text-xs font-medium font-normal transition-colors hover:bg-gray-100 hover:text-green-700 dark:bg-transparent dark:text-white dark:hover:bg-gray-600 dark:hover:text-green-500" | ||
> | ||
<ArrowRightFromLine className="mr-1 flex w-[22px] items-center stroke-1" /> | ||
<span>{localize('com_endpoint_export')}</span> | ||
</button> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export default ExportConversations; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -430,6 +430,71 @@ export const useUploadConversationsMutation = ( | |
}); | ||
}; | ||
|
||
export const useExportConversationsMutation = ( | ||
_options?: t.MutationOptions<t.TImportJobStatus, FormData>, | ||
) => { | ||
const queryClient = useQueryClient(); | ||
const { onSuccess, onError } = _options || {}; | ||
|
||
const checkJobStatus = async (jobId) => { | ||
try { | ||
const response = await dataService.queryExportAllConversationJobStatus(jobId); | ||
return response; | ||
} catch (error) { | ||
throw new Error('Failed to check job status'); | ||
} | ||
}; | ||
|
||
const pollJobStatus = (jobId, onSuccess, onError) => { | ||
const intervalId = setInterval(async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be the same as |
||
try { | ||
const statusResponse = await checkJobStatus(jobId); | ||
console.log('Polling job status:', statusResponse); | ||
if (statusResponse.status === 'completed' || statusResponse.status === 'failed') { | ||
clearInterval(intervalId); | ||
if (statusResponse.status === 'completed') { | ||
onSuccess && onSuccess(); | ||
} else { | ||
onError && | ||
onError(new Error(statusResponse.failReason || 'Failed to export conversations')); | ||
} | ||
} | ||
} catch (error) { | ||
clearInterval(intervalId); | ||
onError && onError(error); | ||
} | ||
}, 500); // Poll every 0,5 seconds. Adjust time as necessary. | ||
}; | ||
|
||
return useMutation<t.TImportStartResponse, unknown, FormData>({ | ||
mutationFn: (formData: FormData) => dataService.exportAllConversationsToJson(formData), | ||
onSuccess: (data, variables, context) => { | ||
queryClient.invalidateQueries([QueryKeys.allConversations]); | ||
const jobId = data.jobId; | ||
if (jobId) { | ||
console.log('Job ID:', jobId); | ||
pollJobStatus( | ||
jobId, | ||
async () => { | ||
queryClient.invalidateQueries([QueryKeys.allConversations]); | ||
onSuccess?.( | ||
(await dataService.exportConversationsFile(jobId)).data, | ||
variables, | ||
context, | ||
); | ||
}, | ||
(error) => { | ||
onError?.(error, variables, { context: 'ExportJobFailed' }); | ||
}, | ||
); | ||
} | ||
}, | ||
onError: (err, variables, context) => { | ||
onError?.(err, variables, context); | ||
}, | ||
}); | ||
}; | ||
|
||
export const useUploadFileMutation = ( | ||
_options?: t.UploadMutationOptions, | ||
): UseMutationResult< | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we put that in a function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember that we wanted to handle user id and job id to make sure we return the appropriate http code