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

194 lines
3.9 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';
2023-09-04 17:18:43 +00:00
export interface WebsiteInfo {
id: string;
name: string;
domain: string | null;
shareId: string | null;
resetAt: string | null;
workspaceId: string;
createdAt: string | null;
updatedAt: string | null;
deletedAt: string | null;
}
export async function getWorkspaceWebsites(
workspaceId: string
): Promise<WebsiteInfo[]> {
const { data } = await request.get('/api/workspace/websites', {
params: {
workspaceId,
},
});
return data.websites;
}
2023-09-05 07:32:16 +00:00
export async function getWorkspaceWebsiteInfo(
workspaceId: string,
websiteId: string
): Promise<WebsiteInfo | null> {
const { data } = await request.get(`/api/workspace/website/${websiteId}`, {
params: {
workspaceId,
},
});
return data.website;
}
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 useWorspaceWebsites(workspaceId: string) {
const { data: websites = [], isLoading } = useQuery(
['websites', workspaceId],
() => {
return getWorkspaceWebsites(workspaceId);
}
);
return { websites, isLoading };
}
2023-09-05 07:32:16 +00:00
export function useWorkspaceWebsiteInfo(
workspaceId: string,
websiteId: string
) {
const { data: website = null, isLoading } = useQuery(
['website', workspaceId, websiteId],
() => {
return getWorkspaceWebsiteInfo(workspaceId, websiteId);
},
{ cacheTime: 0 }
);
return { website, isLoading };
}
2023-09-04 17:18:43 +00:00
export function refreshWorkspaceWebsites(workspaceId: string) {
queryClient.refetchQueries(['websites', workspaceId]);
}
export async function addWorkspaceWebsite(
workspaceId: string,
name: string,
domain: string
) {
await request.post('/api/workspace/website', {
workspaceId,
name,
domain,
});
}
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
}
2023-09-19 11:29:28 +00:00
export interface StatsItemType {
value: number;
change: number;
}
export async function getWorkspaceWebsiteStats(
workspaceId: string,
websiteId: string,
filter: Record<string, any>
): Promise<{
bounces: StatsItemType;
pageviews: StatsItemType;
totaltime: StatsItemType;
uniques: StatsItemType;
}> {
const { data } = await request.get(
`/api/workspace/${workspaceId}/website/${websiteId}/stats`,
{
params: {
...filter,
},
}
);
return data.stats;
}
export function useWorkspaceWebsiteStats(
workspaceId: string,
websiteId: string,
startAt: number,
endAt: number,
unit: DateUnit
) {
const {
data: stats,
isLoading,
refetch,
} = useQuery(
['websiteStats', { workspaceId, websiteId, startAt, endAt }],
() => {
return getWorkspaceWebsiteStats(workspaceId, websiteId, {
startAt,
endAt,
unit,
timezone: getUserTimezone(),
});
}
);
return {
stats,
isLoading,
refetch,
};
}