import { useCallback, useEffect, useState } from 'react';
import { SlackbotType } from '@growi/slack';
import { useTranslation } from 'next-i18next';
import PropTypes from 'prop-types';
import { TabContent, TabPane } from 'reactstrap';
import AdminNotificationContainer from '~/client/services/AdminNotificationContainer';
import { toastError } from '~/client/util/toastr';
import { toArrayIfNot } from '~/utils/array-utils';
import loggerFactory from '~/utils/logger';
import CustomNav from '../../CustomNavigation/CustomNav';
import { withUnstatedContainers } from '../../UnstatedUtils';
import GlobalNotification from './GlobalNotification';
import UserTriggerNotification from './UserTriggerNotification';
const logger = loggerFactory('growi:NotificationSetting');
const SettingsIcon = () => (
settings
);
const navTabMapping = {
user_trigger_notification: {
Icon: SettingsIcon,
i18n: 'User trigger notification',
},
global_notification: {
Icon: SettingsIcon,
i18n: 'Global notification',
},
};
const Badge = ({ isEnabled }) => {
const { t } = useTranslation('admin');
return isEnabled ? (
{t('external_notification.enabled')}
) : (
{t('external_notification.disabled')}
);
};
const SkeletonListItem = () => (
――
...
);
const SlackIntegrationListItem = ({ isEnabled, currentBotType }) => {
const { t } = useTranslation('admin');
const isCautionVisible =
currentBotType === SlackbotType.OFFICIAL ||
currentBotType === SlackbotType.CUSTOM_WITH_PROXY;
return (
{isCautionVisible && (
)}
);
};
const LegacySlackIntegrationListItem = ({ isEnabled }) => {
const { t } = useTranslation('admin');
return (
{isEnabled && (
)}
);
};
function NotificationSetting(props) {
const { adminNotificationContainer } = props;
const { t } = useTranslation('admin');
const [isMounted, setMounted] = useState(false);
const [activeTab, setActiveTab] = useState('user_trigger_notification');
const [activeComponents, setActiveComponents] = useState(
new Set(['user_trigger_notification']),
);
const switchActiveTab = (selectedTab) => {
setActiveTab(selectedTab);
setActiveComponents(activeComponents.add(selectedTab));
};
const fetchData = useCallback(async () => {
try {
await adminNotificationContainer.retrieveNotificationData();
} catch (err) {
const errs = toArrayIfNot(err);
toastError(errs);
logger.error(errs);
retrieveErrors = errs;
} finally {
setMounted(true);
}
}, [adminNotificationContainer]);
useEffect(() => {
fetchData();
}, [fetchData]);
const { isSlackbotConfigured, isSlackLegacyConfigured, currentBotType } =
adminNotificationContainer.state;
const isSlackEnabled = isSlackbotConfigured;
const isSlackLegacyEnabled = !isSlackbotConfigured && isSlackLegacyConfigured;
return (
{t('external_notification.header_status')}
{!isMounted && }
{isMounted && (
<>
{/* Legacy Slack Integration become visible only when new Slack Integration is disabled */}
{!isSlackEnabled && (
)}
>
)}
{t('notification_settings.notification_settings')}
{activeComponents.has('user_trigger_notification') && (
)}
{activeComponents.has('global_notification') && (
)}
);
}
const NotificationSettingWithUnstatedContainer = withUnstatedContainers(
NotificationSetting,
[AdminNotificationContainer],
);
NotificationSetting.propTypes = {
adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer)
.isRequired,
};
export default NotificationSettingWithUnstatedContainer;