Files
parcer/src/components/recorder/AddWhatCondModal.tsx

135 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-07-31 22:46:38 +05:30
import { WhereWhatPair } from "maxun-core";
2025-01-09 19:49:20 +05:30
import { GenericModal } from "../ui/GenericModal";
2024-06-24 22:31:52 +05:30
import { modalStyle } from "./AddWhereCondModal";
import { Button, MenuItem, TextField, Typography } from "@mui/material";
import React, { useRef } from "react";
import { Dropdown as MuiDropdown } from "../ui/DropdownMui";
2024-06-24 22:31:52 +05:30
import { KeyValueForm } from "./KeyValueForm";
2025-01-09 19:46:31 +05:30
import { ClearButton } from "../ui/buttons/ClearButton";
2024-06-24 22:31:52 +05:30
import { useSocketStore } from "../../context/socket";
interface AddWhatCondModalProps {
isOpen: boolean;
onClose: () => void;
pair: WhereWhatPair;
index: number;
}
2025-01-09 17:16:54 +05:30
export const AddWhatCondModal = ({ isOpen, onClose, pair, index }: AddWhatCondModalProps) => {
2024-06-24 22:31:52 +05:30
const [action, setAction] = React.useState<string>('');
const [objectIndex, setObjectIndex] = React.useState<number>(0);
2025-01-09 17:16:54 +05:30
const [args, setArgs] = React.useState<({ type: string, value: (string | number | object | unknown) })[]>([]);
2024-06-24 22:31:52 +05:30
2025-01-09 17:16:54 +05:30
const objectRefs = useRef<({ getObject: () => object } | unknown)[]>([]);
2024-06-24 22:31:52 +05:30
2025-01-09 17:16:54 +05:30
const { socket } = useSocketStore();
2024-06-24 22:31:52 +05:30
const handleSubmit = () => {
2025-01-09 17:16:54 +05:30
const argsArray: (string | number | object | unknown)[] = [];
2024-06-24 22:31:52 +05:30
args.map((arg, index) => {
switch (arg.type) {
case 'string':
case 'number':
argsArray[index] = arg.value;
break;
case 'object':
// @ts-ignore
argsArray[index] = objectRefs.current[arg.value].getObject();
}
})
setArgs([]);
onClose();
pair.what.push({
// @ts-ignore
action,
args: argsArray,
})
2025-01-09 17:16:54 +05:30
socket?.emit('updatePair', { index: index - 1, pair: pair });
2024-06-24 22:31:52 +05:30
}
return (
<GenericModal isOpen={isOpen} onClose={() => {
setArgs([]);
onClose();
}} modalStyle={modalStyle}>
<div>
2025-01-09 17:16:54 +05:30
<Typography sx={{ margin: '20px 0px' }}>Add what condition:</Typography>
<div style={{ margin: '8px' }}>
2024-06-24 22:31:52 +05:30
<Typography>Action:</Typography>
<TextField
size='small'
type="string"
onChange={(e) => setAction(e.target.value)}
value={action}
label='action'
/>
<div>
2025-01-09 17:16:54 +05:30
<Typography>Add new argument of type:</Typography>
<Button onClick={() => setArgs([...args, { type: 'string', value: null }])}>string</Button>
<Button onClick={() => setArgs([...args, { type: 'number', value: null }])}>number</Button>
2024-06-24 22:31:52 +05:30
<Button onClick={() => {
2025-01-09 17:16:54 +05:30
setArgs([...args, { type: 'object', value: objectIndex }])
setObjectIndex(objectIndex + 1);
}}>object</Button>
2024-06-24 22:31:52 +05:30
</div>
<Typography>args:</Typography>
{args.map((arg, index) => {
// @ts-ignore
return (
2025-01-09 17:16:54 +05:30
<div style={{ border: 'solid 1px gray', padding: '10px', display: 'flex', flexDirection: 'row', alignItems: 'center' }}
key={`wrapper-for-${arg.type}-${index}`}>
2024-06-24 22:31:52 +05:30
<ClearButton handleClick={() => {
2025-01-09 17:16:54 +05:30
args.splice(index, 1);
2024-06-24 22:31:52 +05:30
setArgs([...args]);
2025-01-09 17:16:54 +05:30
}} />
<Typography sx={{ margin: '5px' }} key={`number-argument-${arg.type}-${index}`}>{index}: </Typography>
{arg.type === 'string' ?
2024-06-24 22:31:52 +05:30
<TextField
size='small'
2025-01-09 17:16:54 +05:30
type="string"
2024-06-24 22:31:52 +05:30
onChange={(e) => setArgs([
...args.slice(0, index),
2025-01-09 17:16:54 +05:30
{ type: arg.type, value: e.target.value },
2024-06-24 22:31:52 +05:30
...args.slice(index + 1)
])}
value={args[index].value || ''}
2025-01-09 17:16:54 +05:30
label="string"
key={`arg-${arg.type}-${index}`}
/> : arg.type === 'number' ?
<TextField
key={`arg-${arg.type}-${index}`}
size='small'
type="number"
onChange={(e) => setArgs([
...args.slice(0, index),
{ type: arg.type, value: Number(e.target.value) },
...args.slice(index + 1)
])}
value={args[index].value || ''}
label="number"
/> :
<KeyValueForm ref={el =>
//@ts-ignore
objectRefs.current[arg.value] = el} key={`arg-${arg.type}-${index}`} />
}
</div>
)
})}
2024-06-24 22:31:52 +05:30
<Button
onClick={handleSubmit}
variant="outlined"
sx={{
display: "table-cell",
float: "right",
marginRight: "15px",
marginTop: "20px",
}}
>
{"Add Condition"}
</Button>
</div>
2025-01-09 17:16:54 +05:30
</div>
2024-06-24 22:31:52 +05:30
</GenericModal>
)
}