Files
AdGuardHome/client/src/components/Logs/Cells/ClientCell.js
Artem Baskal c72cd58f69 + client: Move the client access check to the server-side
Squashed commit of the following:

commit 1aab0f62e94ce665a1b996552fac41dc4e769b4d
Merge: cdf5eb6e c1f5fdae
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Thu Sep 24 15:36:05 2020 +0300

    Merge branch '1920-client-find' into feature/1925

commit cdf5eb6ea67a665d21a3155d8cf89bba9a5a9948
Merge: b6c20b1c 10f67bd3
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Wed Sep 23 20:28:51 2020 +0300

    Merge branch 'master' into feature/1925

commit b6c20b1c7359a0e5902405b0551712f936848a80
Merge: 97d388ef 96512433
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 15 10:44:25 2020 +0300

    Merge branch 'master' into feature/1925

commit 97d388ef6571d590f21da00f86d889e881ca0c3d
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 15 10:30:50 2020 +0300

    Extract buttons

commit ca45fde11fc2b2812ff2b84dbd67aff0b5341be1
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Thu Sep 10 12:46:09 2020 +0300

    Handle errors in updateLogs

commit f15e03c2e5a7115db984f70f72b0ddd870ece73d
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Thu Sep 10 12:39:34 2020 +0300

    Update mobile block status on click

commit 033b28db3b324f6d529ac1a0ef657886cdbe02bd
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Wed Sep 9 20:53:42 2020 +0300

    Fix mobile block buttons, auto open page on web serve start

commit 2730937b23309167a066b9154728ac53ffe81a49
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Wed Sep 9 13:58:37 2020 +0300

    Disable allow this client button when isNotInAllowedList is true

commit 818cf869d63654c184762ad2701c4429a3e3011e
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Wed Sep 9 13:06:01 2020 +0300

    Update client block state on option click

commit a072b8983757f419645c0207ea78e6e867c440cb
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 8 20:17:16 2020 +0300

    Adapt to api changes

commit 28ab2bd8b3f14f60bc822b5a69fa1801db67d816
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 8 14:12:20 2020 +0300

    Change query log block confirm messages

commit 9b0b6f6f9b1ec168fa71dbedd036152da59006e3
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 8 12:00:46 2020 +0300

    Refactor inner work with disallowed

commit 05f76154b8f489738d032fdaa835edb371ce70c7
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Mon Sep 7 16:11:37 2020 +0300

    + client: Move the client access check to the server-side
2020-09-24 15:48:37 +03:00

185 lines
6.8 KiB
JavaScript

