import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { useDeepCompareMemo } from "@ant-design/pro-components"; import { Button, Collapse, type CollapseProps, Skeleton, Space, Switch, message, notification } from "antd"; import Show from "@/components/Show"; import { notifyChannelsMap } from "@/domain/settings"; import { useZustandShallowSelector } from "@/hooks"; import { useNotifyChannelsStore } from "@/stores/notify"; import { getErrMsg } from "@/utils/error"; import NotifyChannelEditForm, { type NotifyChannelEditFormInstance } from "./NotifyChannelEditForm"; import NotifyTestButton from "./NotifyTestButton"; 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 } = useNotifyChannelsStore(useZustandShallowSelector(["channels", "setChannel"])); const channelConfig = useDeepCompareMemo(() => channels[channel], [channels, channel]); const channelFormRef = useRef(null); const [channelFormChanged, setChannelFormChanged] = useState(false); const handleSubmit = 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 = { className?: string; classNames?: Partial>; style?: React.CSSProperties; styles?: Partial>; }; const NotifyChannels = ({ className, classNames, style, styles }: NotifyChannelsProps) => { const { t, i18n } = useTranslation(); const { channels, loadedAtOnce, setChannel, fetchChannels } = useNotifyChannelsStore( useZustandShallowSelector(["channels", "loadedAtOnce", "setChannel", "fetchChannels"]) ); useEffect(() => { fetchChannels(); }, []); const channelCollapseItems: CollapseProps["items"] = useDeepCompareMemo( () => Array.from(notifyChannelsMap.values()).map((channel) => { return { key: `channel-${channel.type}`, label: <>{t(channel.name)}, children: , extra: (
e.stopPropagation()} onMouseDown={(e) => e.stopPropagation()} onMouseUp={(e) => e.stopPropagation()}> handleSwitchChange(channel.type, checked)} />
), forceRender: true, }; }), [i18n.language, channels] ); const handleSwitchChange = (channel: string, enabled: boolean) => { setChannel(channel, { enabled }); }; return (
}>
); }; export default NotifyChannels;