mirror of
https://github.com/umami-software/umami.git
synced 2026-02-06 13:47:15 +01:00
Refactor filter handling for queries.
This commit is contained in:
parent
5b300f1ff5
commit
ee6c68d27c
107 changed files with 731 additions and 835 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { useApi, useModified } from '@/components/hooks';
|
||||
|
||||
export function useDeleteQuery(path: string, params?: { [key: string]: any }) {
|
||||
export function useDeleteQuery(path: string, params?: Record<string, any>) {
|
||||
const { del, useMutation } = useApi();
|
||||
const { mutate, isPending, error } = useMutation({
|
||||
mutationFn: () => del(path, params),
|
||||
|
|
|
|||
|
|
@ -1,8 +1,27 @@
|
|||
import { useTimezone } from '@/components/hooks/useTimezone';
|
||||
import { REALTIME_INTERVAL } from '@/lib/constants';
|
||||
import { RealtimeData } from '@/lib/types';
|
||||
import { useApi } from '../useApi';
|
||||
|
||||
export interface RealtimeData {
|
||||
countries: Record<string, number>;
|
||||
events: any[];
|
||||
pageviews: any[];
|
||||
referrers: Record<string, number>;
|
||||
timestamp: number;
|
||||
series: {
|
||||
views: any[];
|
||||
visitors: any[];
|
||||
};
|
||||
totals: {
|
||||
views: number;
|
||||
visitors: number;
|
||||
events: number;
|
||||
countries: number;
|
||||
};
|
||||
urls: Record<string, number>;
|
||||
visitors: any[];
|
||||
}
|
||||
|
||||
export function useRealtimeQuery(websiteId: string) {
|
||||
const { get, useQuery } = useApi();
|
||||
const { timezone } = useTimezone();
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { ReactQueryOptions } from '@/lib/types';
|
|||
|
||||
export function useResultQuery<T = any>(
|
||||
type: string,
|
||||
params?: { [key: string]: any },
|
||||
params?: Record<string, any>,
|
||||
options?: ReactQueryOptions<T>,
|
||||
) {
|
||||
const { websiteId } = params;
|
||||
|
|
@ -21,7 +21,7 @@ export function useResultQuery<T = any>(
|
|||
...params,
|
||||
},
|
||||
],
|
||||
queryFn: () => post(`/reports/${type}`, { type, ...filters, ...params }),
|
||||
queryFn: () => post(`/reports/${type}`, { type, filters, ...params }),
|
||||
enabled: !!type,
|
||||
...options,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { useApi } from '../useApi';
|
||||
|
||||
export function useUserQuery(userId: string, options?: { [key: string]: any }) {
|
||||
export function useUserQuery(userId: string, options?: Record<string, any>) {
|
||||
const { get, useQuery } = useApi();
|
||||
return useQuery({
|
||||
queryKey: ['users', userId],
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ import { ReactQueryOptions } from '@/lib/types';
|
|||
|
||||
export function useWebsiteEventsQuery(websiteId: string, options?: ReactQueryOptions<any>) {
|
||||
const { get } = useApi();
|
||||
const filterParams = useFilterParams(websiteId);
|
||||
const queryParams = useFilterParams(websiteId);
|
||||
|
||||
return usePagedQuery({
|
||||
queryKey: ['websites:events', { websiteId, ...filterParams }],
|
||||
queryFn: () => get(`/websites/${websiteId}/events`, { ...filterParams, pageSize: 20 }),
|
||||
queryKey: ['websites:events', { websiteId, ...queryParams }],
|
||||
queryFn: () => get(`/websites/${websiteId}/events`, { ...queryParams, pageSize: 20 }),
|
||||
enabled: !!websiteId,
|
||||
...options,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ export function useWebsiteMetricsQuery(
|
|||
options?: ReactQueryOptions<WebsiteMetricsData>,
|
||||
) {
|
||||
const { get, useQuery } = useApi();
|
||||
const filterParams = useFilterParams(websiteId);
|
||||
const queryParams = useFilterParams(websiteId);
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
return useQuery<WebsiteMetricsData>({
|
||||
|
|
@ -23,13 +23,13 @@ export function useWebsiteMetricsQuery(
|
|||
'websites:metrics',
|
||||
{
|
||||
websiteId,
|
||||
...filterParams,
|
||||
...queryParams,
|
||||
...params,
|
||||
},
|
||||
],
|
||||
queryFn: async () =>
|
||||
get(`/websites/${websiteId}/metrics`, {
|
||||
...filterParams,
|
||||
...queryParams,
|
||||
[searchParams.get('view')]: undefined,
|
||||
...params,
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -12,11 +12,11 @@ export function useWebsitePageviewsQuery(
|
|||
options?: ReactQueryOptions<WebsitePageviewsData>,
|
||||
) {
|
||||
const { get, useQuery } = useApi();
|
||||
const filterParams = useFilterParams(websiteId);
|
||||
const queryParams = useFilterParams(websiteId);
|
||||
|
||||
return useQuery<WebsitePageviewsData>({
|
||||
queryKey: ['websites:pageviews', { websiteId, compare, ...filterParams }],
|
||||
queryFn: () => get(`/websites/${websiteId}/pageviews`, { compare, ...filterParams }),
|
||||
queryKey: ['websites:pageviews', { websiteId, compare, ...queryParams }],
|
||||
queryFn: () => get(`/websites/${websiteId}/pageviews`, { compare, ...queryParams }),
|
||||
enabled: !!websiteId,
|
||||
...options,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { useApi } from '../useApi';
|
||||
|
||||
export function useWebsiteQuery(websiteId: string, options?: { [key: string]: any }) {
|
||||
export function useWebsiteQuery(websiteId: string, options?: Record<string, any>) {
|
||||
const { get, useQuery } = useApi();
|
||||
|
||||
return useQuery({
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
import { useApi } from '../useApi';
|
||||
import { useFilterParams } from '../useFilterParams';
|
||||
|
||||
export function useWebsiteSessionStatsQuery(
|
||||
websiteId: string,
|
||||
options?: { [key: string]: string },
|
||||
) {
|
||||
export function useWebsiteSessionStatsQuery(websiteId: string, options?: Record<string, string>) {
|
||||
const { get, useQuery } = useApi();
|
||||
const params = useFilterParams(websiteId);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { useFilterParams } from '@/components/hooks/useFilterParams';
|
|||
|
||||
export function useWebsiteSessionsQuery(
|
||||
websiteId: string,
|
||||
params?: { [key: string]: string | number },
|
||||
params?: Record<string, string | number>,
|
||||
) {
|
||||
const { get } = useApi();
|
||||
const { modified } = useModified(`sessions`);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { useFilterParams } from '@/components/hooks/useFilterParams';
|
|||
|
||||
export function useWebsiteSessionsWeeklyQuery(
|
||||
websiteId: string,
|
||||
params?: { [key: string]: string | number },
|
||||
params?: Record<string, string | number>,
|
||||
) {
|
||||
const { get, useQuery } = useApi();
|
||||
const { modified } = useModified(`sessions`);
|
||||
|
|
|
|||
|
|
@ -19,15 +19,14 @@ export interface WebsiteStatsData {
|
|||
|
||||
export function useWebsiteStatsQuery(
|
||||
websiteId: string,
|
||||
compare?: string,
|
||||
options?: UseQueryOptions<WebsiteStatsData, Error, WebsiteStatsData>,
|
||||
) {
|
||||
const { get, useQuery } = useApi();
|
||||
const params = useFilterParams(websiteId);
|
||||
const filterParams = useFilterParams(websiteId);
|
||||
|
||||
return useQuery<WebsiteStatsData>({
|
||||
queryKey: ['websites:stats', { websiteId, ...params, compare }],
|
||||
queryFn: () => get(`/websites/${websiteId}/stats`, { ...params, compare }),
|
||||
queryKey: ['websites:stats', { websiteId, ...filterParams }],
|
||||
queryFn: () => get(`/websites/${websiteId}/stats`, { ...filterParams }),
|
||||
enabled: !!websiteId,
|
||||
...options,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ import { useApi } from '../useApi';
|
|||
import { usePagedQuery } from '../usePagedQuery';
|
||||
import { useLoginQuery } from './useLoginQuery';
|
||||
import { useModified } from '../useModified';
|
||||
import { ReactQueryOptions } from '@/lib/types';
|
||||
|
||||
export function useWebsites(
|
||||
export function useWebsitesQuery(
|
||||
{ userId, teamId }: { userId?: string; teamId?: string },
|
||||
params?: { [key: string]: string | number },
|
||||
params?: Record<string, any>,
|
||||
options?: ReactQueryOptions,
|
||||
) {
|
||||
const { get } = useApi();
|
||||
const { user } = useLoginQuery();
|
||||
|
|
@ -13,10 +15,12 @@ export function useWebsites(
|
|||
|
||||
return usePagedQuery({
|
||||
queryKey: ['websites', { userId, teamId, modified, ...params }],
|
||||
queryFn: () => {
|
||||
queryFn: pageParams => {
|
||||
return get(teamId ? `/teams/${teamId}/websites` : `/users/${userId || user.id}/websites`, {
|
||||
...params,
|
||||
...pageParams,
|
||||
});
|
||||
},
|
||||
...options,
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue