tianji/src/client/api/model/website.ts

66 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-09-04 17:18:43 +00:00
import { useQuery } from '@tanstack/react-query';
2023-09-19 11:29:28 +00:00
import { DateUnit } from '../../utils/date';
2023-09-04 17:18:43 +00:00
import { queryClient } from '../cache';
import { request } from '../request';
2023-09-15 16:03:09 +00:00
import { getUserTimezone } from './user';
import { AppRouterOutput } from '../trpc';
2023-09-04 17:18:43 +00:00
export type WebsiteInfo = NonNullable<AppRouterOutput['website']['info']>;
2023-09-04 17:18:43 +00:00
2023-09-06 06:14:12 +00:00
export async function deleteWorkspaceWebsite(
workspaceId: string,
websiteId: string
) {
await request.delete(`/api/workspace/${workspaceId}/website/${websiteId}`);
queryClient.resetQueries(['websites', workspaceId]);
}
2023-09-04 17:18:43 +00:00
export function refreshWorkspaceWebsites(workspaceId: string) {
queryClient.refetchQueries(['websites', workspaceId]);
}
2023-09-15 16:03:09 +00:00
export async function getWorkspaceWebsitePageview(
workspaceId: string,
websiteId: string,
filter: Record<string, any>
) {
const { data } = await request.get(
`/api/workspace/${workspaceId}/website/${websiteId}/pageviews`,
{
params: {
...filter,
},
}
);
return data;
}
export function useWorkspaceWebsitePageview(
workspaceId: string,
websiteId: string,
startAt: number,
2023-09-19 11:29:28 +00:00
endAt: number,
unit: DateUnit
2023-09-15 16:03:09 +00:00
) {
2023-09-17 06:41:50 +00:00
const { data, isLoading, refetch } = useQuery(
2023-09-19 11:29:28 +00:00
['websitePageview', { workspaceId, websiteId, startAt, endAt }],
2023-09-15 16:03:09 +00:00
() => {
return getWorkspaceWebsitePageview(workspaceId, websiteId, {
startAt,
endAt,
2023-09-19 11:29:28 +00:00
unit,
2023-09-15 16:03:09 +00:00
timezone: getUserTimezone(),
});
}
);
2023-09-16 08:12:19 +00:00
return {
pageviews: data?.pageviews ?? [],
sessions: data?.sessions ?? [],
isLoading,
2023-09-17 06:41:50 +00:00
refetch,
2023-09-16 08:12:19 +00:00
};
2023-09-15 16:03:09 +00:00
}