ManageGlobalNotification.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import React, {
  2. useCallback, useMemo, useEffect, useState, type JSX,
  3. } from 'react';
  4. import { useAtomValue } from 'jotai';
  5. import { useTranslation } from 'next-i18next';
  6. import Link from 'next/link';
  7. import { useRouter } from 'next/router';
  8. import { NotifyType, TriggerEventType } from '~/client/interfaces/global-notification';
  9. import { apiv3Post } from '~/client/util/apiv3-client';
  10. import { toastError } from '~/client/util/toastr';
  11. import { isMailerSetupAtom } from '~/states/server-configurations';
  12. import { useSWRxGlobalNotification } from '~/stores/global-notification';
  13. import loggerFactory from '~/utils/logger';
  14. import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow';
  15. import TriggerEventCheckBox from './TriggerEventCheckBox';
  16. const logger = loggerFactory('growi:manageGlobalNotification');
  17. type Props = {
  18. globalNotificationId?: string,
  19. }
  20. const ManageGlobalNotification = (props: Props): JSX.Element => {
  21. const [triggerPath, setTriggerPath] = useState('');
  22. const [notifyType, setNotifyType] = useState<NotifyType>(NotifyType.Email);
  23. const [emailToSend, setEmailToSend] = useState('');
  24. const [slackChannelToSend, setSlackChannelToSend] = useState('');
  25. const [triggerEvents, setTriggerEvents] = useState(new Set());
  26. const { data: globalNotificationData, update: updateGlobalNotification } = useSWRxGlobalNotification(props.globalNotificationId || '');
  27. const globalNotification = useMemo(() => globalNotificationData?.globalNotification, [globalNotificationData?.globalNotification]);
  28. const router = useRouter();
  29. useEffect(() => {
  30. if (globalNotification != null) {
  31. const notifyType = globalNotification.__t;
  32. setNotifyType(notifyType);
  33. setTriggerPath(globalNotification.triggerPath);
  34. setTriggerEvents(new Set(globalNotification.triggerEvents));
  35. if (notifyType === NotifyType.Email) {
  36. setEmailToSend(globalNotification.toEmail);
  37. }
  38. else {
  39. setSlackChannelToSend(globalNotification.slackChannels);
  40. }
  41. }
  42. }, [globalNotification]);
  43. const isLoading = globalNotificationData === undefined;
  44. const notExistsGlobalNotification = !isLoading && globalNotificationData == null;
  45. useEffect(() => {
  46. if (notExistsGlobalNotification) {
  47. router.push('/admin/notification');
  48. }
  49. }, [notExistsGlobalNotification, router]);
  50. const onChangeTriggerEvents = useCallback((triggerEvent) => {
  51. let newTriggerEvents;
  52. if (triggerEvents.has(triggerEvent)) {
  53. newTriggerEvents = ([...triggerEvents].filter(item => item !== triggerEvent));
  54. setTriggerEvents(new Set(newTriggerEvents));
  55. }
  56. else {
  57. newTriggerEvents = [...triggerEvents, triggerEvent];
  58. setTriggerEvents(new Set(newTriggerEvents));
  59. }
  60. }, [triggerEvents]);
  61. const updateButtonClickedHandler = useCallback(async() => {
  62. const requestParams = {
  63. triggerPath,
  64. notifyType,
  65. toEmail: emailToSend,
  66. slackChannels: slackChannelToSend,
  67. triggerEvents: [...triggerEvents],
  68. };
  69. try {
  70. if (props.globalNotificationId != null) {
  71. await updateGlobalNotification(requestParams);
  72. router.push('/admin/notification');
  73. }
  74. else {
  75. await apiv3Post('/notification-setting/global-notification', requestParams);
  76. router.push('/admin/notification');
  77. }
  78. }
  79. catch (err) {
  80. toastError(err);
  81. logger.error(err);
  82. }
  83. }, [emailToSend, notifyType, props.globalNotificationId, router, slackChannelToSend, triggerEvents, triggerPath, updateGlobalNotification]);
  84. // Mailer setup status (unused yet but kept for potential conditional logic)
  85. const isMailerSetup = useAtomValue(isMailerSetupAtom);
  86. const { t } = useTranslation('admin');
  87. return (
  88. <>
  89. <div className="my-3">
  90. <Link href="/admin/notification" className="btn btn-outline-secondary">
  91. <span className="material-symbols-outlined" aria-hidden="true">arrow_left_alt</span>
  92. {t('notification_settings.back_to_list')}
  93. </Link>
  94. </div>
  95. <div className="row">
  96. <div className="form-box col-md-12">
  97. <h2 className="border-bottom mb-5">{t('notification_settings.notification_detail')}</h2>
  98. </div>
  99. <div className="col-sm-4">
  100. <h3>
  101. <label htmlFor="triggerPath" className="form-label">{t('notification_settings.trigger_path')}
  102. {/* eslint-disable-next-line react/no-danger */}
  103. <small dangerouslySetInnerHTML={{ __html: t('notification_settings.trigger_path_help', '<code>*</code>') }} />
  104. </label>
  105. </h3>
  106. <div>
  107. <input
  108. className="form-control"
  109. type="text"
  110. name="triggerPath"
  111. value={triggerPath}
  112. onChange={(e) => { setTriggerPath(e.target.value) }}
  113. required
  114. />
  115. </div>
  116. <h3>{t('notification_settings.notify_to')}</h3>
  117. <div>
  118. <div className="form-check">
  119. <input
  120. className="form-check-input"
  121. type="radio"
  122. id="mail"
  123. name="notifyType"
  124. value="mail"
  125. checked={notifyType === NotifyType.Email}
  126. onChange={() => { setNotifyType(NotifyType.Email) }}
  127. />
  128. <label className="form-label form-check-label" htmlFor="mail">
  129. <p className="fw-bold">Email</p>
  130. </label>
  131. </div>
  132. <div className="form-check ms-2">
  133. <input
  134. className="form-check-input"
  135. type="radio"
  136. id="slack"
  137. name="notifyType"
  138. value="slack"
  139. checked={notifyType === NotifyType.SLACK}
  140. onChange={() => { setNotifyType(NotifyType.SLACK) }}
  141. />
  142. <label className="form-label form-check-label" htmlFor="slack">
  143. <p className="fw-bold">Slack</p>
  144. </label>
  145. </div>
  146. </div>
  147. {notifyType === NotifyType.Email
  148. ? (
  149. <>
  150. <div className="input-group notify-to-option" id="mail-input">
  151. <div>
  152. <span className="input-group-text" id="mail-addon"></span><span className="material-symbols-outlined">mail</span>
  153. </div>
  154. <input
  155. className="form-control"
  156. type="text"
  157. aria-describedby="mail-addon"
  158. name="toEmail"
  159. placeholder="Email"
  160. value={emailToSend}
  161. onChange={(e) => { setEmailToSend(e.target.value) }}
  162. />
  163. </div>
  164. <p className="p-2">
  165. {/* eslint-disable-next-line react/no-danger */}
  166. {!isMailerSetup && <span className="form-text text-muted" dangerouslySetInnerHTML={{ __html: t('admin:mailer_setup_required') }} />}
  167. <b>Hint: </b>
  168. <a href="https://ifttt.com/create" target="blank">{t('notification_settings.email.ifttt_link')}
  169. <span className="material-symbols-outlined">share</span>
  170. </a>
  171. </p>
  172. </>
  173. )
  174. : (
  175. <>
  176. <div className="input-group notify-to-option" id="slack-input">
  177. <div>
  178. <span className="input-group-text" id="slack-channel-addon"></span><span className="material-symbols-outlined">tag</span>
  179. </div>
  180. <input
  181. className="form-control"
  182. type="text"
  183. aria-describedby="slack-channel-addon"
  184. name="notificationGlobal[slackChannels]"
  185. placeholder="Slack Channel"
  186. value={slackChannelToSend}
  187. onChange={(e) => { setSlackChannelToSend(e.target.value) }}
  188. />
  189. </div>
  190. <p className="p-2">
  191. {/* eslint-disable-next-line react/no-danger */}
  192. <span dangerouslySetInnerHTML={{ __html: t('notification_settings.channel_desc') }} />
  193. </p>
  194. </>
  195. )}
  196. </div>
  197. <div className="offset-1 col-sm-5">
  198. <div>
  199. <h3>{t('notification_settings.trigger_events')}</h3>
  200. <div className="my-1">
  201. <TriggerEventCheckBox
  202. checkbox="success"
  203. event={TriggerEventType.CREATE}
  204. checked={triggerEvents.has(TriggerEventType.CREATE)}
  205. onChange={() => onChangeTriggerEvents(TriggerEventType.CREATE)}
  206. >
  207. <span className="badge rounded-pill bg-success">
  208. <span className="material-symbols-outlined">edit_note</span> CREATE
  209. </span>
  210. </TriggerEventCheckBox>
  211. </div>
  212. <div className="my-1">
  213. <TriggerEventCheckBox
  214. checkbox="warning"
  215. event={TriggerEventType.EDIT}
  216. checked={triggerEvents.has(TriggerEventType.EDIT)}
  217. onChange={() => onChangeTriggerEvents(TriggerEventType.EDIT)}
  218. >
  219. <span className="badge rounded-pill bg-warning text-dark">
  220. <span className="imaterial-symbols-outlined">edit</span> EDIT
  221. </span>
  222. </TriggerEventCheckBox>
  223. </div>
  224. <div className="my-1">
  225. <TriggerEventCheckBox
  226. checkbox="pink"
  227. event={TriggerEventType.MOVE}
  228. checked={triggerEvents.has(TriggerEventType.MOVE)}
  229. onChange={() => onChangeTriggerEvents(TriggerEventType.MOVE)}
  230. >
  231. <span className="badge rounded-pill bg-pink">
  232. <span className="material-symbols-outlined">redo</span>MOVE
  233. </span>
  234. </TriggerEventCheckBox>
  235. </div>
  236. <div className="my-1">
  237. <TriggerEventCheckBox
  238. checkbox="danger"
  239. event="pageDelete"
  240. checked={triggerEvents.has(TriggerEventType.DELETE)}
  241. onChange={() => onChangeTriggerEvents(TriggerEventType.DELETE)}
  242. >
  243. <span className="badge rounded-pill bg-danger">
  244. <span className="material-symbols-outlined">delete_forever</span>DELETE
  245. </span>
  246. </TriggerEventCheckBox>
  247. </div>
  248. <div className="my-1">
  249. <TriggerEventCheckBox
  250. checkbox="info"
  251. event={TriggerEventType.LIKE}
  252. checked={triggerEvents.has(TriggerEventType.LIKE)}
  253. onChange={() => onChangeTriggerEvents(TriggerEventType.LIKE)}
  254. >
  255. <span className="badge rounded-pill bg-info">
  256. <span className="material-symbols-outlined">favorite</span>LIKE
  257. </span>
  258. </TriggerEventCheckBox>
  259. </div>
  260. <div className="my-1">
  261. <TriggerEventCheckBox
  262. checkbox="secondary"
  263. event={TriggerEventType.POST}
  264. checked={triggerEvents.has(TriggerEventType.POST)}
  265. onChange={() => onChangeTriggerEvents(TriggerEventType.POST)}
  266. >
  267. <span className="badge rounded-pill bg-primary">
  268. <span className="material-symbols-outlined">language</span>POST
  269. </span>
  270. </TriggerEventCheckBox>
  271. </div>
  272. </div>
  273. </div>
  274. </div>
  275. <AdminUpdateButtonRow
  276. onClick={updateButtonClickedHandler}
  277. disabled={false}
  278. />
  279. </>
  280. );
  281. };
  282. export default ManageGlobalNotification;