feat: add website view count in website list

This commit is contained in:
moonrailgun 2024-06-06 20:55:57 +08:00
parent 4e8d7613a4
commit 8ac5b11d49
3 changed files with 55 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import { useFuseSearch } from '@/hooks/useFuseSearch';
import { Empty } from 'antd';
import { globalEventBus } from '@/utils/event';
import { Spinner } from './ui/spinner';
import { formatNumber } from '@/utils/common';
export interface CommonListItem {
id: string;
@ -99,7 +100,9 @@ export const CommonList: React.FC<CommonListProps> = React.memo((props) => {
<div className="font-semibold">{item.title}</div>
{item.number && item.number > 0 && (
<span className="opacity-60">{item.number}</span>
<span className="opacity-60" title={String(item.number)}>
{formatNumber(item.number)}
</span>
)}
</div>

View File

@ -1,9 +1,8 @@
import { trpc } from '@/api/trpc';
import { CommonHeader } from '@/components/CommonHeader';
import { CommonList } from '@/components/CommonList';
import { CommonList, CommonListItem } from '@/components/CommonList';
import { CommonWrapper } from '@/components/CommonWrapper';
import { Button } from '@/components/ui/button';
import { useDataReady } from '@/hooks/useDataReady';
import { useEvent } from '@/hooks/useEvent';
import { Layout } from '@/components/layout';
import { useCurrentWorkspaceId } from '@/store/user';
@ -29,15 +28,19 @@ function WebsiteComponent() {
const { data = [], isLoading } = trpc.website.all.useQuery({
workspaceId,
});
const { data: overviewData = {} } = trpc.website.allOverview.useQuery({
workspaceId,
});
const navigate = useNavigate();
const pathname = useRouterState({
select: (state) => state.location.pathname,
});
const items = data.map((item) => ({
const items: CommonListItem[] = data.map((item) => ({
id: item.id,
title: item.name,
content: item.domain,
number: overviewData[item.id] ?? 0,
href: `/website/${item.id}`,
}));

View File

@ -84,6 +84,51 @@ export const websiteRouter = router({
return websites;
}),
allOverview: workspaceProcedure
.meta({
openapi: {
method: 'GET',
path: '/workspace/{workspaceId}/website/allOverview',
tags: [OPENAPI_TAG.WEBSITE],
protect: true,
},
})
.output(z.record(z.string(), z.number()))
.query(async ({ input }) => {
const { workspaceId } = input;
const websiteIds = (
await prisma.website.findMany({
where: {
workspaceId,
},
select: {
id: true,
},
})
).map((item) => item.id);
const res = await prisma.websiteEvent.groupBy({
by: ['websiteId'],
where: {
websiteId: {
in: [...websiteIds],
},
createdAt: {
gte: dayjs().subtract(1, 'day').toDate(),
},
},
_count: true,
});
return res.reduce<Record<string, number>>((prev, item) => {
if (item.websiteId) {
prev[item.websiteId] = item._count;
}
return prev;
}, {});
}),
info: workspaceProcedure
.meta(
buildWebsiteOpenapi({