🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor theme handling in charts and components for improved consistency
Bill Yang7f7496c01c2e
diff --git a/client/src/app/[site]/errors/components/ErrorSparklineChart.tsx b/client/src/app/[site]/errors/components/ErrorSparklineChart.tsxindex add42d828..2f416327a 100644--- a/client/src/app/[site]/errors/components/ErrorSparklineChart.tsx+++ b/client/src/app/[site]/errors/components/ErrorSparklineChart.tsx@@ -2,11 +2,10 @@import { GetErrorBucketedResponse } from "@/api/analytics/errors/useGetErrorBucketed";import { hour12, userLocale } from "@/lib/dateTimeUtils";-import { getNivoTheme } from "@/lib/nivo";+import { useNivoTheme } from "@/lib/nivo";import { ResponsiveBar } from "@nivo/bar";import { DateTime } from "luxon";import { useMemo } from "react";-import { useTheme } from "next-themes";interface ErrorSparklineChartProps {data: GetErrorBucketedResponse | undefined;@@ -16,8 +15,7 @@ interface ErrorSparklineChartProps {}export function ErrorSparklineChart({ data, isHovering, errorMessage, isLoading }: ErrorSparklineChartProps) {- const { theme } = useTheme();- const nivoTheme = getNivoTheme(theme === "dark");+ const nivoTheme = useNivoTheme();const chartData = useMemo(() => {if (!data || data.length === 0) {diff --git a/client/src/app/[site]/main/components/MainSection/Chart.tsx b/client/src/app/[site]/main/components/MainSection/Chart.tsxindex c3671a795..7bfedf6c3 100644--- a/client/src/app/[site]/main/components/MainSection/Chart.tsx+++ b/client/src/app/[site]/main/components/MainSection/Chart.tsx@@ -1,8 +1,7 @@"use client";import { TimeBucket } from "@rybbit/shared";-import { getNivoTheme } from "@/lib/nivo";+import { useNivoTheme } from "@/lib/nivo";import { StatType, useStore } from "@/lib/store";-import { useTheme } from "next-themes";import { LineCustomSvgLayer, LineCustomSvgLayerProps, LineSeries, ResponsiveLine } from "@nivo/line";import { useWindowSize } from "@uidotdev/usehooks";import { DateTime } from "luxon";@@ -130,8 +129,7 @@ export function Chart({}) {const { time, bucket, selectedStat } = useStore();const { width } = useWindowSize();- const { theme } = useTheme();- const nivoTheme = getNivoTheme(theme === "dark");+ const nivoTheme = useNivoTheme();const maxTicks = Math.round((width ?? Infinity) / 75);diff --git a/client/src/app/[site]/main/components/MainSection/PreviousChart.tsx b/client/src/app/[site]/main/components/MainSection/PreviousChart.tsxindex 6d40e2736..6d37363bb 100644--- a/client/src/app/[site]/main/components/MainSection/PreviousChart.tsx+++ b/client/src/app/[site]/main/components/MainSection/PreviousChart.tsx@@ -1,5 +1,5 @@"use client";-import { getNivoTheme } from "@/lib/nivo";+import { useNivoTheme } from "@/lib/nivo";import { useStore } from "@/lib/store";import { useTheme } from "next-themes";import { ResponsiveLine } from "@nivo/line";@@ -44,8 +44,8 @@ export function PreviousChart({max: number;}) {const { previousTime: time, selectedStat, bucket } = useStore();- const { theme } = useTheme();- const nivoTheme = getNivoTheme(theme === "dark");+ const { resolvedTheme } = useTheme();+ const nivoTheme = useNivoTheme();const size = (data?.data.length ?? 0 / 2) + 1;const formattedData = data?.data@@ -128,7 +128,7 @@ export function PreviousChart({animate={false}// motionConfig="stiff"enableSlices={"x"}- colors={[theme === "dark" ? "hsl(var(--neutral-700))" : "hsl(var(--neutral-100))"]}+ colors={[resolvedTheme === "dark" ? "hsl(var(--neutral-700))" : "hsl(var(--neutral-100))"]}/>);}diff --git a/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsx b/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsxindex e8386f1a3..875c56b77 100644--- a/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsx+++ b/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsx@@ -1,8 +1,7 @@"use client";-import { getNivoTheme } from "@/lib/nivo";+import { useNivoTheme } from "@/lib/nivo";import { ResponsiveLine } from "@nivo/line";import { DateTime } from "luxon";-import { useTheme } from "next-themes";export function SparklinesChart({data,@@ -11,8 +10,7 @@ export function SparklinesChart({data: { value: number; time: string }[] | undefined;isHovering: boolean;}) {- const { theme } = useTheme();- const nivoTheme = getNivoTheme(theme === "dark");+ const nivoTheme = useNivoTheme();const formattedData = data?.map((e, i) => {diff --git a/client/src/app/[site]/performance/components/PerformanceChart.tsx b/client/src/app/[site]/performance/components/PerformanceChart.tsxindex f3a8011e1..9f25b32d7 100644--- a/client/src/app/[site]/performance/components/PerformanceChart.tsx+++ b/client/src/app/[site]/performance/components/PerformanceChart.tsx@@ -2,13 +2,12 @@import { Card, CardContent, CardLoader } from "@/components/ui/card";import { Skeleton } from "@/components/ui/skeleton";-import { getNivoTheme } from "@/lib/nivo";+import { useNivoTheme } from "@/lib/nivo";import { ResponsiveLine } from "@nivo/line";import { DateTime } from "luxon";import { Tilt_Warp } from "next/font/google";import Link from "next/link";import { useState } from "react";-import { useTheme } from "next-themes";import { useGetPerformanceTimeSeries } from "../../../../api/analytics/performance/useGetPerformanceTimeSeries";import { BucketSelection } from "../../../../components/BucketSelection";import { RybbitLogo } from "../../../../components/RybbitLogo";@@ -29,8 +28,7 @@ export function PerformanceChart() {const session = authClient.useSession();const { site, bucket } = useStore();const { selectedPerformanceMetric, selectedPercentile } = usePerformanceStore();- const { theme } = useTheme();- const nivoTheme = getNivoTheme(theme === "dark");+ const nivoTheme = useNivoTheme();// State for toggling percentile visibilityconst [visiblePercentiles, setVisiblePercentiles] = useState<Set<string>>(new Set(["P50", "P75", "P90", "P99"]));diff --git a/client/src/app/[site]/retention/RetentionChart.tsx b/client/src/app/[site]/retention/RetentionChart.tsxindex 3bf64534e..313804fef 100644--- a/client/src/app/[site]/retention/RetentionChart.tsx+++ b/client/src/app/[site]/retention/RetentionChart.tsx@@ -1,6 +1,6 @@"use client";-import { getNivoTheme } from "@/lib/nivo";+import { useNivoTheme } from "@/lib/nivo";import { ResponsiveLine } from "@nivo/line";import { DateTime } from "luxon";import { useMemo } from "react";@@ -54,8 +54,8 @@ const RetentionChartSkeleton = () => ();export function RetentionChart({ data, isLoading, mode }: RetentionChartProps) {- const { theme } = useTheme();- const nivoTheme = getNivoTheme(theme === "dark");+ const { resolvedTheme } = useTheme();+ const nivoTheme = useNivoTheme();// Get cohort keys once for both chart data and tooltipconst cohortKeys = useMemo(() => {@@ -184,7 +184,7 @@ export function RetentionChart({ data, isLoading, mode }: RetentionChartProps) {},},],- itemTextColor: theme === "dark" ? "hsl(var(--neutral-200))" : "hsl(var(--neutral-700))",+ itemTextColor: resolvedTheme === "dark" ? "hsl(var(--neutral-200))" : "hsl(var(--neutral-700))",},]}tooltip={({ point }) => {diff --git a/client/src/app/[site]/user/[userId]/components/Calendar.tsx b/client/src/app/[site]/user/[userId]/components/Calendar.tsxindex 9f6a87311..06c27a510 100644--- a/client/src/app/[site]/user/[userId]/components/Calendar.tsx+++ b/client/src/app/[site]/user/[userId]/components/Calendar.tsx@@ -3,11 +3,11 @@ import _ from "lodash";import { DateTime } from "luxon";import { useTheme } from "next-themes";import { UserSessionCountResponse } from "../../../../../api/analytics/useGetUserSessions";-import { getNivoTheme } from "../../../../../lib/nivo";+import { useNivoTheme } from "../../../../../lib/nivo";export const VisitCalendar = ({ sessionCount }: { sessionCount: UserSessionCountResponse[] }) => {- const { theme } = useTheme();- const nivoTheme = getNivoTheme(theme === "dark");+ const { resolvedTheme } = useTheme();+ const nivoTheme = useNivoTheme();const data = sessionCount.map(e => ({value: e.sessions,@@ -33,7 +33,7 @@ export const VisitCalendar = ({ sessionCount }: { sessionCount: UserSessionCounttheme={nivoTheme}from={data.at(-1)?.day ?? ""}to={data[0]?.day}- emptyColor={theme === "dark" ? "hsl(var(--neutral-750))" : "hsl(var(--neutral-100))"}+ emptyColor={resolvedTheme === "dark" ? "hsl(var(--neutral-750))" : "hsl(var(--neutral-100))"}colors={["#10452A", "#006D32", "#3E9058", "#3CD456"]}margin={{ top: 20, right: 0, bottom: 1, left: 20 }}monthBorderColor="rgba(0, 0, 0, 0)"diff --git a/client/src/components/ThemeSwitcher.tsx b/client/src/components/ThemeSwitcher.tsxindex d88e5b230..c4ce95ffd 100644--- a/client/src/components/ThemeSwitcher.tsx+++ b/client/src/components/ThemeSwitcher.tsx@@ -5,7 +5,7 @@ import { Moon, Sun } from "lucide-react";import { useEffect, useState } from "react";export function ThemeSwitcher() {- const { theme, setTheme } = useTheme();+ const { resolvedTheme, setTheme } = useTheme();const [mounted, setMounted] = useState(false);// Avoid hydration mismatch@@ -17,7 +17,7 @@ export function ThemeSwitcher() {return <div className="bg-neutral-200 dark:bg-neutral-800 p-0.5 rounded-full w-[46px] h-[24px]" />;}- const isDark = theme === "dark";+ const isDark = resolvedTheme === "dark";return (<buttondiff --git a/client/src/components/UsageChart.tsx b/client/src/components/UsageChart.tsxindex e4d46f2d0..2dd2cda59 100644--- a/client/src/components/UsageChart.tsx+++ b/client/src/components/UsageChart.tsx@@ -1,5 +1,5 @@"use client";-import { getNivoTheme } from "@/lib/nivo";+import { useNivoTheme } from "@/lib/nivo";import { ResponsiveLine } from "@nivo/line";import { useWindowSize } from "@uidotdev/usehooks";import { DateTime } from "luxon";@@ -7,7 +7,6 @@ import { useGetOrgEventCount } from "../api/analytics/useGetOrgEventCount";import { userLocale } from "../lib/dateTimeUtils";import { formatter } from "../lib/utils";import { Badge } from "./ui/badge";-import { useTheme } from "next-themes";import { ChartTooltip } from "./charts/ChartTooltip";interface UsageChartProps {@@ -19,8 +18,7 @@ interface UsageChartProps {export function UsageChart({ organizationId, startDate, endDate, timeZone = "UTC" }: UsageChartProps) {const { width } = useWindowSize();- const { theme } = useTheme();- const nivoTheme = getNivoTheme(theme === "dark");+ const nivoTheme = useNivoTheme();// Fetch the data inside the componentconst { data, isLoading, error } = useGetOrgEventCount({diff --git a/client/src/lib/nivo.ts b/client/src/lib/nivo.tsindex aa35d74a4..a00fb3feb 100644--- a/client/src/lib/nivo.ts+++ b/client/src/lib/nivo.ts@@ -1,4 +1,7 @@+"use client";+import { PartialTheme } from "@nivo/theming";+import { useTheme } from "next-themes";export const getNivoTheme = (isDark: boolean = true): PartialTheme => ({axis: {@@ -46,5 +49,11 @@ export const getNivoTheme = (isDark: boolean = true): PartialTheme => ({},});+// Custom hook that automatically uses the resolved theme+export const useNivoTheme = (): PartialTheme => {+ const { resolvedTheme } = useTheme();+ return getNivoTheme(resolvedTheme === "dark");+};+// Export default dark theme for backwards compatibilityexport const nivoTheme: PartialTheme = getNivoTheme(true);diff --git a/docs/src/components/ThemeSwitcher.tsx b/docs/src/components/ThemeSwitcher.tsxindex 3cdb17ccf..f2d330567 100644--- a/docs/src/components/ThemeSwitcher.tsx+++ b/docs/src/components/ThemeSwitcher.tsx@@ -5,7 +5,7 @@ import { Moon, Sun } from "lucide-react";import { useEffect, useState } from "react";export function ThemeSwitcher() {- const { theme, setTheme } = useTheme();+ const { resolvedTheme, setTheme } = useTheme();const [mounted, setMounted] = useState(false);// Avoid hydration mismatch@@ -17,7 +17,7 @@ export function ThemeSwitcher() {return <div className="bg-neutral-200 dark:bg-neutral-800 p-0.5 rounded-full w-[46px] h-[24px]" />;}- const isDark = theme === "dark";+ const isDark = resolvedTheme === "dark";return (<button