ManageCommandsProcessWithoutProxy.jsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import React, { useCallback, useEffect, useState } from 'react';
  2. import { defaultSupportedCommandsNameForBroadcastUse, defaultSupportedCommandsNameForSingleUse, defaultSupportedSlackEventActions } from '@growi/slack';
  3. import PropTypes from 'prop-types';
  4. import { useTranslation } from 'react-i18next';
  5. import { apiv3Put } from '~/client/util/apiv3-client';
  6. import loggerFactory from '~/utils/logger';
  7. import { toastSuccess, toastError } from '../../../client/util/apiNotification';
  8. const logger = loggerFactory('growi:SlackIntegration:ManageCommandsProcess');
  9. const PermissionTypes = {
  10. ALLOW_ALL: 'allowAll',
  11. DENY_ALL: 'denyAll',
  12. ALLOW_SPECIFIED: 'allowSpecified',
  13. };
  14. const defaultCommandsName = [...defaultSupportedCommandsNameForBroadcastUse, ...defaultSupportedCommandsNameForSingleUse];
  15. // A utility function that returns the new state but identical to the previous state
  16. const getUpdatedChannelsList = (commandPermissionObj, commandName, value) => {
  17. // string to array
  18. const allowedChannelsArray = value.split(',');
  19. // trim whitespace from all elements
  20. const trimedAllowedChannelsArray = allowedChannelsArray.map(channelName => channelName.trim());
  21. commandPermissionObj[commandName] = trimedAllowedChannelsArray;
  22. return commandPermissionObj;
  23. };
  24. // A utility function that returns the new state
  25. const getUpdatedPermissionSettings = (commandPermissionObj, commandName, value) => {
  26. const editedCommandPermissionObj = { ...commandPermissionObj };
  27. switch (value) {
  28. case PermissionTypes.ALLOW_ALL:
  29. editedCommandPermissionObj[commandName] = true;
  30. break;
  31. case PermissionTypes.DENY_ALL:
  32. editedCommandPermissionObj[commandName] = false;
  33. break;
  34. case PermissionTypes.ALLOW_SPECIFIED:
  35. editedCommandPermissionObj[commandName] = [];
  36. break;
  37. default:
  38. logger.error('Not implemented');
  39. break;
  40. }
  41. return editedCommandPermissionObj;
  42. };
  43. const SinglePermissionSettingComponent = ({
  44. commandName, editingCommandPermission, onPermissionTypeClicked, onPermissionListChanged,
  45. }) => {
  46. const { t } = useTranslation();
  47. if (editingCommandPermission == null) {
  48. return null;
  49. }
  50. function permissionTypeClickHandler(e) {
  51. if (onPermissionTypeClicked == null) {
  52. return;
  53. }
  54. onPermissionTypeClicked(e);
  55. }
  56. function onPermissionListChangeHandler(e) {
  57. if (onPermissionListChanged == null) {
  58. return;
  59. }
  60. onPermissionListChanged(e);
  61. }
  62. const permission = editingCommandPermission[commandName];
  63. const hiddenClass = Array.isArray(permission) ? '' : 'd-none';
  64. const textareaDefaultValue = Array.isArray(permission) ? permission.join(',') : '';
  65. return (
  66. <div className="my-1 mb-2">
  67. <div className="row align-items-center mb-3">
  68. <p className="col my-auto text-capitalize align-middle">{commandName}</p>
  69. <div className="col dropdown">
  70. <button
  71. className="btn btn-outline-secondary dropdown-toggle text-right col-12 col-md-auto"
  72. type="button"
  73. id="dropdownMenuButton"
  74. data-toggle="dropdown"
  75. aria-haspopup="true"
  76. aria-expanded="true"
  77. >
  78. <span className="float-left">
  79. {permission === true && t('admin:slack_integration.accordion.allow_all')}
  80. {permission === false && t('admin:slack_integration.accordion.deny_all')}
  81. {Array.isArray(permission) && t('admin:slack_integration.accordion.allow_specified')}
  82. </span>
  83. </button>
  84. <div className="dropdown-menu">
  85. <button
  86. className="dropdown-item"
  87. type="button"
  88. name={commandName}
  89. value={PermissionTypes.ALLOW_ALL}
  90. onClick={e => permissionTypeClickHandler(e)}
  91. >
  92. {t('admin:slack_integration.accordion.allow_all_long')}
  93. </button>
  94. <button
  95. className="dropdown-item"
  96. type="button"
  97. name={commandName}
  98. value={PermissionTypes.DENY_ALL}
  99. onClick={e => permissionTypeClickHandler(e)}
  100. >
  101. {t('admin:slack_integration.accordion.deny_all_long')}
  102. </button>
  103. <button
  104. className="dropdown-item"
  105. type="button"
  106. name={commandName}
  107. value={PermissionTypes.ALLOW_SPECIFIED}
  108. onClick={e => permissionTypeClickHandler(e)}
  109. >
  110. {t('admin:slack_integration.accordion.allow_specified_long')}
  111. </button>
  112. </div>
  113. </div>
  114. </div>
  115. <div className={`row-12 row-md-6 ${hiddenClass}`}>
  116. <textarea
  117. className="form-control"
  118. type="textarea"
  119. name={commandName}
  120. value={textareaDefaultValue}
  121. onChange={e => onPermissionListChangeHandler(e)}
  122. />
  123. <p className="form-text text-muted small">
  124. {t('admin:slack_integration.accordion.allowed_channels_description', { commandName })}
  125. <br />
  126. </p>
  127. </div>
  128. </div>
  129. );
  130. };
  131. SinglePermissionSettingComponent.propTypes = {
  132. commandName: PropTypes.string,
  133. editingCommandPermission: PropTypes.object,
  134. onPermissionTypeClicked: PropTypes.func,
  135. onPermissionListChanged: PropTypes.func,
  136. };
  137. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  138. const ManageCommandsProcessWithoutProxy = ({ commandPermission, eventActionsPermission }) => {
  139. const { t } = useTranslation();
  140. const [editingCommandPermission, setEditingCommandPermission] = useState({});
  141. const [editingEventActionsPermission, setEditingEventActionsPermission] = useState({});
  142. useEffect(() => {
  143. if (commandPermission == null) {
  144. return;
  145. }
  146. const updatedState = { ...commandPermission };
  147. setEditingCommandPermission(updatedState);
  148. }, [commandPermission]);
  149. useEffect(() => {
  150. if (eventActionsPermission == null) {
  151. return;
  152. }
  153. const updatedState = { ...eventActionsPermission };
  154. setEditingEventActionsPermission(updatedState);
  155. }, [eventActionsPermission]);
  156. const updatePermissionsCommandsState = useCallback((e) => {
  157. const { target } = e;
  158. const { name: commandName, value } = target;
  159. setEditingCommandPermission(commandPermissionObj => getUpdatedPermissionSettings(commandPermissionObj, commandName, value));
  160. }, []);
  161. const updatePermissionsEventsState = useCallback((e) => {
  162. const { target } = e;
  163. const { name: actionName, value } = target;
  164. setEditingEventActionsPermission(eventActionPermissionObj => getUpdatedPermissionSettings(eventActionPermissionObj, actionName, value));
  165. }, []);
  166. const updateCommandsChannelsListState = useCallback((e) => {
  167. const { target } = e;
  168. const { name: commandName, value } = target;
  169. setEditingCommandPermission(commandPermissionObj => ({ ...getUpdatedChannelsList(commandPermissionObj, commandName, value) }));
  170. }, []);
  171. const updateEventsChannelsListState = useCallback((e) => {
  172. const { target } = e;
  173. const { name: actionName, value } = target;
  174. setEditingEventActionsPermission(eventActionPermissionObj => ({ ...getUpdatedChannelsList(eventActionPermissionObj, actionName, value) }));
  175. }, []);
  176. const updateCommandsHandler = async(e) => {
  177. try {
  178. await apiv3Put('/slack-integration-settings/without-proxy/update-permissions', {
  179. commandPermission: editingCommandPermission,
  180. eventActionsPermission: editingEventActionsPermission,
  181. });
  182. toastSuccess(t('toaster.update_successed', { target: 'the permission for commands' }));
  183. }
  184. catch (err) {
  185. toastError(err);
  186. logger.error(err);
  187. }
  188. };
  189. return (
  190. <div className="py-4 px-5">
  191. <p className="mb-4 font-weight-bold">{t('admin:slack_integration.accordion.growi_commands')}</p>
  192. <div className="row d-flex flex-column align-items-center">
  193. <div className="col-8">
  194. <div className="custom-control custom-checkbox">
  195. <div className="row mb-5 d-block">
  196. { defaultCommandsName.map((commandName) => {
  197. // eslint-disable-next-line max-len
  198. return (
  199. <SinglePermissionSettingComponent
  200. key={`${commandName}-component`}
  201. commandName={commandName}
  202. editingCommandPermission={editingCommandPermission}
  203. onPermissionTypeClicked={updatePermissionsCommandsState}
  204. onPermissionListChanged={updateCommandsChannelsListState}
  205. />
  206. );
  207. })}
  208. </div>
  209. </div>
  210. </div>
  211. </div>
  212. <p className="mb-4 font-weight-bold">Events</p>
  213. <div className="row d-flex flex-column align-items-center">
  214. <div className="col-8">
  215. <div className="custom-control custom-checkbox">
  216. <div className="row mb-5 d-block">
  217. { defaultSupportedSlackEventActions.map(actionName => (
  218. <SinglePermissionSettingComponent
  219. key={`${actionName}-component`}
  220. commandName={actionName}
  221. editingCommandPermission={editingEventActionsPermission}
  222. onPermissionTypeClicked={updatePermissionsEventsState}
  223. onPermissionListChanged={updateEventsChannelsListState}
  224. />
  225. ))}
  226. </div>
  227. </div>
  228. </div>
  229. </div>
  230. <div className="row">
  231. <button
  232. type="submit"
  233. className="btn btn-primary mx-auto"
  234. onClick={updateCommandsHandler}
  235. >
  236. { t('Update') }
  237. </button>
  238. </div>
  239. </div>
  240. );
  241. };
  242. ManageCommandsProcessWithoutProxy.propTypes = {
  243. commandPermission: PropTypes.object,
  244. eventActionsPermission: PropTypes.object,
  245. };
  246. export default ManageCommandsProcessWithoutProxy;