Open source CapCut alternative.
TypeScript
perf(site): the su activity grid draws only the rows on screen
David Le0c51b6477e70
diff --git a/site/src/app/su/analytics/product/page.tsx b/site/src/app/su/analytics/product/page.tsxindex 02c40ce7..2e43cf02 100644--- a/site/src/app/su/analytics/product/page.tsx+++ b/site/src/app/su/analytics/product/page.tsx@@ -1,7 +1,7 @@"use client";import { ArrowUpRight } from "lucide-react";-import { useMemo } from "react";+import { useMemo, useRef } from "react";import {Area,AreaChart,@@ -34,6 +34,7 @@ import { REFERRAL_SOURCES } from "@/lib/onboarding/sequence";import { useLocalPref } from "@/cut/lib/uiState";import { cn } from "@/lib/utils";import { DragBlock, useReorder } from "@/app/su/analytics/Reorder";+import { useRowWindow } from "@/app/su/analytics/rowWindow";import { useAnalyticsRollup } from "@/queries/analytics";import { ApiError } from "@/queries/apiClient";@@ -42,12 +43,21 @@ import { ApiError } from "@/queries/apiClient";// any source bit for the day; "working" narrows to the DB event sources, i.e.// the user did something beyond opening the app.+// Formatting a day goes through the locale machinery, which is slow enough+// that a label per grid cell held the page for over a second. The rollup+// names a few hundred distinct days, so each is formatted once.+const dayLabels = new Map<string, string>();function formatDay(iso: string): string {- return new Date(`${iso}T00:00:00Z`).toLocaleDateString("en-US", {- month: "short",- day: "numeric",- timeZone: "UTC",- });+ let label = dayLabels.get(iso);+ if (label === undefined) {+ label = new Date(`${iso}T00:00:00Z`).toLocaleDateString("en-US", {+ month: "short",+ day: "numeric",+ timeZone: "UTC",+ });+ dayLabels.set(iso, label);+ }+ return label;}const dollars = (micros: bigint | string) => Number(micros) / 1e6;@@ -574,12 +584,10 @@ function ChartCard({function ActivityDot({mask,workBits,- label,unknown,}: {mask: number;workBits: number;- label: string;unknown: boolean;}) {const worked = (mask & workBits) !== 0;@@ -596,7 +604,6 @@ function ActivityDot({? "bg-[var(--chart-1)] opacity-40": "bg-muted",)}- title={label}/>);}@@ -667,17 +674,35 @@ function ActivityGrid({(v) => USER_SORTS.some((option) => option.id === v),);const users = useMemo(() => rankUsers(rollup, workBits, sort), [rollup, workBits, sort]);+ const body = useRef<HTMLTableSectionElement>(null);+ const rows = useRowWindow(users.length, body);- const dotLabel = (email: string, day: string, mask: number) => {- if (missingDays.has(day)) return `${email} — ${formatDay(day)}: no data`;- if (mask === 0) return `${email} — ${formatDay(day)}: inactive`;- const sources = rollup.sources.filter((_, i) => (mask & (1 << i)) !== 0);- return `${email} — ${formatDay(day)}: ${sources.join(", ")}`;- };// Newest day sits in the leftmost column so the current dots are in view// before any horizontal scroll; each column keeps its index into the// activity masks.- const columns = rollup.days.map((day, i) => ({ day, i })).reverse();+ const columns = useMemo(+ () => rollup.days.map((day, i) => ({ day, i, unknown: missingDays.has(day) })).reverse(),+ [rollup.days, missingDays],+ );+ const columnCount = columns.length + 1;++ // A dot's tooltip is built when the pointer reaches it. Building one per+ // cell up front was the bulk of the page's arrival cost.+ const describeDot = (event: React.PointerEvent<HTMLTableSectionElement>) => {+ const cell = (event.target as HTMLElement).closest<HTMLElement>("[data-dot]");+ if (!cell || cell.title) return;+ const user = users[Number(cell.dataset.user)];+ const column = columns[Number(cell.dataset.col)];+ if (!user || !column) return;+ const mask = user.activity[column.i] ?? 0;+ const when = formatDay(column.day);+ cell.title = column.unknown+ ? `${user.email} — ${when}: no data`+ : mask === 0+ ? `${user.email} — ${when}: inactive`+ : `${user.email} — ${when}: ${rollup.sources.filter((_, i) => (mask & (1 << i)) !== 0).join(", ")}`;+ };+return (<div className="rounded-xl border bg-card p-5"><div className="flex flex-wrap items-start justify-between gap-3">@@ -722,59 +747,71 @@ function ActivityGrid({))}</tr></thead>- <tbody>- {users.map((user) => (- <tr key={user.id}>- <td className="sticky left-0 z-10 bg-card py-1 pr-4 whitespace-nowrap">- <span className="flex items-center gap-1.5">- <span className="block max-w-56 truncate text-sm" title={user.name}>- {user.email}- </span>- {user.superUser === true && (- <span className="rounded-sm bg-muted px-1 text-[10px] text-muted-foreground">- su- </span>- )}- </span>- <span className="block text-xs text-muted-foreground">- {user.activeDays === 0- ? "no activity"- : `${user.activeDays} active ${user.activeDays === 1 ? "day" : "days"} · last ${formatDay(rollup.days[user.lastActive])}`}{" "}- · joined {formatDay(user.registeredAt.slice(0, 10))} ·{" "}- {formatMicros(user.balanceMicros)}- {user.fundedMicros !== undefined && (- <span className="text-emerald-700 dark:text-emerald-500">- {" "}- · paid {formatMicros(user.fundedMicros)}- </span>- )}- {user.storageBytes !== undefined && (- <span- className={storageTone(Number(user.storageBytes), user.storageQuotaBytes)}- title={- user.storageQuotaBytes == null- ? "unlimited storage"- : `of ${formatGb(user.storageQuotaBytes)}`- }- >- {" "}- · {formatGb(Number(user.storageBytes))}+ <tbody ref={body} onPointerOver={describeDot}>+ {rows.above > 0 && (+ <tr aria-hidden>+ <td colSpan={columnCount} className="p-0" style={{ height: rows.above }} />+ </tr>+ )}+ {users.slice(rows.start, rows.end).map((user, offset) => {+ const index = rows.start + offset;+ return (+ <tr key={user.id} data-row="">+ <td className="sticky left-0 z-10 bg-card py-1 pr-4 whitespace-nowrap">+ <span className="flex items-center gap-1.5">+ <span className="block max-w-56 truncate text-sm" title={user.name}>+ {user.email}</span>- )}- </span>- </td>- {columns.map(({ day, i }) => (- <td key={day} className="p-0.5">- <ActivityDot- label={dotLabel(user.email, day, user.activity[i] ?? 0)}- mask={user.activity[i] ?? 0}- unknown={missingDays.has(day)}- workBits={workBits}- />+ {user.superUser === true && (+ <span className="rounded-sm bg-muted px-1 text-[10px] text-muted-foreground">+ su+ </span>+ )}+ </span>+ <span className="block text-xs text-muted-foreground">+ {user.activeDays === 0+ ? "no activity"+ : `${user.activeDays} active ${user.activeDays === 1 ? "day" : "days"} · last ${formatDay(rollup.days[user.lastActive])}`}{" "}+ · joined {formatDay(user.registeredAt.slice(0, 10))} ·{" "}+ {formatMicros(user.balanceMicros)}+ {user.fundedMicros !== undefined && (+ <span className="text-emerald-700 dark:text-emerald-500">+ {" "}+ · paid {formatMicros(user.fundedMicros)}+ </span>+ )}+ {user.storageBytes !== undefined && (+ <span+ className={storageTone(Number(user.storageBytes), user.storageQuotaBytes)}+ title={+ user.storageQuotaBytes == null+ ? "unlimited storage"+ : `of ${formatGb(user.storageQuotaBytes)}`+ }+ >+ {" "}+ · {formatGb(Number(user.storageBytes))}+ </span>+ )}+ </span></td>- ))}+ {columns.map(({ day, i, unknown }, col) => (+ <td key={day} className="p-0.5" data-dot data-user={index} data-col={col}>+ <ActivityDot+ mask={user.activity[i] ?? 0}+ unknown={unknown}+ workBits={workBits}+ />+ </td>+ ))}+ </tr>+ );+ })}+ {rows.below > 0 && (+ <tr aria-hidden>+ <td colSpan={columnCount} className="p-0" style={{ height: rows.below }} /></tr>- ))}+ )}</tbody></table></div>diff --git a/site/src/app/su/analytics/rowWindow.ts b/site/src/app/su/analytics/rowWindow.tsnew file mode 100644index 00000000..92f885fc--- /dev/null+++ b/site/src/app/su/analytics/rowWindow.ts@@ -0,0 +1,90 @@+"use client";++import { useEffect, useState, type RefObject } from "react";++/**+ * Which rows of a long table are on screen.+ *+ * A table of every account, sixty cells a row, is tens of thousands of nodes;+ * React builds all of them on arrival and again on every sort, and the page+ * holds the thread for seconds each time. This keeps the rows to the ones in+ * view plus a margin: a spacer above and below stands in for the rest, so the+ * scrollbar and the sticky column read as if the table were whole.+ *+ * Rows are one height, measured from the first data row rendered (marked+ * data-row, so the spacer above it is skipped). The scroll+ * handler books one animation frame and reads layout once inside it.+ */+export interface RowWindow {+ start: number;+ end: number;+ /** Pixels standing in for the rows left out above and below. */+ above: number;+ below: number;+}++const ROW_HEIGHT_GUESS = 44;+const OVERSCAN = 16;++// The nearest ancestor that scrolls vertically. Overflow set on one axis+// computes to auto on the other, so the table's own sideways wrapper reads+// as a vertical scroller too; asking for real vertical overflow skips it.+function scrollParentOf(el: HTMLElement): HTMLElement | null {+ for (let node = el.parentElement; node; node = node.parentElement) {+ const overflow = getComputedStyle(node).overflowY;+ if ((overflow === "auto" || overflow === "scroll") && node.scrollHeight > node.clientHeight) {+ return node;+ }+ }+ return null;+}++export function useRowWindow(+ count: number,+ body: RefObject<HTMLTableSectionElement | null>,+): RowWindow {+ const [rowHeight, setRowHeight] = useState(ROW_HEIGHT_GUESS);+ const [range, setRange] = useState(() => ({ start: 0, end: Math.min(count, 40) }));++ useEffect(() => {+ const el = body.current;+ if (!el) return;+ const scroller = scrollParentOf(el);+ let frame = 0;+ const measure = () => {+ frame = 0;+ const probe = el.querySelector<HTMLTableRowElement>("tr[data-row]");+ const h = probe?.offsetHeight;+ if (h && h !== rowHeight) {+ setRowHeight(h);+ return;+ }+ const viewTop = scroller+ ? scroller.getBoundingClientRect().top - el.getBoundingClientRect().top+ : -el.getBoundingClientRect().top;+ const viewHeight = scroller ? scroller.clientHeight : window.innerHeight;+ const start = Math.min(count, Math.max(0, Math.floor(viewTop / rowHeight) - OVERSCAN));+ const end = Math.min(+ count,+ Math.max(start, Math.ceil((viewTop + viewHeight) / rowHeight) + OVERSCAN),+ );+ setRange((r) => (r.start === start && r.end === end ? r : { start, end }));+ };+ const schedule = () => {+ if (!frame) frame = requestAnimationFrame(measure);+ };+ measure();+ const target: EventTarget = scroller ?? window;+ target.addEventListener("scroll", schedule, { passive: true });+ window.addEventListener("resize", schedule);+ return () => {+ if (frame) cancelAnimationFrame(frame);+ target.removeEventListener("scroll", schedule);+ window.removeEventListener("resize", schedule);+ };+ }, [body, count, rowHeight]);++ const start = Math.min(range.start, count);+ const end = Math.min(range.end, count);+ return { above: start * rowHeight, below: (count - end) * rowHeight, end, start };+}