From 9e1e0dee1d58cfcdd59aaf0552ecc7e188e2675b Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Fri, 20 Dec 2024 14:08:30 +0800 Subject: [PATCH] fix(ui): couldn't detect form changed in NotifyChannels --- .../notification/NotifyChannels.tsx | 97 ++++++++++--------- ui/src/i18n/locales/en/nls.settings.json | 1 - ui/src/i18n/locales/zh/nls.settings.json | 1 - 3 files changed, 52 insertions(+), 47 deletions(-) diff --git a/ui/src/components/notification/NotifyChannels.tsx b/ui/src/components/notification/NotifyChannels.tsx index b9a3a22a..25f4c0f3 100644 --- a/ui/src/components/notification/NotifyChannels.tsx +++ b/ui/src/components/notification/NotifyChannels.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef } from "react"; +import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { useDeepCompareMemo } from "@ant-design/pro-components"; import { Button, Collapse, message, notification, Skeleton, Space, Switch, Tooltip, type CollapseProps } from "antd"; @@ -9,6 +9,55 @@ import { notifyChannelsMap } from "@/domain/settings"; import { useNotifyChannelStore } from "@/stores/notify"; import { getErrMsg } from "@/utils/error"; +type NotifyChannelProps = { + className?: string; + style?: React.CSSProperties; + channel: string; +}; + +const NotifyChannel = ({ className, style, channel }: NotifyChannelProps) => { + const { t } = useTranslation(); + + const [messageApi, MessageContextHolder] = message.useMessage(); + const [notificationApi, NotificationContextHolder] = notification.useNotification(); + + const { channels, setChannel } = useNotifyChannelStore(); + + const channelConfig = useDeepCompareMemo(() => channels[channel], [channels, channel]); + const [channelFormChanged, setChannelFormChanged] = useState(false); + const channelFormRef = useRef(null); + + const handleClickSubmit = async () => { + await channelFormRef.current!.validateFields(); + + try { + setChannel(channel, channelFormRef.current!.getFieldsValue()); + setChannelFormChanged(false); + + messageApi.success(t("common.text.operation_succeeded")); + } catch (err) { + notificationApi.error({ message: t("common.text.request_error"), description: getErrMsg(err) }); + } + }; + + return ( +
+ {MessageContextHolder} + {NotificationContextHolder} + + setChannelFormChanged(true)} /> + + + + + {channelConfig != null ? : null} + +
+ ); +}; + type NotifyChannelsSemanticDOM = "collapse" | "form"; export type NotifyChannelsProps = { @@ -21,40 +70,18 @@ export type NotifyChannelsProps = { const NotifyChannels = ({ className, classNames, style, styles }: NotifyChannelsProps) => { const { t, i18n } = useTranslation(); - const [messageApi, MessageContextHolder] = message.useMessage(); - const [notificationApi, NotificationContextHolder] = notification.useNotification(); - const { initialized, channels, setChannel, fetchChannels } = useNotifyChannelStore(); useEffect(() => { fetchChannels(); }, [fetchChannels]); - const channelFormRefs = useRef>([]); const channelCollapseItems: CollapseProps["items"] = useDeepCompareMemo( () => - Array.from(notifyChannelsMap.values()).map((channel, index) => { + Array.from(notifyChannelsMap.values()).map((channel) => { return { key: `channel-${channel.type}`, label: <>{t(channel.name)}, - children: ( -
- (channelFormRefs.current[index] = ref)} model={channels[channel.type]} channel={channel.type} /> - - - - - {channels[channel.type] ? ( - - <> - - - - ) : null} - -
- ), + children: , extra: (
e.stopPropagation()} onMouseDown={(e) => e.stopPropagation()} onMouseUp={(e) => e.stopPropagation()}> { - const form = channelFormRefs.current[index]; - if (!form) { - return; - } - - await form.validateFields(); - - try { - setChannel(channel, form.getFieldsValue()); - - messageApi.success(t("common.text.operation_succeeded")); - } catch (err) { - notificationApi.error({ message: t("common.text.request_error"), description: getErrMsg(err) }); - } - }; - return (
- {MessageContextHolder} - {NotificationContextHolder} - {!initialized ? ( ) : ( diff --git a/ui/src/i18n/locales/en/nls.settings.json b/ui/src/i18n/locales/en/nls.settings.json index 3f02a3ae..60e79051 100644 --- a/ui/src/i18n/locales/en/nls.settings.json +++ b/ui/src/i18n/locales/en/nls.settings.json @@ -27,7 +27,6 @@ "settings.notification.channel.enabled.on": "On", "settings.notification.channel.enabled.off": "Off", "settings.notification.push_test.button": "Send Test Notification", - "settings.notification.push_test.tooltip": "Note: Please save settings before testing push.", "settings.notification.push_test.pushed": "Sent", "settings.notification.channel.form.bark_server_url.label": "Server URL", "settings.notification.channel.form.bark_server_url.placeholder": "Please enter server URL", diff --git a/ui/src/i18n/locales/zh/nls.settings.json b/ui/src/i18n/locales/zh/nls.settings.json index ddbff40e..276a90fa 100644 --- a/ui/src/i18n/locales/zh/nls.settings.json +++ b/ui/src/i18n/locales/zh/nls.settings.json @@ -27,7 +27,6 @@ "settings.notification.channel.enabled.on": "启用", "settings.notification.channel.enabled.off": "未启用", "settings.notification.push_test.button": "推送测试消息", - "settings.notification.push_test.tooltip": "提示:修改后请先保存设置再测试推送。", "settings.notification.push_test.pushed": "已推送", "settings.notification.channel.form.bark_server_url.label": "服务器地址", "settings.notification.channel.form.bark_server_url.placeholder": "请输入服务器地址",