diff --git a/src/components/screens/URLTracking/index.tsx b/src/components/screens/URLTracking/index.tsx index 35931ba1..342dee51 100644 --- a/src/components/screens/URLTracking/index.tsx +++ b/src/components/screens/URLTracking/index.tsx @@ -1,4 +1,3 @@ -import { UrlForwardMeta, UrlShortenerHistory } from '@prisma/client'; import { Calendar, Copy, Globe, MapPin, RefreshCw, Search, UserCheck, UserX } from '@styled-icons/feather'; import { JsonViewer } from '@textea/json-viewer'; import { getStats, parseUA } from 'api/requests'; @@ -13,6 +12,7 @@ import { useCallback, useEffect, useState } from 'react'; import { toast } from 'react-hot-toast'; import { useMutation, useQuery } from 'react-query'; import { BASE_URL_SHORT, Window } from 'types/constants'; +import { UrlHistoryWithMeta } from 'types/stats'; import { MIXPANEL_EVENT } from 'types/utils'; import { UAParser } from 'ua-parser-js'; import { Referer, detectReferer } from 'utils/agent'; @@ -30,10 +30,7 @@ export const URLTracking = ({ hash }: { hash: string }) => { const [needValidate, setNeedValidate] = useState(); const [parsedUA, setParsedUA] = useState(); const [qc, setQc] = useState(undefined); - const [history, setHistory] = useState<(UrlShortenerHistory & { urlForwardMeta: UrlForwardMeta[] }) | undefined>( - undefined, - ); - + const [history, setHistory] = useState(undefined); const getStatsQuery = useCallback(async () => getStats({ hash }), [hash]); /* now data has only 1 history */ const { data, isLoading, isSuccess, refetch } = useQuery({ @@ -43,11 +40,7 @@ export const URLTracking = ({ hash }: { hash: string }) => { retry: false, ...strictRefetch, onSuccess(data) { - setHistory( - !!data.history?.length - ? (data.history[0] as UrlShortenerHistory & { urlForwardMeta: UrlForwardMeta[] }) - : undefined, - ); + setHistory(!!data.history?.length ? (data.history[0] as UrlHistoryWithMeta) : undefined); }, onError(err: any) { if (err.message === 'UNAUTHORIZED') { @@ -65,7 +58,10 @@ export const URLTracking = ({ hash }: { hash: string }) => { if (h) { return { ...h, - urlForwardMeta: [...h.urlForwardMeta, ...(!!data.history?.length ? data.history[0].urlForwardMeta! : [])], + UrlForwardMeta: [ + ...(h.UrlForwardMeta || []), + ...(!!data.history?.length ? data.history[0].UrlForwardMeta || [] : []), + ], }; } return undefined; @@ -82,7 +78,7 @@ export const URLTracking = ({ hash }: { hash: string }) => { const onLoadMore = () => { setQc((_) => { - const _qc = history?.urlForwardMeta?.at(-1)?.id; + const _qc = history?.UrlForwardMeta?.at(-1)?.id; if (!statsMore.isLoading) { statsMore.mutate({ hash, queryCursor: _qc }); } @@ -104,9 +100,9 @@ export const URLTracking = ({ hash }: { hash: string }) => { return null; } - const hasData = !!history?.urlForwardMeta?.length; + const hasData = !!history?.UrlForwardMeta?.length; const hasPassword = history?.password !== null; - const hasMore = hasData && (history?.urlForwardMeta?.length || 0) % PAGE_SIZE === 0; + const hasMore = hasData && (history?.UrlForwardMeta?.length || 0) % PAGE_SIZE === 0; const clickableUrl = history ? (history.url.startsWith('http') ? history.url : `//${history.url}`) : ''; return ( @@ -156,14 +152,14 @@ export const URLTracking = ({ hash }: { hash: string }) => {

{`${t('totalClick')}: `}{' '} - {(history as any)?._count?.urlForwardMeta} + {(history as any)?._count?.UrlForwardMeta}

@@ -192,7 +188,7 @@ export const URLTracking = ({ hash }: { hash: string }) => { - {history?.urlForwardMeta?.map((m, j) => { + {history?.UrlForwardMeta?.map((m, j) => { const UA = m.userAgent ? new UAParser(m.userAgent) : undefined; const ref = detectReferer(m.userAgent); return ( diff --git a/src/types/stats.ts b/src/types/stats.ts index e99199a9..367cfbba 100644 --- a/src/types/stats.ts +++ b/src/types/stats.ts @@ -1,7 +1,9 @@ import { UrlForwardMeta, UrlShortenerHistory, UrlShortenerRecord } from '@prisma/client'; import { Response } from './api'; +export type UrlHistoryWithMeta = UrlShortenerHistory & { UrlForwardMeta?: UrlForwardMeta[] }; + export type Stats = Response & { record?: UrlShortenerRecord | null; - history?: (UrlShortenerHistory & { urlForwardMeta?: UrlForwardMeta[] })[] | null; + history?: UrlHistoryWithMeta[] | null; };