|
import { useFetchFlow, useResetFlow, useSetFlow } from '@/hooks/flow-hooks'; |
|
import { useDebounceEffect } from 'ahooks'; |
|
import dayjs from 'dayjs'; |
|
import { useCallback, useEffect, useState } from 'react'; |
|
import { Node } from 'reactflow'; |
|
import { useParams } from 'umi'; |
|
import useGraphStore from '../store'; |
|
import { useBuildDslData } from './use-build-dsl'; |
|
|
|
export const useSaveGraph = () => { |
|
const { data } = useFetchFlow(); |
|
const { setFlow, loading } = useSetFlow(); |
|
const { id } = useParams(); |
|
const { buildDslData } = useBuildDslData(); |
|
|
|
const saveGraph = useCallback( |
|
async (currentNodes?: Node[]) => { |
|
return setFlow({ |
|
id, |
|
title: data.title, |
|
dsl: buildDslData(currentNodes), |
|
}); |
|
}, |
|
[setFlow, id, data.title, buildDslData], |
|
); |
|
|
|
return { saveGraph, loading }; |
|
}; |
|
|
|
export const useSaveGraphBeforeOpeningDebugDrawer = (show: () => void) => { |
|
const { saveGraph, loading } = useSaveGraph(); |
|
const { resetFlow } = useResetFlow(); |
|
|
|
const handleRun = useCallback( |
|
async (nextNodes?: Node[]) => { |
|
const saveRet = await saveGraph(nextNodes); |
|
if (saveRet?.code === 0) { |
|
|
|
const resetRet = await resetFlow(); |
|
|
|
if (resetRet?.code === 0) { |
|
show(); |
|
} |
|
} |
|
}, |
|
[saveGraph, resetFlow, show], |
|
); |
|
|
|
return { handleRun, loading }; |
|
}; |
|
|
|
export const useWatchAgentChange = (chatDrawerVisible: boolean) => { |
|
const [time, setTime] = useState<string>(); |
|
const nodes = useGraphStore((state) => state.nodes); |
|
const edges = useGraphStore((state) => state.edges); |
|
const { saveGraph } = useSaveGraph(); |
|
const { data: flowDetail } = useFetchFlow(); |
|
|
|
const setSaveTime = useCallback((updateTime: number) => { |
|
setTime(dayjs(updateTime).format('YYYY-MM-DD HH:mm:ss')); |
|
}, []); |
|
|
|
useEffect(() => { |
|
setSaveTime(flowDetail?.update_time); |
|
}, [flowDetail, setSaveTime]); |
|
|
|
const saveAgent = useCallback(async () => { |
|
if (!chatDrawerVisible) { |
|
const ret = await saveGraph(); |
|
setSaveTime(ret.data.update_time); |
|
} |
|
}, [chatDrawerVisible, saveGraph, setSaveTime]); |
|
|
|
useDebounceEffect( |
|
() => { |
|
saveAgent(); |
|
}, |
|
[nodes, edges], |
|
{ |
|
wait: 1000 * 20, |
|
}, |
|
); |
|
|
|
return time; |
|
}; |
|
|