Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
✨ Open the Share dialog after creation through a cookie flash (#3164)
Luke Vellaba8ded412283
diff --git a/apps/web/src/features/poll/components/create-poll.tsx b/apps/web/src/features/poll/components/create-poll.tsxindex fe43d390a..6ee1aba69 100644--- a/apps/web/src/features/poll/components/create-poll.tsx+++ b/apps/web/src/features/poll/components/create-poll.tsx@@ -22,9 +22,11 @@ import { PollDetailsForm } from "@/features/poll/components/forms/poll-details-fimport PollOptionsForm from "@/features/poll/components/forms/poll-options-form/poll-options-form";import { PollSettingsForm } from "@/features/poll/components/forms/poll-settings";import type { NewEventData } from "@/features/poll/components/forms/types";+import { SHARE_POLL_FLASH_KEY } from "@/features/poll/constants";import { useUser } from "@/features/user/client";import { UserDropdown } from "@/features/user/components/user-dropdown";import { Trans, useTranslation } from "@/i18n/client";+import { setFlash } from "@/lib/flash/client";import { getBrowserTimeZone } from "@/lib/utils/date-time-utils";import { trpc } from "@/trpc/client";@@ -235,7 +237,8 @@ export const CreatePoll = ({ nav }: { nav?: React.ReactNode }) => {clear();form.reset();setCreatedPollId(res.data.id);- router.push(`/poll/${res.data.id}?share=1`);+ setFlash(SHARE_POLL_FLASH_KEY, res.data.id);+ router.push(`/poll/${res.data.id}`);} else {toast.error(t("inappropriateContent", {diff --git a/apps/web/src/features/poll/components/share-dialog.tsx b/apps/web/src/features/poll/components/share-dialog.tsxindex dc7bd6807..e3123b7d5 100644--- a/apps/web/src/features/poll/components/share-dialog.tsx+++ b/apps/web/src/features/poll/components/share-dialog.tsx@@ -12,35 +12,34 @@ import {} from "@rallly/ui/dialog";import { Separator } from "@rallly/ui/separator";import { Share2Icon } from "lucide-react";-import { useSearchParams } from "next/navigation";import React from "react";import { useIsFree } from "@/features/billing/client";import { usePoll } from "@/features/poll/client";import { InviteByEmail } from "@/features/poll/components/invite-by-email";import { InviteLinkRow } from "@/features/poll/components/invite-link-row";+import { SHARE_POLL_FLASH_KEY } from "@/features/poll/constants";import type { PollInviteListItem } from "@/features/poll/invite/types";import { Trans } from "@/i18n/client";+import { useFlash } from "@/lib/flash/client";export function ShareDialog({ invites }: { invites: PollInviteListItem[] }) {const poll = usePoll();const dialog = useDialog();const isFree = useIsFree();const isOpen = dialog.dialogProps.open;- const hasShareParam = useSearchParams().has("share");+ const sharePollFlash = useFlash(SHARE_POLL_FLASH_KEY);const openSource = React.useRef<"poll_created" | "manual">("manual");- // The create page redirects here with ?share so the dialog is the- // confirmation. Drop the param once consumed so refresh and back don't- // reopen it; replaceState keeps the layout mounted and the dialog open.- // biome-ignore lint/correctness/useExhaustiveDependencies: runs once per param arrival+ // The create page flashes the new poll's id so this dialog is the+ // confirmation. The flash is consumed on read, so refresh and back don't+ // reopen it. Keyed on the flash alone so a later poll id change can't+ // replay it.+ // biome-ignore lint/correctness/useExhaustiveDependencies: runs once per flash arrivalReact.useEffect(() => {- if (!hasShareParam) return;+ if (sharePollFlash !== poll.id) return;openSource.current = "poll_created";dialog.trigger();- const url = new URL(window.location.href);- url.searchParams.delete("share");- window.history.replaceState(window.history.state, "", url);- }, [hasShareParam]);+ }, [sharePollFlash]);React.useEffect(() => {if (!isOpen) return;diff --git a/apps/web/src/features/poll/constants.ts b/apps/web/src/features/poll/constants.tsindex de297c96b..e496f4c19 100644--- a/apps/web/src/features/poll/constants.ts+++ b/apps/web/src/features/poll/constants.ts@@ -1 +1,5 @@export const MAX_POLL_OPTIONS = 100;++// Flashed by the create page with the new poll's id; the poll page opens+// the Share dialog when it matches.+export const SHARE_POLL_FLASH_KEY = "share-poll";diff --git a/apps/web/src/lib/flash/client.test.tsx b/apps/web/src/lib/flash/client.test.tsxnew file mode 100644index 000000000..0768024c2--- /dev/null+++ b/apps/web/src/lib/flash/client.test.tsx@@ -0,0 +1,26 @@+import { renderHook } from "@testing-library/react";+import Cookies from "js-cookie";+import { setFlash, useFlash } from "@/lib/flash/client";+import { flashCookieName } from "@/lib/flash/constants";++describe("useFlash", () => {+ beforeEach(() => {+ Cookies.remove(flashCookieName("test-key"), { path: "/" });+ });++ it("returns the value once and clears it", async () => {+ setFlash("test-key", "poll_1");++ const first = renderHook(() => useFlash("test-key"));+ await vi.waitFor(() => expect(first.result.current).toBe("poll_1"));+ expect(Cookies.get(flashCookieName("test-key"))).toBeUndefined();++ const second = renderHook(() => useFlash("test-key"));+ expect(second.result.current).toBeNull();+ });++ it("stays null when nothing was flashed", () => {+ const { result } = renderHook(() => useFlash("test-key"));+ expect(result.current).toBeNull();+ });+});diff --git a/apps/web/src/lib/flash/client.tsx b/apps/web/src/lib/flash/client.tsxnew file mode 100644index 000000000..459428610--- /dev/null+++ b/apps/web/src/lib/flash/client.tsx@@ -0,0 +1,30 @@+"use client";++import Cookies from "js-cookie";+import React from "react";+import { FLASH_MAX_AGE, flashCookieName } from "@/lib/flash/constants";++export function setFlash(key: string, value: string) {+ Cookies.set(flashCookieName(key), value, {+ path: "/",+ sameSite: "lax",+ secure: window.location.protocol === "https:",+ expires: new Date(Date.now() + FLASH_MAX_AGE * 1000),+ });+}++// Resolves to the flash value on the first mount after it was set and clears+// it in the same step, so a reload or a later visit never replays it.+export function useFlash(key: string) {+ const [value, setValue] = React.useState<string | null>(null);++ React.useEffect(() => {+ const name = flashCookieName(key);+ const flash = Cookies.get(name);+ if (flash === undefined) return;+ Cookies.remove(name, { path: "/" });+ setValue(flash);+ }, [key]);++ return value;+}diff --git a/apps/web/src/lib/flash/constants.ts b/apps/web/src/lib/flash/constants.tsnew file mode 100644index 000000000..d59d254d8--- /dev/null+++ b/apps/web/src/lib/flash/constants.ts@@ -0,0 +1,9 @@+// One-shot UI intent carried across a navigation, in the spirit of Remix's+// session.flash(): set before the redirect, read once on the next page, gone.+// Host-only and script-readable so the consuming client can clear it; the TTL+// bounds a flash nothing ever consumed. Consumers own their keys.+export const FLASH_MAX_AGE = 5 * 60;++export function flashCookieName(key: string) {+ return `rallly_flash_${key}`;+}diff --git a/apps/web/src/lib/flash/server.ts b/apps/web/src/lib/flash/server.tsnew file mode 100644index 000000000..d0fedfea3--- /dev/null+++ b/apps/web/src/lib/flash/server.ts@@ -0,0 +1,17 @@+import "server-only";++import { cookies } from "next/headers";+import { FLASH_MAX_AGE, flashCookieName } from "@/lib/flash/constants";++// Server actions and route handlers only: a server component render cannot+// write cookies.+export async function setFlash(key: string, value: string) {+ const cookieStore = await cookies();+ cookieStore.set(flashCookieName(key), value, {+ path: "/",+ maxAge: FLASH_MAX_AGE,+ sameSite: "lax",+ httpOnly: false,+ secure: process.env.NEXT_PUBLIC_BASE_URL?.startsWith("https://"),+ });+}diff --git a/apps/web/tests/create-delete-poll.spec.ts b/apps/web/tests/create-delete-poll.spec.tsindex 584bd6bfb..2f2bc9286 100644--- a/apps/web/tests/create-delete-poll.spec.ts+++ b/apps/web/tests/create-delete-poll.spec.ts@@ -14,7 +14,8 @@ test.describe.serial(() => {test("create a new poll", async () => {const newPollPage = new NewPollPage(page);await newPollPage.goto();- await newPollPage.create({ name: "Monthly Meetup" });+ const pollPage = await newPollPage.create({ name: "Monthly Meetup" });+ await pollPage.closeShareDialog();await expect(page.getByRole("heading", { name: "Monthly Meetup" }),diff --git a/apps/web/tests/edit-options.spec.ts b/apps/web/tests/edit-options.spec.tsindex c1ed95dac..b7cb9212c 100644--- a/apps/web/tests/edit-options.spec.ts+++ b/apps/web/tests/edit-options.spec.ts@@ -12,6 +12,7 @@ test.describe("edit options", () => {const newPollPage = new NewPollPage(page);await newPollPage.goto();const pollPage = await newPollPage.create({ name: "Monthly Meetup" });+ await pollPage.closeShareDialog();await pollPage.addParticipant("Mark");editOptionsPage = await pollPage.editOptions();});diff --git a/apps/web/tests/email-invites.spec.ts b/apps/web/tests/email-invites.spec.tsindex 050ac772b..992b244a6 100644--- a/apps/web/tests/email-invites.spec.ts+++ b/apps/web/tests/email-invites.spec.ts@@ -18,6 +18,7 @@ import {const PRO_HOST = "[email protected]";const FREE_HOST = "[email protected]";+const CLEAN_URL_HOST = "[email protected]";const INVITEE = "[email protected]";const POLL_TITLE = "Email Invites Poll";@@ -25,7 +26,7 @@ test.describe.configure({ mode: "serial" });async function cleanup() {await prisma.user.deleteMany({- where: { email: { in: [PRO_HOST, FREE_HOST] } },+ where: { email: { in: [PRO_HOST, FREE_HOST, CLEAN_URL_HOST] } },});}@@ -36,14 +37,10 @@ async function openAsGuest(browser: Browser, inviteUrl: string) {return { guestPage, close: () => context.close() };}+// The invite list is server data, so a guest's activity only shows after+// the host's page reloads.async function reopenShareDialog(page: Page) {- // Load the poll page without the post-creation `share` param: the dialog- // strips it with a raw replaceState the router does not track, and a- // router refresh (dev HMR, revalidation) can bring it back before a- // reload, which would reopen the dialog and block the Share button.- const url = new URL(page.url());- url.searchParams.delete("share");- await page.goto(url.toString());+ await page.reload();await page.getByRole("button", { name: "Share" }).click();return page.getByRole("dialog", { name: "Share" });}@@ -73,7 +70,6 @@ test.describe("Email invites", () => {await newPollPage.create({ name: POLL_TITLE });await deleteAllMessages();- await page.getByRole("button", { name: "Share" }).click();const dialog = page.getByRole("dialog", { name: "Share" });await expect(dialog.getByRole("button", { name: "Copy" })).toBeVisible();@@ -161,6 +157,41 @@ test.describe("Email invites", () => {await expect(respondedRow.getByText("Responded")).toBeVisible();});+ test("creation opens the dialog and leaves the URL clean", async ({+ page,+ }) => {+ const user = await createUserInDb({+ email: CLEAN_URL_HOST,+ name: "Clean URL Host",+ });+ const space = await prisma.space.findFirstOrThrow({+ where: { ownerId: user.id },+ });+ await upgradeSpaceToPro({ spaceId: space.id, userId: user.id, seats: 1 });++ await loginWithEmail(page, { email: CLEAN_URL_HOST });+ const newPollPage = new NewPollPage(page);+ await newPollPage.goto();+ await newPollPage.create({ name: `${POLL_TITLE} Clean URL` });++ const dialog = page.getByRole("dialog", { name: "Share" });+ await expect(page).toHaveURL(/\/poll\/[^/?]+$/);++ // Sending an invite refreshes the route; a router sync must not put the+ // consumed param back on the address bar.+ const field = dialog.getByLabel("Email address");+ await field.fill(INVITEE);+ await field.press("Enter");+ const row = dialog.getByRole("listitem").filter({ hasText: INVITEE });+ await expect(row.getByText("Sent")).toBeVisible();+ await expect(page).toHaveURL(/\/poll\/[^/?]+$/);+ await expect(dialog).toBeVisible();++ await page.reload();+ await expect(page.getByRole("button", { name: "Share" })).toBeVisible();+ await expect(dialog).toBeHidden();+ });+test("free host sees the pay wall and nothing is sent", async ({ page }) => {await createUserInDb({ email: FREE_HOST, name: "Free Host" });await loginWithEmail(page, { email: FREE_HOST });@@ -168,7 +199,6 @@ test.describe("Email invites", () => {await newPollPage.goto();await newPollPage.create({ name: `${POLL_TITLE} Free` });- await page.getByRole("button", { name: "Share" }).click();const dialog = page.getByRole("dialog", { name: "Share" });const sendButton = dialog.getByRole("button", { name: /Send invite/ });await expect(sendButton).toBeEnabled();diff --git a/apps/web/tests/guest-to-user-migration.spec.ts b/apps/web/tests/guest-to-user-migration.spec.tsindex 55d070634..da7282df3 100644--- a/apps/web/tests/guest-to-user-migration.spec.ts+++ b/apps/web/tests/guest-to-user-migration.spec.ts@@ -30,7 +30,8 @@ test.describe.serial(() => {// Step 1: Create a poll as guestconst newPollPage = new NewPollPage(page);await newPollPage.goto();- await newPollPage.create({ name: "Monthly Meetup" });+ const pollPage = await newPollPage.create({ name: "Monthly Meetup" });+ await pollPage.closeShareDialog();// Step 2: Navigate to registration (redirects to the combined login page)await page.click("text=Create an account");@@ -47,7 +48,8 @@ test.describe.serial(() => {// Step 1: Create a poll as guestconst newPollPage = new NewPollPage(page);await newPollPage.goto();- await newPollPage.create({ name: "Board Meeting" });+ const pollPage = await newPollPage.create({ name: "Board Meeting" });+ await pollPage.closeShareDialog();await expect(page.getByRole("heading", { name: "Board Meeting" }),).toBeVisible();diff --git a/apps/web/tests/list-unsubscribe.spec.ts b/apps/web/tests/list-unsubscribe.spec.tsindex 286b5583b..2aa763a96 100644--- a/apps/web/tests/list-unsubscribe.spec.ts+++ b/apps/web/tests/list-unsubscribe.spec.ts@@ -64,7 +64,7 @@ test.describe("List-Unsubscribe", () => {expect(pollId).not.toBe("");inviteUrl = await pollPage.copyInviteLink();- await pollPage.closeDialog();+ await pollPage.closeShareDialog();await page.close();// Drop the login OTP so the first capture is the notification.diff --git a/apps/web/tests/new-poll-page.ts b/apps/web/tests/new-poll-page.tsindex 51dfd94fc..8d77941ba 100644--- a/apps/web/tests/new-poll-page.ts+++ b/apps/web/tests/new-poll-page.ts@@ -51,14 +51,10 @@ export class NewPollPage {await page.getByRole("button", { name: /^create poll$/i }).click();- // Creation redirects to the poll page with the Share dialog open; the- // poll page is what callers want, so dismiss the dialog before returning.+ // The Share dialog opening on the poll page is the success signal, so+ // assert it here; callers that need the page behind it close it.await page.waitForURL(/\/poll\/[^/?]+/);- const pollPage = new PollPage(page);- await pollPage.closeDialog();- await page- .getByRole("dialog", { name: "Share" })- .waitFor({ state: "hidden" });- return pollPage;+ await expect(page.getByRole("dialog", { name: "Share" })).toBeVisible();+ return new PollPage(page);}}diff --git a/apps/web/tests/poll-page.ts b/apps/web/tests/poll-page.tsindex 5bbac93d2..c9e82b07e 100644--- a/apps/web/tests/poll-page.ts+++ b/apps/web/tests/poll-page.ts@@ -6,18 +6,10 @@ import { InvitePage } from "./invite-page";export class PollPage {constructor(public readonly page: Page) {}- async closeDialog() {- const page = this.page;-- const dialog = page.getByRole("dialog");-- await dialog.waitFor({ state: "visible" });-- const closeDialogButton = dialog.getByRole("button", { name: "Close" });-- await closeDialogButton.waitFor({ state: "visible" });-- await closeDialogButton.click();+ async closeShareDialog() {+ const dialog = this.page.getByRole("dialog", { name: "Share" });+ await dialog.getByRole("button", { name: "Close" }).click();+ await expect(dialog).toBeHidden();}async addComment() {@@ -33,11 +25,12 @@ export class PollPage {}async openShareDialog() {- const page = this.page;-- await page.getByRole("button", { name: "Share" }).click();-- return page.getByRole("dialog");+ const dialog = this.page.getByRole("dialog", { name: "Share" });+ if (!(await dialog.isVisible())) {+ await this.page.getByRole("button", { name: "Share" }).click();+ }+ await expect(dialog).toBeVisible();+ return dialog;}async copyInviteLink() {diff --git a/apps/web/tests/vote-and-comment.spec.ts b/apps/web/tests/vote-and-comment.spec.tsindex 207e58b29..4c954cde9 100644--- a/apps/web/tests/vote-and-comment.spec.ts+++ b/apps/web/tests/vote-and-comment.spec.ts@@ -20,6 +20,7 @@ test.describe(() => {name: "Monthly Meetup",enableComments: true,});+ await pollPage.closeShareDialog();// Extract the poll ID from the URLconst url = page.url();@@ -42,7 +43,7 @@ test.describe(() => {test("copy participant link", async () => {const inviteLink = await pollPage.copyInviteLink();- await pollPage.closeDialog();+ await pollPage.closeShareDialog();expect(inviteLink).toMatch(/\/invite\/[a-zA-Z0-9]+/);});