ManageGlobalNotification.tsx 12 KB

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