Skip to content

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

Refactor StandardSection to include loading skeleton and improve data rendering

Bill Yangbb6a3398c66e
diff --git a/client/src/app/[site]/components/shared/StandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection.tsx
index d1a086ee7..59fd1e388 100644
--- a/client/src/app/[site]/components/shared/StandardSection.tsx
+++ b/client/src/app/[site]/components/shared/StandardSection.tsx
@@ -1,13 +1,6 @@
"use client";
import { Button } from "@/components/ui/button";
-import {
- Card,
- CardContent,
- CardHeader,
- CardLoader,
- CardTitle,
-} from "@/components/ui/card";
import {
Dialog,
DialogContent,
@@ -16,14 +9,65 @@ import {
DialogTrigger,
} from "@/components/ui/dialog";
import { round } from "lodash";
-import { ReactNode, useMemo } from "react";
-import { addFilter, FilterParameter } from "../../../../lib/store";
-import { formatter } from "../../../../lib/utils";
import { SquareArrowOutUpRight } from "lucide-react";
+import { memo, ReactNode, useMemo } from "react";
import {
SingleColResponse,
useSingleCol,
} from "../../../../api/analytics/useSingleCol";
+import { addFilter, FilterParameter } from "../../../../lib/store";
+import { formatter } from "../../../../lib/utils";
+
+const Skeleton = memo(() => {
+ // Generate widths following Pareto principle with top item at 100%
+ const widths = Array.from({ length: 10 }, (_, i) => {
+ if (i === 0) {
+ // First item always has 100% width
+ return 100;
+ } else if (i === 1) {
+ // Second item gets large width (60-80%)
+ return 60 + Math.random() * 20;
+ } else {
+ // Remaining 8 items get progressively smaller widths (10-40%)
+ const factor = 1 - (i - 2) / 8; // Creates a declining factor from 1 to 0.125
+ return 10 + factor * 30; // Creates widths from ~40% down to ~15%
+ }
+ });
+
+ // Generate random widths for label and value placeholders
+ const labelWidths = Array.from({ length: 10 }, (_, i) => {
+ // First few items get wider labels (increased by 2.5x)
+ return i < 3 ? 75 + Math.random() * 100 : 40 + Math.random() * 60;
+ });
+
+ const valueWidths = Array.from(
+ { length: 10 },
+ () => 20 + Math.random() * 40 // Between 20px and 60px (increased by 2.5x)
+ );
+
+ return Array.from({ length: 10 }).map((_, index) => (
+ <div key={index} className="relative h-7 flex items-center">
+ <div
+ className="absolute inset-0 bg-neutral-800 py-2 rounded-md animate-pulse"
+ style={{ width: `${widths[index]}%` }}
+ ></div>
+ <div className="z-10 mx-2 flex justify-between items-center text-sm w-full">
+ <div className="flex items-center gap-1">
+ <div
+ className="h-4 bg-neutral-800 rounded animate-pulse"
+ style={{ width: `${labelWidths[index]}px` }}
+ ></div>
+ </div>
+ <div className="text-sm flex gap-2">
+ <div
+ className="h-4 bg-neutral-800 rounded animate-pulse"
+ style={{ width: `${valueWidths[index]}px` }}
+ ></div>
+ </div>
+ </div>
+ </div>
+ ));
+});
export function StandardSection({
title,
@@ -50,6 +94,9 @@ export function StandardSection({
periodTime: "previous",
});
+ // Determine if we're in a loading state
+ const isLoading = isFetching || previousIsFetching;
+
const previousDataMap = useMemo(() => {
return previousData?.data?.reduce((acc, curr) => {
acc[getKey(curr)] = curr;
@@ -62,69 +109,72 @@ export function StandardSection({
: 1;
return (
- // <Card>
- // {isFetching && <CardLoader />}
- // <CardHeader>
- // <CardTitle>{title}</CardTitle>
- // </CardHeader>
- // <CardContent className="flex flex-col gap-2">
<div className="flex flex-col gap-2">
- {data?.data?.slice(0, 10).map((e) => (
- <div
- key={getKey(e)}
- className="relative h-7 flex items-center cursor-pointer hover:bg-neutral-850 group"
- onClick={() =>
- addFilter({
- parameter: filterParameter,
- value: [getValue(e)],
- type: "equals",
- })
- }
- >
+ {isLoading ? (
+ <Skeleton />
+ ) : (
+ data?.data?.slice(0, 10).map((e) => (
<div
- className="absolute inset-0 bg-accent-400 py-2 opacity-30 rounded-md"
- style={{ width: `${e.percentage * ratio}%` }}
- ></div>
- <div className="z-10 mx-2 flex justify-between items-center text-sm w-full">
- <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="text-sm flex gap-2">
- <div className="hidden group-hover:block text-neutral-400">
- {round(e.percentage, 1)}%
+ key={getKey(e)}
+ className="relative h-7 flex items-center cursor-pointer hover:bg-neutral-850 group"
+ 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 mx-2 flex justify-between items-center text-sm w-full">
+ <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>{formatter(e.count)}</div>
- {/* <div>
- {previousDataMap?.[getKey(e)]?.percentage ? (
- <div className="text-sm flex gap-2">
- <div>
- {round(
- e.percentage -
- (previousDataMap?.[getKey(e)]?.percentage || 0),
- 1
- )}
- %
+ <div className="text-sm flex gap-2">
+ <div className="hidden group-hover:block text-neutral-400">
+ {round(e.percentage, 1)}%
+ </div>
+ <div>{formatter(e.count)}</div>
+ {/* <div>
+ {previousDataMap?.[getKey(e)]?.percentage ? (
+ <div className="text-sm flex gap-2">
+ <div>
+ {round(
+ e.percentage -
+ (previousDataMap?.[getKey(e)]?.percentage || 0),
+ 1
+ )}
+ %
+ </div>
</div>
- </div>
- ) : null}
- </div> */}
+ ) : null}
+ </div> */}
+ </div>
</div>
</div>
- </div>
- ))}
- {data?.data?.length && data?.data?.length > 10 ? (
+ ))
+ )}
+ {isLoading ? (
+ // Skeleton for "View All" button when loading
+ <Button variant="outline" disabled className="opacity-50">
+ View All
+ </Button>
+ ) : data?.data?.length && data?.data?.length > 10 ? (
<Dialog>
<DialogTrigger asChild>
<Button variant="outline">View All</Button>
@@ -181,7 +231,5 @@ export function StandardSection({
</Dialog>
) : null}
</div>
- // </CardContent>
- // </Card>
);
}