mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-24 07:26:47 +08:00
### What problem does this PR solve? Feat: Upload agent file #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import { useToast } from '@/components/hooks/use-toast';
|
|
import { FileMimeType, Platform } from '@/constants/common';
|
|
import { useSetModalState } from '@/hooks/common-hooks';
|
|
import { useFetchFlow } from '@/hooks/flow-hooks';
|
|
import { IGraph } from '@/interfaces/database/flow';
|
|
import { downloadJsonFile } from '@/utils/file-util';
|
|
import { message } from 'antd';
|
|
import isEmpty from 'lodash/isEmpty';
|
|
import { useCallback } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useBuildDslData } from './use-build-dsl';
|
|
import { useSetGraphInfo } from './use-set-graph';
|
|
|
|
export const useHandleExportOrImportJsonFile = () => {
|
|
const { buildDslData } = useBuildDslData();
|
|
const {
|
|
visible: fileUploadVisible,
|
|
hideModal: hideFileUploadModal,
|
|
showModal: showFileUploadModal,
|
|
} = useSetModalState();
|
|
const setGraphInfo = useSetGraphInfo();
|
|
const { data } = useFetchFlow();
|
|
const { t } = useTranslation();
|
|
const { toast } = useToast();
|
|
|
|
const onFileUploadOk = useCallback(
|
|
async ({
|
|
fileList,
|
|
platform,
|
|
}: {
|
|
fileList: File[];
|
|
platform: Platform;
|
|
}) => {
|
|
console.log('🚀 ~ useHandleExportOrImportJsonFile ~ platform:', platform);
|
|
if (fileList.length > 0) {
|
|
const file = fileList[0];
|
|
if (file.type !== FileMimeType.Json) {
|
|
toast({ title: t('flow.jsonUploadTypeErrorMessage') });
|
|
return;
|
|
}
|
|
|
|
const graphStr = await file.text();
|
|
const errorMessage = t('flow.jsonUploadContentErrorMessage');
|
|
try {
|
|
const graph = JSON.parse(graphStr);
|
|
if (graphStr && !isEmpty(graph) && Array.isArray(graph?.nodes)) {
|
|
setGraphInfo(graph ?? ({} as IGraph));
|
|
hideFileUploadModal();
|
|
} else {
|
|
message.error(errorMessage);
|
|
}
|
|
} catch (error) {
|
|
message.error(errorMessage);
|
|
}
|
|
}
|
|
},
|
|
[hideFileUploadModal, setGraphInfo, t, toast],
|
|
);
|
|
|
|
const handleExportJson = useCallback(() => {
|
|
downloadJsonFile(buildDslData().graph, `${data.title}.json`);
|
|
}, [buildDslData, data.title]);
|
|
|
|
return {
|
|
fileUploadVisible,
|
|
handleExportJson,
|
|
handleImportJson: showFileUploadModal,
|
|
hideFileUploadModal,
|
|
onFileUploadOk,
|
|
};
|
|
};
|