| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- 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>(NotifyType.Email);
- const [emailToSend, setEmailToSend] = useState('');
- const [slackChannelToSend, setSlackChannelToSend] = useState('');
- const [triggerEvents, setTriggerEvents] = useState(new Set<string>());
- 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 (
- <>
- <div className="my-3">
- <Link href="/admin/notification" className="btn btn-outline-secondary">
- <span className="material-symbols-outlined" aria-hidden="true">
- arrow_left_alt
- </span>
- {t('notification_settings.back_to_list')}
- </Link>
- </div>
- <div className="row">
- <div className="form-box col-md-12">
- <h2 className="border-bottom mb-5">
- {t('notification_settings.notification_detail')}
- </h2>
- </div>
- <div className="col-sm-4">
- <h3>
- <label htmlFor="triggerPath" className="form-label">
- {t('notification_settings.trigger_path')}
- <small
- // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
- dangerouslySetInnerHTML={{
- __html: t(
- 'notification_settings.trigger_path_help',
- '<code>*</code>',
- ),
- }}
- />
- </label>
- </h3>
- <div>
- <input
- className="form-control"
- type="text"
- name="triggerPath"
- value={triggerPath}
- onChange={(e) => {
- setTriggerPath(e.target.value);
- }}
- required
- />
- </div>
- <h3>{t('notification_settings.notify_to')}</h3>
- <div>
- <div className="form-check">
- <input
- className="form-check-input"
- type="radio"
- id="mail"
- name="notifyType"
- value="mail"
- checked={notifyType === NotifyType.Email}
- onChange={() => {
- setNotifyType(NotifyType.Email);
- }}
- />
- <label className="form-label form-check-label" htmlFor="mail">
- <p className="fw-bold">Email</p>
- </label>
- </div>
- <div className="form-check ms-2">
- <input
- className="form-check-input"
- type="radio"
- id="slack"
- name="notifyType"
- value="slack"
- checked={notifyType === NotifyType.SLACK}
- onChange={() => {
- setNotifyType(NotifyType.SLACK);
- }}
- />
- <label className="form-label form-check-label" htmlFor="slack">
- <p className="fw-bold">Slack</p>
- </label>
- </div>
- </div>
- {notifyType === NotifyType.Email ? (
- <>
- <div className="input-group notify-to-option" id="mail-input">
- <div>
- <span className="input-group-text" id="mail-addon"></span>
- <span className="material-symbols-outlined">mail</span>
- </div>
- <input
- className="form-control"
- type="text"
- aria-describedby="mail-addon"
- name="toEmail"
- placeholder="Email"
- value={emailToSend}
- onChange={(e) => {
- setEmailToSend(e.target.value);
- }}
- />
- </div>
- <p className="p-2">
- {!isMailerSetup && (
- <span
- className="form-text text-muted"
- // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
- dangerouslySetInnerHTML={{
- __html: t('admin:mailer_setup_required'),
- }}
- />
- )}
- <b>Hint: </b>
- <a href="https://ifttt.com/create" target="blank">
- {t('notification_settings.email.ifttt_link')}
- <span className="material-symbols-outlined">share</span>
- </a>
- </p>
- </>
- ) : (
- <>
- <div className="input-group notify-to-option" id="slack-input">
- <div>
- <span
- className="input-group-text"
- id="slack-channel-addon"
- ></span>
- <span className="material-symbols-outlined">tag</span>
- </div>
- <input
- className="form-control"
- type="text"
- aria-describedby="slack-channel-addon"
- name="notificationGlobal[slackChannels]"
- placeholder="Slack Channel"
- value={slackChannelToSend}
- onChange={(e) => {
- setSlackChannelToSend(e.target.value);
- }}
- />
- </div>
- <p className="p-2">
- <span
- // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
- dangerouslySetInnerHTML={{
- __html: t('notification_settings.channel_desc'),
- }}
- />
- </p>
- </>
- )}
- </div>
- <div className="offset-1 col-sm-5">
- <div>
- <h3>{t('notification_settings.trigger_events')}</h3>
- <div className="my-1">
- <TriggerEventCheckBox
- checkbox="success"
- event={TriggerEventType.CREATE}
- checked={triggerEvents.has(TriggerEventType.CREATE)}
- onChange={() => onChangeTriggerEvents(TriggerEventType.CREATE)}
- >
- <span className="badge rounded-pill bg-success">
- <span className="material-symbols-outlined">edit_note</span>{' '}
- CREATE
- </span>
- </TriggerEventCheckBox>
- </div>
- <div className="my-1">
- <TriggerEventCheckBox
- checkbox="warning"
- event={TriggerEventType.EDIT}
- checked={triggerEvents.has(TriggerEventType.EDIT)}
- onChange={() => onChangeTriggerEvents(TriggerEventType.EDIT)}
- >
- <span className="badge rounded-pill bg-warning text-dark">
- <span className="imaterial-symbols-outlined">edit</span> EDIT
- </span>
- </TriggerEventCheckBox>
- </div>
- <div className="my-1">
- <TriggerEventCheckBox
- checkbox="pink"
- event={TriggerEventType.MOVE}
- checked={triggerEvents.has(TriggerEventType.MOVE)}
- onChange={() => onChangeTriggerEvents(TriggerEventType.MOVE)}
- >
- <span className="badge rounded-pill bg-pink">
- <span className="material-symbols-outlined">redo</span>MOVE
- </span>
- </TriggerEventCheckBox>
- </div>
- <div className="my-1">
- <TriggerEventCheckBox
- checkbox="danger"
- event="pageDelete"
- checked={triggerEvents.has(TriggerEventType.DELETE)}
- onChange={() => onChangeTriggerEvents(TriggerEventType.DELETE)}
- >
- <span className="badge rounded-pill bg-danger">
- <span className="material-symbols-outlined">
- delete_forever
- </span>
- DELETE
- </span>
- </TriggerEventCheckBox>
- </div>
- <div className="my-1">
- <TriggerEventCheckBox
- checkbox="info"
- event={TriggerEventType.LIKE}
- checked={triggerEvents.has(TriggerEventType.LIKE)}
- onChange={() => onChangeTriggerEvents(TriggerEventType.LIKE)}
- >
- <span className="badge rounded-pill bg-info">
- <span className="material-symbols-outlined">favorite</span>
- LIKE
- </span>
- </TriggerEventCheckBox>
- </div>
- <div className="my-1">
- <TriggerEventCheckBox
- checkbox="secondary"
- event={TriggerEventType.POST}
- checked={triggerEvents.has(TriggerEventType.POST)}
- onChange={() => onChangeTriggerEvents(TriggerEventType.POST)}
- >
- <span className="badge rounded-pill bg-primary">
- <span className="material-symbols-outlined">language</span>
- POST
- </span>
- </TriggerEventCheckBox>
- </div>
- </div>
- </div>
- </div>
- <AdminUpdateButtonRow
- onClick={updateButtonClickedHandler}
- disabled={false}
- />
- </>
- );
- };
- export default ManageGlobalNotification;
|