Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor BaseStandardSection to use BaseStandardSectionDialog for improved modularity

Bill Yang0a0123ef4911
diff --git a/client/src/app/[site]/components/shared/StandardSection/BaseStandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection/BaseStandardSection.tsx
index 81df1dc13..6ca0555c1 100644
--- a/client/src/app/[site]/components/shared/StandardSection/BaseStandardSection.tsx
+++ b/client/src/app/[site]/components/shared/StandardSection/BaseStandardSection.tsx
@@ -1,11 +1,4 @@
import { Button } from "@/components/ui/button";
-import {
- Dialog,
- DialogContent,
- DialogHeader,
- DialogTitle,
- DialogTrigger,
-} from "@/components/ui/dialog";
import { round } from "lodash";
import { AlertCircle, RefreshCcw, SquareArrowOutUpRight } from "lucide-react";
import { ReactNode } from "react";
@@ -13,6 +6,7 @@ import { SingleColResponse } from "../../../../../api/analytics/useSingleCol";
import { addFilter, FilterParameter } from "../../../../../lib/store";
import { formatter } from "../../../../../lib/utils";
import { Skeleton } from "./Skeleton";
+import { BaseStandardSectionDialog } from "./BaseStandardSectionDialog";
export const Row = ({
e,
@@ -82,6 +76,7 @@ interface BaseStandardSectionProps {
refetch: () => void;
getKey: (item: SingleColResponse) => string;
getLabel: (item: SingleColResponse) => ReactNode;
+ getFilterLabel?: (item: SingleColResponse) => string;
getValue: (item: SingleColResponse) => string;
getLink?: (item: SingleColResponse) => string;
countLabel?: string;
@@ -96,6 +91,7 @@ export function BaseStandardSection({
refetch,
getKey,
getLabel,
+ getFilterLabel,
getValue,
getLink,
countLabel,
@@ -163,66 +159,18 @@ export function BaseStandardSection({
View All
</Button>
) : !hasError && data?.data?.length && data?.data?.length > 10 ? (
- <Dialog>
- <DialogTrigger asChild>
- <Button variant="outline">View All</Button>
- </DialogTrigger>
- <DialogContent className="max-w-[90vw] w-[1000px]">
- <DialogHeader>
- <DialogTitle>{title}</DialogTitle>
- </DialogHeader>
- <div className="flex flex-col gap-2">
- <div className="flex flex-row gap-2 justify-between pr-20 text-sm text-neutral-400">
- <div>{title}</div>
- <div>{countLabel || "Sessions"}</div>
- </div>
- <div className="flex flex-col gap-2 max-h-[85vh] overflow-x-hidden">
- {data?.data?.map((e) => (
- <div
- key={getKey(e)}
- className="relative flex items-center mr-3 cursor-pointer hover:bg-neutral-850"
- onClick={() =>
- addFilter({
- parameter: filterParameter,
- value: [getValue(e)],
- type: "equals",
- })
- }
- >
- <div
- className="absolute inset-0 bg-accent-400 py-2 opacity-30 rounded-md"
- style={{ width: `${e.percentage * ratio}%` }}
- ></div>
- <div className="z-10 ml-2 mr-4 flex justify-between items-center text-xs w-full h-6">
- <div className="flex items-center gap-1">
- {getLabel(e)}
- {getLink && (
- <a
- href={getLink(e)}
- target="_blank"
- onClick={(e) => e.stopPropagation()}
- >
- <SquareArrowOutUpRight
- className="w-3 h-3 text-neutral-300 hover:text-neutral-100"
- strokeWidth={3}
- />
- </a>
- )}
- </div>
- <div className="flex">
- <div>{e.count.toLocaleString()}</div>
- <div className="mx-2 bg-neutral-400 w-[1px] rounded-full h-5"></div>
- <div className="w-10 text-neutral-400">
- {round(e.percentage, 2)}%
- </div>
- </div>
- </div>
- </div>
- ))}
- </div>
- </div>
- </DialogContent>
- </Dialog>
+ <BaseStandardSectionDialog
+ title={title}
+ data={data.data}
+ ratio={ratio}
+ getKey={getKey}
+ getLabel={getLabel}
+ getValue={getValue}
+ getFilterLabel={getFilterLabel}
+ getLink={getLink}
+ countLabel={countLabel}
+ filterParameter={filterParameter}
+ />
) : null}
</div>
);
diff --git a/client/src/app/[site]/components/shared/StandardSection/BaseStandardSectionDialog.tsx b/client/src/app/[site]/components/shared/StandardSection/BaseStandardSectionDialog.tsx
new file mode 100644
index 000000000..540ccad91
--- /dev/null
+++ b/client/src/app/[site]/components/shared/StandardSection/BaseStandardSectionDialog.tsx
@@ -0,0 +1,135 @@
+import {
+ Dialog,
+ DialogContent,
+ DialogHeader,
+ DialogTitle,
+ DialogTrigger,
+} from "@/components/ui/dialog";
+import { Button } from "@/components/ui/button";
+import { round } from "lodash";
+import { SquareArrowOutUpRight, Search } from "lucide-react";
+import { ReactNode, useState } from "react";
+import { SingleColResponse } from "../../../../../api/analytics/useSingleCol";
+import { addFilter, FilterParameter } from "../../../../../lib/store";
+import { Input } from "@/components/ui/input";
+
+interface BaseStandardSectionDialogProps {
+ title: string;
+ data?: SingleColResponse[];
+ ratio: number;
+ getKey: (item: SingleColResponse) => string;
+ getLabel: (item: SingleColResponse) => ReactNode;
+ getValue: (item: SingleColResponse) => string;
+ getFilterLabel?: (item: SingleColResponse) => string;
+ getLink?: (item: SingleColResponse) => string;
+ countLabel?: string;
+ filterParameter: FilterParameter;
+}
+
+export function BaseStandardSectionDialog({
+ title,
+ data,
+ ratio,
+ getKey,
+ getLabel,
+ getValue,
+ getFilterLabel,
+ getLink,
+ countLabel,
+ filterParameter,
+}: BaseStandardSectionDialogProps) {
+ const [searchTerm, setSearchTerm] = useState("");
+
+ if (!data || data.length === 0) return null;
+
+ const labelFnToUse = getFilterLabel || getValue;
+
+ // Filter data based on search term
+ const filteredData = data.filter((item) => {
+ const label =
+ typeof labelFnToUse(item) === "string"
+ ? (labelFnToUse(item) as string)
+ : labelFnToUse(item);
+
+ return label.toLowerCase().includes(searchTerm.toLowerCase());
+ });
+
+ return (
+ <Dialog>
+ <DialogTrigger asChild>
+ <Button variant="outline">View All</Button>
+ </DialogTrigger>
+ <DialogContent className="max-w-[90vw] w-[1000px]">
+ <DialogHeader>
+ <DialogTitle>{title}</DialogTitle>
+ </DialogHeader>
+ <div className="flex flex-col gap-2">
+ <div className="relative">
+ <Search className="absolute left-2.5 top-2.5 h-4 w-4 text-neutral-400" />
+ <Input
+ type="text"
+ placeholder={`Filter ${data.length} items...`}
+ className="pl-9 bg-neutral-900 border-neutral-700 focus-visible:ring-accent-400"
+ value={searchTerm}
+ onChange={(e) => setSearchTerm(e.target.value)}
+ />
+ </div>
+ <div className="flex flex-row gap-2 justify-between pr-20 text-sm text-neutral-400">
+ <div>{title}</div>
+ <div>{countLabel || "Sessions"}</div>
+ </div>
+ <div className="flex flex-col gap-2 max-h-[85vh] overflow-x-hidden">
+ {filteredData.length > 0 ? (
+ filteredData.map((e) => (
+ <div
+ key={getKey(e)}
+ className="relative flex items-center mr-3 cursor-pointer hover:bg-neutral-850"
+ onClick={() =>
+ addFilter({
+ parameter: filterParameter,
+ value: [getValue(e)],
+ type: "equals",
+ })
+ }
+ >
+ <div
+ className="absolute inset-0 bg-accent-400 py-2 opacity-30 rounded-md"
+ style={{ width: `${e.percentage * ratio}%` }}
+ ></div>
+ <div className="z-10 ml-2 mr-4 flex justify-between items-center text-xs w-full h-6">
+ <div className="flex items-center gap-1">
+ {getLabel(e)}
+ {getLink && (
+ <a
+ href={getLink(e)}
+ target="_blank"
+ onClick={(e) => e.stopPropagation()}
+ >
+ <SquareArrowOutUpRight
+ className="w-3 h-3 text-neutral-300 hover:text-neutral-100"
+ strokeWidth={3}
+ />
+ </a>
+ )}
+ </div>
+ <div className="flex">
+ <div>{e.count.toLocaleString()}</div>
+ <div className="mx-2 bg-neutral-400 w-[1px] rounded-full h-5"></div>
+ <div className="w-10 text-neutral-400">
+ {round(e.percentage, 2)}%
+ </div>
+ </div>
+ </div>
+ </div>
+ ))
+ ) : (
+ <div className="py-10 text-center text-neutral-400">
+ No results match your search
+ </div>
+ )}
+ </div>
+ </div>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx
index 766372490..05b946ffa 100644
--- a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx
+++ b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx
@@ -13,6 +13,7 @@ export function StandardSection({
getKey,
getLabel,
getValue,
+ getFilterLabel,
getLink,
countLabel,
filterParameter,
@@ -22,6 +23,7 @@ export function StandardSection({
getKey: (item: SingleColResponse) => string;
getLabel: (item: SingleColResponse) => ReactNode;
getValue: (item: SingleColResponse) => string;
+ getFilterLabel?: (item: SingleColResponse) => string;
getLink?: (item: SingleColResponse) => string;
countLabel?: string;
filterParameter: FilterParameter;
@@ -56,6 +58,7 @@ export function StandardSection({
getKey={getKey}
getLabel={getLabel}
getValue={getValue}
+ getFilterLabel={getFilterLabel}
getLink={getLink}
countLabel={countLabel}
filterParameter={filterParameter}
diff --git a/client/src/app/[site]/main/components/sections/Countries.tsx b/client/src/app/[site]/main/components/sections/Countries.tsx
index 9cc3e029d..683204246 100644
--- a/client/src/app/[site]/main/components/sections/Countries.tsx
+++ b/client/src/app/[site]/main/components/sections/Countries.tsx
@@ -73,6 +73,7 @@ export function Countries({ isRealtime = false }: { isRealtime?: boolean }) {
title="Countries"
getValue={(e) => e.value}
getKey={(e) => e.value}
+ getFilterLabel={(e) => getCountryName(e.value)}
getLabel={(e) => {
return (
<div className="flex gap-2 items-center">
@@ -89,6 +90,12 @@ export function Countries({ isRealtime = false }: { isRealtime?: boolean }) {
title="Regions"
getValue={(e) => e.value}
getKey={(e) => e.value}
+ getFilterLabel={(e) => {
+ const region = subdivisions?.features.find(
+ (feature) => feature.properties.iso_3166_2 === e.value
+ )?.properties;
+ return region?.name ?? "";
+ }}
getLabel={(e) => {
if (!e.value) {
return "Unknown";
@@ -131,6 +138,7 @@ export function Countries({ isRealtime = false }: { isRealtime?: boolean }) {
title="Languages"
getValue={(e) => e.value}
getKey={(e) => e.value}
+ getFilterLabel={(e) => getLanguageName(e.value) ?? ""}
getLabel={(e) => (
<div className="flex gap-2 items-center">
{getCountryFromLanguage(e.value) ? (
diff --git a/client/src/app/[site]/main/page.tsx b/client/src/app/[site]/main/page.tsx
index 847f1c78b..8767475be 100644
--- a/client/src/app/[site]/main/page.tsx
+++ b/client/src/app/[site]/main/page.tsx
@@ -28,7 +28,7 @@ function MainPageContent() {
}
return (
- <div className="p-4 max-w-[1300px] mx-auto space-y-3">
+ <div className="p-4 max-w-[1100px] mx-auto space-y-3">
<SubHeader />
<MainSection />
<div className="grid grid-cols-2 gap-4 mt-4">
diff --git a/client/src/app/[site]/realtime/page.tsx b/client/src/app/[site]/realtime/page.tsx
index 4732b811c..d0ba384d4 100644
--- a/client/src/app/[site]/realtime/page.tsx
+++ b/client/src/app/[site]/realtime/page.tsx
@@ -28,10 +28,14 @@ export default function RealtimePage() {
const { data } = useGetLiveUsercount(Number(minutes));
return (
- <div className="relative overflow-hidden" ref={ref}>
+ <div
+ className="relative overflow-hidden"
+ style={{ height: "calc(100vh - 36px)" }}
+ ref={ref}
+ >
<World width={width ?? 0} />
- <div className="absolute top-4 left-4 w-full h-full">
+ <div className="absolute top-4 left-4 ">
<div className="flex flex-col p-3 bg-neutral-900 rounded-lg shadow-lg border border-neutral-750 w-[400px]">
<div className="p-2">
<div className="text-sm text-gray-400 flex gap-2 text-nowrap items-center">