Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
♻️ Fire analytics from event handlers instead of effects (#3510)
Luke Vella901503724251
diff --git a/apps/web/package.json b/apps/web/package.jsonindex 22598409d..ce5748fab 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -63,6 +63,7 @@"calendar-link": "^2.6.0","class-variance-authority": "^0.7.1","cookie": "^0.7.0",+ "copy-to-clipboard": "^3.3.3","dayjs": "^1.11.13","effect": "4.0.0","es-toolkit": "^1.39.9",diff --git a/apps/web/src/features/poll/components/desktop-poll.tsx b/apps/web/src/features/poll/components/desktop-poll.tsxindex c62f0a179..930cc9405 100644--- a/apps/web/src/features/poll/components/desktop-poll.tsx+++ b/apps/web/src/features/poll/components/desktop-poll.tsx@@ -4,7 +4,6 @@ import { cn } from "@rallly/ui";import { Badge } from "@rallly/ui/badge";import { Button } from "@rallly/ui/button";import { Card, CardHeader, CardTitle } from "@rallly/ui/card";-import { useDialog } from "@rallly/ui/dialog";import { Tooltip, TooltipContent, TooltipTrigger } from "@rallly/ui/tooltip";import {ArrowLeftIcon,@@ -34,7 +33,10 @@ import {usePoll,useRole,} from "@/features/poll/client";-import { SharePollDialog } from "@/features/poll/components/share-dialog";+import {+ SharePollDialog,+ useSharePollDialog,+} from "@/features/poll/components/share-dialog";import { useVisibleParticipants } from "@/features/poll/components/visibility";import { VotingFooter } from "@/features/poll/components/voting-footer";import { useVotingForm } from "@/features/poll/components/voting-form";@@ -198,12 +200,32 @@ function TableControls({);}-function DesktopPollEmptyState() {+// Only admins see it, and the share dialog reads the space tier, which public+// poll pages don't provide+function EmptyStateShareButton() {const poll = usePoll();+ const { dialog, open } = useSharePollDialog(poll.id);++ return (+ <>+ <Button {...dialog.triggerProps} onClick={() => open("empty_state")}>+ <Share2Icon data-icon="inline-start" />+ <Trans i18nKey="share" defaults="Share" />+ </Button>+ <SharePollDialog+ {...dialog.dialogProps}+ pollId={poll.id}+ pollStatus={poll.status}+ inviteLink={poll.inviteLink}+ />+ </>+ );+}++function DesktopPollEmptyState() {const role = useRole();const { canAddNewParticipant } = usePermissions();const votingForm = useVotingForm();- const shareDialog = useDialog();const canShare = role === "admin";return (@@ -245,21 +267,7 @@ function DesktopPollEmptyState() {<Trans i18nKey="addResponse" defaults="Add response" /></Button>) : null}- {canShare ? (- <>- <Button {...shareDialog.triggerProps}>- <Share2Icon data-icon="inline-start" />- <Trans i18nKey="share" defaults="Share" />- </Button>- <SharePollDialog- {...shareDialog.dialogProps}- pollId={poll.id}- pollStatus={poll.status}- inviteLink={poll.inviteLink}- source="empty_state"- />- </>- ) : null}+ {canShare ? <EmptyStateShareButton /> : null}</EmptyStateFooter>) : null}</EmptyState>diff --git a/apps/web/src/features/poll/components/invite-link-row.tsx b/apps/web/src/features/poll/components/invite-link-row.tsxindex 5eeaf0314..d06e6d30d 100644--- a/apps/web/src/features/poll/components/invite-link-row.tsx+++ b/apps/web/src/features/poll/components/invite-link-row.tsx@@ -2,9 +2,9 @@import { posthog } from "@rallly/posthog/client";import { Button } from "@rallly/ui/button";+import copy from "copy-to-clipboard";import { CheckIcon, CopyIcon, LinkIcon } from "lucide-react";import React from "react";-import { useCopyToClipboard } from "react-use";import { Trans, useTranslation } from "@/i18n/client";/**@@ -20,20 +20,16 @@ export function InviteLinkRow({}) {const { t } = useTranslation();const [didCopy, setDidCopy] = React.useState(false);- const [state, copyToClipboard] = useCopyToClipboard();- // react-use records a failed copy as `error` and a successful one as- // `value`; the button only claims success in the second case.- React.useEffect(() => {- if (state.error) {- console.error(`Unable to copy value: ${state.error.message}`);+ // The button only claims success when the copy actually went through+ const copyInviteLink = () => {+ if (!copy(inviteLink)) {+ console.error("Unable to copy invite link");return;}- if (state.value) {- setDidCopy(true);- posthog?.capture("poll_share:invite_link_copy", { poll_id: pollId });- }- }, [state, pollId]);+ setDidCopy(true);+ posthog?.capture("poll_share:invite_link_copy", { poll_id: pollId });+ };React.useEffect(() => {if (!didCopy) return;@@ -58,7 +54,7 @@ export function InviteLinkRow({<Buttonvariant="ghost"className="shrink-0 text-primary"- onClick={() => copyToClipboard(inviteLink)}+ onClick={copyInviteLink}>{didCopy ? (<CheckIcon data-icon="inline-start" />diff --git a/apps/web/src/features/poll/components/invitee-list.tsx b/apps/web/src/features/poll/components/invitee-list.tsxindex 8e9e78cec..d0a87f095 100644--- a/apps/web/src/features/poll/components/invitee-list.tsx+++ b/apps/web/src/features/poll/components/invitee-list.tsx@@ -12,14 +12,13 @@ import {} from "@rallly/ui/dropdown-menu";import { toast } from "@rallly/ui/sonner";import { useMutation } from "@tanstack/react-query";+import copy from "copy-to-clipboard";import {LinkIcon,MailIcon,MoreHorizontalIcon,Trash2Icon,} from "lucide-react";-import React from "react";-import { useCopyToClipboard } from "react-use";import { Spinner } from "@/components/spinner";import { usePoll } from "@/features/poll/client";import { revokePollInviteAction } from "@/features/poll/invite/actions";@@ -81,7 +80,6 @@ function InviteeRowMenu({}) {const poll = usePoll();const { t } = useTranslation();- const [state, copyToClipboard] = useCopyToClipboard();// The success handler refetches the list, which is what drops the row.const revoke = useMutation(@@ -110,11 +108,8 @@ function InviteeRowMenu({}),);- // react-use records a failed copy as `error` and a successful one as- // `value`; the toast only claims success in the second case.- React.useEffect(() => {- if (state.error) {- console.error(`Unable to copy value: ${state.error.message}`);+ const copyInviteUrl = () => {+ if (!copy(inviteUrl)) {toast.error(t("shareDialogCopyFailed", {defaultValue: "Couldn't copy the link. Try again.",@@ -122,19 +117,17 @@ function InviteeRowMenu({);return;}- if (state.value) {- toast(- t("shareDialogPersonalLinkCopied", {- defaultValue: "Personal link for {email} copied",- email,- }),- );- posthog?.capture("poll_share:invitee_link_copy", {- poll_id: poll.id,- status,- });- }- }, [state, email, status, poll.id, t]);+ toast(+ t("shareDialogPersonalLinkCopied", {+ defaultValue: "Personal link for {email} copied",+ email,+ }),+ );+ posthog?.capture("poll_share:invitee_link_copy", {+ poll_id: poll.id,+ status,+ });+ };return (<DropdownMenu>@@ -154,7 +147,7 @@ function InviteeRowMenu({<MoreHorizontalIcon className="text-muted-foreground" /></DropdownMenuTrigger><DropdownMenuContent align="end">- <DropdownMenuItem onClick={() => copyToClipboard(inviteUrl)}>+ <DropdownMenuItem onClick={copyInviteUrl}><LinkIcon /><Transi18nKey="shareDialogCopyPersonalLink"diff --git a/apps/web/src/features/poll/components/participant-dropdown.tsx b/apps/web/src/features/poll/components/participant-dropdown.tsxindex 46a6fdf63..fb0f464bc 100644--- a/apps/web/src/features/poll/components/participant-dropdown.tsx+++ b/apps/web/src/features/poll/components/participant-dropdown.tsx@@ -28,13 +28,13 @@ import {} from "@rallly/ui/form";import { Input } from "@rallly/ui/input";import { toast } from "@rallly/ui/sonner";+import copy from "copy-to-clipboard";import { LinkIcon, PencilIcon, TagIcon, TrashIcon } from "lucide-react";import React from "react";import type { SubmitHandler } from "react-hook-form";import { useForm } from "react-hook-form";-import { useCopyToClipboard, useMount } from "react-use";+import { useMount } from "react-use";import * as z from "zod";-import { usePoll } from "@/features/poll/client";import {useDeleteParticipant,@@ -147,13 +147,9 @@ const CopyEditLinkMenuItem = ({}) => {const poll = usePoll();const { t } = useTranslation();- const [state, copyToClipboard] = useCopyToClipboard();- // react-use records a failed copy as `error` and a successful one as- // `value`; the toast only claims success in the second case.- React.useEffect(() => {- if (state.error) {- console.error(`Unable to copy value: ${state.error.message}`);+ const copyEditLink = () => {+ if (!copy(editUrl)) {toast.error(t("participantEditLinkCopyFailed", {defaultValue: "Couldn't copy the edit link. Try again.",@@ -161,21 +157,19 @@ const CopyEditLinkMenuItem = ({);return;}- if (state.value) {- toast(- t("participantEditLinkCopied", {- defaultValue: "Edit link for {name} copied",- name: participantName,- }),- );- posthog?.capture("poll_page:participant_edit_link_copy", {- poll_id: poll.id,- });- }- }, [state, participantName, poll.id, t]);+ toast(+ t("participantEditLinkCopied", {+ defaultValue: "Edit link for {name} copied",+ name: participantName,+ }),+ );+ posthog?.capture("poll_page:participant_edit_link_copy", {+ poll_id: poll.id,+ });+ };return (- <DropdownMenuItem onClick={() => copyToClipboard(editUrl)}>+ <DropdownMenuItem onClick={copyEditLink}><LinkIcon /><Trans i18nKey="copyEditLink" defaults="Copy edit link" /></DropdownMenuItem>diff --git a/apps/web/src/features/poll/components/share-dialog.tsx b/apps/web/src/features/poll/components/share-dialog.tsxindex f373a9a1d..ea04d727c 100644--- a/apps/web/src/features/poll/components/share-dialog.tsx+++ b/apps/web/src/features/poll/components/share-dialog.tsx@@ -23,6 +23,23 @@ import { useFlash } from "@/lib/flash/client";type ShareSource = "poll_created" | "manual" | "empty_state";+/** Dialog state for SharePollDialog whose `open` records how it was opened. */+export function useSharePollDialog(pollId: string) {+ const dialog = useDialog();+ const isFree = useIsFree();++ const open = (source: ShareSource) => {+ posthog?.capture("poll_share:dialog_open", {+ poll_id: pollId,+ tier: isFree ? "free" : "pro",+ source,+ });+ dialog.trigger();+ };++ return { dialog, open };+}+export function ShareDialog({pollId,pollStatus,@@ -34,9 +51,8 @@ export function ShareDialog({inviteLink: string;variant?: "primary" | "default";}) {- const dialog = useDialog();+ const { dialog, open } = useSharePollDialog(pollId);const sharePollFlash = useFlash(SHARE_POLL_FLASH_KEY);- const [source, setSource] = React.useState<ShareSource>("manual");// 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@@ -45,8 +61,7 @@ export function ShareDialog({// biome-ignore lint/correctness/useExhaustiveDependencies: runs once per flash arrivalReact.useEffect(() => {if (sharePollFlash !== pollId) return;- setSource("poll_created");- dialog.trigger();+ open("poll_created");}, [sharePollFlash]);return (@@ -54,10 +69,7 @@ export function ShareDialog({<Buttonvariant={variant}{...dialog.triggerProps}- onClick={() => {- setSource("manual");- dialog.trigger();- }}+ onClick={() => open("manual")}><Share2Icon data-icon="inline-start" /><span className="sr-only sm:not-sr-only">@@ -69,7 +81,6 @@ export function ShareDialog({pollId={pollId}pollStatus={pollStatus}inviteLink={inviteLink}- source={source}/></>);@@ -79,27 +90,12 @@ export function SharePollDialog({pollId,pollStatus,inviteLink,- source,...dialogProps}: React.ComponentProps<typeof Dialog> & {pollId: string;pollStatus: PollStatus;inviteLink: string;- source: ShareSource;}) {- const isFree = useIsFree();- const isOpen = dialogProps.open;-- // biome-ignore lint/correctness/useExhaustiveDependencies: capture once per open- React.useEffect(() => {- if (!isOpen) return;- posthog?.capture("poll_share:dialog_open", {- poll_id: pollId,- tier: isFree ? "free" : "pro",- source,- });- }, [isOpen]);-return (<Dialog {...dialogProps}><DialogContent size="lg" data-testid="invite-participant-dialog">diff --git a/apps/web/src/features/poll/limit/client.tsx b/apps/web/src/features/poll/limit/client.tsxindex 9f06dc65e..e4c2871cd 100644--- a/apps/web/src/features/poll/limit/client.tsx+++ b/apps/web/src/features/poll/limit/client.tsx@@ -1,5 +1,6 @@"use client";+import { posthog } from "@rallly/posthog/client";import { create } from "zustand";import type { OpenPollUsage } from "./types";@@ -21,11 +22,18 @@ type OpenPollLimitStore = {export const useOpenPollLimitStore = create<OpenPollLimitStore>((set) => ({isOpen: false,content: null,- show: (source, usage) =>+ show: (source, usage) => {+ posthog?.capture("poll_limit:wall_view", {+ tier: usage.tier,+ open_count: usage.openCount,+ limit: usage.limit,+ source,+ });set({isOpen: true,content: { source, usage, pathname: window.location.pathname },- }),+ });+ },hide: () => set({ isOpen: false }),}));diff --git a/apps/web/src/features/poll/limit/components/open-poll-limit-dialog.tsx b/apps/web/src/features/poll/limit/components/open-poll-limit-dialog.tsxindex 812227a27..95e06baf7 100644--- a/apps/web/src/features/poll/limit/components/open-poll-limit-dialog.tsx+++ b/apps/web/src/features/poll/limit/components/open-poll-limit-dialog.tsx@@ -92,10 +92,6 @@ function OpenPollLimitContent({[data, source],);- React.useEffect(() => {- posthog?.capture("poll_limit:wall_view", eventProperties);- }, [eventProperties]);-if (data.limit === null) {return null;}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 5dde9737c..3a6cc5894 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -318,6 +318,9 @@ importers:cookie:specifier: ^0.7.0version: 0.7.2+ copy-to-clipboard:+ specifier: ^3.3.3+ version: 3.3.3dayjs:specifier: ^1.11.13version: 1.11.19