Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
♻️ Extract shared Setting primitives and adopt on notifications page (#2836)
Luke Vellaa7fbfbd5caa7
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex 6713656ca..1facff2d9 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -492,7 +492,9 @@"notificationToggleMutedToast": "Notifications are off for this poll","notificationToggleUnmutedToast": "Notifications are on for this poll","notifyNewComment": "New comment",+ "notifyNewCommentDescription": "Receive an email when someone comments on your poll.","notifyNewResponse": "New response",+ "notifyNewResponseDescription": "Receive an email when a participant submits a response.","noUsers": "No users found","noUsersDescription": "Try adjusting your search","noVotes": "No one has voted for this option",diff --git a/apps/web/src/app/[locale]/(space)/settings/notifications/notifications-page.tsx b/apps/web/src/app/[locale]/(space)/settings/notifications/notifications-page.tsxindex f1cb0aebd..fb73179fb 100644--- a/apps/web/src/app/[locale]/(space)/settings/notifications/notifications-page.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/notifications/notifications-page.tsx@@ -1,6 +1,7 @@"use client";import { Switch } from "@rallly/ui/switch";+import { InboxIcon, MessageCircleIcon } from "lucide-react";import {PageSection,PageSectionContent,@@ -9,6 +10,14 @@ import {PageSectionHeader,PageSectionTitle,} from "@/components/page-layout";+import {+ Setting,+ SettingControl,+ SettingDescription,+ SettingIcon,+ SettingsGroup,+ SettingTitle,+} from "@/components/setting";import { Trans } from "@/i18n/client";import { trpc } from "@/trpc/client";@@ -49,36 +58,58 @@ export function NotificationsPage() {</PageSectionDescription></PageSectionHeader><PageSectionContent>- <div className="grid gap-4">- <div className="flex items-center justify-between gap-4">- <span className="font-medium text-sm">+ <SettingsGroup>+ <Setting>+ <SettingIcon>+ <InboxIcon />+ </SettingIcon>+ <SettingTitle><Trans i18nKey="notifyNewResponse" defaults="New response" />- </span>- <Switch- checked={preferences["poll.response.submitted"]}- onCheckedChange={(enabled) => {- updatePreference.mutate({- eventType: "poll.response.submitted",- enabled,- });- }}- />- </div>- <div className="flex items-center justify-between gap-4">- <span className="font-medium text-sm">+ </SettingTitle>+ <SettingDescription>+ <Trans+ i18nKey="notifyNewResponseDescription"+ defaults="Receive an email when a participant submits a response."+ />+ </SettingDescription>+ <SettingControl>+ <Switch+ checked={preferences["poll.response.submitted"]}+ onCheckedChange={(enabled) => {+ updatePreference.mutate({+ eventType: "poll.response.submitted",+ enabled,+ });+ }}+ />+ </SettingControl>+ </Setting>+ <Setting>+ <SettingIcon>+ <MessageCircleIcon />+ </SettingIcon>+ <SettingTitle><Trans i18nKey="notifyNewComment" defaults="New comment" />- </span>- <Switch- checked={preferences["poll.comment.added"]}- onCheckedChange={(enabled) => {- updatePreference.mutate({- eventType: "poll.comment.added",- enabled,- });- }}- />- </div>- </div>+ </SettingTitle>+ <SettingDescription>+ <Trans+ i18nKey="notifyNewCommentDescription"+ defaults="Receive an email when someone comments on your poll."+ />+ </SettingDescription>+ <SettingControl>+ <Switch+ checked={preferences["poll.comment.added"]}+ onCheckedChange={(enabled) => {+ updatePreference.mutate({+ eventType: "poll.comment.added",+ enabled,+ });+ }}+ />+ </SettingControl>+ </Setting>+ </SettingsGroup></PageSectionContent></PageSection></PageSectionGroup>diff --git a/apps/web/src/components/setting.tsx b/apps/web/src/components/setting.tsxnew file mode 100644index 000000000..200083fb4--- /dev/null+++ b/apps/web/src/components/setting.tsx@@ -0,0 +1,107 @@+"use client";++import { InfoIcon } from "lucide-react";+import React from "react";+import { PageIcon } from "@/components/page-icons";++const SettingContext = React.createContext<{+ titleId: string;+ descriptionId: string;+ hintId: string;+} | null>(null);++const useSetting = () => {+ const context = React.useContext(SettingContext);+ if (!context) {+ throw new Error("Setting components must be used within <Setting>");+ }+ return context;+};++export const SettingsGroup = ({ children }: React.PropsWithChildren) => {+ return <div className="divide-y">{children}</div>;+};++export const Setting = ({ children }: React.PropsWithChildren) => {+ const titleId = React.useId();+ const descriptionId = React.useId();+ const hintId = React.useId();+ const value = React.useMemo(+ () => ({ titleId, descriptionId, hintId }),+ [titleId, descriptionId, hintId],+ );++ return (+ <SettingContext.Provider value={value}>+ {/* biome-ignore lint/a11y/noLabelWithoutControl: control is rendered inside */}+ <label className="grid cursor-pointer select-none grid-cols-[1fr_auto] gap-x-3 py-4 [grid-template-areas:'title_control'_'description_control'_'hint_hint'] first:pt-0 last:pb-0 sm:grid-cols-[auto_1fr_auto] sm:[grid-template-areas:'icon_title_control'_'icon_description_control'_'icon_hint_hint']">+ {children}+ </label>+ </SettingContext.Provider>+ );+};++export const SettingIcon = ({ children }: React.PropsWithChildren) => {+ return (+ <div className="hidden self-start [grid-area:icon] sm:block">+ <PageIcon size="lg">{children}</PageIcon>+ </div>+ );+};++export const SettingTitle = ({ children }: React.PropsWithChildren) => {+ const { titleId } = useSetting();+ return (+ <div+ id={titleId}+ className="flex items-center gap-x-2 text-sm [grid-area:title]"+ >+ {children}+ </div>+ );+};++export const SettingDescription = ({ children }: React.PropsWithChildren) => {+ const { descriptionId } = useSetting();+ return (+ <div+ id={descriptionId}+ className="mt-0.5 text-muted-foreground text-xs [grid-area:description]"+ >+ {children}+ </div>+ );+};++export const SettingControl = ({+ children,+}: {+ children: React.ReactElement<{+ "aria-labelledby"?: string;+ "aria-describedby"?: string;+ }>;+}) => {+ const { titleId, descriptionId, hintId } = useSetting();+ return (+ <div className="self-start justify-self-end [grid-area:control]">+ {/* A dangling hint id is ignored by assistive tech while no hint is rendered */}+ {React.cloneElement(children, {+ "aria-labelledby": titleId,+ "aria-describedby": `${descriptionId} ${hintId}`,+ })}+ </div>+ );+};++export const SettingHint = ({ children }: React.PropsWithChildren) => {+ const { hintId } = useSetting();+ return (+ <div+ id={hintId}+ className="mt-4 flex items-start gap-x-1.5 text-muted-foreground text-xs [grid-area:hint]"+ >+ <InfoIcon className="size-3.5 shrink-0 translate-y-px" />+ <span>{children}</span>+ </div>+ );+};diff --git a/apps/web/src/features/poll/components/forms/poll-settings.tsx b/apps/web/src/features/poll/components/forms/poll-settings.tsxindex 39aa1ed62..e34213daf 100644--- a/apps/web/src/features/poll/components/forms/poll-settings.tsx+++ b/apps/web/src/features/poll/components/forms/poll-settings.tsx@@ -7,117 +7,25 @@ import { FormField } from "@rallly/ui/form";import { Switch } from "@rallly/ui/switch";import {BarChart2Icon,- InfoIcon,MailIcon,MessageCircleIcon,VenetianMaskIcon,} from "lucide-react";-import React from "react";import { useFormContext } from "react-hook-form";-import { PageIcon } from "@/components/page-icons";+import {+ Setting,+ SettingControl,+ SettingDescription,+ SettingHint,+ SettingIcon,+ SettingsGroup,+ SettingTitle,+} from "@/components/setting";import { showPayWall, useIsFree } from "@/features/billing/client";import { ProBadge } from "@/features/billing/components/pro-badge";import type { PollSettingsFormData } from "@/features/poll/components/forms/types";import { Trans } from "@/i18n/client";-const PollSettingContext = React.createContext<{- titleId: string;- descriptionId: string;- hintId: string;-} | null>(null);--const usePollSetting = () => {- const context = React.useContext(PollSettingContext);- if (!context) {- throw new Error("PollSetting components must be used within <PollSetting>");- }- return context;-};--const PollSetting = ({ children }: React.PropsWithChildren) => {- const titleId = React.useId();- const descriptionId = React.useId();- const hintId = React.useId();- const value = React.useMemo(- () => ({ titleId, descriptionId, hintId }),- [titleId, descriptionId, hintId],- );-- return (- <PollSettingContext.Provider value={value}>- {/* biome-ignore lint/a11y/noLabelWithoutControl: switch is rendered inside */}- <label className="grid cursor-pointer select-none grid-cols-[1fr_auto] gap-x-3 py-4 [grid-template-areas:'title_control'_'description_control'_'hint_hint'] first:pt-0 last:pb-0 sm:grid-cols-[auto_1fr_auto] sm:[grid-template-areas:'icon_title_control'_'icon_description_control'_'icon_hint_hint']">- {children}- </label>- </PollSettingContext.Provider>- );-};--const PollSettingIcon = ({ children }: React.PropsWithChildren) => {- return (- <div className="hidden self-start [grid-area:icon] sm:block">- <PageIcon size="lg">{children}</PageIcon>- </div>- );-};--const PollSettingTitle = ({ children }: React.PropsWithChildren) => {- const { titleId } = usePollSetting();- return (- <div- id={titleId}- className="flex items-center gap-x-2 text-sm [grid-area:title]"- >- {children}- </div>- );-};--const PollSettingDescription = ({ children }: React.PropsWithChildren) => {- const { descriptionId } = usePollSetting();- return (- <div- id={descriptionId}- className="mt-0.5 text-muted-foreground text-xs [grid-area:description]"- >- {children}- </div>- );-};--const PollSettingControl = ({- children,-}: {- children: React.ReactElement<{- "aria-labelledby"?: string;- "aria-describedby"?: string;- }>;-}) => {- const { titleId, descriptionId, hintId } = usePollSetting();- return (- <div className="self-start justify-self-end [grid-area:control]">- {/* A dangling hint id is ignored by assistive tech while no hint is rendered */}- {React.cloneElement(children, {- "aria-labelledby": titleId,- "aria-describedby": `${descriptionId} ${hintId}`,- })}- </div>- );-};--const PollSettingHint = ({ children }: React.PropsWithChildren) => {- const { hintId } = usePollSetting();- return (- <div- id={hintId}- className="mt-4 flex items-start gap-x-1.5 text-muted-foreground text-xs [grid-area:hint]"- >- <InfoIcon className="size-3.5 shrink-0 translate-y-px" />- <span>{children}</span>- </div>- );-};-export const PollSettingsForm = ({ children }: React.PropsWithChildren) => {const form = useFormContext<PollSettingsFormData>();const isFree = useIsFree();@@ -130,29 +38,29 @@ export const PollSettingsForm = ({ children }: React.PropsWithChildren) => {</CardTitle></div><CardContent>- <div className="divide-y">+ <SettingsGroup><FormFieldcontrol={form.control}name="requireParticipantEmail"render={({ field }) => (- <PollSetting>- <PollSettingIcon>+ <Setting>+ <SettingIcon><MailIcon />- </PollSettingIcon>- <PollSettingTitle>+ </SettingIcon>+ <SettingTitle><Transi18nKey="requireParticipantEmailTitle"defaults="Require email"/>{isFree ? <ProBadge /> : null}- </PollSettingTitle>- <PollSettingDescription>+ </SettingTitle>+ <SettingDescription><Transi18nKey="requireParticipantEmailDescription"defaults="Participants must provide an email address to respond."/>- </PollSettingDescription>- <PollSettingControl>+ </SettingDescription>+ <SettingControl><Switchchecked={!!field.value}onCheckedChange={(checked) => {@@ -166,32 +74,32 @@ export const PollSettingsForm = ({ children }: React.PropsWithChildren) => {}}}/>- </PollSettingControl>- </PollSetting>+ </SettingControl>+ </Setting>)}/><FormFieldcontrol={form.control}name="hideParticipants"render={({ field }) => (- <PollSetting>- <PollSettingIcon>+ <Setting>+ <SettingIcon><VenetianMaskIcon />- </PollSettingIcon>- <PollSettingTitle>+ </SettingIcon>+ <SettingTitle><Transi18nKey="hideParticipantsTitle"defaults="Hide participant names"/>{isFree ? <ProBadge /> : null}- </PollSettingTitle>- <PollSettingDescription>+ </SettingTitle>+ <SettingDescription><Transi18nKey="hideParticipantsDescription"defaults="Participants will not be able to see the names of other respondents."/>- </PollSettingDescription>- <PollSettingControl>+ </SettingDescription>+ <SettingControl><Switchchecked={!!field.value}onCheckedChange={(checked) => {@@ -205,29 +113,29 @@ export const PollSettingsForm = ({ children }: React.PropsWithChildren) => {}}}/>- </PollSettingControl>- </PollSetting>+ </SettingControl>+ </Setting>)}/><FormFieldcontrol={form.control}name="hideScores"render={({ field }) => (- <PollSetting>- <PollSettingIcon>+ <Setting>+ <SettingIcon><BarChart2Icon />- </PollSettingIcon>- <PollSettingTitle>+ </SettingIcon>+ <SettingTitle><Trans i18nKey="hideScoresTitle" defaults="Hide votes" />{isFree ? <ProBadge /> : null}- </PollSettingTitle>- <PollSettingDescription>+ </SettingTitle>+ <SettingDescription><Transi18nKey="hideScoresDescription"defaults="Hide everyone's votes from a participant until they cast their own."/>- </PollSettingDescription>- <PollSettingControl>+ </SettingDescription>+ <SettingControl><Switchchecked={!!field.value}onCheckedChange={(checked) => {@@ -241,19 +149,19 @@ export const PollSettingsForm = ({ children }: React.PropsWithChildren) => {}}}/>- </PollSettingControl>- </PollSetting>+ </SettingControl>+ </Setting>)}/><FormFieldcontrol={form.control}name="enableComments"render={({ field }) => (- <PollSetting>- <PollSettingIcon>+ <Setting>+ <SettingIcon><MessageCircleIcon />- </PollSettingIcon>- <PollSettingTitle>+ </SettingIcon>+ <SettingTitle><Trans i18nKey="commentsSettingTitle" defaults="Comments" /><Badge size="sm"><Trans@@ -261,14 +169,14 @@ export const PollSettingsForm = ({ children }: React.PropsWithChildren) => {defaults="Legacy"/></Badge>- </PollSettingTitle>- <PollSettingDescription>+ </SettingTitle>+ <SettingDescription><Transi18nKey="commentsSettingDescription"defaults="Allow participants to post comments on the poll."/>- </PollSettingDescription>- <PollSettingControl>+ </SettingDescription>+ <SettingControl><Switchchecked={!!field.value}onCheckedChange={(checked) => {@@ -278,19 +186,19 @@ export const PollSettingsForm = ({ children }: React.PropsWithChildren) => {});}}/>- </PollSettingControl>+ </SettingControl>{field.value ? (- <PollSettingHint>+ <SettingHint><Transi18nKey="commentsSettingPhaseOutHint"defaults="Comments are being phased out. Participants can include a note with their response instead."/>- </PollSettingHint>+ </SettingHint>) : null}- </PollSetting>+ </Setting>)}/>- </div>+ </SettingsGroup></CardContent>{children}</Card>