shlink-web-client/shlink-web-component/short-urls/helpers/ShortUrlVisitsCount.tsx

75 lines
2.7 KiB
TypeScript
Raw Normal View History

import { faInfoCircle as infoIcon } from '@fortawesome/free-solid-svg-icons';
2023-02-18 13:11:01 +03:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import classNames from 'classnames';
2023-02-18 13:11:01 +03:00
import { UncontrolledTooltip } from 'reactstrap';
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';
import './ShortUrlVisitsCount.scss';
2021-03-20 18:32:12 +03:00
interface ShortUrlVisitsCountProps {
shortUrl?: ShortUrl | null;
visitsCount: number;
active?: boolean;
2023-07-24 18:30:58 +03:00
asLink?: boolean;
}
export const ShortUrlVisitsCount = (
2023-07-24 18:30:58 +03:00
{ visitsCount, shortUrl, active = false, asLink = false }: ShortUrlVisitsCountProps,
) => {
const { maxVisits, validSince, validUntil } = shortUrl?.meta ?? {};
const hasLimit = !!maxVisits || !!validSince || !!validUntil;
const visitsLink = (
2023-07-24 18:30:58 +03:00
<ShortUrlDetailLink shortUrl={shortUrl} suffix="visits" asLink={asLink}>
<strong
className={classNames('short-url-visits-count__amount', { 'short-url-visits-count__amount--big': active })}
>
{prettify(visitsCount)}
</strong>
2021-03-20 18:32:12 +03:00
</ShortUrlDetailLink>
);
if (!hasLimit) {
return visitsLink;
}
const tooltipRef = useElementRef<HTMLElement>();
return (
2020-11-14 00:44:26 +03:00
<>
2020-01-12 14:08:26 +03:00
<span className="indivisible">
{visitsLink}
<small className="short-urls-visits-count__max-visits-control" ref={tooltipRef}>
{maxVisits && <> / {prettify(maxVisits)}</>}
<sup className="ms-1">
<FontAwesomeIcon icon={infoIcon} />
</sup>
</small>
</span>
<UncontrolledTooltip target={tooltipRef} placement="bottom">
<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>
</UncontrolledTooltip>
2020-11-14 00:44:26 +03:00
</>
);
};