import { ScaffoldSection, ScaffoldSectionTitle, ScaffoldSectionContent, } from 'components/layouts/Scaffold' import { Card, CardContent, CardHeader, CardTitle, cn, Tooltip, TooltipTrigger, TooltipContent, } from 'ui' import Link from 'next/link' import { useParams } from 'common' import { ChevronRight, Loader2 } from 'lucide-react' import { Reports } from 'icons' import { getChangeColor, fetchAllAuthMetrics, processAllAuthMetrics, calculatePercentageChange, } from './OverviewUsage.constants' import { useQuery } from '@tanstack/react-query' import dayjs from 'dayjs' import AlertError from 'components/ui/AlertError' export const StatCard = ({ title, current, previous, loading, suffix = '', invert = false, href, }: { title: string current: number previous: number loading: boolean suffix?: string invert?: boolean href?: string }) => { const isZeroChange = previous === 0 const changeColor = isZeroChange ? 'text-foreground-lighter' : invert ? previous >= 0 ? 'text-destructive' : 'text-brand' : getChangeColor(previous) const formattedCurrent = suffix === 'ms' ? current.toFixed(2) : suffix === '%' ? current.toFixed(1) : Math.round(current) const signChar = previous > 0 ? '+' : previous < 0 ? '-' : '' return ( {title} Go to Auth Report {loading ? ( ) : ( <>

{`${formattedCurrent}${suffix}`}

{`${signChar}${Math.abs(previous).toFixed(1)}%`}
)}
) } export const OverviewUsage = () => { const { ref } = useParams() const { data: currentData, isLoading: currentLoading, error: currentError, } = useQuery({ queryKey: ['auth-metrics', ref, 'current'], queryFn: () => fetchAllAuthMetrics(ref as string, 'current'), enabled: !!ref, }) const { data: previousData, isLoading: previousLoading, error: previousError, } = useQuery({ queryKey: ['auth-metrics', ref, 'previous'], queryFn: () => fetchAllAuthMetrics(ref as string, 'previous'), enabled: !!ref, }) const metrics = processAllAuthMetrics(currentData?.result || [], previousData?.result || []) const isLoading = currentLoading || previousLoading const isError = !!previousError || !!currentError const errorMessage = (previousError as any)?.message || (currentError as any)?.message || 'There was an error fetching the auth metrics.' const activeUsersChange = calculatePercentageChange( metrics.current.activeUsers, metrics.previous.activeUsers ) const signUpsChange = calculatePercentageChange(metrics.current.signUps, metrics.previous.signUps) const endDate = dayjs().toISOString() const startDate = dayjs().subtract(24, 'hour').toISOString() // No charts on overview; keep date range for link only return ( {isError && ( )}
Usage View all reports
) }