🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
add StandardCard
Bill Yang6cb5378703c3
diff --git a/client/src/app/actions/getBrowsers.ts b/client/src/app/actions/getBrowsers.tsindex 207f2a63c..dd24ec463 100644--- a/client/src/app/actions/getBrowsers.ts+++ b/client/src/app/actions/getBrowsers.ts@@ -3,7 +3,7 @@import { clickhouse } from "@/lib/clickhouse";import { getTimeStatement } from "./utils";-type Response = {+type GetBrowsersResponse = {browser: string;count: number;percentage: number;@@ -17,7 +17,7 @@ export async function getBrowsers({startDate: string;endDate: string;timezone: string;-}): Promise<{ data?: Response; error?: string }> {+}): Promise<{ data?: GetBrowsersResponse; error?: string }> {const query = `SELECTbrowser,@@ -35,7 +35,7 @@ export async function getBrowsers({format: "JSONEachRow",});- const data: Response = await result.json();+ const data: GetBrowsersResponse = await result.json();return { data };} catch (error) {console.error("Error fetching browsers:", error);diff --git a/client/src/app/actions/getDevices.ts b/client/src/app/actions/getDevices.tsindex abf6b8ff6..db185984f 100644--- a/client/src/app/actions/getDevices.ts+++ b/client/src/app/actions/getDevices.ts@@ -3,7 +3,7 @@import { clickhouse } from "@/lib/clickhouse";import { getTimeStatement } from "./utils";-type Response = {+type GetDevicesResponse = {device_type: string;count: number;percentage: number;@@ -17,7 +17,7 @@ export async function getDevices({startDate: string;endDate: string;timezone: string;-}): Promise<{ data?: Response; error?: string }> {+}): Promise<{ data?: GetDevicesResponse; error?: string }> {const query = `SELECTdevice_type,@@ -35,7 +35,7 @@ export async function getDevices({format: "JSONEachRow",});- const data: Response = await result.json();+ const data: GetDevicesResponse = await result.json();return { data };} catch (error) {console.error("Error fetching devices:", error);diff --git a/client/src/app/actions/getOperatingSystems.ts b/client/src/app/actions/getOperatingSystems.tsindex 115c481fd..60d4a5cce 100644--- a/client/src/app/actions/getOperatingSystems.ts+++ b/client/src/app/actions/getOperatingSystems.ts@@ -3,7 +3,7 @@import { clickhouse } from "@/lib/clickhouse";import { getTimeStatement } from "./utils";-type Response = {+type GetOperatingSystemsResponse = {operating_system: string;count: number;percentage: number;@@ -17,7 +17,7 @@ export async function getOperatingSystems({startDate: string;endDate: string;timezone: string;-}): Promise<{ data?: Response; error?: string }> {+}): Promise<{ data?: GetOperatingSystemsResponse; error?: string }> {const query = `SELECToperating_system,@@ -35,7 +35,7 @@ export async function getOperatingSystems({format: "JSONEachRow",});- const data: Response = await result.json();+ const data: GetOperatingSystemsResponse = await result.json();return { data };} catch (error) {console.error("Error fetching operating systems:", error);diff --git a/client/src/app/actions/getPages.ts b/client/src/app/actions/getPages.tsindex a3f59f8eb..698f07ae8 100644--- a/client/src/app/actions/getPages.ts+++ b/client/src/app/actions/getPages.ts@@ -3,7 +3,7 @@import { clickhouse } from "@/lib/clickhouse";import { getTimeStatement } from "./utils";-type Response = {+type GetPagesResponse = {pathname: string;count: number;percentage: number;@@ -17,7 +17,7 @@ export async function getPages({startDate: string;endDate: string;timezone: string;-}): Promise<{ data?: Response; error?: string }> {+}): Promise<{ data?: GetPagesResponse; error?: string }> {const query = `SELECTpathname,@@ -37,7 +37,7 @@ export async function getPages({format: "JSONEachRow",});- const data: Response = await result.json();+ const data: GetPagesResponse = await result.json();return { data };} catch (error) {console.error("Error fetching pages:", error);diff --git a/client/src/app/actions/getPageviews.ts b/client/src/app/actions/getPageviews.tsindex ba9d16fbf..9f63ea8c0 100644--- a/client/src/app/actions/getPageviews.ts+++ b/client/src/app/actions/getPageviews.ts@@ -3,7 +3,7 @@import { clickhouse } from "@/lib/clickhouse";import { getTimeStatement } from "./utils";-type Response = { time: string; pageviews: number }[];+type GetPageViewsResponse = { time: string; pageviews: number }[];export async function getPageViews({startDate,@@ -13,7 +13,7 @@ export async function getPageViews({startDate: string;endDate: string;timezone: string;-}): Promise<{ data?: Response; error?: string }> {+}): Promise<{ data?: GetPageViewsResponse; error?: string }> {const query = `SELECTtoStartOfHour(toTimeZone(timestamp, '${timezone}')) AS time,@@ -33,7 +33,7 @@ export async function getPageViews({format: "JSONEachRow",});- const data: Response = await result.json();+ const data: GetPageViewsResponse = await result.json();return { data };} catch (error) {console.error("Error fetching pageviews:", error);diff --git a/client/src/app/components/sections/Browsers/Browsers.tsx b/client/src/app/components/sections/Browsers/Browsers.tsxindex 7c7d8f682..b158cf1a5 100644--- a/client/src/app/components/sections/Browsers/Browsers.tsx+++ b/client/src/app/components/sections/Browsers/Browsers.tsx@@ -1,41 +1,17 @@"use client";-import {- Card,- CardContent,- CardHeader,- CardTitle,-} from "../../../../components/ui/card";import { useGetBrowsers } from "../../../hooks/useGetBrowsers";+import { StandardCard } from "../../shared/StandardCard";export function Browsers() {const { data, isLoading } = useGetBrowsers();return (- <Card>- <CardHeader>- <CardTitle>Browsers</CardTitle>- </CardHeader>- <CardContent className="flex flex-col gap-2">- {data?.data?.slice(0, 10).map((e) => (- <div key={e.browser} className="relative">- <div- className="absolute inset-0 bg-red-500 py-2 opacity-50"- style={{ width: `${e.percentage}%` }}- ></div>- <div className="relative z-10 ml-1 flex justify-between items-center">- <div>{e.browser || "Other"}</div>- <div className="text-sm flex">- <div>{e.count}</div>- <div className="ml-1 text-neutral-500">|</div>- <div className="ml-1 w-10 text-neutral-500">- {e.percentage}%- </div>- </div>- </div>- </div>- ))}- </CardContent>- </Card>+ <StandardCard+ title="Browsers"+ data={data}+ getKey={(e) => e.browser}+ getLabel={(e) => e.browser || "Other"}+ />);}diff --git a/client/src/app/components/sections/Devices/Devices.tsx b/client/src/app/components/sections/Devices/Devices.tsxindex a80612dee..3ee2743ae 100644--- a/client/src/app/components/sections/Devices/Devices.tsx+++ b/client/src/app/components/sections/Devices/Devices.tsx@@ -1,41 +1,16 @@"use client";-import {- Card,- CardContent,- CardHeader,- CardTitle,-} from "../../../../components/ui/card";import { useGetDevices } from "../../../hooks/useGetDevices";+import { StandardCard } from "../../shared/StandardCard";export function Devices() {const { data, isLoading } = useGetDevices();-return (- <Card>- <CardHeader>- <CardTitle>Devices</CardTitle>- </CardHeader>- <CardContent className="flex flex-col gap-2">- {data?.data?.slice(0, 10).map((e) => (- <div key={e.device_type} className="relative">- <div- className="absolute inset-0 bg-red-500 py-2 opacity-50"- style={{ width: `${e.percentage}%` }}- ></div>- <div className="relative z-10 ml-1 flex justify-between items-center">- <div>{e.device_type || "Other"}</div>- <div className="text-sm flex">- <div>{e.count}</div>- <div className="ml-1 text-neutral-500">|</div>- <div className="ml-1 w-10 text-neutral-500">- {e.percentage}%- </div>- </div>- </div>- </div>- ))}- </CardContent>- </Card>+ <StandardCard+ title="Devices"+ data={data}+ getKey={(e) => e.device_type}+ getLabel={(e) => e.device_type || "Other"}+ />);}diff --git a/client/src/app/components/sections/OperatingSystems/OperatingSystems.tsx b/client/src/app/components/sections/OperatingSystems/OperatingSystems.tsxindex 127d58e40..cb29f5e30 100644--- a/client/src/app/components/sections/OperatingSystems/OperatingSystems.tsx+++ b/client/src/app/components/sections/OperatingSystems/OperatingSystems.tsx@@ -1,41 +1,16 @@"use client";-import {- Card,- CardContent,- CardHeader,- CardTitle,-} from "../../../../components/ui/card";import { useGetOperatingSystems } from "../../../hooks/useGetOperatingSystems";+import { StandardCard } from "../../shared/StandardCard";export function OperatingSystems() {const { data, isLoading } = useGetOperatingSystems();-return (- <Card>- <CardHeader>- <CardTitle>Operating Systems</CardTitle>- </CardHeader>- <CardContent className="flex flex-col gap-2">- {data?.data?.map((e) => (- <div key={e.operating_system} className="relative">- <div- className="absolute inset-0 bg-red-500 py-2 opacity-50"- style={{ width: `${e.percentage}%` }}- ></div>- <div className="relative z-10 ml-1 flex justify-between items-center">- <div>{e.operating_system || "Other"}</div>- <div className="text-sm flex">- <div>{e.count}</div>- <div className="ml-1 text-neutral-500">|</div>- <div className="ml-1 w-10 text-neutral-500">- {e.percentage}%- </div>- </div>- </div>- </div>- ))}- </CardContent>- </Card>+ <StandardCard+ title="Operating Systems"+ data={data}+ getKey={(e) => e.operating_system}+ getLabel={(e) => e.operating_system || "Other"}+ />);}diff --git a/client/src/app/components/sections/Pages/Pages.tsx b/client/src/app/components/sections/Pages/Pages.tsxindex f8acf7304..e5c066199 100644--- a/client/src/app/components/sections/Pages/Pages.tsx+++ b/client/src/app/components/sections/Pages/Pages.tsx@@ -1,41 +1,16 @@"use client";-import {- Card,- CardContent,- CardHeader,- CardTitle,-} from "../../../../components/ui/card";import { useGetPages } from "../../../hooks/useGetPages";+import { StandardCard } from "../../shared/StandardCard";export function Pages() {const { data, isLoading } = useGetPages();-return (- <Card>- <CardHeader>- <CardTitle>Pages</CardTitle>- </CardHeader>- <CardContent className="flex flex-col gap-2">- {data?.data?.slice(0, 10).map((e) => (- <div key={e.pathname} className="relative">- <div- className="absolute inset-0 bg-red-500 py-2 opacity-50"- style={{ width: `${e.percentage}%` }}- ></div>- <div className="relative z-10 ml-1 flex justify-between items-center">- <div>{e.pathname || "Other"}</div>- <div className="text-sm flex">- <div>{e.count}</div>- <div className="ml-1 text-neutral-500">|</div>- <div className="ml-1 w-10 text-neutral-500">- {e.percentage}%- </div>- </div>- </div>- </div>- ))}- </CardContent>- </Card>+ <StandardCard+ title="Pages"+ data={data}+ getKey={(e) => e.pathname}+ getLabel={(e) => e.pathname || "Other"}+ />);}diff --git a/client/src/app/components/shared/StandardCard.tsx b/client/src/app/components/shared/StandardCard.tsxnew file mode 100644index 000000000..340068193--- /dev/null+++ b/client/src/app/components/shared/StandardCard.tsx@@ -0,0 +1,53 @@+"use client";++import {+ Card,+ CardContent,+ CardHeader,+ CardTitle,+} from "../../../components/ui/card";++export function StandardCard<T extends { percentage: number; count: number }>({+ title,+ data,+ getKey,+ getLabel,+}: {+ title: string;+ data:+ | {+ data?: T[];+ error?: string;+ }+ | undefined;+ getKey: (item: T) => string;+ getLabel: (item: T) => string;+}) {+ return (+ <Card>+ <CardHeader>+ <CardTitle>{title}</CardTitle>+ </CardHeader>+ <CardContent className="flex flex-col gap-2">+ {data?.data?.slice(0, 10).map((e) => (+ <div key={getKey(e)} className="relative">+ <div+ className="absolute inset-0 bg-red-500 py-2 opacity-50"+ style={{ width: `${e.percentage}%` }}+ ></div>+ <div className="relative z-10 ml-1 flex justify-between items-center">+ <div>{getLabel(e)}</div>+ <div className="text-sm flex">+ <div>{e.count}</div>+ <div className="ml-1 text-neutral-500">|</div>+ <div className="ml-1 w-10 text-neutral-500">+ {e.percentage}%+ </div>+ </div>+ </div>+ </div>+ ))}+ </CardContent>+ </Card>+ );+}