({ script, visible, onCancel, onOk })
| 22 | onOk: () => void; |
| 23 | onCancel: () => void; |
| 24 | }> = ({ script, visible, onCancel, onOk }) => { |
| 25 | const [data, setData] = useState<ValueModel[]>([]); |
| 26 | const [rawData, setRawData] = useState<{ [key: string]: any }>({}); |
| 27 | const [currentValue, setCurrentValue] = useState<ValueModel>(); |
| 28 | const [visibleEdit, setVisibleEdit] = useState(false); |
| 29 | const [isEdit, setIsEdit] = useState(false); |
| 30 | const [editValue, setEditValue] = useState(""); |
| 31 | const [form] = Form.useForm(); |
| 32 | const { t } = useTranslation(); |
| 33 | |
| 34 | // 保存单个键值 |
| 35 | const saveData = (key: string, value: any) => { |
| 36 | valueClient.setScriptValue({ uuid: script!.uuid, key, value, ts: Date.now() }); |
| 37 | const newRawData = { ...rawData, [key]: value }; |
| 38 | if (value === undefined) { |
| 39 | delete newRawData[key]; |
| 40 | } |
| 41 | updateRawData(newRawData); |
| 42 | }; |
| 43 | |
| 44 | // 保存所有键值 |
| 45 | const saveRawData = (newRawValue: { [key: string]: any }) => { |
| 46 | const keyValuePairs = [] as TKeyValuePair[]; |
| 47 | for (const [key, value] of Object.entries(newRawValue)) { |
| 48 | keyValuePairs.push([key, encodeRValue(value)]); |
| 49 | } |
| 50 | valueClient.setScriptValues({ uuid: script!.uuid, keyValuePairs, isReplace: true, ts: Date.now() }); |
| 51 | updateRawData(newRawValue); |
| 52 | }; |
| 53 | |
| 54 | // 更新UI数据 |
| 55 | const updateRawData = (newRawValue: { [key: string]: any }) => { |
| 56 | setRawData(newRawValue); |
| 57 | setEditValue(JSON.stringify(newRawValue, null, 2)); |
| 58 | setData( |
| 59 | Object.keys(newRawValue).map((key) => { |
| 60 | return { key: key, value: newRawValue[key] }; |
| 61 | }) |
| 62 | ); |
| 63 | }; |
| 64 | |
| 65 | // 删除单个键值 |
| 66 | const deleteData = (key: string) => { |
| 67 | saveData(key, undefined); |
| 68 | Message.info({ |
| 69 | content: t("delete_success"), |
| 70 | }); |
| 71 | }; |
| 72 | |
| 73 | // 清空所有键值 |
| 74 | const clearData = () => { |
| 75 | saveRawData({}); |
| 76 | Message.info({ |
| 77 | content: t("clear_success"), |
| 78 | }); |
| 79 | }; |
| 80 | |
| 81 | useEffect(() => { |
nothing calls this directly
no test coverage detected