OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
dashboard: stop throwing the server-rendered tree away
Carl-Gerhard Lindesvärd1d92441a733f
diff --git a/apps/start/src/components/organization/prompt-card.tsx b/apps/start/src/components/organization/prompt-card.tsxindex 10c769762..22bf51908 100644--- a/apps/start/src/components/organization/prompt-card.tsx+++ b/apps/start/src/components/organization/prompt-card.tsx@@ -1,5 +1,6 @@import { AnimatePresence, motion } from 'framer-motion';import { XIcon } from 'lucide-react';+import { useEffect, useState } from 'react';import { Button } from '@/components/ui/button';interface PromptCardProps {@@ -19,9 +20,20 @@ export function PromptCard({gradientColor = 'rgb(16 185 129)',show,}: PromptCardProps) {+ // `show` comes from a cookie, which the server can read, so this card can+ // render during SSR. framer writes its `initial` transform and opacity into+ // the style attribute and the client's first frame differs, so React threw+ // the tree away (ISSUES.md H10). Waiting for mount keeps the slide-in —+ // `initial={false}` would have removed it — and costs nothing, since the+ // card is a popup nobody expects in the server-rendered HTML.+ const [mounted, setMounted] = useState(false);+ useEffect(() => {+ setMounted(true);+ }, []);+return (<AnimatePresence>- {show && (+ {show && mounted && (<motion.divanimate={{ opacity: 1, x: 0, scale: 1 }}className="fixed right-0 bottom-0 z-50 max-w-sm p-4"diff --git a/apps/start/src/components/report-chart/bar/index.tsx b/apps/start/src/components/report-chart/bar/index.tsxindex 6656952e7..0f9c5382a 100644--- a/apps/start/src/components/report-chart/bar/index.tsx+++ b/apps/start/src/components/report-chart/bar/index.tsx@@ -7,6 +7,13 @@ import { Chart } from './chart';import { useTRPC } from '@/integrations/trpc/react';import { cn } from '@/utils/cn';+// Fixed, not random. The server and the browser each rendered their own+// widths, so every skeleton row was an attribute mismatch and React threw the+// SSR tree away (ISSUES.md H10). The exact values do not matter; only that+// both renders agree.+const SKELETON_NAME_WIDTHS = [186, 134, 158, 112, 170, 126, 194, 148, 120, 176];+const SKELETON_BAR_WIDTHS = [72, 48, 63, 35, 57, 41, 78, 52, 29, 66];+export function ReportBarChart() {const { isLazyLoading, shareId } = useReportChartContext();const chartInput = useChartInput();@@ -71,7 +78,7 @@ function Loading() {<divclassName="h-4 rounded bg-def-200 dark:bg-def-700"style={{- width: `${Math.random() * 100 + 100}px`,+ width: `${SKELETON_NAME_WIDTHS[index % SKELETON_NAME_WIDTHS.length]}px`,}}/></div>@@ -89,7 +96,7 @@ function Loading() {<divclassName="h-full rounded-full bg-def-200 dark:bg-def-700"style={{- width: `${Math.random() * 60 + 20}%`,+ width: `${SKELETON_BAR_WIDTHS[index % SKELETON_BAR_WIDTHS.length]}%`,}}/></div>diff --git a/apps/start/src/components/sidebar-organization-menu.tsx b/apps/start/src/components/sidebar-organization-menu.tsxindex b71295e82..67f5b0e8d 100644--- a/apps/start/src/components/sidebar-organization-menu.tsx+++ b/apps/start/src/components/sidebar-organization-menu.tsx@@ -157,7 +157,11 @@ export function ActionCTAButton() {><PlusIcon className="size-5 shrink-0" /><div className="relative flex h-5 flex-1 items-center overflow-hidden">- <AnimatePresence mode="popLayout">+ {/* `initial={false}` suppresses only the FIRST mount: framer writes+ transform/opacity into the style attribute, and the SSR string+ differs from the client's first frame (ISSUES.md H10). The+ cycling animation between actions is unaffected. */}+ <AnimatePresence initial={false} mode="popLayout"><motion.spananimate={{ y: 0, opacity: 1 }}className="absolute whitespace-nowrap"diff --git a/apps/start/src/components/sidebar-project-menu.tsx b/apps/start/src/components/sidebar-project-menu.tsxindex febbbf256..6055b07a1 100644--- a/apps/start/src/components/sidebar-project-menu.tsx+++ b/apps/start/src/components/sidebar-project-menu.tsx@@ -169,7 +169,11 @@ export function ActionCTAButton() {><PlusIcon className="size-5 shrink-0" /><div className="relative flex h-5 flex-1 items-center overflow-hidden">- <AnimatePresence mode="popLayout">+ {/* `initial={false}` suppresses only the FIRST mount: framer writes+ transform/opacity into the style attribute, and the SSR string+ differs from the client's first frame (ISSUES.md H10). The+ cycling animation between actions is unaffected. */}+ <AnimatePresence initial={false} mode="popLayout"><motion.spananimate={{ y: 0, opacity: 1 }}className="absolute whitespace-nowrap"diff --git a/apps/start/src/components/widget-table.tsx b/apps/start/src/components/widget-table.tsxindex bdd70dbf3..801d5bfdb 100644--- a/apps/start/src/components/widget-table.tsx+++ b/apps/start/src/components/widget-table.tsx@@ -118,10 +118,13 @@ export function WidgetTable<T>({.join(' ')}`: '1fr';- const containerId = React.useMemo(- () => `widget-table-${Math.random().toString(36).substring(7)}`,- []- );+ // `useId` is stable across SSR and hydration where `Math.random()` was not:+ // the id lands in a className AND inside injected <style> text, so both the+ // attribute and the stylesheet differed and React regenerated the subtree+ // (ISSUES.md H10). React's ids contain colons, which are not valid in a CSS+ // selector, so they are stripped.+ const reactId = React.useId();+ const containerId = `widget-table-${reactId.replace(/:/g, '')}`;// Generate CSS for container queriesconst containerQueryStyles = React.useMemo(() => {diff --git a/apps/start/src/modals/share-dashboard-modal.tsx b/apps/start/src/modals/share-dashboard-modal.tsxindex c0c5dd411..6dd629c9e 100644--- a/apps/start/src/modals/share-dashboard-modal.tsx+++ b/apps/start/src/modals/share-dashboard-modal.tsx@@ -124,7 +124,7 @@ export default function ShareDashboardModal({</div><div className="flex items-center gap-1"><Input className="flex-1 text-sm" readOnly value={shareUrl} />- <Tooltiper content="Copy link">+ <Tooltiper asChild content="Copy link"><ButtononClick={handleCopyLink}size="sm"@@ -138,7 +138,7 @@ export default function ShareDashboardModal({)}</Button></Tooltiper>- <Tooltiper content="Open in new tab">+ <Tooltiper asChild content="Open in new tab"><ButtononClick={() => window.open(shareUrl, '_blank')}size="sm"@@ -148,7 +148,7 @@ export default function ShareDashboardModal({<ExternalLink className="size-4" /></Button></Tooltiper>- <Tooltiper content="Make private">+ <Tooltiper asChild content="Make private"><ButtononClick={handleMakePrivate}type="button"diff --git a/apps/start/src/modals/share-overview-modal.tsx b/apps/start/src/modals/share-overview-modal.tsxindex 3223aa22b..ddecef8c0 100644--- a/apps/start/src/modals/share-overview-modal.tsx+++ b/apps/start/src/modals/share-overview-modal.tsx@@ -115,7 +115,7 @@ export default function ShareOverviewModal() {</div><div className="flex items-center gap-1"><Input className="flex-1 text-sm" readOnly value={shareUrl} />- <Tooltiper content="Copy link">+ <Tooltiper asChild content="Copy link"><ButtononClick={handleCopyLink}size="sm"@@ -129,7 +129,7 @@ export default function ShareOverviewModal() {)}</Button></Tooltiper>- <Tooltiper content="Open in new tab">+ <Tooltiper asChild content="Open in new tab"><ButtononClick={() => window.open(shareUrl, '_blank')}size="sm"@@ -139,7 +139,7 @@ export default function ShareOverviewModal() {<ExternalLink className="size-4" /></Button></Tooltiper>- <Tooltiper content="Make private">+ <Tooltiper asChild content="Make private"><ButtononClick={handleMakePrivate}type="button"diff --git a/apps/start/src/modals/share-report-modal.tsx b/apps/start/src/modals/share-report-modal.tsxindex fb91ab0ba..0798b1433 100644--- a/apps/start/src/modals/share-report-modal.tsx+++ b/apps/start/src/modals/share-report-modal.tsx@@ -116,7 +116,7 @@ export default function ShareReportModal({ reportId }: { reportId: string }) {</div><div className="flex items-center gap-1"><Input className="flex-1 text-sm" readOnly value={shareUrl} />- <Tooltiper content="Copy link">+ <Tooltiper asChild content="Copy link"><ButtononClick={handleCopyLink}size="sm"@@ -130,7 +130,7 @@ export default function ShareReportModal({ reportId }: { reportId: string }) {)}</Button></Tooltiper>- <Tooltiper content="Open in new tab">+ <Tooltiper asChild content="Open in new tab"><ButtononClick={() => window.open(shareUrl, '_blank')}size="sm"@@ -140,7 +140,7 @@ export default function ShareReportModal({ reportId }: { reportId: string }) {<ExternalLink className="size-4" /></Button></Tooltiper>- <Tooltiper content="Make private">+ <Tooltiper asChild content="Make private"><ButtononClick={handleMakePrivate}type="button"diff --git a/apps/start/src/utils/date.ts b/apps/start/src/utils/date.tsindex 27dd26dc8..c5011e940 100644--- a/apps/start/src/utils/date.ts+++ b/apps/start/src/utils/date.ts@@ -8,12 +8,22 @@ export function dateDifferanceInDays(date1: Date, date2: Date) {return Math.ceil(diffTime / (1000 * 60 * 60 * 24));}+/**+ * Pinned, deliberately. This used to return `navigator.language` and fall back+ * to `en-US` only when `navigator` was undefined — which is dead code on+ * modern runtimes, since node defines `navigator` too. The server therefore+ * formatted with the MACHINE locale while the browser used the visitor's, so+ * the two renders disagreed and React discarded the tree: `21:35:55` against+ * `9:35:55 PM`, and Swedish month names in an English UI (ISSUES.md H10).+ *+ * Numbers were already pinned this way in `use-numer-formatter.ts`.+ *+ * Note this pins the LOCALE, not the timezone: a server in a different zone+ * from the visitor still renders different clock times. That is a separate+ * problem and this does not solve it.+ */export function getLocale() {- if (typeof navigator === 'undefined') {- return 'en-US';- }-- return navigator.language ?? 'en-US';+ return 'en-US';}export function formatDate(date: Date) {