import React, { type JSX, useCallback, useEffect, useMemo, useState, } from 'react'; import Link from 'next/link'; import { useRouter } from 'next/router'; import { useAtomValue } from 'jotai'; import { useTranslation } from 'next-i18next'; import { NotifyType, TriggerEventType, } from '~/client/interfaces/global-notification'; import { apiv3Post } from '~/client/util/apiv3-client'; import { toastError } from '~/client/util/toastr'; import { isMailerSetupAtom } from '~/states/server-configurations'; import { useSWRxGlobalNotification } from '~/stores/global-notification'; import loggerFactory from '~/utils/logger'; import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow'; import TriggerEventCheckBox from './TriggerEventCheckBox'; const logger = loggerFactory('growi:manageGlobalNotification'); type Props = { globalNotificationId?: string; }; const ManageGlobalNotification = (props: Props): JSX.Element => { const [triggerPath, setTriggerPath] = useState(''); const [notifyType, setNotifyType] = useState(NotifyType.Email); const [emailToSend, setEmailToSend] = useState(''); const [slackChannelToSend, setSlackChannelToSend] = useState(''); const [triggerEvents, setTriggerEvents] = useState(new Set()); const { data: globalNotificationData, update: updateGlobalNotification } = useSWRxGlobalNotification(props.globalNotificationId || ''); const globalNotification = useMemo( () => globalNotificationData?.globalNotification, [globalNotificationData?.globalNotification], ); const router = useRouter(); useEffect(() => { if (globalNotification != null) { const notifyType = globalNotification.__t; setNotifyType(notifyType); setTriggerPath(globalNotification.triggerPath); setTriggerEvents(new Set(globalNotification.triggerEvents)); if (notifyType === NotifyType.Email) { setEmailToSend(globalNotification.toEmail); } else { setSlackChannelToSend(globalNotification.slackChannels); } } }, [globalNotification]); const isLoading = globalNotificationData === undefined; const notExistsGlobalNotification = !isLoading && globalNotificationData == null; useEffect(() => { if (notExistsGlobalNotification) { router.push('/admin/notification'); } }, [notExistsGlobalNotification, router]); const onChangeTriggerEvents = useCallback( (triggerEvent: string) => { let newTriggerEvents: string[]; if (triggerEvents.has(triggerEvent)) { newTriggerEvents = [...triggerEvents].filter( (item) => item !== triggerEvent, ); setTriggerEvents(new Set(newTriggerEvents)); } else { newTriggerEvents = [...triggerEvents, triggerEvent]; setTriggerEvents(new Set(newTriggerEvents)); } }, [triggerEvents], ); const updateButtonClickedHandler = useCallback(async () => { const requestParams = { triggerPath, notifyType, toEmail: emailToSend, slackChannels: slackChannelToSend, triggerEvents: [...triggerEvents], }; try { if (props.globalNotificationId != null) { await updateGlobalNotification(requestParams); router.push('/admin/notification'); } else { await apiv3Post( '/notification-setting/global-notification', requestParams, ); router.push('/admin/notification'); } } catch (err) { toastError(err); logger.error(err); } }, [ emailToSend, notifyType, props.globalNotificationId, router, slackChannelToSend, triggerEvents, triggerPath, updateGlobalNotification, ]); // Mailer setup status (unused yet but kept for potential conditional logic) const isMailerSetup = useAtomValue(isMailerSetupAtom); const { t } = useTranslation('admin'); return ( <>
{t('notification_settings.back_to_list')}

{t('notification_settings.notification_detail')}

{ setTriggerPath(e.target.value); }} required />

{t('notification_settings.notify_to')}

{ setNotifyType(NotifyType.Email); }} />
{ setNotifyType(NotifyType.SLACK); }} />
{notifyType === NotifyType.Email ? ( <>
mail
{ setEmailToSend(e.target.value); }} />

{!isMailerSetup && ( )} Hint: {t('notification_settings.email.ifttt_link')} share

) : ( <>
tag
{ setSlackChannelToSend(e.target.value); }} />

)}

{t('notification_settings.trigger_events')}

onChangeTriggerEvents(TriggerEventType.CREATE)} > edit_note{' '} CREATE
onChangeTriggerEvents(TriggerEventType.EDIT)} > edit EDIT
onChangeTriggerEvents(TriggerEventType.MOVE)} > redoMOVE
onChangeTriggerEvents(TriggerEventType.DELETE)} > delete_forever DELETE
onChangeTriggerEvents(TriggerEventType.LIKE)} > favorite LIKE
onChangeTriggerEvents(TriggerEventType.POST)} > language POST
); }; export default ManageGlobalNotification;