Skip to content

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

Paginate single col (#291)

Bill Yangd8dfa49e1d74
diff --git a/client/src/api/analytics/useGetPageTitles.ts b/client/src/api/analytics/useGetPageTitles.ts
index 15e552a29..0f1a44ffd 100644
--- a/client/src/api/analytics/useGetPageTitles.ts
+++ b/client/src/api/analytics/useGetPageTitles.ts
@@ -26,7 +26,7 @@ export type PageTitlesStandardResponse = PageTitleItem[];
type UseGetPageTitlesOptions = {
limit?: number;
- offset?: number;
+ page?: number;
useFilters?: boolean;
// No specific 'parameter' needed as this hook is for page_titles
};
@@ -34,7 +34,7 @@ type UseGetPageTitlesOptions = {
// Hook for paginated fetching (e.g., for a dedicated "All Page Titles" screen)
export function useGetPageTitlesPaginated({
limit = 10,
- offset = 0,
+ page = 1,
useFilters = true,
}: UseGetPageTitlesOptions): UseQueryResult<
APIResponse<PageTitlesPaginatedResponse>
@@ -51,7 +51,7 @@ export function useGetPageTitlesPaginated({
timeZone: timeZone,
minutes: 24 * 60, // 24 hours in minutes
limit,
- offset,
+ page,
filters: useFilters ? filters : undefined,
}
: {
@@ -59,7 +59,7 @@ export function useGetPageTitlesPaginated({
...getStartAndEndDate(time),
timeZone: timeZone,
limit,
- offset,
+ page,
filters: useFilters ? filters : undefined,
};
@@ -70,7 +70,7 @@ export function useGetPageTitlesPaginated({
site,
filters,
limit,
- offset,
+ page,
isPast24HoursMode ? "past-minutes" : "date-range",
],
queryFn: () => {
diff --git a/client/src/api/analytics/useInfiniteSingleCol.ts b/client/src/api/analytics/useInfiniteSingleCol.ts
new file mode 100644
index 000000000..053ffc041
--- /dev/null
+++ b/client/src/api/analytics/useInfiniteSingleCol.ts
@@ -0,0 +1,68 @@
+import { SingleColResponse } from "@/api/analytics/useSingleCol";
+import { authedFetch, getStartAndEndDate } from "@/api/utils";
+import { BACKEND_URL } from "@/lib/const";
+import { timeZone } from "@/lib/dateTimeUtils";
+import { FilterParameter, useStore } from "@/lib/store";
+import {
+ useInfiniteQuery,
+ UseInfiniteQueryResult,
+ InfiniteData,
+} from "@tanstack/react-query";
+
+type UseInfiniteSingleColOptions = {
+ parameter: FilterParameter;
+ limit?: number;
+ useFilters?: boolean;
+};
+
+export type InfinitePaginatedResponse = {
+ data: SingleColResponse[];
+ totalCount: number;
+};
+
+export function useInfiniteSingleCol({
+ parameter,
+ limit = 25,
+ useFilters = true,
+}: UseInfiniteSingleColOptions): UseInfiniteQueryResult<
+ InfiniteData<InfinitePaginatedResponse>
+> {
+ const { time, site, filters } = useStore();
+
+ return useInfiniteQuery({
+ queryKey: [parameter, time, site, filters, limit, "infinite-single-col"],
+ queryFn: async ({ pageParam = 1 }) => {
+ const queryParams = {
+ ...getStartAndEndDate(time),
+ timeZone: timeZone,
+ parameter,
+ limit,
+ page: pageParam,
+ filters: useFilters ? filters : undefined,
+ };
+
+ const response = await authedFetch(
+ `${BACKEND_URL}/single-col/${site}`,
+ queryParams
+ );
+ const json = await response.json();
+ return json.data;
+ },
+ initialPageParam: 1,
+ getNextPageParam: (lastPage, allPages) => {
+ // If we've fetched all items, don't get next page
+ const totalItems = lastPage.totalCount;
+ const fetchedItemCount = allPages.reduce(
+ (acc, page) => acc + page.data.length,
+ 0
+ );
+
+ if (fetchedItemCount >= totalItems) {
+ return undefined;
+ }
+
+ return allPages.length + 1;
+ },
+ staleTime: Infinity,
+ });
+}
diff --git a/client/src/api/analytics/usePaginatedSingleCol.ts b/client/src/api/analytics/usePaginatedSingleCol.ts
index 2a0af7d7d..f84c52430 100644
--- a/client/src/api/analytics/usePaginatedSingleCol.ts
+++ b/client/src/api/analytics/usePaginatedSingleCol.ts
@@ -1,19 +1,17 @@
-import { useQuery, UseQueryResult } from "@tanstack/react-query";
-import { FilterParameter, useStore } from "@/lib/store";
-import { timeZone } from "@/lib/dateTimeUtils";
-import { APIResponse } from "@/api/types";
-import { BACKEND_URL } from "@/lib/const";
-import { getStartAndEndDate, authedFetch } from "@/api/utils";
import { SingleColResponse } from "@/api/analytics/useSingleCol";
+import { authedFetch, getStartAndEndDate } from "@/api/utils";
+import { BACKEND_URL } from "@/lib/const";
+import { timeZone } from "@/lib/dateTimeUtils";
+import { FilterParameter, useStore } from "@/lib/store";
+import { useQuery, UseQueryResult } from "@tanstack/react-query";
type UsePaginatedSingleColOptions = {
parameter: FilterParameter;
limit?: number;
- offset?: number;
+ page?: number;
useFilters?: boolean;
};
-// Reverted: Backend will now return this structure inside APIResponse.data
type PaginatedResponse = {
data: SingleColResponse[];
totalCount: number;
@@ -22,11 +20,9 @@ type PaginatedResponse = {
export function usePaginatedSingleCol({
parameter,
limit = 10,
- offset = 0,
+ page = 1,
useFilters = true,
-}: UsePaginatedSingleColOptions): UseQueryResult<
- APIResponse<PaginatedResponse> // Adjusted return type back
-> {
+}: UsePaginatedSingleColOptions): UseQueryResult<PaginatedResponse> {
const { time, site, filters } = useStore();
// Determine the query parameters based on mode
@@ -35,7 +31,7 @@ export function usePaginatedSingleCol({
timeZone: timeZone,
parameter,
limit,
- offset,
+ page,
filters: useFilters ? filters : undefined,
};
@@ -46,13 +42,13 @@ export function usePaginatedSingleCol({
site,
filters,
limit,
- offset,
+ page,
"paginated-single-col",
], // More specific key
queryFn: () => {
- return authedFetch(`${BACKEND_URL}/single-col/${site}`, queryParams).then(
- (res) => res.json()
- );
+ return authedFetch(`${BACKEND_URL}/single-col/${site}`, queryParams)
+ .then((res) => res.json())
+ .then(({ data }) => data);
},
staleTime: Infinity,
placeholderData: (_, query: any) => {
diff --git a/client/src/app/[site]/components/shared/StandardSection/BaseStandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection/BaseStandardSection.tsx
deleted file mode 100644
index a5d1b1506..000000000
--- a/client/src/app/[site]/components/shared/StandardSection/BaseStandardSection.tsx
+++ /dev/null
@@ -1,210 +0,0 @@
-import { Button } from "@/components/ui/button";
-import NumberFlow from "@number-flow/react";
-import { round } from "lodash";
-import {
- AlertCircle,
- Info,
- RefreshCcw,
- SquareArrowOutUpRight,
-} from "lucide-react";
-import { ReactNode } from "react";
-import { SingleColResponse } from "../../../../../api/analytics/useSingleCol";
-import {
- addFilter,
- FilterParameter,
- removeFilter,
- useStore,
-} from "../../../../../lib/store";
-import { BaseStandardSectionDialog } from "./BaseStandardSectionDialog";
-import { Skeleton } from "./Skeleton";
-
-export const Row = ({
- e,
- ratio,
- getKey,
- getLabel,
- getValue,
- getLink,
- filterParameter,
-}: {
- e: SingleColResponse;
- ratio: number;
- getKey: (item: SingleColResponse) => string;
- getLabel: (item: SingleColResponse) => ReactNode;
- getValue: (item: SingleColResponse) => string;
- getLink?: (item: SingleColResponse) => string;
- filterParameter: FilterParameter;
-}) => {
- const filters = useStore((state) => state.filters);
-
- return (
- <div
- key={getKey(e)}
- className="relative h-6 flex items-center cursor-pointer hover:bg-neutral-850 group"
- onClick={() => {
- const foundFilter = filters.find(
- (f) =>
- f.parameter === filterParameter &&
- f.value.some((v) => v === getValue(e))
- );
- if (foundFilter) {
- removeFilter(foundFilter);
- } else {
- addFilter({
- parameter: filterParameter,
- value: [getValue(e)],
- type: "equals",
- });
- }
- }}
- >
- <div
- className="absolute inset-0 bg-dataviz py-2 opacity-25 rounded-md"
- style={{ width: `${e.percentage * ratio}%` }}
- ></div>
- <div className="z-10 mx-2 flex justify-between items-center text-xs 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-xs flex gap-2">
- <div className="hidden group-hover:block text-neutral-400">
- {round(e.percentage, 1)}%
- </div>
- <NumberFlow
- respectMotionPreference={false}
- value={e.count}
- format={{ notation: "compact" }}
- />
- </div>
- </div>
- </div>
- );
-};
-
-interface BaseStandardSectionProps {
- title: string;
- data: { data?: SingleColResponse[] } | undefined;
- isLoading: boolean;
- error: Error | null;
- refetch: () => void;
- getKey: (item: SingleColResponse) => string;
- getLabel: (item: SingleColResponse) => ReactNode;
- getFilterLabel?: (item: SingleColResponse) => string;
- getValue: (item: SingleColResponse) => string;
- getLink?: (item: SingleColResponse) => string;
- countLabel?: string;
- filterParameter: FilterParameter;
- expanded: boolean;
- close: () => void;
-}
-
-export function BaseStandardSection({
- title,
- data,
- isLoading,
- error,
- refetch,
- getKey,
- getLabel,
- getFilterLabel,
- getValue,
- getLink,
- countLabel,
- filterParameter,
- expanded,
- close,
-}: BaseStandardSectionProps) {
- const ratio = data?.data?.[0]?.percentage
- ? 100 / data?.data?.[0]?.percentage
- : 1;
-
- const hasError = error;
- const errorMessage =
- error?.message || "An error occurred while fetching data";
-
- return (
- <div className="flex flex-col gap-2">
- {isLoading ? (
- <Skeleton />
- ) : hasError ? (
- <div className="py-6 flex-1 flex flex-col items-center justify-center gap-3 transition-all">
- <AlertCircle className="text-amber-400 w-8 h-8" />
- <div className="text-center">
- <div className="text-neutral-100 font-medium mb-1">
- Failed to load data
- </div>
- <div className="text-sm text-neutral-400 max-w-md mx-auto mb-3">
- {errorMessage}
- </div>
- </div>
- <Button
- variant="outline"
- size="sm"
- className="bg-transparent hover:bg-neutral-800 border-neutral-700 text-neutral-300 hover:text-neutral-100"
- onClick={() => refetch()}
- >
- <RefreshCcw className="w-3 h-3" /> Try Again
- </Button>
- </div>
- ) : (
- <div className="flex flex-col gap-2">
- <div className="flex flex-row gap-2 justify-between pr-1 text-xs text-neutral-400">
- <div>{title}</div>
- <div>{countLabel || "Sessions"}</div>
- </div>
- {data?.data?.length ? (
- data?.data
- ?.slice(0, 10)
- .map((e) => (
- <Row
- key={getKey(e)}
- e={e}
- ratio={ratio}
- getKey={getKey}
- getLabel={getLabel}
- getValue={getValue}
- getLink={getLink}
- filterParameter={filterParameter}
- />
- ))
- ) : (
- <div className="text-neutral-300 w-full text-center mt-6 flex flex-row gap-2 items-center justify-center">
- <Info className="w-5 h-5" />
- No Data
- </div>
- )}
- </div>
- )}
- {!isLoading && !hasError && data?.data?.length ? (
- <div className="flex flex-row gap-2 justify-between items-center">
- <BaseStandardSectionDialog
- title={title}
- data={data.data}
- ratio={ratio}
- getKey={getKey}
- getLabel={getLabel}
- getValue={getValue}
- getFilterLabel={getFilterLabel}
- getLink={getLink}
- countLabel={countLabel}
- filterParameter={filterParameter}
- expanded={expanded}
- close={close}
- />
- </div>
- ) : null}
- </div>
- );
-}
diff --git a/client/src/app/[site]/components/shared/StandardSection/BaseStandardSectionDialog.tsx b/client/src/app/[site]/components/shared/StandardSection/BaseStandardSectionDialog.tsx
index b486736f5..6f7994789 100644
--- a/client/src/app/[site]/components/shared/StandardSection/BaseStandardSectionDialog.tsx
+++ b/client/src/app/[site]/components/shared/StandardSection/BaseStandardSectionDialog.tsx
@@ -2,7 +2,7 @@ import {
Dialog,
DialogContent,
DialogHeader,
- DialogTitle
+ DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import {
@@ -13,21 +13,22 @@ import {
SortingState,
useReactTable,
} from "@tanstack/react-table";
-import { useDebounce } from "@uidotdev/usehooks";
+import { useDebounce, useIntersectionObserver } from "@uidotdev/usehooks";
import {
ChevronDown,
ChevronUp,
+ Loader2,
Search,
- SquareArrowOutUpRight
+ SquareArrowOutUpRight,
} from "lucide-react";
-import { ReactNode, useMemo, useState } from "react";
+import { ReactNode, useEffect, useMemo, useState } from "react";
+import { useInfiniteSingleCol } from "../../../../../api/analytics/useInfiniteSingleCol";
import { SingleColResponse } from "../../../../../api/analytics/useSingleCol";
import { addFilter, FilterParameter } from "../../../../../lib/store";
import { cn, formatSecondsAsMinutesAndSeconds } from "../../../../../lib/utils";
interface BaseStandardSectionDialogProps {
title: string;
- data?: SingleColResponse[];
ratio: number;
getKey: (item: SingleColResponse) => string;
getLabel: (item: SingleColResponse) => ReactNode;
@@ -44,7 +45,6 @@ const columnHelper = createColumnHelper<SingleColResponse>();
export function BaseStandardSectionDialog({
title,
- data,
ratio,
getKey,
getLabel,
@@ -56,31 +56,56 @@ export function BaseStandardSectionDialog({
expanded,
close,
}: BaseStandardSectionDialogProps) {
+ const {
+ data,
+ isLoading,
+ isFetching,
+ error,
+ refetch,
+ fetchNextPage,
+ hasNextPage,
+ isFetchingNextPage,
+ } = useInfiniteSingleCol({
+ parameter: filterParameter,
+ limit: 100,
+ });
+
const [searchTerm, setSearchTerm] = useState("");
const debouncedSearchTerm = useDebounce(searchTerm, 200);
const [sorting, setSorting] = useState<SortingState>([
{ id: "count", desc: true },
]);
- if (!data || data.length === 0) return null;
+ // Use the intersection observer hook
+ const [ref, entry] = useIntersectionObserver({
+ threshold: 0,
+ root: null,
+ rootMargin: "0px 0px 200px 0px", // Load more when user is 200px from the bottom
+ });
- const labelFnToUse = getFilterLabel || getValue;
+ // Flatten the pages data
+ const allItems = useMemo(() => {
+ if (!data) return [];
+ return data.pages.flatMap((page) => page.data);
+ }, [data]);
// Filter data based on search term
- const filteredData = useMemo(
- () =>
- data.filter((item) => {
- const label =
- typeof labelFnToUse(item) === "string"
- ? (labelFnToUse(item) as string)
- : labelFnToUse(item);
+ const filteredData = useMemo(() => {
+ if (!allItems) return [];
- return String(label)
- .toLowerCase()
- .includes(debouncedSearchTerm.toLowerCase());
- }),
- [data, labelFnToUse, debouncedSearchTerm]
- );
+ const labelFnToUse = getFilterLabel || getValue;
+
+ return allItems.filter((item: SingleColResponse) => {
+ const label =
+ typeof labelFnToUse(item) === "string"
+ ? (labelFnToUse(item) as string)
+ : labelFnToUse(item);
+
+ return String(label)
+ .toLowerCase()
+ .includes(debouncedSearchTerm.toLowerCase());
+ });
+ }, [allItems, getFilterLabel, getValue, debouncedSearchTerm]);
const columns = useMemo(() => {
const cols = [
@@ -122,49 +147,71 @@ export function BaseStandardSectionDialog({
}),
];
- const hasPageviews =
- data?.[0]?.pageviews !== undefined &&
- data?.[0]?.pageviews_percentage !== undefined;
- if (hasPageviews) {
- cols.push(
- columnHelper.accessor("pageviews", {
- header: "Pageviews",
- cell: (info) => (
- <div className="flex flex-row gap-1 items-center sm:justify-end">
- {info.getValue()?.toLocaleString()}
- </div>
- ),
- }) as any
- );
- cols.push(
- columnHelper.accessor("pageviews_percentage", {
- header: "Pageviews %",
- cell: (info) => (
- <div className="flex flex-row gap-1 items-center sm:justify-end">
- {info.getValue()?.toFixed(1)}%
- </div>
- ),
- }) as any
- );
- }
+ // Only check for additional columns if we have data
+ if (filteredData.length > 0) {
+ const hasPageviews =
+ filteredData[0]?.pageviews !== undefined &&
+ filteredData[0]?.pageviews_percentage !== undefined;
+ if (hasPageviews) {
+ cols.push(
+ columnHelper.accessor("pageviews", {
+ header: "Pageviews",
+ cell: (info) => (
+ <div className="flex flex-row gap-1 items-center sm:justify-end">
+ {info.getValue()?.toLocaleString()}
+ </div>
+ ),
+ }) as any
+ );
+ cols.push(
+ columnHelper.accessor("pageviews_percentage", {
+ header: "Pageviews %",
+ cell: (info) => (
+ <div className="flex flex-row gap-1 items-center sm:justify-end">
+ {info.getValue()?.toFixed(1)}%
+ </div>
+ ),
+ }) as any
+ );
+ }
- const hasDuration = data?.[0]?.time_on_page_seconds !== undefined;
- if (hasDuration) {
- cols.push(
- columnHelper.accessor("time_on_page_seconds", {
- header: "Duration",
- cell: (info) => (
- <div className="text-right">
- {formatSecondsAsMinutesAndSeconds(
- Math.round(info.getValue() ?? 0)
- )}
- </div>
- ),
- }) as any
- );
+ const hasDuration = filteredData[0]?.time_on_page_seconds !== undefined;
+ if (hasDuration) {
+ cols.push(
+ columnHelper.accessor("time_on_page_seconds", {
+ header: "Duration",
+ cell: (info) => (
+ <div className="text-right">
+ {formatSecondsAsMinutesAndSeconds(
+ Math.round(info.getValue() ?? 0)
+ )}
+ </div>
+ ),
+ }) as any
+ );
+ }
}
+
return cols;
- }, []);
+ }, [filteredData, title, getLabel, getLink]);
+
+ // Fetch next page when intersection observer detects the target is visible
+ useEffect(() => {
+ if (
+ entry?.isIntersecting &&
+ hasNextPage &&
+ !isFetchingNextPage &&
+ !isLoading
+ ) {
+ fetchNextPage();
+ }
+ }, [
+ entry?.isIntersecting,
+ fetchNextPage,
+ hasNextPage,
+ isFetchingNextPage,
+ isLoading,
+ ]);
// Set up table instance
const table = useReactTable({
@@ -180,28 +227,36 @@ export function BaseStandardSectionDialog({
sortDescFirst: true,
});
+ if (isLoading || !data) {
+ return (
+ <Dialog open={expanded} onOpenChange={close}>
+ <DialogContent className="max-w-[1000px] w-[calc(100vw-2rem)] p-2 sm:p-4">
+ <div className="flex justify-center items-center h-40">
+ <Loader2 className="h-8 w-8 animate-spin text-neutral-400" />
+ </div>
+ </DialogContent>
+ </Dialog>
+ );
+ }
+ const totalCount = data.pages[0]?.totalCount || 0;
+
return (
<Dialog open={expanded} onOpenChange={close}>
- {/* <DialogTrigger asChild>
- <Button variant="default" size="sm">
- <Expand className="w-4 h-4" /> Expand
- </Button>
- </DialogTrigger> */}
<DialogContent className="max-w-[1000px] w-[calc(100vw-2rem)] p-2 sm:p-4">
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
</DialogHeader>
+ <div className="relative">
+ <Search className="absolute left-2.5 top-2.5 h-4 w-4 text-neutral-400" />
+ <Input
+ type="text"
+ placeholder={`Filter ${allItems.length} items...`}
+ className="pl-9 bg-neutral-900 border-neutral-700 text-xs"
+ value={searchTerm}
+ onChange={(e) => setSearchTerm(e.target.value)}
+ />
+ </div>
<div className="flex flex-col gap-2 overflow-x-auto">
- <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 text-xs"
- value={searchTerm}
- onChange={(e) => setSearchTerm(e.target.value)}
- />
- </div>
<div className="max-h-[80vh] overflow-y-auto">
<table className="w-full text-xs text-left min-w-max">
<thead className="bg-neutral-900 text-neutral-400 sticky top-0 z-10">
@@ -211,14 +266,20 @@ export function BaseStandardSectionDialog({
<th
key={header.id}
scope="col"
- className={cn("px-2 py-1 font-medium whitespace-nowrap cursor-pointer select-none", index === 0 ? "text-left" : "text-right")}
+ className={cn(
+ "px-2 py-1 font-medium whitespace-nowrap cursor-pointer select-none",
+ index === 0 ? "text-left" : "text-right"
+ )}
style={{
minWidth: header.id === "user_id" ? "100px" : "auto",
}}
onClick={header.column.getToggleSortingHandler()}
>
<div
- className={cn("flex items-center gap-1", index !== 0 && "justify-end")}
+ className={cn(
+ "flex items-center gap-1",
+ index !== 0 && "justify-end"
+ )}
>
{header.isPlaceholder
? null
@@ -241,7 +302,10 @@ export function BaseStandardSectionDialog({
return (
<tr
key={row.id}
- className={cn("border-b border-neutral-800 hover:bg-neutral-850 cursor-pointer group", rowIndex % 2 === 0 ? "bg-neutral-900" : "bg-neutral-950")}
+ className={cn(
+ "border-b border-neutral-800 hover:bg-neutral-850 cursor-pointer group",
+ rowIndex % 2 === 0 ? "bg-neutral-900" : "bg-neutral-950"
+ )}
onClick={() =>
addFilter({
parameter: filterParameter,
@@ -253,17 +317,12 @@ export function BaseStandardSectionDialog({
{row.getVisibleCells().map((cell, cellIndex) => (
<td
key={cell.id}
- className={cn("px-2 py-2 relative", cellIndex !== 0 && "text-right")}
- >
- {cellIndex === 0 && (
- <div
- // className="absolute h-0.5 bottom-0 bg-dataviz opacity-90"
- // className="absolute inset-0 bg-dataviz py-2 opacity-30 rounded-md h-6 mt-1"
- // style={{
- // width: `${row.original.percentage * ratio}%`,
- // }}
- ></div>
+ className={cn(
+ "px-2 py-2 relative",
+ cellIndex !== 0 && "text-right"
)}
+ >
+ {cellIndex === 0 && <div></div>}
<span className="relative z-0">
{flexRender(
cell.column.columnDef.cell,
@@ -277,6 +336,23 @@ export function BaseStandardSectionDialog({
})}
</tbody>
</table>
+
+ {/* Infinite scroll loading indicator and observer anchor */}
+ {filteredData.length > 0 && (
+ <div ref={ref} className="py-4 flex justify-center">
+ {isFetchingNextPage && (
+ <div className="flex items-center gap-2 text-neutral-400 text-xs">
+ <Loader2 className="h-4 w-4 animate-spin" />
+ Loading more...
+ </div>
+ )}
+ {!hasNextPage && !isFetchingNextPage && (
+ <div className="text-neutral-500 text-xs">
+ All items loaded
+ </div>
+ )}
+ </div>
+ )}
</div>
</div>
</DialogContent>
diff --git a/client/src/app/[site]/components/shared/StandardSection/Row.tsx b/client/src/app/[site]/components/shared/StandardSection/Row.tsx
new file mode 100644
index 000000000..48c133855
--- /dev/null
+++ b/client/src/app/[site]/components/shared/StandardSection/Row.tsx
@@ -0,0 +1,86 @@
+import NumberFlow from "@number-flow/react";
+import { round } from "lodash";
+import { SquareArrowOutUpRight } from "lucide-react";
+import { ReactNode } from "react";
+import { SingleColResponse } from "../../../../../api/analytics/useSingleCol";
+import {
+ addFilter,
+ FilterParameter,
+ removeFilter,
+ useStore,
+} from "../../../../../lib/store";
+
+export const Row = ({
+ e,
+ ratio,
+ getKey,
+ getLabel,
+ getValue,
+ getLink,
+ filterParameter,
+}: {
+ e: SingleColResponse;
+ ratio: number;
+ getKey: (item: SingleColResponse) => string;
+ getLabel: (item: SingleColResponse) => ReactNode;
+ getValue: (item: SingleColResponse) => string;
+ getLink?: (item: SingleColResponse) => string;
+ filterParameter: FilterParameter;
+}) => {
+ const filters = useStore((state) => state.filters);
+
+ return (
+ <div
+ key={getKey(e)}
+ className="relative h-6 flex items-center cursor-pointer hover:bg-neutral-850 group"
+ onClick={() => {
+ const foundFilter = filters.find(
+ (f) =>
+ f.parameter === filterParameter &&
+ f.value.some((v) => v === getValue(e))
+ );
+ if (foundFilter) {
+ removeFilter(foundFilter);
+ } else {
+ addFilter({
+ parameter: filterParameter,
+ value: [getValue(e)],
+ type: "equals",
+ });
+ }
+ }}
+ >
+ <div
+ className="absolute inset-0 bg-dataviz py-2 opacity-25 rounded-md"
+ style={{ width: `${e.percentage * ratio}%` }}
+ ></div>
+ <div className="z-10 mx-2 flex justify-between items-center text-xs 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-xs flex gap-2">
+ <div className="hidden group-hover:block text-neutral-400">
+ {round(e.percentage, 1)}%
+ </div>
+ <NumberFlow
+ respectMotionPreference={false}
+ value={e.count}
+ format={{ notation: "compact" }}
+ />
+ </div>
+ </div>
+ </div>
+ );
+};
diff --git a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx
index 211b83b29..f5a2abc4a 100644
--- a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx
+++ b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx
@@ -1,13 +1,15 @@
"use client";
-import { ReactNode, useMemo } from "react";
-import {
- SingleColResponse,
- useSingleCol,
-} from "../../../../../api/analytics/useSingleCol";
-import { FilterParameter } from "../../../../../lib/store";
-import { BaseStandardSection } from "./BaseStandardSection";
+import { Button } from "@/components/ui/button";
+import { AlertCircle, Info, RefreshCcw } from "lucide-react";
+import { ReactNode } from "react";
+import { usePaginatedSingleCol } from "../../../../../api/analytics/usePaginatedSingleCol";
+import { SingleColResponse } from "../../../../../api/analytics/useSingleCol";
import { CardLoader } from "../../../../../components/ui/card";
+import { FilterParameter } from "../../../../../lib/store";
+import { BaseStandardSectionDialog } from "./BaseStandardSectionDialog";
+import { Row } from "./Row";
+import { Skeleton } from "./Skeleton";
export function StandardSection({
title,
@@ -32,30 +34,19 @@ export function StandardSection({
expanded: boolean;
close: () => void;
}) {
- const { data, isLoading, isFetching, error, refetch } = useSingleCol({
- parameter: filterParameter,
- });
-
- // const {
- // data: previousData,
- // isLoading: previousIsLoading,
- // isFetching: previousIsFetching,
- // } = useSingleCol({
- // parameter: filterParameter,
- // periodTime: "previous",
- // });
+ const { data, isLoading, isFetching, error, refetch } = usePaginatedSingleCol(
+ {
+ parameter: filterParameter,
+ limit: 100,
+ page: 1,
+ }
+ );
- // Create combined loading state
- const loading = isLoading;
- // const loading = isLoading || previousIsLoading;
+ const itemsForDisplay = data?.data;
- // For potential additional features that use previous data
- // const previousDataMap = useMemo(() => {
- // return previousData?.data?.reduce((acc, curr) => {
- // acc[getKey(curr)] = curr;
- // return acc;
- // }, {} as Record<string, SingleColResponse>);
- // }, [previousData, getKey]);
+ const ratio = itemsForDisplay?.[0]?.percentage
+ ? 100 / itemsForDisplay[0].percentage
+ : 1;
return (
<>
@@ -64,22 +55,76 @@ export function StandardSection({
<CardLoader />
</div>
)}
- <BaseStandardSection
- title={title}
- data={data}
- isLoading={loading}
- error={error}
- refetch={refetch}
- getKey={getKey}
- getLabel={getLabel}
- getValue={getValue}
- getFilterLabel={getFilterLabel}
- getLink={getLink}
- countLabel={countLabel}
- filterParameter={filterParameter}
- expanded={expanded}
- close={close}
- />
+ <div className="flex flex-col gap-2">
+ {isLoading ? (
+ <Skeleton />
+ ) : error ? (
+ <div className="py-6 flex-1 flex flex-col items-center justify-center gap-3 transition-all">
+ <AlertCircle className="text-amber-400 w-8 h-8" />
+ <div className="text-center">
+ <div className="text-neutral-100 font-medium mb-1">
+ Failed to load data
+ </div>
+ <div className="text-sm text-neutral-400 max-w-md mx-auto mb-3">
+ {error.message || "An error occurred while fetching data"}
+ </div>
+ </div>
+ <Button
+ variant="outline"
+ size="sm"
+ className="bg-transparent hover:bg-neutral-800 border-neutral-700 text-neutral-300 hover:text-neutral-100"
+ onClick={() => refetch()}
+ >
+ <RefreshCcw className="w-3 h-3" /> Try Again
+ </Button>
+ </div>
+ ) : (
+ <div className="flex flex-col gap-2">
+ <div className="flex flex-row gap-2 justify-between pr-1 text-xs text-neutral-400">
+ <div>{title}</div>
+ <div>{countLabel || "Sessions"}</div>
+ </div>
+ {itemsForDisplay?.length ? (
+ itemsForDisplay
+ .slice(0, 10)
+ .map((e) => (
+ <Row
+ key={getKey(e)}
+ e={e}
+ ratio={ratio}
+ getKey={getKey}
+ getLabel={getLabel}
+ getValue={getValue}
+ getLink={getLink}
+ filterParameter={filterParameter}
+ />
+ ))
+ ) : (
+ <div className="text-neutral-300 w-full text-center mt-6 flex flex-row gap-2 items-center justify-center">
+ <Info className="w-5 h-5" />
+ No Data
+ </div>
+ )}
+ </div>
+ )}
+ {!isLoading && !error && itemsForDisplay?.length ? (
+ <div className="flex flex-row gap-2 justify-between items-center">
+ <BaseStandardSectionDialog
+ title={title}
+ ratio={ratio}
+ getKey={getKey}
+ getLabel={getLabel}
+ getValue={getValue}
+ getFilterLabel={getFilterLabel}
+ getLink={getLink}
+ countLabel={countLabel}
+ filterParameter={filterParameter}
+ expanded={expanded}
+ close={close}
+ />
+ </div>
+ ) : null}
+ </div>
</>
);
}
diff --git a/client/src/app/[site]/pages/page.tsx b/client/src/app/[site]/pages/page.tsx
index 6e4f30093..7edffab5a 100644
--- a/client/src/app/[site]/pages/page.tsx
+++ b/client/src/app/[site]/pages/page.tsx
@@ -31,8 +31,8 @@ export default function Pages() {
return null;
}
- // Calculate offset based on current page
- const offset = pagination.pageIndex * pagination.pageSize;
+ // Get page number (1-based) from pageIndex (0-based)
+ const pageNumber = pagination.pageIndex + 1;
const {
data: apiResponse,
@@ -43,7 +43,7 @@ export default function Pages() {
isFetching,
} = useGetPageTitlesPaginated({
limit: pagination.pageSize,
- offset: offset,
+ page: pageNumber,
});
const pagesDataArray: PageTitleItem[] | undefined = apiResponse?.data?.data;
@@ -80,7 +80,7 @@ export default function Pages() {
} else if (
pagesDataArray &&
pagesDataArray.length === 0 &&
- offset === 0
+ pagination.pageIndex === 0
) {
// Fallback if totalCount is somehow undefined but we have an empty array on page 1
setTotalPages(0);
@@ -108,7 +108,6 @@ export default function Pages() {
isFetching,
pagination.pageIndex,
pagination.pageSize,
- offset,
]);
return (
diff --git a/client/src/app/[site]/realtime/page.tsx b/client/src/app/[site]/realtime/page.tsx
index fac5448b8..396e59169 100644
--- a/client/src/app/[site]/realtime/page.tsx
+++ b/client/src/app/[site]/realtime/page.tsx
@@ -1,26 +1,22 @@
"use client";
-import { useMeasure, useWindowSize } from "@uidotdev/usehooks";
-import { Countries } from "../main/components/sections/Countries";
-import { Events } from "../main/components/sections/Events";
-import { Pages } from "../main/components/sections/Pages";
-import { Referrers } from "../main/components/sections/Referrers";
-import { RealtimeChart } from "./RealtimeChart/RealtimeChart";
-import { World } from "./RealtimeGlobe/RealtimeGlobe";
-import { RealtimeEvents } from "./RealtimeEvents/RealtimeEvents";
+import NumberFlow from "@number-flow/react";
+import { useMeasure } from "@uidotdev/usehooks";
+import { useAtom } from "jotai";
import { useGetLiveUsercount } from "../../../api/analytics/useLiveUserCount";
+import { MobileSidebar } from "../../../components/MobileSidebar";
import {
Select,
- SelectTrigger,
- SelectItem,
SelectContent,
+ SelectItem,
+ SelectTrigger,
SelectValue,
} from "../../../components/ui/select";
-import { useAtom } from "jotai";
-import { minutesAtom, MinutesType } from "./realtimeStore";
-import NumberFlow from "@number-flow/react";
-import { MobileSidebar } from "../../../components/MobileSidebar";
import { useSetPageTitle } from "../../../hooks/useSetPageTitle";
+import { RealtimeChart } from "./RealtimeChart/RealtimeChart";
+import { RealtimeEvents } from "./RealtimeEvents/RealtimeEvents";
+import { World } from "./RealtimeGlobe/RealtimeGlobe";
+import { minutesAtom, MinutesType } from "./realtimeStore";
export default function RealtimePage() {
useSetPageTitle("Rybbit · Realtime");
@@ -31,10 +27,7 @@ export default function RealtimePage() {
const { data } = useGetLiveUsercount(Number(minutes));
return (
- <div
- className="relative overflow-hidden"
- ref={ref}
- >
+ <div className="relative overflow-hidden" ref={ref}>
<World width={width ?? 0} />
<div className="absolute top-2 left-2 md:top-4 md:left-4 flex flex-row gap-2 items-start">
diff --git a/server/src/api/analytics/getPageTitles.ts b/server/src/api/analytics/getPageTitles.ts
index 074c55d90..e2347f6cb 100644
--- a/server/src/api/analytics/getPageTitles.ts
+++ b/server/src/api/analytics/getPageTitles.ts
@@ -18,15 +18,15 @@ interface GetPageTitlesRequest {
timeZone: string;
filters: string;
limit?: number;
- offset?: number;
+ page?: number;
};
}
-// Represents a single item in the response
-type PageTitleItem = {
- value: string; // The page_title itself
- pathname: string; // A representative pathname for this title
- count: number; // Unique sessions or pageviews for this title
+// This type represents a single item in the array
+export type PageTitleItem = {
+ value: string;
+ pathname: string;
+ count: number;
percentage: number;
};
@@ -40,7 +40,7 @@ const getPageTitlesQuery = (
request: FastifyRequest<GetPageTitlesRequest>,
isCountQuery: boolean = false
) => {
- const { startDate, endDate, timeZone, filters, limit, offset, minutes } =
+ const { startDate, endDate, timeZone, filters, limit, page, minutes } =
request.query;
const filterStatement = getFilterStatement(filters);
@@ -68,10 +68,11 @@ const getPageTitlesQuery = (
: "LIMIT 10";
let validatedOffset: number | null = null;
- if (!isCountQuery && offset !== undefined) {
- const parsedOffset = parseInt(String(offset), 10);
- if (!isNaN(parsedOffset) && parsedOffset >= 0) {
- validatedOffset = parsedOffset;
+ if (!isCountQuery && page !== undefined) {
+ const parsedPage = parseInt(String(page), 10);
+ if (!isNaN(parsedPage) && parsedPage >= 1) {
+ const pageOffset = (parsedPage - 1) * (validatedLimit || 10);
+ validatedOffset = pageOffset;
}
}
const offsetStatement =
@@ -97,18 +98,11 @@ const getPageTitlesQuery = (
`;
if (isCountQuery) {
- return `
- SELECT COUNT(*) as totalCount FROM (
- ${coreLogic}
- );
- `;
+ return `SELECT COUNT(*) as totalCount FROM (${coreLogic})`;
}
- // For the data query, we calculate percentages after getting unique_sessions per title
return `
- WITH TitleStats AS (
- ${coreLogic}
- )
+ WITH PageTitleStats AS (${coreLogic})
SELECT
value,
pathname,
@@ -117,10 +111,10 @@ const getPageTitlesQuery = (
unique_sessions * 100.0 / SUM(unique_sessions) OVER (),
2
) as percentage
- FROM TitleStats
+ FROM PageTitleStats
ORDER BY count DESC
${limitStatement}
- ${offsetStatement};
+ ${offsetStatement}
`;
};
@@ -129,14 +123,14 @@ export async function getPageTitles(
res: FastifyReply
) {
const site = req.params.site;
- const { offset } = req.query;
+ const { page } = req.query;
const userHasAccessToSite = await getUserHasAccessToSitePublic(req, site);
if (!userHasAccessToSite) {
return res.status(403).send({ error: "Forbidden" });
}
- const isPaginatedRequest = offset !== undefined; // True if offset is present
+ const isPaginatedRequest = page !== undefined; // True if page is present
const dataQuery = getPageTitlesQuery(req, false);
diff --git a/server/src/api/analytics/getSingleCol.ts b/server/src/api/analytics/getSingleCol.ts
index 32f5744a5..5df1d16ba 100644
--- a/server/src/api/analytics/getSingleCol.ts
+++ b/server/src/api/analytics/getSingleCol.ts
@@ -9,7 +9,7 @@ import {
processResults,
} from "./utils.js";
-interface GenericRequest {
+interface GetSingleColRequest {
Params: {
site: string;
};
@@ -21,7 +21,7 @@ interface GenericRequest {
filters: string;
parameter: FilterParameter;
limit?: number;
- offset?: number;
+ page?: number;
};
}
@@ -57,7 +57,7 @@ type GetSingleColPaginatedResponse = {
};
const getQuery = (
- request: FastifyRequest<GenericRequest>,
+ request: FastifyRequest<GetSingleColRequest>,
isCountQuery: boolean = false
) => {
const {
@@ -67,7 +67,7 @@ const getQuery = (
filters,
parameter,
limit,
- offset,
+ page,
minutes,
} = request.query;
@@ -95,10 +95,11 @@ const getQuery = (
: "LIMIT 100";
let validatedOffset: number | null = null;
- if (!isCountQuery && offset !== undefined) {
- const parsedOffset = parseInt(String(offset), 10);
- if (!isNaN(parsedOffset) && parsedOffset >= 0) {
- validatedOffset = parsedOffset;
+ if (!isCountQuery && page !== undefined) {
+ const parsedPage = parseInt(String(page), 10);
+ if (!isNaN(parsedPage) && parsedPage >= 1) {
+ const pageOffset = (parsedPage - 1) * (validatedLimit || 100);
+ validatedOffset = pageOffset;
}
}
const offsetStatement =
@@ -350,10 +351,10 @@ const getQuery = (
};
export async function getSingleCol(
- req: FastifyRequest<GenericRequest>,
+ req: FastifyRequest<GetSingleColRequest>,
res: FastifyReply
) {
- const { parameter, offset, limit } = req.query;
+ const { parameter, page } = req.query;
const site = req.params.site;
const userHasAccessToSite = await getUserHasAccessToSitePublic(req, site);
@@ -361,7 +362,7 @@ export async function getSingleCol(
return res.status(403).send({ error: "Forbidden" });
}
- const isPaginatedRequest = offset !== undefined;
+ const isPaginatedRequest = page !== undefined;
const dataQuery = getQuery(req, false);
@@ -375,24 +376,18 @@ export async function getSingleCol(
});
const items = await processResults<SingleColItem>(dataResult);
- if (isPaginatedRequest) {
- const countQuery = getQuery(req, true);
- const countResult = await clickhouse.query({
- query: countQuery,
- format: "JSONEachRow",
- query_params: {
- siteId: Number(site),
- },
- });
- const countData = await processResults<{ totalCount: number }>(
- countResult
- );
- const totalCount = countData.length > 0 ? countData[0].totalCount : 0;
+ const countQuery = getQuery(req, true);
+ const countResult = await clickhouse.query({
+ query: countQuery,
+ format: "JSONEachRow",
+ query_params: {
+ siteId: Number(site),
+ },
+ });
+ const countData = await processResults<{ totalCount: number }>(countResult);
+ const totalCount = countData.length > 0 ? countData[0].totalCount : 0;
- return res.send({ data: { data: items, totalCount } });
- } else {
- return res.send({ data: items });
- }
+ return res.send({ data: { data: items, totalCount } });
} catch (error) {
console.error(`Error fetching ${parameter}:`, error);
console.error("Failed dataQuery:", dataQuery);