Initial Upload
Some checks failed
CI / Lint & Typecheck (push) Has been cancelled
CI / Test (routes) (push) Has been cancelled
CI / Test (security) (push) Has been cancelled
CI / Test (services) (push) Has been cancelled
CI / Test (unit) (push) Has been cancelled
CI / Test (integration) (push) Has been cancelled
CI / Test Coverage (push) Has been cancelled
CI / Build (push) Has been cancelled
Some checks failed
CI / Lint & Typecheck (push) Has been cancelled
CI / Test (routes) (push) Has been cancelled
CI / Test (security) (push) Has been cancelled
CI / Test (services) (push) Has been cancelled
CI / Test (unit) (push) Has been cancelled
CI / Test (integration) (push) Has been cancelled
CI / Test Coverage (push) Has been cancelled
CI / Build (push) Has been cancelled
This commit is contained in:
102
apps/web/src/hooks/queries/useStats.ts
Normal file
102
apps/web/src/hooks/queries/useStats.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import '@tracearr/shared';
|
||||
import { api, type StatsTimeRange } from '@/lib/api';
|
||||
|
||||
// Re-export for backwards compatibility and convenience
|
||||
export type { StatsTimeRange };
|
||||
|
||||
export function useDashboardStats(serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'dashboard', serverId],
|
||||
queryFn: () => api.stats.dashboard(serverId ?? undefined),
|
||||
staleTime: 1000 * 30, // 30 seconds
|
||||
refetchInterval: 1000 * 60, // 1 minute
|
||||
});
|
||||
}
|
||||
|
||||
export function usePlaysStats(timeRange?: StatsTimeRange, serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'plays', timeRange, serverId],
|
||||
queryFn: () => api.stats.plays(timeRange ?? { period: 'week' }, serverId ?? undefined),
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
export function useUserStats(timeRange?: StatsTimeRange, serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'users', timeRange, serverId],
|
||||
queryFn: () => api.stats.users(timeRange ?? { period: 'month' }, serverId ?? undefined),
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
export interface LocationStatsFilters {
|
||||
timeRange?: StatsTimeRange;
|
||||
serverUserId?: string;
|
||||
serverId?: string;
|
||||
mediaType?: 'movie' | 'episode' | 'track';
|
||||
}
|
||||
|
||||
export function useLocationStats(filters?: LocationStatsFilters) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'locations', filters],
|
||||
queryFn: () => api.stats.locations(filters),
|
||||
staleTime: 1000 * 60, // 1 minute
|
||||
});
|
||||
}
|
||||
|
||||
export function usePlaysByDayOfWeek(timeRange?: StatsTimeRange, serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'plays-by-dayofweek', timeRange, serverId],
|
||||
queryFn: () => api.stats.playsByDayOfWeek(timeRange ?? { period: 'month' }, serverId ?? undefined),
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
export function usePlaysByHourOfDay(timeRange?: StatsTimeRange, serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'plays-by-hourofday', timeRange, serverId],
|
||||
queryFn: () => api.stats.playsByHourOfDay(timeRange ?? { period: 'month' }, serverId ?? undefined),
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
export function usePlatformStats(timeRange?: StatsTimeRange, serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'platforms', timeRange, serverId],
|
||||
queryFn: () => api.stats.platforms(timeRange ?? { period: 'month' }, serverId ?? undefined),
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
export function useQualityStats(timeRange?: StatsTimeRange, serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'quality', timeRange, serverId],
|
||||
queryFn: () => api.stats.quality(timeRange ?? { period: 'month' }, serverId ?? undefined),
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
export function useTopUsers(timeRange?: StatsTimeRange, serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'top-users', timeRange, serverId],
|
||||
queryFn: () => api.stats.topUsers(timeRange ?? { period: 'month' }, serverId ?? undefined),
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
export function useTopContent(timeRange?: StatsTimeRange, serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'top-content', timeRange, serverId],
|
||||
queryFn: () => api.stats.topContent(timeRange ?? { period: 'month' }, serverId ?? undefined),
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
export function useConcurrentStats(timeRange?: StatsTimeRange, serverId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['stats', 'concurrent', timeRange, serverId],
|
||||
queryFn: () => api.stats.concurrent(timeRange ?? { period: 'month' }, serverId ?? undefined),
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user