import PropTypes from 'prop-types';
import * as React from 'react';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Collapse from '@mui/material/Collapse';
import ListItem from '@mui/material/ListItem';
import TextField from '@mui/material/TextField';
import Autocomplete from '@mui/material/Autocomplete';
import Chip from '@mui/material/Chip';
import useFormatMessage from 'hooks/useFormatMessage';
import useApps from 'hooks/useApps';
import { EditorContext } from 'contexts/Editor';
import FlowSubstepTitle from 'components/FlowSubstepTitle';
import { StepPropType, SubstepPropType } from 'propTypes/propTypes';
import useTriggers from 'hooks/useTriggers';
import useActions from 'hooks/useActions';
const optionGenerator = (app) => ({
label: app.name,
value: app.key,
});
const eventOptionGenerator = (app) => ({
label: app.name,
value: app.key,
type: app?.type,
});
const getOption = (options, selectedOptionValue) =>
options.find((option) => option.value === selectedOptionValue);
function ChooseAppAndEventSubstep(props) {
const {
substep,
expanded = false,
onExpand,
onCollapse,
step,
onSubmit,
onChange,
} = props;
const formatMessage = useFormatMessage();
const editorContext = React.useContext(EditorContext);
const isTrigger = step.type === 'trigger';
const isAction = step.type === 'action';
const useAppsOptions = {};
if (isTrigger) {
useAppsOptions.onlyWithTriggers = true;
}
if (isAction) {
useAppsOptions.onlyWithActions = true;
}
const { data: apps } = useApps(useAppsOptions);
const app = apps?.data?.find(
(currentApp) => currentApp?.key === step?.appKey,
);
const { data: triggers } = useTriggers(app?.key);
const { data: actions } = useActions(app?.key);
const appOptions = React.useMemo(
() => apps?.data?.map((app) => optionGenerator(app)) || [],
[apps?.data],
);
const actionsOrTriggers = (isTrigger ? triggers?.data : actions?.data) || [];
const actionOrTriggerOptions = React.useMemo(
() => actionsOrTriggers.map((trigger) => eventOptionGenerator(trigger)),
[actionsOrTriggers],
);
const selectedActionOrTrigger = actionsOrTriggers.find(
(actionOrTrigger) => actionOrTrigger.key === step?.key,
);
const isWebhook = isTrigger && selectedActionOrTrigger?.type === 'webhook';
const { name } = substep;
const valid = !!step.key && !!step.appKey;
// placeholders
const onEventChange = React.useCallback(
(event, selectedOption) => {
if (typeof selectedOption === 'object') {
// TODO: try to simplify type casting below.
const typedSelectedOption = selectedOption;
const option = typedSelectedOption;
const eventKey = option?.value;
if (step.key !== eventKey) {
onChange({
step: {
...step,
key: eventKey,
},
});
}
}
},
[step, onChange],
);
const onAppChange = React.useCallback(
(event, selectedOption) => {
if (typeof selectedOption === 'object') {
// TODO: try to simplify type casting below.
const typedSelectedOption = selectedOption;
const option = typedSelectedOption;
const appKey = option?.value;
if (step.appKey !== appKey) {
onChange({
step: {
...step,
key: '',
appKey,
parameters: {},
},
});
}
}
},
[step, onChange],
);
const onToggle = expanded ? onCollapse : onExpand;
return (