From 07e1237f058cd1264e8950b194d3c0ae3c225957 Mon Sep 17 00:00:00 2001 From: smanylov Date: Thu, 7 May 2026 12:06:02 +0700 Subject: [PATCH] add tracking statistic --- src/app/actions/trackingActions.ts | 38 ++++++++++++++++++- src/app/hooks/react-query/useTrackingStats.ts | 29 ++++++++++++++ src/app/ui/icons/icons.tsx | 8 ++++ .../ui/tracking-page/tracking-statistic.tsx | 37 +++++++++++++----- 4 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 src/app/hooks/react-query/useTrackingStats.ts diff --git a/src/app/actions/trackingActions.ts b/src/app/actions/trackingActions.ts index 7bf5735..e18bc2a 100644 --- a/src/app/actions/trackingActions.ts +++ b/src/app/actions/trackingActions.ts @@ -185,7 +185,43 @@ export async function fetchHistoryView(page: number, size: number) { headers: { 'Content-Type': 'application/json' } - }); + }); + + if (response.ok) { + const parsed = await response.json(); + + if (parsed.message_code === 0) { + return parsed.message_body; + } else { + return null; + } + + } else { + throw new Error(`${response.status}`); + } + } catch (error) { + return null; + } +} + +export async function fetchTrackingStats() { + const token = await getSessionData('token'); + + try { + const response = await fetch(`${API_BASE_URL}/api/v1/data`, { + method: 'POST', + body: JSON.stringify({ + version: 1, + msg_id: 30028, + message_body: { + token: token, + action: 'view_stats' + } + }), + headers: { + 'Content-Type': 'application/json' + } + }); if (response.ok) { const parsed = await response.json(); diff --git a/src/app/hooks/react-query/useTrackingStats.ts b/src/app/hooks/react-query/useTrackingStats.ts new file mode 100644 index 0000000..e2d140a --- /dev/null +++ b/src/app/hooks/react-query/useTrackingStats.ts @@ -0,0 +1,29 @@ +import { useQuery } from '@tanstack/react-query'; +import { fetchTrackingStats } from '@/app/actions/trackingActions'; + +export interface useTrackingStats { + totalViews: number, + uniqueIps: number, + uniqueUsers: number, + documentsCount: number, + uniqueCountryCount: number, + uniqueCityCount: number, + firstViewDate: string, + lastViewDate: string +} + +export const useTrackingStats = () => { + return useQuery({ + queryKey: ['trackingStats'], + queryFn: () => { + return fetchTrackingStats() + }, + select: (data: useTrackingStats | null) => { + if (!data) { + return null + } + return data; + }, + retry: false + }); +}; \ No newline at end of file diff --git a/src/app/ui/icons/icons.tsx b/src/app/ui/icons/icons.tsx index 10f3113..22d619c 100644 --- a/src/app/ui/icons/icons.tsx +++ b/src/app/ui/icons/icons.tsx @@ -282,4 +282,12 @@ export function IconGlobe() { ) +} + +export function IconCity() { + return ( + + + + ) } \ No newline at end of file diff --git a/src/app/ui/tracking-page/tracking-statistic.tsx b/src/app/ui/tracking-page/tracking-statistic.tsx index 11096d2..7974a34 100644 --- a/src/app/ui/tracking-page/tracking-statistic.tsx +++ b/src/app/ui/tracking-page/tracking-statistic.tsx @@ -1,5 +1,11 @@ -import { IconLink, IconDocument, IconCheck, IconPerson, IconGlobe } from '@/app/ui/icons/icons'; +'use client' + +import {IconLink, IconDocument, IconCheck, IconPerson, IconGlobe, IconCity} from '@/app/ui/icons/icons'; +import { useTrackingStats } from '@/app/hooks/react-query/useTrackingStats'; + export function TrackingStatistic() { + const { data } = useTrackingStats(); + return (
- 33 + {data?.documentsCount ? data?.documentsCount : 0}
Документов @@ -31,29 +37,29 @@ export function TrackingStatistic() {
- 103 + {data?.totalViews? data?.totalViews : 0}
Открытий
-
+{/*
- 247 + {data?.uniqueUsers ? data?.uniqueUsers : 0}
- Верификаций + Уникальных пользователей
-
+
*/}
- 27 + {data?.uniqueIps ? data?.uniqueIps : 0}
Уникальных IP @@ -64,10 +70,21 @@ export function TrackingStatistic() {
- 7 + {data?.uniqueCountryCount ? data?.uniqueCountryCount : 0}
- Стран + Уникальных стран +
+
+
+
+ +
+
+ {data?.uniqueCountryCount ? data?.uniqueCountryCount : 0} +
+
+ Уникальных городов