import React, { useState } from 'react';
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
import { nanoid } from 'nanoid';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import propTypes from 'prop-types';
import { checkFiltered, getBlockingClientName } from '../../../helpers/helpers';
import { BLOCK_ACTIONS } from '../../../helpers/constants';
import { toggleBlocking, toggleBlockingForClient } from '../../../actions';
import IconTooltip from './IconTooltip';
import { renderFormattedClientCell } from '../../../helpers/renderFormattedClientCell';
import { toggleClientBlock } from '../../../actions/access';
import { getBlockClientInfo } from './helpers';
import { getStats } from '../../../actions/stats';
import { updateLogs } from '../../../actions/queryLogs';
const ClientCell = ({
client,
domain,
info,
info: {
name, whois_info, disallowed, disallowed_rule,
},
reason,
}) => {
const { t } = useTranslation();
const dispatch = useDispatch();
const autoClients = useSelector((state) => state.dashboard.autoClients, shallowEqual);
const processingRules = useSelector((state) => state.filtering.processingRules);
const isDetailed = useSelector((state) => state.queryLogs.isDetailed);
const [isOptionsOpened, setOptionsOpened] = useState(false);
const autoClient = autoClients.find((autoClient) => autoClient.name === client);
const source = autoClient?.source;
const whoisAvailable = whois_info && Object.keys(whois_info).length > 0;
const id = nanoid();
const data = {
address: client,
name,
country: whois_info?.country,
city: whois_info?.city,
network: whois_info?.orgname,
source_label: source,
};
const processedData = Object.entries(data);
const isFiltered = checkFiltered(reason);
const nameClass = classNames('w-90 o-hidden d-flex flex-column', {
'mt-2': isDetailed && !name && !whoisAvailable,
'white-space--nowrap': isDetailed,
});
const hintClass = classNames('icons mr-4 icon--24 icon--lightgray', {
'my-3': isDetailed,
});
const renderBlockingButton = (isFiltered, domain) => {
const buttonType = isFiltered ? BLOCK_ACTIONS.UNBLOCK : BLOCK_ACTIONS.BLOCK;
const clients = useSelector((state) => state.dashboard.clients);
const {
confirmMessage,
buttonKey: blockingClientKey,
isNotInAllowedList,
} = getBlockClientInfo(client, disallowed, disallowed_rule);
const blockingForClientKey = isFiltered ? 'unblock_for_this_client_only' : 'block_for_this_client_only';
const clientNameBlockingFor = getBlockingClientName(clients, client);
const BUTTON_OPTIONS = [
{
name: blockingForClientKey,
onClick: () => {
dispatch(toggleBlockingForClient(buttonType, domain, clientNameBlockingFor));
},
},
{
name: blockingClientKey,
onClick: async () => {
if (window.confirm(confirmMessage)) {
await dispatch(toggleClientBlock(client, disallowed, disallowed_rule));
await dispatch(updateLogs());
}
},
disabled: isNotInAllowedList,
},
];
const onClick = async () => {
await dispatch(toggleBlocking(buttonType, domain));
await dispatch(getStats());
};
const getOptions = (options) => {
if (options.length === 0) {
return null;
}
return <>{options.map(({ name, onClick, disabled }) => <button
key={name}
className="button-action--arrow-option px-4 py-2"
onClick={onClick}
disabled={disabled}
>{t(name)}
</button>)}</>;
};
const content = getOptions(BUTTON_OPTIONS);
const buttonClass = classNames('button-action button-action--main', {
'button-action--unblock': isFiltered,
'button-action--with-options': content,
'button-action--active': isOptionsOpened,
});
const buttonArrowClass = classNames('button-action button-action--arrow', {
'button-action--unblock': isFiltered,
'button-action--active': isOptionsOpened,
});
const containerClass = classNames('button-action__container', {
'button-action__container--detailed': isDetailed,
});
return <div className={containerClass}>
<button type="button"
className={buttonClass}
onClick={onClick}
disabled={processingRules}
>
{t(buttonType)}
</button>
{content && <button className={buttonArrowClass} disabled={processingRules}>
<IconTooltip
className='h-100'
tooltipClass='button-action--arrow-option-container'
xlinkHref='chevron-down'
triggerClass='button-action--icon'
content={content} placement="bottom-end" trigger="click"
onVisibilityChange={setOptionsOpened}
/>
</button>}
</div>;
};
return <div className="o-hidden h-100 logs__cell logs__cell--client" role="gridcell">
<IconTooltip className={hintClass} columnClass='grid grid--limited' tooltipClass='px-5 pb-5 pt-4 mw-75'
xlinkHref='question' contentItemClass="contentItemClass" title="client_details"
content={processedData} placement="bottom" />
<div className={nameClass}>
<div data-tip={true} data-for={id}>
{renderFormattedClientCell(client, info, isDetailed, true)}
</div>
{isDetailed && name && !whoisAvailable
&& <div className="detailed-info d-none d-sm-block logs__text"
title={name}>{name}</div>}
</div>
{renderBlockingButton(isFiltered, domain)}
</div>;
};
ClientCell.propTypes = {
client: propTypes.string.isRequired,
domain: propTypes.string.isRequired,
info: propTypes.oneOfType([
propTypes.string,
propTypes.shape({
name: propTypes.string.isRequired,
whois_info: propTypes.shape({
country: propTypes.string,
city: propTypes.string,
orgname: propTypes.string,
}),
disallowed: propTypes.bool.isRequired,
disallowed_rule: propTypes.string.isRequired,
}),
]),
reason: propTypes.string.isRequired,
};
export default ClientCell;