2020-11-14 00:44:26 +03:00
|
|
|
import { useEffect } from 'react';
|
2022-02-06 22:07:18 +03:00
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
2023-07-23 11:14:58 +03:00
|
|
|
import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub';
|
|
|
|
import { Topics } from '../mercure/helpers/Topics';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrlIdentifier } from '../short-urls/data';
|
|
|
|
import { urlDecodeShortCode } from '../short-urls/helpers';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShortUrlDetail } from '../short-urls/reducers/shortUrlDetail';
|
2023-07-29 11:43:15 +03:00
|
|
|
import { useGoBack } from '../utils/helpers/hooks';
|
2023-07-27 23:23:46 +03:00
|
|
|
import { parseQuery } from '../utils/helpers/query';
|
|
|
|
import type { ReportExporter } from '../utils/services/ReportExporter';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { LoadShortUrlVisits, ShortUrlVisits as ShortUrlVisitsState } from './reducers/shortUrlVisits';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { ShortUrlVisitsHeader } from './ShortUrlVisitsHeader';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { NormalizedVisit, VisitsParams } from './types';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { toApiParams } from './types/helpers';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { VisitsStats } from './VisitsStats';
|
2020-09-04 20:05:41 +03:00
|
|
|
|
2023-07-23 19:30:59 +03:00
|
|
|
export interface ShortUrlVisitsProps {
|
2022-11-12 11:38:24 +03:00
|
|
|
getShortUrlVisits: (params: LoadShortUrlVisits) => void;
|
2020-09-04 20:05:41 +03:00
|
|
|
shortUrlVisits: ShortUrlVisitsState;
|
2022-11-06 21:16:51 +03:00
|
|
|
getShortUrlDetail: (shortUrl: ShortUrlIdentifier) => void;
|
2020-09-04 20:05:41 +03:00
|
|
|
shortUrlDetail: ShortUrlDetail;
|
2020-09-05 09:49:18 +03:00
|
|
|
cancelGetShortUrlVisits: () => void;
|
2020-09-04 20:05:41 +03:00
|
|
|
}
|
|
|
|
|
2022-05-28 12:16:59 +03:00
|
|
|
export const ShortUrlVisits = ({ exportVisits }: ReportExporter) => boundToMercureHub(({
|
2020-09-04 20:05:41 +03:00
|
|
|
shortUrlVisits,
|
|
|
|
shortUrlDetail,
|
|
|
|
getShortUrlVisits,
|
|
|
|
getShortUrlDetail,
|
|
|
|
cancelGetShortUrlVisits,
|
|
|
|
}: ShortUrlVisitsProps) => {
|
2022-02-06 22:07:18 +03:00
|
|
|
const { shortCode = '' } = useParams<{ shortCode: string }>();
|
|
|
|
const { search } = useLocation();
|
|
|
|
const goBack = useGoBack();
|
2020-12-20 21:28:09 +03:00
|
|
|
const { domain } = parseQuery<{ domain?: string }>(search);
|
2022-11-12 11:38:24 +03:00
|
|
|
const loadVisits = (params: VisitsParams, doIntervalFallback?: boolean) => getShortUrlVisits({
|
|
|
|
shortCode: urlDecodeShortCode(shortCode),
|
|
|
|
query: { ...toApiParams(params), domain },
|
|
|
|
doIntervalFallback,
|
|
|
|
});
|
2021-03-14 14:49:12 +03:00
|
|
|
const exportCsv = (visits: NormalizedVisit[]) => exportVisits(
|
|
|
|
`short-url_${shortUrlDetail.shortUrl?.shortUrl.replace(/https?:\/\//g, '')}_visits.csv`,
|
|
|
|
visits,
|
|
|
|
);
|
2020-09-04 20:05:41 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-11-06 21:16:51 +03:00
|
|
|
getShortUrlDetail({ shortCode: urlDecodeShortCode(shortCode), domain });
|
2020-09-04 20:05:41 +03:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2020-12-20 21:28:09 +03:00
|
|
|
<VisitsStats
|
|
|
|
getVisits={loadVisits}
|
|
|
|
cancelGetVisits={cancelGetShortUrlVisits}
|
|
|
|
visitsInfo={shortUrlVisits}
|
2021-03-14 14:49:12 +03:00
|
|
|
exportCsv={exportCsv}
|
2020-12-20 21:28:09 +03:00
|
|
|
>
|
2020-09-04 20:05:41 +03:00
|
|
|
<ShortUrlVisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={goBack} />
|
|
|
|
</VisitsStats>
|
|
|
|
);
|
2022-08-07 19:19:53 +03:00
|
|
|
}, (_, params) => (params.shortCode ? [Topics.shortUrlVisits(urlDecodeShortCode(params.shortCode))] : []));
|