From cb95d3992431d721d88f4528834e64cf44f8aae7 Mon Sep 17 00:00:00 2001 From: yangon <2689991790@qq.com> Date: Fri, 7 Jun 2024 16:21:59 +0800 Subject: [PATCH] Add InputTimeComp and fix some bugs (#434) --- ui/src/components/InputTimeComp/index.tsx | 63 +++++++++++++++++++ ui/src/pages/Alert/Channel/ChannelDrawer.tsx | 60 +++++++++--------- ui/src/pages/Alert/Channel/index.tsx | 21 +++---- ui/src/pages/Alert/Event/index.tsx | 26 ++++++-- ui/src/pages/Alert/Rules/RuleDrawerForm.tsx | 4 +- ui/src/pages/Alert/Rules/index.tsx | 15 +---- .../Subscriptions/SubscripDrawerForm.tsx | 26 ++++---- ui/src/pages/Alert/Subscriptions/index.tsx | 31 +++++++-- 8 files changed, 167 insertions(+), 79 deletions(-) create mode 100644 ui/src/components/InputTimeComp/index.tsx diff --git a/ui/src/components/InputTimeComp/index.tsx b/ui/src/components/InputTimeComp/index.tsx new file mode 100644 index 000000000..04983323b --- /dev/null +++ b/ui/src/components/InputTimeComp/index.tsx @@ -0,0 +1,63 @@ +import { InputNumber, Select } from 'antd'; +import { useState } from 'react'; + +interface InputTimeCompProps { + onChange?: (value: number | null) => void; + value?: number; +} + +type UnitType = 'second' | 'minute' | 'hour'; + +const SELECT_OPTIONS = [ + { + label: '秒', + value: 'second', + }, + { + label: '分钟', + value: 'minute', + }, + { + label: '小时', + value: 'hour', + }, +]; + +export default function InputTimeComp({ onChange, value }: InputTimeCompProps) { + const [unit, setUnit] = useState('minute'); + + const durationChange = (value: number | null) => { + if (!value || unit === 'second') onChange?.(value); + if (unit === 'minute') { + onChange?.(value! * 60); + } + if (unit === 'hour') { + onChange?.(value! * 3600); + } + }; + const getValue = (value: number | undefined) => { + if (!value) return value; + if (unit === 'minute') { + return Math.floor(value / 60); + } + if (unit === 'hour') { + return Math.floor(value / 3600); + } + return value; + }; + + return ( + setUnit(val)} + options={SELECT_OPTIONS} + /> + } + /> + ); +} diff --git a/ui/src/pages/Alert/Channel/ChannelDrawer.tsx b/ui/src/pages/Alert/Channel/ChannelDrawer.tsx index 2dfe24e19..8e5630c99 100644 --- a/ui/src/pages/Alert/Channel/ChannelDrawer.tsx +++ b/ui/src/pages/Alert/Channel/ChannelDrawer.tsx @@ -50,7 +50,9 @@ export default function ChannelDrawer({ manual: true, }, ); - const receiverNames = listReceiversRes?.data?.map((receiver) => receiver.name); + const receiverNames = listReceiversRes?.data?.map( + (receiver) => receiver.name, + ); const listReceiverTemplates = listReceiverTemplatesRes?.data; const Footer = () => { return ( @@ -80,6 +82,10 @@ export default function ChannelDrawer({ } }); }; + const typeChange = (type: string) => { + const template = listReceiverTemplates?.find((item) => item.type === type); + if (template) form.setFieldValue('config', template.template); + }; useEffect(() => { if (status !== 'create' && name) { @@ -144,6 +150,7 @@ export default function ChannelDrawer({ ) : (