2020-01-11 21:58:04 +03:00
|
|
|
import { faInfoCircle as infoIcon } from '@fortawesome/free-solid-svg-icons';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-04-18 11:50:01 +03:00
|
|
|
import classNames from 'classnames';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { UncontrolledTooltip } from 'reactstrap';
|
2023-07-24 21:14:59 +03:00
|
|
|
import { formatHumanFriendly, parseISO } from '../../../src/utils/helpers/date';
|
|
|
|
import { useElementRef } from '../../../src/utils/helpers/hooks';
|
|
|
|
import { prettify } from '../../../src/utils/helpers/numbers';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrl } from '../data';
|
|
|
|
import { ShortUrlDetailLink } from './ShortUrlDetailLink';
|
2022-12-18 15:17:49 +03:00
|
|
|
import './ShortUrlVisitsCount.scss';
|
2020-01-11 21:58:04 +03:00
|
|
|
|
2021-03-20 18:32:12 +03:00
|
|
|
interface ShortUrlVisitsCountProps {
|
|
|
|
shortUrl?: ShortUrl | null;
|
2020-08-30 10:59:14 +03:00
|
|
|
visitsCount: number;
|
|
|
|
active?: boolean;
|
2023-07-24 18:30:58 +03:00
|
|
|
asLink?: boolean;
|
2020-08-30 10:59:14 +03:00
|
|
|
}
|
2020-01-11 21:58:04 +03:00
|
|
|
|
2022-05-28 12:16:59 +03:00
|
|
|
export const ShortUrlVisitsCount = (
|
2023-07-24 18:30:58 +03:00
|
|
|
{ visitsCount, shortUrl, active = false, asLink = false }: ShortUrlVisitsCountProps,
|
2022-05-28 12:16:59 +03:00
|
|
|
) => {
|
2022-12-18 15:17:49 +03:00
|
|
|
const { maxVisits, validSince, validUntil } = shortUrl?.meta ?? {};
|
|
|
|
const hasLimit = !!maxVisits || !!validSince || !!validUntil;
|
2020-02-08 11:07:55 +03:00
|
|
|
const visitsLink = (
|
2023-07-24 18:30:58 +03:00
|
|
|
<ShortUrlDetailLink shortUrl={shortUrl} suffix="visits" asLink={asLink}>
|
2020-04-18 11:50:01 +03:00
|
|
|
<strong
|
|
|
|
className={classNames('short-url-visits-count__amount', { 'short-url-visits-count__amount--big': active })}
|
|
|
|
>
|
2020-04-18 12:03:49 +03:00
|
|
|
{prettify(visitsCount)}
|
2020-04-18 11:50:01 +03:00
|
|
|
</strong>
|
2021-03-20 18:32:12 +03:00
|
|
|
</ShortUrlDetailLink>
|
2020-02-08 11:07:55 +03:00
|
|
|
);
|
2020-01-11 21:58:04 +03:00
|
|
|
|
2022-12-18 15:17:49 +03:00
|
|
|
if (!hasLimit) {
|
2020-02-08 11:07:55 +03:00
|
|
|
return visitsLink;
|
2020-01-11 21:58:04 +03:00
|
|
|
}
|
|
|
|
|
2023-01-10 22:04:47 +03:00
|
|
|
const tooltipRef = useElementRef<HTMLElement>();
|
2020-04-18 12:03:49 +03:00
|
|
|
|
2020-01-11 21:58:04 +03:00
|
|
|
return (
|
2020-11-14 00:44:26 +03:00
|
|
|
<>
|
2020-01-12 14:08:26 +03:00
|
|
|
<span className="indivisible">
|
2020-02-08 11:07:55 +03:00
|
|
|
{visitsLink}
|
2023-01-10 22:04:47 +03:00
|
|
|
<small className="short-urls-visits-count__max-visits-control" ref={tooltipRef}>
|
2022-12-18 15:17:49 +03:00
|
|
|
{maxVisits && <> / {prettify(maxVisits)}</>}
|
|
|
|
<sup className="ms-1">
|
2020-01-11 21:58:04 +03:00
|
|
|
<FontAwesomeIcon icon={infoIcon} />
|
|
|
|
</sup>
|
|
|
|
</small>
|
|
|
|
</span>
|
2023-01-10 22:04:47 +03:00
|
|
|
<UncontrolledTooltip target={tooltipRef} placement="bottom">
|
2022-12-18 15:17:49 +03:00
|
|
|
<ul className="list-unstyled mb-0">
|
|
|
|
{maxVisits && (
|
|
|
|
<li className="short-url-visits-count__tooltip-list-item">
|
|
|
|
This short URL will not accept more than <b>{prettify(maxVisits)}</b> visit{maxVisits === 1 ? '' : 's'}.
|
|
|
|
</li>
|
|
|
|
)}
|
|
|
|
{validSince && (
|
|
|
|
<li className="short-url-visits-count__tooltip-list-item">
|
|
|
|
This short URL will not accept visits
|
|
|
|
before <b className="indivisible">{formatHumanFriendly(parseISO(validSince))}</b>.
|
|
|
|
</li>
|
|
|
|
)}
|
|
|
|
{validUntil && (
|
|
|
|
<li className="short-url-visits-count__tooltip-list-item">
|
|
|
|
This short URL will not accept visits
|
|
|
|
after <b className="indivisible">{formatHumanFriendly(parseISO(validUntil))}</b>.
|
|
|
|
</li>
|
|
|
|
)}
|
|
|
|
</ul>
|
2020-01-11 21:58:04 +03:00
|
|
|
</UncontrolledTooltip>
|
2020-11-14 00:44:26 +03:00
|
|
|
</>
|
2020-01-11 21:58:04 +03:00
|
|
|
);
|
|
|
|
};
|