Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
📈 Track timing, rank, host and video link at finalize, and wizard drop-off (#3508)
Luke Vellaeea934c662d5
diff --git a/apps/web/src/features/poll/components/finalize-poll-dialog.tsx b/apps/web/src/features/poll/components/finalize-poll-dialog.tsxindex d2e628cff..79099dec1 100644--- a/apps/web/src/features/poll/components/finalize-poll-dialog.tsx+++ b/apps/web/src/features/poll/components/finalize-poll-dialog.tsx@@ -557,9 +557,18 @@ function SummaryStep({ option }: { option: RankedOption }) {);}-// Mounted fresh with each open, so the wizard starts at step one with-// nothing carried over from a previous run.-function FinalizeWizard({ onClose }: { onClose: () => void }) {+// Mounted fresh with each open, so the wizard starts with nothing carried+// over from a previous run. The step lives in the dialog, which reads it+// when the host closes it.+function FinalizeWizard({+ step,+ onStepChange,+ onClose,+}: {+ step: Step;+ onStepChange: (step: Step) => void;+ onClose: () => void;+}) {const poll = usePoll();const { participants } = useParticipants();const router = useRouter();@@ -575,18 +584,21 @@ function FinalizeWizard({ onClose }: { onClose: () => void }) {() => rankOptionsByPopularity({ options: poll.options, participants }),[poll.options, participants],);- const [step, setStep] = React.useState<Step>("date");const [optionId, setOptionId] = React.useState<string | null>(null);const [notifyIds, setNotifyIds] = React.useState(() => new Set(participants.filter((p) => p.email).map((p) => p.id)),);const option = options.find((o) => o.id === optionId);const [skippedNotifications, setSkippedNotifications] = React.useState(0);+ const goTo = (next: Step) => {+ posthog?.capture("poll_finalize:step_view", { step: next });+ onStepChange(next);+ };const finalize = trpc.polls.book.useMutation({onSuccess: (data) => {setSkippedNotifications(data?.skippedNotifications ?? 0);- setStep("done");+ goTo("done");// The page behind the dialog picks up the scheduled status.router.refresh();},@@ -623,7 +635,7 @@ function FinalizeWizard({ onClose }: { onClose: () => void }) {onPick={(id) => {setOptionId(id);// With nobody to notify, the notify step has nothing to ask.- setStep(participants.length > 0 ? "notify" : "summary");+ goTo(participants.length > 0 ? "notify" : "summary");}}/></div>@@ -687,7 +699,7 @@ function FinalizeWizard({ onClose }: { onClose: () => void }) {/><DialogFooter><Button- onClick={() => setStep(participants.length > 0 ? "notify" : "date")}+ onClick={() => goTo(participants.length > 0 ? "notify" : "date")}><Trans i18nKey="back" defaults="Back" /></Button>@@ -732,10 +744,10 @@ function FinalizeWizard({ onClose }: { onClose: () => void }) {onChange={setNotifyIds}/><DialogFooter>- <Button onClick={() => setStep("date")}>+ <Button onClick={() => goTo("date")}><Trans i18nKey="back" defaults="Back" /></Button>- <Button variant="primary" onClick={() => setStep("summary")}>+ <Button variant="primary" onClick={() => goTo("summary")}><Trans i18nKey="next" defaults="Next" /></Button></DialogFooter>@@ -744,10 +756,29 @@ function FinalizeWizard({ onClose }: { onClose: () => void }) {}export function FinalizePollDialog(props: DialogProps) {+ const [step, setStep] = React.useState<Step>("date");+ const onOpenChange = (open: boolean) => {+ if (!open && step !== "done") {+ posthog?.capture("poll_finalize:dialog_dismiss", { step });+ }+ props.onOpenChange?.(open);+ };return (- <Dialog {...props}>+ <Dialog+ {...props}+ onOpenChange={onOpenChange}+ // Reset once the close animation ends, so the closing dialog keeps+ // showing the step it was on.+ onOpenChangeComplete={(open) => {+ if (!open) setStep("date");+ }}+ ><DialogContent className="w-auto sm:max-w-none">- <FinalizeWizard onClose={() => props.onOpenChange?.(false)} />+ <FinalizeWizard+ step={step}+ onStepChange={setStep}+ onClose={() => onOpenChange(false)}+ /></DialogContent></Dialog>);diff --git a/apps/web/src/features/poll/components/poll-primary-actions.tsx b/apps/web/src/features/poll/components/poll-primary-actions.tsxindex 4ac113c7f..12b35bf54 100644--- a/apps/web/src/features/poll/components/poll-primary-actions.tsx+++ b/apps/web/src/features/poll/components/poll-primary-actions.tsx@@ -1,5 +1,6 @@"use client";+import { posthog } from "@rallly/posthog/client";import { Button } from "@rallly/ui/button";import { useDialog } from "@rallly/ui/dialog";import { CalendarCheck2Icon } from "lucide-react";@@ -27,7 +28,14 @@ export function PollPrimaryActions() {variant={canFinalize ? "default" : "primary"}/>{canFinalize ? (- <Button variant="primary" {...finalizeDialog.triggerProps}>+ <Button+ variant="primary"+ {...finalizeDialog.triggerProps}+ onClick={() => {+ posthog?.capture("poll_finalize:dialog_open");+ finalizeDialog.trigger();+ }}+ ><CalendarCheck2Icon data-icon="inline-start" /><span className="sr-only sm:not-sr-only"><Trans i18nKey="finalize" defaults="Finalize" />diff --git a/apps/web/src/features/poll/utils.test.ts b/apps/web/src/features/poll/utils.test.tsindex 398a9507f..152bf7a37 100644--- a/apps/web/src/features/poll/utils.test.ts+++ b/apps/web/src/features/poll/utils.test.ts@@ -2,7 +2,9 @@ import { describe, expect, it } from "vitest";import {filterCommentsForViewer,filterParticipantsByVote,+ getChosenOptionRank,getFinalizePlanGate,+ getFinalizeTiming,maskParticipantsForViewer,rankOptionsByPopularity,summarizeNotifySelection,@@ -255,6 +257,102 @@ describe("rankOptionsByPopularity", () => {});});+describe("getChosenOptionRank", () => {+ const options = [{ id: "a" }, { id: "b" }, { id: "c" }];+ const participants = [+ {+ id: "p1",+ votes: [+ { optionId: "a", type: "yes" as const },+ { optionId: "b", type: "yes" as const },+ { optionId: "c", type: "no" as const },+ ],+ },+ ];++ it("ranks a joint winner first and tied", () => {+ expect(+ getChosenOptionRank({ options, participants, optionId: "b" }),+ ).toEqual({ chosen_rank: 1, chosen_tied: true, option_count: 3 });+ });++ it("ranks an option below the winners after all of them", () => {+ expect(+ getChosenOptionRank({ options, participants, optionId: "c" }),+ ).toEqual({ chosen_rank: 3, chosen_tied: false, option_count: 3 });+ });++ it("returns no rank for an option that is not in the poll", () => {+ expect(+ getChosenOptionRank({ options, participants, optionId: "gone" }),+ ).toEqual({ chosen_rank: null, chosen_tied: false, option_count: 3 });+ });+});++describe("getFinalizeTiming", () => {+ const now = new Date("2026-10-13T12:00:00Z");++ it("counts hours ahead for a zoned time", () => {+ expect(+ getFinalizeTiming({+ start: new Date("2026-10-14T12:00:00Z"),+ end: new Date("2026-10-14T13:00:00Z"),+ allDay: false,+ timeZone: "Europe/London",+ now,+ }),+ ).toEqual({ hours_to_start: 24, is_past: false, floating_time: false });+ });++ it("marks a zoned time past as soon as it starts", () => {+ expect(+ getFinalizeTiming({+ start: new Date("2026-10-13T11:00:00Z"),+ end: new Date("2026-10-13T12:00:00Z"),+ allDay: false,+ timeZone: "Europe/London",+ now,+ }).is_past,+ ).toBe(true);+ });++ it("keeps a floating time open until it has passed in every zone", () => {+ const timing = getFinalizeTiming({+ start: new Date("2026-10-13T11:00:00Z"),+ end: new Date("2026-10-13T12:00:00Z"),+ allDay: false,+ timeZone: null,+ now,+ });+ expect(timing.is_past).toBe(false);+ expect(timing.floating_time).toBe(true);+ });++ it("treats today's all-day date as not past", () => {+ expect(+ getFinalizeTiming({+ start: new Date("2026-10-13T00:00:00Z"),+ end: new Date("2026-10-14T00:00:00Z"),+ allDay: true,+ timeZone: null,+ now,+ }).is_past,+ ).toBe(false);+ });++ it("treats an all-day date that ended everywhere as past", () => {+ expect(+ getFinalizeTiming({+ start: new Date("2026-10-11T00:00:00Z"),+ end: new Date("2026-10-12T00:00:00Z"),+ allDay: true,+ timeZone: null,+ now,+ }).is_past,+ ).toBe(true);+ });+});+describe("getFinalizePlanGate", () => {it("lets a pro space notify and mint a meeting", () => {expect(diff --git a/apps/web/src/features/poll/utils.ts b/apps/web/src/features/poll/utils.tsindex df1ccd4b9..bf4f1cf16 100644--- a/apps/web/src/features/poll/utils.ts+++ b/apps/web/src/features/poll/utils.ts@@ -164,11 +164,6 @@ export function rankOptionsByPopularity<T extends { id: string }>({votesByOptionId.get(vote.optionId)?.[vote.type].push(participant.id);}}- const score = (votes: OptionVotes) => [- votes.yes.length + votes.ifNeedBe.length,- votes.yes.length,- votes.ifNeedBe.length,- ];return options.map((option) => ({...option,@@ -178,14 +173,91 @@ export function rankOptionsByPopularity<T extends { id: string }>({no: [],},}))- .sort((a, b) => {- const sa = score(a.votes);- const sb = score(b.votes);- for (let i = 0; i < sa.length; i++) {- if (sa[i] !== sb[i]) return sb[i] - sa[i];- }- return 0;- });+ .sort((a, b) => comparePopularity(a.votes, b.votes));+}++function comparePopularity(a: OptionVotes, b: OptionVotes) {+ const score = (votes: OptionVotes) => [+ votes.yes.length + votes.ifNeedBe.length,+ votes.yes.length,+ votes.ifNeedBe.length,+ ];+ const sa = score(a);+ const sb = score(b);+ for (let i = 0; i < sa.length; i++) {+ if (sa[i] !== sb[i]) return sb[i] - sa[i];+ }+ return 0;+}++/**+ * Where the chosen option stood in the finalize list. Tied options share a+ * rank, so picking one of several joint winners reads as rank 1, tied.+ */+export function getChosenOptionRank({+ options,+ participants,+ optionId,+}: {+ options: { id: string }[];+ participants: { id: string; votes: { optionId: string; type: VoteType }[] }[];+ optionId: string;+}) {+ const ranked = rankOptionsByPopularity({ options, participants });+ const chosen = ranked.find((option) => option.id === optionId);+ if (!chosen) {+ return {+ chosen_rank: null,+ chosen_tied: false,+ option_count: ranked.length,+ };+ }+ let better = 0;+ let tied = 0;+ for (const option of ranked) {+ if (option.id === optionId) continue;+ const order = comparePopularity(option.votes, chosen.votes);+ if (order < 0) better++;+ if (order === 0) tied++;+ }+ return {+ chosen_rank: better + 1,+ chosen_tied: tied > 0,+ option_count: ranked.length,+ };+}++const HOUR_MS = 3_600_000;+// Floating times are stored as wall clock in UTC; the viewer's zone can be+// up to 14 hours either side.+const FLOATING_SLACK_MS = 14 * HOUR_MS;++/**+ * How far ahead of finalize the event starts. A floating time (all day, or a+ * poll without a zone) only counts as past once it is past in every zone.+ */+export function getFinalizeTiming({+ start,+ end,+ allDay,+ timeZone,+ now,+}: {+ start: Date;+ end: Date;+ allDay: boolean;+ timeZone: string | null;+ now: Date;+}) {+ const floating = allDay || !timeZone;+ const pastAfter = allDay+ ? end.getTime() + FLOATING_SLACK_MS+ : start.getTime() + (floating ? FLOATING_SLACK_MS : 0);+ return {+ hours_to_start: Math.round((start.getTime() - now.getTime()) / HOUR_MS),+ is_past: now.getTime() > pastAfter,+ floating_time: floating,+ };}const notifyBuckets = {diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.tsindex c8fe76c84..924b122e8 100644--- a/apps/web/src/trpc/routers/polls.ts+++ b/apps/web/src/trpc/routers/polls.ts@@ -45,7 +45,9 @@ import {} from "@/features/poll/limit/mutations";import { MAX_POLL_DESCRIPTION_LENGTH } from "@/features/poll/schema";import {+ getChosenOptionRank,getFinalizePlanGate,+ getFinalizeTiming,summarizeNotifySelection,} from "@/features/poll/utils";import { queueReviewRequest } from "@/features/review-request/mutations";@@ -1185,6 +1187,7 @@ export const polls = router({timeFormat: true,},},+ options: { select: { id: true } },participants: {select: {id: true,@@ -1192,6 +1195,7 @@ export const polls = router({email: true,locale: true,timeZone: true,+ userId: true,user: {select: {email: true,@@ -1533,6 +1537,8 @@ export const polls = router({scheduleQueuedEmailDelivery({ batchId: scheduledEvent.id });+ const ownerId = poll.user.id;+ const ownerEmail = poll.user.email.trim().toLowerCase();track(ctx.user, {event: "poll_schedule",properties: {@@ -1547,6 +1553,25 @@ export const polls = router({optionId: input.optionId,notifyParticipantIds: input.notifyParticipantIds,}),+ ...getChosenOptionRank({+ options: poll.options,+ participants: poll.participants,+ optionId: input.optionId,+ }),+ ...getFinalizeTiming({+ start: eventTimes.start,+ end: eventTimes.end,+ allDay: eventTimes.allDay,+ timeZone: eventTimes.timeZone,+ now: new Date(),+ }),+ host_is_participant: poll.participants.some(+ (p) =>+ p.userId === ownerId ||+ p.email?.trim().toLowerCase() === ownerEmail,+ ),+ conferencing_type: conferencing?.provider ?? "none",+ has_location: Boolean(poll.location),},groups: {poll: poll.id,