import React, { useCallback, useState } from 'react'; import PropTypes from 'prop-types'; import { useTranslation } from 'react-i18next'; import { defaultSupportedCommandsNameForBroadcastUse, defaultSupportedCommandsNameForSingleUse, defaultSupportedSlackEventActions } from '@growi/slack'; import loggerFactory from '~/utils/logger'; import { toastSuccess, toastError } from '../../../client/util/apiNotification'; const logger = loggerFactory('growi:SlackIntegration:ManageCommandsProcess'); const PermissionTypes = { ALLOW_ALL: 'allowAll', DENY_ALL: 'denyAll', ALLOW_SPECIFIED: 'allowSpecified', }; const CommandUsageTypes = { BROADCAST_USE: 'broadcastUse', SINGLE_USE: 'singleUse', }; const EventTypes = { LINK_SHARING: 'linkSharing', }; // A utility function that returns the new state but identical to the previous state const getUpdatedChannelsList = (prevState, commandName, value) => { // string to array const allowedChannelsArray = value.split(','); // trim whitespace from all elements const trimedAllowedChannelsArray = allowedChannelsArray.map(channelName => channelName.trim()); prevState[commandName] = trimedAllowedChannelsArray; return prevState; }; // A utility function that returns the new state const getUpdatedPermissionSettings = (prevState, commandName, value) => { const newState = { ...prevState }; switch (value) { case PermissionTypes.ALLOW_ALL: newState[commandName] = true; break; case PermissionTypes.DENY_ALL: newState[commandName] = false; break; case PermissionTypes.ALLOW_SPECIFIED: newState[commandName] = []; break; default: logger.error('Not implemented'); break; } return newState; }; // A utility function that returns the permission type from the permission value const getPermissionTypeFromValue = (value) => { if (Array.isArray(value)) { return PermissionTypes.ALLOW_SPECIFIED; } if (typeof value === 'boolean') { return value ? PermissionTypes.ALLOW_ALL : PermissionTypes.DENY_ALL; } logger.error('The value type must be boolean or string[]'); }; const PermissionSettingForEachPermissionTypeComponent = ({ keyName, onUpdatePermissions, onUpdateChannels, singleCommandDescription, allowedChannelsDescription, currentPermissionType, permissionSettings, }) => { const { t } = useTranslation(); const hiddenClass = currentPermissionType === PermissionTypes.ALLOW_SPECIFIED ? '' : 'd-none'; const permission = permissionSettings[keyName]; if (permission === undefined) logger.error('Must be implemented'); const textareaDefaultValue = Array.isArray(permission) ? permission.join(',') : ''; return (

{keyName} {singleCommandDescription && ( { singleCommandDescription } )}