import React, { type JSX, useCallback, useEffect, useState } from 'react'; import { SlackbotType } from '@growi/slack'; import { LoadingSpinner } from '@growi/ui/dist/components'; import { useTranslation } from 'next-i18next'; import { apiv3Delete, apiv3Get, apiv3Post, apiv3Put, } from '~/client/util/apiv3-client'; import { toastError, toastSuccess } from '~/client/util/toastr'; import { BotTypeCard } from './BotTypeCard'; import ConfirmBotChangeModal from './ConfirmBotChangeModal'; import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings'; import CustomBotWithProxySettings from './CustomBotWithProxySettings'; import { DeleteSlackBotSettingsModal } from './DeleteSlackBotSettingsModal'; import OfficialBotSettings from './OfficialBotSettings'; const botTypes = Object.values(SlackbotType); export const SlackIntegration = (): JSX.Element => { const { t } = useTranslation(); const [currentBotType, setCurrentBotType] = useState< SlackbotType | undefined >(); const [selectedBotType, setSelectedBotType] = useState< SlackbotType | undefined >(); const [slackSigningSecret, setSlackSigningSecret] = useState(null); const [slackBotToken, setSlackBotToken] = useState(null); const [slackSigningSecretEnv, setSlackSigningSecretEnv] = useState(''); const [slackBotTokenEnv, setSlackBotTokenEnv] = useState(''); const [commandPermission, setCommandPermission] = useState(null); const [eventActionsPermission, setEventActionsPermission] = useState(null); const [isDeleteConfirmModalShown, setIsDeleteConfirmModalShown] = useState(false); const [slackAppIntegrations, setSlackAppIntegrations] = useState(); const [proxyServerUri, setProxyServerUri] = useState(); const [connectionStatuses, setConnectionStatuses] = useState({}); const [errorMsg, setErrorMsg] = useState(null); const [errorCode, setErrorCode] = useState(null); const [isLoading, setIsLoading] = useState(true); const fetchSlackIntegrationData = useCallback(async () => { try { const { data } = await apiv3Get('/slack-integration-settings'); const { slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars, slackAppIntegrations, proxyServerUri, commandPermission, eventActionsPermission, } = data.settings; setErrorMsg(data.errorMsg); setErrorCode(data.errorCode); setConnectionStatuses(data.connectionStatuses); setCurrentBotType(data.currentBotType); setSlackSigningSecret(slackSigningSecret); setSlackBotToken(slackBotToken); setSlackSigningSecretEnv(slackSigningSecretEnvVars); setSlackBotTokenEnv(slackBotTokenEnvVars); setSlackAppIntegrations(slackAppIntegrations); setProxyServerUri(proxyServerUri); setCommandPermission(commandPermission); setEventActionsPermission(eventActionsPermission); } catch (err) { toastError(err); } finally { setIsLoading(false); } }, []); const resetAllSettings = async () => { try { await apiv3Delete('/slack-integration-settings/bot-type'); fetchSlackIntegrationData(); toastSuccess(t('admin:slack_integration.bot_all_reset_successful')); } catch (error) { toastError(error); } }; const createSlackIntegrationData = async () => { try { await apiv3Post('/slack-integration-settings/slack-app-integrations'); fetchSlackIntegrationData(); toastSuccess( t( 'admin:slack_integration.adding_slack_ws_integration_settings_successful', ), ); } catch (error) { toastError(error); } }; const changeSecretAndToken = (secret, token) => { setSlackSigningSecret(secret); setSlackBotToken(token); }; useEffect(() => { fetchSlackIntegrationData(); }, [fetchSlackIntegrationData]); const changeCurrentBotSettings = async (botType?: SlackbotType) => { try { await apiv3Put('/slack-integration-settings/bot-type', { currentBotType: botType, }); setSelectedBotType(undefined); fetchSlackIntegrationData(); } catch (err) { toastError(err); } }; const botTypeSelectHandler = async (botType: SlackbotType) => { if (botType === currentBotType) { return; } if (currentBotType == null) { return changeCurrentBotSettings(botType); } setSelectedBotType(botType); }; const changeCurrentBotSettingsHandler = async () => { changeCurrentBotSettings(selectedBotType); toastSuccess(t('admin:slack_integration.bot_reset_successful')); }; const cancelBotChangeHandler = () => { setSelectedBotType(undefined); }; let settingsComponent = <>; switch (currentBotType) { case SlackbotType.OFFICIAL: settingsComponent = ( ); break; case SlackbotType.CUSTOM_WITHOUT_PROXY: settingsComponent = ( ); break; case SlackbotType.CUSTOM_WITH_PROXY: settingsComponent = ( ); break; } if (isLoading) { return (
); } return (
setIsDeleteConfirmModalShown(false)} onClickDeleteButton={resetAllSettings} />

{t('admin:slack_integration.selecting_bot_types.slack_bot')} {t('admin:slack_integration.selecting_bot_types.slack_bot')}

{errorCode && (
ERROR: {errorMsg} ({errorCode})
)}
{botTypes.map((botType) => { return (
); })}
{settingsComponent}
); }; SlackIntegration.displayName = 'SlackIntegration';