| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- import React, { useState, useEffect, useCallback } from 'react';
- import PropTypes from 'prop-types';
- import { useTranslation } from 'react-i18next';
- import AppContainer from '../../../services/AppContainer';
- import { withUnstatedContainers } from '../../UnstatedUtils';
- import { toastSuccess, toastError } from '../../../util/apiNotification';
- import AccessTokenSettings from './AccessTokenSettings';
- import OfficialBotSettings from './OfficialBotSettings';
- import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings';
- import CustomBotWithProxySettings from './CustomBotWithProxySettings';
- import ConfirmBotChangeModal from './ConfirmBotChangeModal';
- const SlackIntegration = (props) => {
- const { appContainer } = props;
- const { t } = useTranslation();
- const [currentBotType, setCurrentBotType] = useState(null);
- const [selectedBotType, setSelectedBotType] = useState(null);
- const [accessToken, setAccessToken] = useState('');
- const fetchData = useCallback(async() => {
- try {
- const response = await appContainer.apiv3.get('slack-integration/');
- const { currentBotType, accessToken } = response.data.slackBotSettingParams;
- setCurrentBotType(currentBotType);
- setAccessToken(accessToken);
- }
- catch (err) {
- toastError(err);
- }
- }, [appContainer.apiv3]);
- useEffect(() => {
- fetchData();
- }, [fetchData]);
- const handleBotTypeSelect = (clickedBotType) => {
- if (clickedBotType === currentBotType) {
- return;
- }
- if (currentBotType === null) {
- setCurrentBotType(clickedBotType);
- return;
- }
- setSelectedBotType(clickedBotType);
- };
- const cancelBotChangeHandler = () => {
- setSelectedBotType(null);
- };
- const changeCurrentBotSettingsHandler = async() => {
- try {
- const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
- slackSigningSecret: '',
- slackBotToken: '',
- currentBotType: selectedBotType,
- });
- setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
- setSelectedBotType(null);
- toastSuccess(t('admin:slack_integration.bot_reset_successful'));
- }
- catch (err) {
- toastError(err);
- }
- };
- const generateTokenHandler = async() => {
- try {
- await appContainer.apiv3.put('slack-integration/access-token');
- fetchData();
- }
- catch (err) {
- toastError(err);
- }
- };
- const discardTokenHandler = async() => {
- try {
- await appContainer.apiv3.delete('slack-integration/access-token');
- fetchData();
- toastSuccess(t('admin:slack_integration.bot_reset_successful'));
- }
- catch (err) {
- toastError(err);
- }
- };
- let settingsComponent = null;
- switch (currentBotType) {
- case 'official-bot':
- settingsComponent = <OfficialBotSettings />;
- break;
- case 'custom-bot-without-proxy':
- settingsComponent = (
- <CustomBotWithoutProxySettings />
- );
- break;
- case 'custom-bot-with-proxy':
- settingsComponent = <CustomBotWithProxySettings />;
- break;
- }
- const showBotTypeLevel = (level) => {
- return <span>{t(`admin:slack_integration.selecting_bot_types.${level}`)}</span>;
- };
- const showBotTypeLabel = (label) => {
- return <span>{t(`admin:slack_integration.selecting_bot_types.${label}`)}</span>;
- };
- const showBotTypeDiscription = (desc) => {
- return <span className={`bot-type-disc-${desc}`}>{t(`admin:slack_integration.selecting_bot_types.${desc}`)}</span>;
- };
- return (
- <>
- <ConfirmBotChangeModal
- isOpen={selectedBotType != null}
- onConfirmClick={changeCurrentBotSettingsHandler}
- onCancelClick={cancelBotChangeHandler}
- />
- <AccessTokenSettings
- accessToken={accessToken}
- onClickDiscardButton={discardTokenHandler}
- onClickGenerateToken={generateTokenHandler}
- />
- <div className="selecting-bot-type my-5">
- <h2 className="admin-setting-header mb-4">
- {t('admin:slack_integration.selecting_bot_types.slack_bot')}
- <span className="ml-2 btn-link">
- <span className="mr-1">{t('admin:slack_integration.selecting_bot_types.detailed_explanation')}</span>
- {/* TODO: add an appropriate link by GW-5614 */}
- <i className="fa fa-external-link" aria-hidden="true"></i>
- </span>
- </h2>
- {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
- <div className="row my-4">
- <div className="card-deck mx-auto">
- <div
- className={`card admin-bot-card mx-3 rounded border-radius-sm shadow ${currentBotType === 'official-bot' ? 'border-primary' : ''}`}
- onClick={() => handleBotTypeSelect('official-bot')}
- >
- <div>
- <h3 className={`card-header mb-0 py-3 text-center ${currentBotType === 'official-bot' ? 'bg-primary text-light' : ''}`}>
- <span className="mr-2">
- {t('admin:slack_integration.selecting_bot_types.official_bot')}
- </span>
- <span className="badge badge-info mr-2">
- {t('admin:slack_integration.selecting_bot_types.recommended')}
- </span>
- {/* TODO: add an appropriate link by GW-5614 */}
- <i className={`fa fa-external-link btn-link ${currentBotType === 'official-bot' ? 'bg-primary text-light' : ''}`} aria-hidden="true"></i>
- </h3>
- </div>
- <div className="card-body p-4">
- <p className="card-text">
- <div className="text-center">
- {showBotTypeLevel('for_beginners')}
- </div>
- <div className="my-4">
- <div className="d-flex justify-content-between mb-2">
- {showBotTypeLabel('set_up')}
- {showBotTypeDiscription('easy')}
- </div>
- <div className="d-flex justify-content-between mb-2">
- {showBotTypeLabel('multiple_workspaces_integration')}
- {showBotTypeDiscription('possible')}
- </div>
- <div className="d-flex justify-content-between">
- {showBotTypeLabel('security_control')}
- {showBotTypeDiscription('impossible')}
- </div>
- </div>
- </p>
- </div>
- </div>
- <div
- className={`card admin-bot-card mx-3 rounded shadow ${currentBotType === 'custom-bot-without-proxy' ? 'border-primary' : ''}`}
- onClick={() => handleBotTypeSelect('custom-bot-without-proxy')}
- >
- <h3 className={`card-header mb-0 py-3 text-center text-nowrap ${currentBotType === 'custom-bot-without-proxy' ? 'bg-primary text-light' : ''}`}>
- <span className="mr-2">
- {t('admin:slack_integration.selecting_bot_types.custom_bot')}
- </span>
- <span className="supplementary-desc mr-2">
- {t('admin:slack_integration.selecting_bot_types.without_proxy')}
- </span>
- {/* TODO: add an appropriate link by GW-5614 */}
- <i
- className={`fa fa-external-link btn-link ${currentBotType === 'custom-bot-without-proxy' ? 'bg-primary text-light' : ''}`}
- aria-hidden="true"
- >
- </i>
- </h3>
- <div className="card-body p-4">
- <p className="card-text">
- <div className="text-center">
- {showBotTypeLevel('for_intermediate')}
- </div>
- <div className="my-4">
- <div className="d-flex justify-content-between mb-2">
- {showBotTypeLabel('set_up')}
- {showBotTypeDiscription('normal')}
- </div>
- <div className="d-flex justify-content-between mb-2">
- {showBotTypeLabel('multiple_workspaces_integration')}
- {showBotTypeDiscription('impossible')}
- </div>
- <div className="d-flex justify-content-between">
- {showBotTypeLabel('security_control')}
- {showBotTypeDiscription('possible')}
- </div>
- </div>
- </p>
- </div>
- </div>
- <div
- className={`card admin-bot-card mx-3 rounded shadow ${currentBotType === 'custom-bot-with-proxy' ? 'border-primary' : ''}`}
- onClick={() => handleBotTypeSelect('custom-bot-with-proxy')}
- >
- <h3 className={`card-header mb-0 py-3 text-center text-nowrap ${currentBotType === 'custom-bot-with-proxy' ? 'bg-primary text-light' : ''}`}>
- <span className="mr-2">
- {t('admin:slack_integration.selecting_bot_types.custom_bot')}
- </span>
- <span className="supplementary-desc mr-2">
- {t('admin:slack_integration.selecting_bot_types.with_proxy')}
- </span>
- {/* TODO: add an appropriate link by GW-5614 */}
- <i
- className={`fa fa-external-link btn-link ${currentBotType === 'custom-bot-with-proxy' ? 'bg-primary text-light' : ''}`}
- aria-hidden="true"
- >
- </i>
- </h3>
- <div className="card-body p-4">
- <p className="card-text">
- <div className="text-center">
- {showBotTypeLevel('for_advanced')}
- </div>
- <div className="my-4">
- <div className="d-flex justify-content-between mb-2">
- {showBotTypeLabel('set_up')}
- {showBotTypeDiscription('hard')}
- </div>
- <div className="d-flex justify-content-between mb-2">
- {showBotTypeLabel('multiple_workspaces_integration')}
- {showBotTypeDiscription('possible')}
- </div>
- <div className="d-flex justify-content-between">
- {showBotTypeLabel('security_control')}
- {showBotTypeDiscription('impossible')}
- </div>
- </div>
- </p>
- </div>
- </div>
- </div>
- </div>
- </div>
- {settingsComponent}
- </>
- );
- };
- const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
- SlackIntegration.propTypes = {
- appContainer: PropTypes.instanceOf(AppContainer).isRequired,
- };
- export default SlackIntegrationWrapper;
|