import React, { useCallback, useEffect, useState } from 'react';
import {
ActionAreaProvider,
ActionAreaStub,
ActionContextProvider,
cx,
SchemaComponent,
useAPIClient,
useApp,
useCompile,
useDocumentTitle,
usePlugin,
useResourceActionContext,
} from '@tachybase/client';
import { str2moment } from '@tachybase/utils/client';
import { DownOutlined, ExclamationCircleFilled, StopOutlined } from '@ant-design/icons';
import { Breadcrumb, Button, Dropdown, message, Modal, Result, Space, Spin, Tag, Tooltip } from 'antd';
import { useTranslation } from 'react-i18next';
import { Link, useNavigate } from 'react-router-dom';
import WorkflowPlugin from '.';
import { CanvasContent } from './CanvasContent';
import { StatusButton } from './components/StatusButton';
import { ExecutionStatusOptionsMap, JobStatusOptions } from './constants';
import { FlowContext, useFlowContext } from './FlowContext';
import { lang, NAMESPACE } from './locale';
import useStyles from './style';
import { getWorkflowDetailPath, getWorkflowExecutionsPath, linkNodes } from './utils';
function attachJobs(nodes, jobs: any[] = []): void {
const nodesMap = new Map();
nodes.forEach((item) => {
item.jobs = [];
nodesMap.set(item.id, item);
});
jobs.forEach((item) => {
const node = nodesMap.get(item.nodeId);
node.jobs.push(item);
item.node = {
id: node.id,
key: node.key,
title: node.title,
type: node.type,
};
});
nodes.forEach((item) => {
item.jobs = item.jobs.sort((a, b) => a.id - b.id);
});
}
function JobModal() {
const { instructions } = usePlugin(WorkflowPlugin);
const compile = useCompile();
const { viewJob: job, setViewJob } = useFlowContext();
const { styles } = useStyles();
const { node = {} } = job ?? {};
const instruction = instructions.get(node.type);
return (
{compile(instruction?.title)}
{node.title}
#{node.id}
),
properties: {
status: {
type: 'number',
title: `{{t("Status", { ns: "${NAMESPACE}" })}}`,
'x-decorator': 'FormItem',
'x-component': 'Select',
enum: JobStatusOptions,
'x-read-pretty': true,
},
updatedAt: {
type: 'string',
title: `{{t("Executed at", { ns: "${NAMESPACE}" })}}`,
'x-decorator': 'FormItem',
'x-component': 'DatePicker',
'x-component-props': {
showTime: true,
},
'x-read-pretty': true,
},
result: {
type: 'object',
title: `{{t("Node result", { ns: "${NAMESPACE}" })}}`,
'x-decorator': 'FormItem',
'x-component': 'Input.JSON',
'x-component-props': {
className: styles.nodeJobResultClass,
},
'x-read-pretty': true,
},
},
},
},
}}
/>
);
}
function ExecutionsDropdown(props) {
const { execution } = useFlowContext();
const apiClient = useAPIClient();
const navigate = useNavigate();
const { styles } = useStyles();
const [executionsBefore, setExecutionsBefore] = useState([]);
const [executionsAfter, setExecutionsAfter] = useState([]);
useEffect(() => {
if (!execution) {
return;
}
apiClient
.resource('executions')
.list({
filter: {
key: execution.key,
id: {
$lt: execution.id,
},
},
sort: '-createdAt',
pageSize: 10,
fields: ['id', 'status', 'createdAt'],
})
.then(({ data }) => {
setExecutionsBefore(data.data);
})
.catch(() => {});
}, [execution]);
useEffect(() => {
if (!execution) {
return;
}
apiClient
.resource('executions')
.list({
filter: {
key: execution.key,
id: {
$gt: execution.id,
},
},
sort: 'createdAt',
pageSize: 10,
fields: ['id', 'status', 'createdAt'],
})
.then(({ data }) => {
setExecutionsAfter(data.data.reverse());
})
.catch(() => {});
}, [execution]);
const onClick = useCallback(
({ key }) => {
if (key != execution.id) {
navigate(getWorkflowExecutionsPath(key));
}
},
[execution],
);
return execution ? (
{
return {
key: item.id,
label: (
<>
{`#${item.id}`}
>
),
icon: (
),
};
}),
}}
>
{`#${execution.id}`}
) : null;
}
export function ExecutionCanvas() {
const { t } = useTranslation();
const compile = useCompile();
const { data, loading, refresh } = useResourceActionContext();
const { setTitle } = useDocumentTitle();
const [viewJob, setViewJob] = useState(null);
const app = useApp();
const apiClient = useAPIClient();
useEffect(() => {
const { workflow } = data?.data ?? {};
setTitle?.(`${workflow?.title ? `${workflow.title} - ` : ''}${lang('Execution history')}`);
}, [data?.data]);
const onCancel = useCallback(() => {
Modal.confirm({
title: lang('Cancel the execution'),
icon: ,
content: lang('Are you sure you want to cancel the execution?'),
onOk: () => {
apiClient
.resource('executions')
.cancel({
filterByTk: data?.data.id,
})
.then(() => {
message.success(t('Operation succeeded'));
refresh();
})
.catch((response) => {
console.error(response.data.error);
});
},
});
}, [data?.data]);
if (!data?.data) {
if (loading) {
return ;
}
return ;
}
const { jobs = [], workflow: { nodes = [], revisions = [], ...workflow } = {}, ...execution } = data?.data ?? {};
linkNodes(nodes);
attachJobs(nodes, jobs);
const entry = nodes.find((item) => !item.upstream);
const statusOption = ExecutionStatusOptionsMap[execution.status];
return (
{lang('Workflow')} },
{ title: {workflow.title} },
{ title: },
]}
/>
);
}