diff --git a/apps/web/src/app/(app)/@admin/page.tsx b/apps/web/src/app/(app)/@admin/page.tsx index 4b752d1..93e008c 100644 --- a/apps/web/src/app/(app)/@admin/page.tsx +++ b/apps/web/src/app/(app)/@admin/page.tsx @@ -1,97 +1,12 @@ -import { AreaChart } from "@/components/area-chart"; +import { StatisticsContainer } from "@/components/statistics-container"; import { api } from "@/trpc/server"; -import { Chip, Divider, Tooltip } from "@nextui-org/react"; -import moment from "moment"; - -import { Card, CardContent, CardHeader, CardTitle } from "@tanya.in/ui/card"; export default async function HomePage() { - const analytics = await api.analytics.get(); - const data = analytics.map((item) => ({ - date: item.key, - "Page Views": item.total, - Visitors: item.devices, - })); - - const { visitors, pageViews } = analytics.reduce( - (acc, { devices, total }) => { - acc.visitors += devices; - acc.pageViews += total; - return acc; - }, - { - visitors: 0, - pageViews: 0, - }, - ); - const { visitorsLastWeek, pageViewsLastWeek } = analytics.reduce( - (acc, { devices, total, key }) => { - if (moment(key).isSameOrBefore(moment().subtract(1, "week"))) { - acc.visitorsLastWeek += devices; - acc.pageViewsLastWeek += total; - } - return acc; - }, - { - visitorsLastWeek: 0, - pageViewsLastWeek: 0, - }, - ); + const data = await api.analytics.get(); return (
- - - Statistics - - - -
-
-
-

{pageViews}

- pageViewsLastWeek ? "more" : "less"} page views than last week`} - color="foreground" - > - - {pageViews > pageViewsLastWeek - ? "↑ " + Math.abs(pageViews - pageViewsLastWeek) - : "↓ " + Math.abs(pageViews - pageViewsLastWeek)} - - -
-

Page Views

-
- -
-
-
-

{visitors}

- visitorsLastWeek ? "more" : "less"} visitors than last week`} - color="foreground" - > - - {visitors > visitorsLastWeek - ? "↑ " + Math.abs(visitors - visitorsLastWeek) - : "↓ " + Math.abs(visitors - visitorsLastWeek)} - - -
-

Visitors

-
- - +
); } diff --git a/apps/web/src/components/statistics-container.tsx b/apps/web/src/components/statistics-container.tsx new file mode 100644 index 0000000..45d2793 --- /dev/null +++ b/apps/web/src/components/statistics-container.tsx @@ -0,0 +1,100 @@ +"use client"; + +import type { Analytics } from "@/server/api/routers/analytics/analytics.schema"; +import { AreaChart } from "@/components/area-chart"; +import { api } from "@/trpc/react"; +import { Chip, Divider, Tooltip } from "@nextui-org/react"; +import moment from "moment"; + +import { Card, CardContent, CardHeader, CardTitle } from "@tanya.in/ui/card"; + +export function StatisticsContainer(props: { initialData: Analytics[] }) { + const { data } = api.analytics.get.useQuery(undefined, { + initialData: props.initialData, + }); + const mappedData = data.map((item) => ({ + date: item.key, + "Page Views": item.total, + Visitors: item.devices, + })); + + const { visitors, pageViews } = data.reduce( + (acc, { devices, total }) => { + acc.visitors += devices; + acc.pageViews += total; + return acc; + }, + { + visitors: 0, + pageViews: 0, + }, + ); + const { visitorsLastWeek, pageViewsLastWeek } = data.reduce( + (acc, { devices, total, key }) => { + if (moment(key).isSameOrBefore(moment().subtract(1, "week"))) { + acc.visitorsLastWeek += devices; + acc.pageViewsLastWeek += total; + } + return acc; + }, + { + visitorsLastWeek: 0, + pageViewsLastWeek: 0, + }, + ); + + return ( + + + Statistics + + + +
+
+
+

{pageViews}

+ pageViewsLastWeek ? "more" : "less"} page views than last week`} + color="foreground" + > + + {pageViews > pageViewsLastWeek + ? "↑ " + Math.abs(pageViews - pageViewsLastWeek) + : "↓ " + Math.abs(pageViews - pageViewsLastWeek)} + + +
+

Page Views

+
+ +
+
+
+

{visitors}

+ visitorsLastWeek ? "more" : "less"} visitors than last week`} + color="foreground" + > + + {visitors > visitorsLastWeek + ? "↑ " + Math.abs(visitors - visitorsLastWeek) + : "↓ " + Math.abs(visitors - visitorsLastWeek)} + + +
+

Visitors

+
+ + + ); +} diff --git a/apps/web/src/server/api/routers/analytics/analytics.schema.ts b/apps/web/src/server/api/routers/analytics/analytics.schema.ts index ce816b7..9f7000a 100644 --- a/apps/web/src/server/api/routers/analytics/analytics.schema.ts +++ b/apps/web/src/server/api/routers/analytics/analytics.schema.ts @@ -5,3 +5,5 @@ export const analyticsSchema = z.object({ total: z.number().int(), devices: z.number().int(), }); + +export type Analytics = z.infer;