Updates #4420
Squashed commit of the following:
commit 461a59e1541626020bf0bcfaf34ba7d2f4509dc7
Merge: 5c5e7b5d 2a1ad532
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Apr 25 18:46:02 2022 +0300
Merge branch 'master' into 4420-loading-log
commit 5c5e7b5d1a69d30e40e71f49f46dea89fa8c40a2
Author: Ildar Kamalov <ik@adguard.com>
Date: Sun Apr 24 22:18:22 2022 +0300
client: fix constant loading for blocked requests
99 lines
2.9 KiB
JavaScript
99 lines
2.9 KiB
JavaScript
import React, {
|
|
useCallback,
|
|
useEffect,
|
|
useRef,
|
|
} from 'react';
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
import { useTranslation } from 'react-i18next';
|
|
import propTypes from 'prop-types';
|
|
import throttle from 'lodash/throttle';
|
|
import Loading from '../ui/Loading';
|
|
import Header from './Cells/Header';
|
|
import { getLogs } from '../../actions/queryLogs';
|
|
import Row from './Cells';
|
|
import { isScrolledIntoView } from '../../helpers/helpers';
|
|
import { QUERY_LOGS_PAGE_LIMIT } from '../../helpers/constants';
|
|
|
|
const InfiniteTable = ({
|
|
isLoading,
|
|
items,
|
|
isSmallScreen,
|
|
setDetailedDataCurrent,
|
|
setButtonType,
|
|
setModalOpened,
|
|
}) => {
|
|
const { t } = useTranslation();
|
|
const dispatch = useDispatch();
|
|
const loader = useRef(null);
|
|
const loadingRef = useRef(null);
|
|
|
|
const isEntireLog = useSelector((state) => state.queryLogs.isEntireLog);
|
|
const processingGetLogs = useSelector((state) => state.queryLogs.processingGetLogs);
|
|
const loading = isLoading || processingGetLogs;
|
|
|
|
const listener = useCallback(() => {
|
|
if (!loadingRef.current && loader.current && isScrolledIntoView(loader.current)) {
|
|
dispatch(getLogs());
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
loadingRef.current = processingGetLogs;
|
|
}, [processingGetLogs]);
|
|
|
|
useEffect(() => {
|
|
listener();
|
|
}, [items.length < QUERY_LOGS_PAGE_LIMIT, isEntireLog]);
|
|
|
|
useEffect(() => {
|
|
const THROTTLE_TIME = 100;
|
|
const throttledListener = throttle(listener, THROTTLE_TIME);
|
|
|
|
window.addEventListener('scroll', throttledListener);
|
|
return () => {
|
|
window.removeEventListener('scroll', throttledListener);
|
|
};
|
|
}, []);
|
|
|
|
const renderRow = (row, idx) => <Row
|
|
key={idx}
|
|
rowProps={row}
|
|
isSmallScreen={isSmallScreen}
|
|
setDetailedDataCurrent={setDetailedDataCurrent}
|
|
setButtonType={setButtonType}
|
|
setModalOpened={setModalOpened}
|
|
/>;
|
|
|
|
const isNothingFound = items.length === 0 && !processingGetLogs;
|
|
|
|
return (
|
|
<div className="logs__table" role="grid">
|
|
{loading && <Loading />}
|
|
<Header />
|
|
{isNothingFound ? (
|
|
<label className="logs__no-data">{t('nothing_found')}</label>
|
|
) : (
|
|
<>
|
|
{items.map(renderRow)}
|
|
{!isEntireLog && (
|
|
<div ref={loader} className="logs__loading text-center">
|
|
{t('loading_table_status')}
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
InfiniteTable.propTypes = {
|
|
isLoading: propTypes.bool.isRequired,
|
|
items: propTypes.array.isRequired,
|
|
isSmallScreen: propTypes.bool.isRequired,
|
|
setDetailedDataCurrent: propTypes.func.isRequired,
|
|
setButtonType: propTypes.func.isRequired,
|
|
setModalOpened: propTypes.func.isRequired,
|
|
};
|
|
|
|
export default InfiniteTable;
|