import React, { useEffect } from 'react'; import PropTypes from 'prop-types'; import { HashLink as Link } from 'react-router-hash-link'; import { Trans, useTranslation } from 'react-i18next'; import classNames from 'classnames'; import Statistics from './Statistics'; import Counters from './Counters'; import Clients from './Clients'; import QueriedDomains from './QueriedDomains'; import BlockedDomains from './BlockedDomains'; import { DISABLE_PROTECTION_TIMINGS, ONE_SECOND_IN_MS, SETTINGS_URLS } from '../../helpers/constants'; import { msToSeconds, msToMinutes, msToHours, msToDays, } from '../../helpers/helpers'; import PageTitle from '../ui/PageTitle'; import Loading from '../ui/Loading'; import './Dashboard.css'; import Dropdown from '../ui/Dropdown'; import UpstreamResponses from './UpstreamResponses'; import UpstreamAvgTime from './UpstreamAvgTime'; const Dashboard = ({ getAccessList, getStats, getStatsConfig, dashboard, dashboard: { protectionEnabled, processingProtection, protectionDisabledDuration }, toggleProtection, stats, access, }) => { const { t } = useTranslation(); const getAllStats = () => { getAccessList(); getStats(); getStatsConfig(); }; useEffect(() => { getAllStats(); }, []); const getSubtitle = () => { const ONE_DAY = 1; const intervalInDays = msToDays(stats.interval); if (intervalInDays < ONE_DAY) { return t('stats_disabled_short'); } return intervalInDays === ONE_DAY ? t('for_last_24_hours') : t('for_last_days', { count: msToDays(stats.interval) }); }; const buttonClass = classNames('btn btn-sm dashboard-protection-button', { 'btn-gray': protectionEnabled, 'btn-success': !protectionEnabled, }); const refreshButton = ; const statsProcessing = stats.processingStats || stats.processingGetConfig || access.processing; const subtitle = getSubtitle(); const DISABLE_PROTECTION_ITEMS = [ { text: t('disable_for_seconds', { count: msToSeconds(DISABLE_PROTECTION_TIMINGS.HALF_MINUTE) }), disableTime: DISABLE_PROTECTION_TIMINGS.HALF_MINUTE, }, { text: t('disable_for_minutes', { count: msToMinutes(DISABLE_PROTECTION_TIMINGS.MINUTE) }), disableTime: DISABLE_PROTECTION_TIMINGS.MINUTE, }, { text: t('disable_for_minutes', { count: msToMinutes(DISABLE_PROTECTION_TIMINGS.TEN_MINUTES) }), disableTime: DISABLE_PROTECTION_TIMINGS.TEN_MINUTES, }, { text: t('disable_for_hours', { count: msToHours(DISABLE_PROTECTION_TIMINGS.HOUR) }), disableTime: DISABLE_PROTECTION_TIMINGS.HOUR, }, { text: t('disable_until_tomorrow'), disableTime: DISABLE_PROTECTION_TIMINGS.TOMORROW, }, ]; const getDisableProtectionItems = () => ( Object.values(DISABLE_PROTECTION_ITEMS) .map((item, index) => (
{ toggleProtection(protectionEnabled, item.disableTime - ONE_SECOND_IN_MS); }} > {item.text}
)) ); const getRemaningTimeText = (milliseconds) => { if (!milliseconds) { return ''; } const date = new Date(milliseconds); const hh = date.getUTCHours(); const mm = `0${date.getUTCMinutes()}`.slice(-2); const ss = `0${date.getUTCSeconds()}`.slice(-2); const formattedHH = `0${hh}`.slice(-2); return hh ? `${formattedHH}:${mm}:${ss}` : `${mm}:${ss}`; }; const getProtectionBtnText = (status) => (status ? t('disable_protection') : t('enable_protection')); return <>
{protectionEnabled && {getDisableProtectionItems()} }
{statsProcessing && } {!statsProcessing &&
{stats.interval === 0 && (
link , ]}> stats_disabled
)}
} ; }; Dashboard.propTypes = { dashboard: PropTypes.object.isRequired, stats: PropTypes.object.isRequired, access: PropTypes.object.isRequired, getStats: PropTypes.func.isRequired, getStatsConfig: PropTypes.func.isRequired, toggleProtection: PropTypes.func.isRequired, getClients: PropTypes.func.isRequired, getAccessList: PropTypes.func.isRequired, }; export default Dashboard;