Skip to content

Open source CapCut alternative.

TypeScript

feat(site): the referrals chart says what "other" was

David Le89719c811729
diff --git a/site/src/app/cut/app/su/analytics/page.tsx b/site/src/app/cut/app/su/analytics/page.tsx
index 6d31d04e..8697243c 100644
--- a/site/src/app/cut/app/su/analytics/page.tsx
+++ b/site/src/app/cut/app/su/analytics/page.tsx
@@ -144,7 +144,7 @@ function deriveView(rollup: AnalyticsRollup): RollupView {
type ReferralView = {
config: ChartConfig;
trendConfig: ChartConfig;
- series: Record<string, number | string>[];
+ series: Record<string, number | string | string[]>[];
cumulative: Record<string, number | string>[];
respondents: number;
};
@@ -173,11 +173,14 @@ function deriveReferrals(referrals: AnalyticsReferrals): ReferralView {
};
let respondents = 0;
const running = new Map<string, number>();
- const series: Record<string, number | string>[] = [];
+ const series: Record<string, number | string | string[]>[] = [];
const cumulative: Record<string, number | string>[] = [];
for (const entry of referrals.days) {
respondents += entry.respondents;
- const daily: Record<string, number | string> = { day: entry.day };
+ const daily: Record<string, number | string | string[]> = {
+ day: entry.day,
+ otherAnswers: entry.others,
+ };
const total: Record<string, number | string> = { day: entry.day, totalResponses: respondents };
referrals.sources.forEach((id, i) => {
daily[id] = entry.counts[i] ?? 0;
@@ -198,6 +201,46 @@ function NonZeroTooltipContent(props: React.ComponentProps<typeof ChartTooltipCo
);
}
+/** The sources tooltip, which carries what "Other" meant: the day's free-text
+ * answers hang under that row as an indented bulleted list. */
+function SourcesTooltipContent({
+ config,
+ ...props
+}: React.ComponentProps<typeof ChartTooltipContent> & { config: ChartConfig }) {
+ return (
+ <NonZeroTooltipContent
+ {...props}
+ formatter={(value, name, item) => {
+ const id = String(name);
+ const answers = (item.payload as { otherAnswers?: string[] } | undefined)?.otherAnswers;
+ return (
+ <>
+ <div
+ className="size-2.5 shrink-0 rounded-[2px]"
+ style={{ background: item.color }}
+ />
+ <div className="flex flex-1 items-center justify-between gap-3 leading-none">
+ <span className="text-muted-foreground">{config[id]?.label ?? id}</span>
+ <span className="font-mono font-medium text-foreground tabular-nums">
+ {typeof value === "number" ? value.toLocaleString() : String(value)}
+ </span>
+ </div>
+ {id === "other" && answers?.length ? (
+ <ul className="ml-[1.125rem] w-full list-disc space-y-0.5 pl-3 text-muted-foreground marker:text-muted-foreground/60">
+ {answers.map((answer, i) => (
+ <li key={`${answer}-${i}`} className="break-words">
+ {answer}
+ </li>
+ ))}
+ </ul>
+ ) : null}
+ </>
+ );
+ }}
+ />
+ );
+}
+
const activesConfig = {
active: { label: "Active", color: "var(--chart-1)" },
working: { label: "Working", color: "var(--chart-2)" },
@@ -869,7 +912,10 @@ export default function SuAnalyticsPage() {
<YAxis allowDecimals={false} axisLine={false} tickLine={false} width={48} />
<ChartTooltip
content={
- <NonZeroTooltipContent labelFormatter={(label) => formatDay(String(label))} />
+ <SourcesTooltipContent
+ config={referrals.config}
+ labelFormatter={(label) => formatDay(String(label))}
+ />
}
/>
{Object.keys(referrals.config).map((id) => (
@@ -964,7 +1010,7 @@ export default function SuAnalyticsPage() {
</div>
)}
- <div className="grid gap-4 sm:grid-cols-2 xl:grid-cols-4">
+ <div className="grid gap-4 sm:grid-cols-2 xl:grid-cols-4" {...tiles.containerProps}>
{tiles.order.map((id) => (
<DragBlock key={id} {...tiles.blockProps(id)}>
{tileNodes[id]}
@@ -972,7 +1018,7 @@ export default function SuAnalyticsPage() {
))}
</div>
- <div className="grid gap-6 xl:grid-cols-2">
+ <div className="grid gap-6 xl:grid-cols-2" {...cards.containerProps}>
{cards.order
.filter((id) => cardNodes[id] !== undefined)
.map((id) => (
diff --git a/site/src/lib/analytics/pipeline.ts b/site/src/lib/analytics/pipeline.ts
index 5d84794b..f71cd34b 100644
--- a/site/src/lib/analytics/pipeline.ts
+++ b/site/src/lib/analytics/pipeline.ts
@@ -193,7 +193,9 @@ async function writeSnapshot(): Promise<AnalyticsSnapshotFile> {
email: true,
id: true,
name: true,
- onboarding: { select: { referralAnsweredAt: true, referralSources: true } },
+ onboarding: {
+ select: { referralAnsweredAt: true, referralOther: true, referralSources: true },
+ },
superUser: true,
},
take: SNAPSHOT_PAGE_SIZE,
@@ -210,6 +212,9 @@ async function writeSnapshot(): Promise<AnalyticsSnapshotFile> {
? {
referralAnsweredAt: u.onboarding.referralAnsweredAt.toISOString(),
referralSources: u.onboarding.referralSources,
+ ...(u.onboarding.referralOther?.trim()
+ ? { referralOther: u.onboarding.referralOther.trim() }
+ : {}),
}
: {}),
}));
@@ -415,7 +420,13 @@ function buildReferrals(
.flatMap((u) =>
u.referralAnsweredAt === undefined
? []
- : [{ day: utcDayOf(new Date(u.referralAnsweredAt)), sources: u.referralSources ?? [] }],
+ : [
+ {
+ day: utcDayOf(new Date(u.referralAnsweredAt)),
+ other: u.referralOther,
+ sources: u.referralSources ?? [],
+ },
+ ],
)
.filter((a) => a.day <= through);
if (answered.length === 0) return undefined;
@@ -426,10 +437,13 @@ function buildReferrals(
}
const first = answered.reduce((min, a) => (a.day < min ? a.day : min), through);
- const byDay = new Map<string, { day: string; respondents: number; counts: number[] }>();
+ const byDay = new Map<
+ string,
+ { day: string; respondents: number; counts: number[]; others: string[] }
+ >();
const days: NonNullable<AnalyticsRollup["referrals"]>["days"] = [];
for (let day = first; day <= through; day = addUtcDays(day, 1)) {
- const entry = { counts: sources.map(() => 0), day, respondents: 0 };
+ const entry = { counts: sources.map(() => 0), day, others: [], respondents: 0 };
byDay.set(day, entry);
days.push(entry);
}
@@ -437,6 +451,7 @@ function buildReferrals(
const entry = byDay.get(a.day);
if (!entry) continue;
entry.respondents++;
+ if (a.other) entry.others.push(a.other);
for (const id of a.sources) entry.counts[sources.indexOf(id)]++;
}
return { days, sources };
diff --git a/site/src/lib/analytics/schema.ts b/site/src/lib/analytics/schema.ts
index 85255f2f..c3606494 100644
--- a/site/src/lib/analytics/schema.ts
+++ b/site/src/lib/analytics/schema.ts
@@ -75,6 +75,8 @@ export const analyticsSnapshotFileSchema = z.object({
// The onboarding referral answer; present once the user answered.
referralAnsweredAt: z.string().optional(),
referralSources: z.array(z.string()).optional(),
+ // The free text that comes with picking "other".
+ referralOther: z.string().optional(),
}),
),
balances: z.array(z.object({ userId: z.string(), balanceMicros: z.string() })),
@@ -125,6 +127,8 @@ export const analyticsRollupSchema = z.object({
day: analyticsDaySchema,
respondents: z.number(),
counts: z.array(z.number()),
+ // The free text behind that day's "other" answers, in no order.
+ others: z.array(z.string()).default([]),
}),
),
})