mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-09-18 20:53:39 +00:00

### What problem does this PR solve? feat: fetch flow #918 feat: save graph ### Type of change - [x] New Feature (non-breaking change which adds functionality)
93 lines
2.4 KiB
TypeScript
93 lines
2.4 KiB
TypeScript
import { DSL, IFlow } from '@/interfaces/database/flow';
|
|
import i18n from '@/locales/config';
|
|
import flowService from '@/services/flow-service';
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { message } from 'antd';
|
|
import { useParams } from 'umi';
|
|
|
|
export const useFetchFlowTemplates = () => {
|
|
const { data } = useQuery({
|
|
queryKey: ['fetchFlowTemplates'],
|
|
initialData: [],
|
|
queryFn: async () => {
|
|
const { data } = await flowService.listTemplates();
|
|
|
|
return data;
|
|
},
|
|
});
|
|
|
|
return data;
|
|
};
|
|
|
|
export const useFetchFlowList = (): { data: IFlow[]; loading: boolean } => {
|
|
const { data, isFetching: loading } = useQuery({
|
|
queryKey: ['fetchFlowList'],
|
|
initialData: [],
|
|
queryFn: async () => {
|
|
const { data } = await flowService.listCanvas();
|
|
|
|
return data?.data ?? [];
|
|
},
|
|
});
|
|
|
|
return { data, loading };
|
|
};
|
|
|
|
export const useFetchFlow = (): { data: IFlow; loading: boolean } => {
|
|
const { id } = useParams();
|
|
const { data, isFetching: loading } = useQuery({
|
|
queryKey: ['flowDetail'],
|
|
initialData: {} as IFlow,
|
|
queryFn: async () => {
|
|
const { data } = await flowService.getCanvas({}, id);
|
|
|
|
return data?.data ?? {};
|
|
},
|
|
});
|
|
|
|
return { data, loading };
|
|
};
|
|
|
|
export const useSetFlow = () => {
|
|
const queryClient = useQueryClient();
|
|
const {
|
|
data,
|
|
isPending: loading,
|
|
mutateAsync,
|
|
} = useMutation({
|
|
mutationKey: ['setFlow'],
|
|
mutationFn: async (params: { id?: string; title?: string; dsl?: DSL }) => {
|
|
const { data } = await flowService.setCanvas(params);
|
|
if (data.retcode === 0) {
|
|
message.success(
|
|
i18n.t(`message.${params?.id ? 'modified' : 'created'}`),
|
|
);
|
|
queryClient.invalidateQueries({ queryKey: ['fetchFlowList'] });
|
|
}
|
|
return data?.retcode;
|
|
},
|
|
});
|
|
|
|
return { data, loading, setFlow: mutateAsync };
|
|
};
|
|
|
|
export const useDeleteFlow = () => {
|
|
const queryClient = useQueryClient();
|
|
const {
|
|
data,
|
|
isPending: loading,
|
|
mutateAsync,
|
|
} = useMutation({
|
|
mutationKey: ['deleteFlow'],
|
|
mutationFn: async (canvasIds: string[]) => {
|
|
const { data } = await flowService.removeCanvas({ canvasIds });
|
|
if (data.retcode === 0) {
|
|
queryClient.invalidateQueries({ queryKey: ['fetchFlowList'] });
|
|
}
|
|
return data?.data ?? [];
|
|
},
|
|
});
|
|
|
|
return { data, loading, deleteFlow: mutateAsync };
|
|
};
|