Skip to content

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

Refactor Admin Service Event Count and enhance Organizations component

Bill Yang562e442ff83f
diff --git a/client/src/api/admin/useGetAdminServiceEventCount.ts b/client/src/api/admin/useGetAdminServiceEventCount.ts
index 649ef7f85..403467a6d 100644
--- a/client/src/api/admin/useGetAdminServiceEventCount.ts
+++ b/client/src/api/admin/useGetAdminServiceEventCount.ts
@@ -16,11 +16,11 @@ export type GetServiceEventCountResponse = {
async function getAdminServiceEventCount({
startDate,
endDate,
- timeZone = "UTC",
+ timeZone,
}: {
startDate?: string;
endDate?: string;
- timeZone?: string;
+ timeZone: string;
}): Promise<GetServiceEventCountResponse> {
const params = new URLSearchParams();
if (startDate) params.append("startDate", startDate);
@@ -33,12 +33,12 @@ async function getAdminServiceEventCount({
export function useGetAdminServiceEventCount({
startDate,
endDate,
- timeZone = "UTC",
+ timeZone,
enabled = true,
}: {
startDate?: string;
endDate?: string;
- timeZone?: string;
+ timeZone: string;
enabled?: boolean;
}) {
return useQuery({
diff --git a/client/src/app/admin/components/organizations/Organizations.tsx b/client/src/app/admin/components/organizations/Organizations.tsx
index c26623cb5..599441e70 100644
--- a/client/src/app/admin/components/organizations/Organizations.tsx
+++ b/client/src/app/admin/components/organizations/Organizations.tsx
@@ -41,6 +41,8 @@ import { GrowthChart } from "../shared/GrowthChart";
import { OverviewCards } from "../shared/OverviewCards";
import { ServiceUsageChart } from "../shared/ServiceUsageChart";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
+import { Switch } from "@/components/ui/switch";
+import { Label } from "@/components/ui/label";
export function Organizations() {
const router = useRouter();
@@ -54,6 +56,11 @@ export function Organizations() {
pageSize: 50,
});
+ // Filter states
+ const [showZeroEvents, setShowZeroEvents] = useState(true);
+ const [showFreeUsers, setShowFreeUsers] = useState(true);
+ const [showOnlyOverLimit, setShowOnlyOverLimit] = useState(false);
+
// Calculate stats from organizations data
const stats = useMemo(() => {
if (!organizations) {
@@ -100,25 +107,45 @@ export function Organizations() {
[expandedOrgs]
);
- // Filter organizations based on search query
+ // Filter organizations based on search query and filter toggles
const filteredOrganizations = useMemo(() => {
if (!organizations) return [];
- if (!searchQuery.trim()) return organizations;
-
- const lowerSearchQuery = searchQuery.toLowerCase();
- return organizations.filter(org => {
- return (
- org.name.toLowerCase().includes(lowerSearchQuery) ||
- org.sites.some(site => site.domain.toLowerCase().includes(lowerSearchQuery)) ||
- org.members.some(
- member =>
- member.email.toLowerCase().includes(lowerSearchQuery) ||
- member.name.toLowerCase().includes(lowerSearchQuery)
- )
- );
- });
- }, [organizations, searchQuery]);
+ let filtered = organizations;
+
+ // Apply search filter
+ if (searchQuery.trim()) {
+ const lowerSearchQuery = searchQuery.toLowerCase();
+ filtered = filtered.filter(org => {
+ return (
+ org.name.toLowerCase().includes(lowerSearchQuery) ||
+ org.sites.some(site => site.domain.toLowerCase().includes(lowerSearchQuery)) ||
+ org.members.some(
+ member =>
+ member.email.toLowerCase().includes(lowerSearchQuery) ||
+ member.name.toLowerCase().includes(lowerSearchQuery)
+ )
+ );
+ });
+ }
+
+ // Filter out organizations with 0 events in last 30 days
+ if (!showZeroEvents) {
+ filtered = filtered.filter(org => org.sites.some(site => site.eventsLast30Days > 0));
+ }
+
+ // Filter out free users
+ if (!showFreeUsers) {
+ filtered = filtered.filter(org => org.subscription.planName !== "free");
+ }
+
+ // Show only organizations over their event limit
+ if (showOnlyOverLimit) {
+ filtered = filtered.filter(org => org.overMonthlyLimit);
+ }
+
+ return filtered;
+ }, [organizations, searchQuery, showZeroEvents, showFreeUsers, showOnlyOverLimit]);
// Impersonation handler
const handleImpersonate = useCallback(
@@ -336,6 +363,27 @@ export function Organizations() {
/>
</div>
+ <div className="flex items-center gap-6 mb-4 p-4 bg-neutral-900 border border-neutral-700 rounded-lg">
+ <div className="flex items-center gap-2">
+ <Switch id="show-zero-events" checked={showZeroEvents} onCheckedChange={setShowZeroEvents} />
+ <Label htmlFor="show-zero-events" className="text-sm cursor-pointer">
+ Show orgs with 0 events (30d)
+ </Label>
+ </div>
+ <div className="flex items-center gap-2">
+ <Switch id="show-free-users" checked={showFreeUsers} onCheckedChange={setShowFreeUsers} />
+ <Label htmlFor="show-free-users" className="text-sm cursor-pointer">
+ Show free users
+ </Label>
+ </div>
+ <div className="flex items-center gap-2">
+ <Switch id="show-only-over-limit" checked={showOnlyOverLimit} onCheckedChange={setShowOnlyOverLimit} />
+ <Label htmlFor="show-only-over-limit" className="text-sm cursor-pointer">
+ Only over limit
+ </Label>
+ </div>
+ </div>
+
<div className="rounded-md border border-neutral-700">
<Table>
<TableHeader>
diff --git a/client/src/app/admin/components/shared/GrowthChart.tsx b/client/src/app/admin/components/shared/GrowthChart.tsx
index 8b600f018..b85ba268f 100644
--- a/client/src/app/admin/components/shared/GrowthChart.tsx
+++ b/client/src/app/admin/components/shared/GrowthChart.tsx
@@ -2,7 +2,11 @@
import { useMemo } from "react";
import { ResponsiveLine } from "@nivo/line";
-import { parseUtcTimestamp } from "@/lib/dateTimeUtils";
+import { parseUtcTimestamp, userLocale } from "@/lib/dateTimeUtils";
+import { nivoTheme } from "@/lib/nivo";
+import { formatter } from "@/lib/utils";
+import { DateTime } from "luxon";
+import { useWindowSize } from "@uidotdev/usehooks";
interface GrowthChartProps {
data?: Array<{ createdAt: string }>;
@@ -11,6 +15,9 @@ interface GrowthChartProps {
}
export function GrowthChart({ data, color = "#3b82f6", title }: GrowthChartProps) {
+ const { width } = useWindowSize();
+ const maxTicks = Math.round((width ?? Infinity) / 200);
+
const chartData = useMemo(() => {
if (!data || data.length === 0) return [];
@@ -39,126 +46,80 @@ export function GrowthChart({ data, color = "#3b82f6", title }: GrowthChartProps
}, [data]);
if (data === undefined) {
- return <div className="h-48 sm:h-64 flex items-center justify-center text-neutral-400 text-sm">Loading...</div>;
+ return <div className="h-64 flex items-center justify-center text-neutral-400 text-sm">Loading...</div>;
}
if (!data || data.length === 0) {
- return (
- <div className="h-48 sm:h-64 flex items-center justify-center text-neutral-400 text-sm">No data available</div>
- );
+ return <div className="h-64 flex items-center justify-center text-neutral-400 text-sm">No data available</div>;
}
return (
- <div className="h-48 sm:h-64">
+ <div className="h-64">
<ResponsiveLine
data={chartData}
- margin={{
- top: 10,
- right: 10,
- bottom: 40,
- left: 25,
- }}
+ theme={nivoTheme}
+ margin={{ top: 10, right: 10, bottom: 25, left: 50 }}
xScale={{
type: "time",
format: "%Y-%m-%d",
precision: "day",
+ useUTC: true,
}}
- xFormat="time:%Y-%m-%d"
yScale={{
type: "linear",
min: 0,
- max: "auto",
stacked: false,
+ reverse: false,
}}
- enableSlices="x"
- curve="linear"
+ enableGridX={false}
+ enableGridY={true}
+ gridYValues={5}
+ yFormat=" >-.0f"
axisTop={null}
axisRight={null}
axisBottom={{
- format: "%m/%d",
- tickValues: "every 14 days",
- tickRotation: -45,
- tickSize: 5,
- tickPadding: 5,
+ tickSize: 0,
+ tickPadding: 10,
+ tickRotation: 0,
+ truncateTickAt: 0,
+ tickValues: Math.min(maxTicks, 10),
+ format: value => {
+ const dt = DateTime.fromJSDate(value).setLocale(userLocale);
+ return dt.toFormat("MMM d");
+ },
}}
axisLeft={{
- tickSize: 5,
- tickPadding: 5,
+ tickSize: 0,
+ tickPadding: 10,
tickRotation: 0,
+ truncateTickAt: 0,
+ tickValues: 5,
+ format: formatter,
}}
- colors={[color]}
- pointSize={4}
- pointColor={{ theme: "background" }}
- pointBorderWidth={2}
- pointBorderColor={{ from: "serieColor" }}
- pointLabelYOffset={-12}
+ enableTouchCrosshair={true}
+ enablePoints={false}
useMesh={true}
- enableGridX={false}
- enableGridY={true}
- gridYValues={5}
- theme={{
- background: "transparent",
- text: {
- fontSize: 11,
- fill: "#a3a3a3",
- },
- axis: {
- domain: {
- line: {
- stroke: "#525252",
- strokeWidth: 1,
- },
- },
- legend: {
- text: {
- fontSize: 11,
- fill: "#a3a3a3",
- },
- },
- ticks: {
- line: {
- stroke: "#525252",
- strokeWidth: 1,
- },
- text: {
- fontSize: 10,
- fill: "#a3a3a3",
- },
- },
- },
- grid: {
- line: {
- stroke: "#404040",
- strokeWidth: 1,
- },
- },
- crosshair: {
- line: {
- stroke: "#ffffff",
- strokeWidth: 1,
- strokeOpacity: 0.35,
- },
- },
- tooltip: {
- container: {
- background: "#262626",
- color: "#ffffff",
- fontSize: "11px",
- borderRadius: "4px",
- boxShadow: "0 4px 6px -1px rgba(0, 0, 0, 0.1)",
- border: "1px solid #404040",
- padding: "6px 8px",
- },
- },
- }}
- tooltip={({ point }) => (
- <div className="bg-neutral-800 border border-neutral-600 rounded px-2 py-1 text-xs sm:text-sm">
- <div className="font-medium text-white">{point.data.xFormatted}</div>
- <div className="text-neutral-300">
- {point.data.y} new {title.toLowerCase()}
+ animate={false}
+ enableSlices={"x"}
+ colors={[color]}
+ enableArea={false}
+ sliceTooltip={({ slice }: any) => {
+ const point = slice.points[0];
+ const currentTime = DateTime.fromSQL(point.data.x as string);
+
+ return (
+ <div className="text-sm bg-neutral-850 p-3 rounded-md min-w-[100px] border border-neutral-750">
+ <div className="font-medium mb-1">{currentTime.toLocaleString(DateTime.DATE_MED)}</div>
+ <div className="flex justify-between gap-4 text-sm">
+ <div className="flex items-center gap-2 text-neutral-300">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: point.seriesColor }} />
+ <span>New {title}</span>
+ </div>
+ <div>{formatter(Number(point.data.yFormatted))}</div>
+ </div>
</div>
- </div>
- )}
+ );
+ }}
/>
</div>
);
diff --git a/client/src/app/admin/components/shared/ServiceUsageChart.tsx b/client/src/app/admin/components/shared/ServiceUsageChart.tsx
index acb13f77a..0597c161f 100644
--- a/client/src/app/admin/components/shared/ServiceUsageChart.tsx
+++ b/client/src/app/admin/components/shared/ServiceUsageChart.tsx
@@ -5,17 +5,16 @@ import { ResponsiveLine } from "@nivo/line";
import { useWindowSize } from "@uidotdev/usehooks";
import { DateTime } from "luxon";
import { useGetAdminServiceEventCount } from "@/api/admin/useGetAdminServiceEventCount";
-import { userLocale } from "@/lib/dateTimeUtils";
+import { userLocale, timeZone } from "@/lib/dateTimeUtils";
import { formatter } from "@/lib/utils";
import { Badge } from "@/components/ui/badge";
interface ServiceUsageChartProps {
startDate?: string;
endDate?: string;
- timeZone?: string;
}
-export function ServiceUsageChart({ startDate, endDate, timeZone = "UTC" }: ServiceUsageChartProps) {
+export function ServiceUsageChart({ startDate, endDate }: ServiceUsageChartProps) {
const { width } = useWindowSize();
// Fetch the data inside the component