From 8ac5b11d4962de05cefe3d5be7c014f4f8bb7c9a Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Thu, 6 Jun 2024 20:55:57 +0800 Subject: [PATCH] feat: add website view count in website list --- src/client/components/CommonList.tsx | 5 +++- src/client/routes/website.tsx | 9 ++++-- src/server/trpc/routers/website.ts | 45 ++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/client/components/CommonList.tsx b/src/client/components/CommonList.tsx index b402f19..e1ddc63 100644 --- a/src/client/components/CommonList.tsx +++ b/src/client/components/CommonList.tsx @@ -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 = React.memo((props) => {
{item.title}
{item.number && item.number > 0 && ( - {item.number} + + {formatNumber(item.number)} + )} diff --git a/src/client/routes/website.tsx b/src/client/routes/website.tsx index c173731..0d2f367 100644 --- a/src/client/routes/website.tsx +++ b/src/client/routes/website.tsx @@ -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}`, })); diff --git a/src/server/trpc/routers/website.ts b/src/server/trpc/routers/website.ts index 6638ee2..8438ec1 100644 --- a/src/server/trpc/routers/website.ts +++ b/src/server/trpc/routers/website.ts @@ -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>((prev, item) => { + if (item.websiteId) { + prev[item.websiteId] = item._count; + } + + return prev; + }, {}); + }), info: workspaceProcedure .meta( buildWebsiteOpenapi({