'use client' import type { FC } from 'react' import React, { useCallback, useEffect, useState } from 'react' import { useContext } from 'use-context-selector' import produce from 'immer' import NodePanel from './node' import Loading from '@/app/components/base/loading' import { fetchTracingList } from '@/service/log' import { useStore as useAppStore } from '@/app/components/app/store' import type { NodeTracing } from '@/types/workflow' import { ToastContext } from '@/app/components/base/toast' type TracingProps = { runID: string } const Tracing: FC = ({ runID }) => { const { notify } = useContext(ToastContext) const { appDetail } = useAppStore() const [loading, setLoading] = useState(true) const [list, setList] = useState([]) const [collapseState, setCollapseState] = useState([]) const getTracingList = useCallback(async (appID: string, runID: string) => { setLoading(true) try { const { data: nodeList } = await fetchTracingList({ url: `/apps/${appID}/workflow-runs/${runID}/node-executions`, }) const collapseState = nodeList.map(node => node.status === 'succeeded') setList(nodeList.reverse()) setCollapseState(collapseState) setLoading(false) } catch (err) { notify({ type: 'error', message: `${err}`, }) setLoading(false) } }, [notify]) useEffect(() => { if (appDetail && runID) getTracingList(appDetail.id, runID) }, [appDetail, getTracingList, runID]) const collapseStateChange = (index: number) => { const newCollapseState = produce(collapseState, (draft: boolean[]) => { draft[index] = !draft[index] }) setCollapseState(newCollapseState) } if (loading) { return (
) } return (
{list.map((node, index) => ( collapseStateChange(index)} /> ))}
) } export default Tracing