import React, { useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'next-i18next'; import PropTypes from 'prop-types'; import { apiv3Delete, apiv3Put } from '~/client/util/apiv3-client'; import { toastError, toastSuccess } from '~/client/util/toastr'; import { useAppTitle } from '~/states/global'; import loggerFactory from '~/utils/logger'; import { CustomBotWithProxyConnectionStatus } from './CustomBotWithProxyConnectionStatus'; import { DeleteSlackBotSettingsModal } from './DeleteSlackBotSettingsModal'; import { SlackAppIntegrationControl } from './SlackAppIntegrationControl'; import WithProxyAccordions from './WithProxyAccordions'; const logger = loggerFactory( 'growi:cli:SlackIntegration:CustomBotWithProxySettings', ); const CustomBotWithProxySettings = (props) => { const { slackAppIntegrations, proxyServerUri, onClickAddSlackWorkspaceBtn, onPrimaryUpdated, connectionStatuses, onUpdateTokens, onSubmitForm, } = props; const [newProxyServerUri, setNewProxyServerUri] = useState(); const [integrationIdToDelete, setIntegrationIdToDelete] = useState(null); const [siteName, setSiteName] = useState(''); const { t } = useTranslation(); const appTitle = useAppTitle(); // componentDidUpdate useEffect(() => { setNewProxyServerUri(proxyServerUri); }, [proxyServerUri]); const addSlackAppIntegrationHandler = async () => { if (onClickAddSlackWorkspaceBtn != null) { onClickAddSlackWorkspaceBtn(); } }; const isPrimaryChangedHandler = useCallback( async (slackIntegrationToChange, newValue) => { // do nothing when turning off if (!newValue) { return; } try { await apiv3Put( `/slack-integration-settings/slack-app-integrations/${slackIntegrationToChange._id}/make-primary`, ); if (onPrimaryUpdated != null) { onPrimaryUpdated(); } toastSuccess( t('toaster.update_successed', { target: 'Primary', ns: 'commons' }), ); } catch (err) { toastError(err); logger.error('Failed to change isPrimary', err); } }, [t, onPrimaryUpdated], ); const deleteSlackAppIntegrationHandler = async () => { try { await apiv3Delete( `/slack-integration-settings/slack-app-integrations/${integrationIdToDelete}`, ); if (props.onDeleteSlackAppIntegration != null) { props.onDeleteSlackAppIntegration(); } toastSuccess( t('admin:slack_integration.toastr.delete_slack_integration_procedure'), ); } catch (err) { toastError(err); logger.error('Failed to delete', err); } }; const updateProxyUri = async () => { try { await apiv3Put('/slack-integration-settings/proxy-uri', { proxyUri: newProxyServerUri, }); toastSuccess( t('toaster.update_successed', { target: 'Proxy URL', ns: 'commons' }), ); } catch (err) { toastError(err); logger.error('Failed to update', err); } }; useEffect(() => { setSiteName(appTitle); }, [appTitle]); return ( <>