"use client"; import "./components/ui/tidal.css"; import { useEffect, useMemo, useRef, useState } from "react"; import { TidalNotificationPreferences, type TidalNotificationChannel, type TidalNotificationPreferenceGroup } from "./components/ui/notification-preferences"; import "./components/ui/notification-preferences.css"; const tidalExampleStyles = ".tidal-scope .notification-preferences-demo,\n.tidal-scope .notification-preferences-demo * {\n box-sizing: border-box;\n}\n\n.tidal-scope .notification-preferences-demo {\n display: grid;\n width: min(100%, 1180px);\n min-width: 0;\n gap: 22px;\n margin-inline: auto;\n padding: clamp(16px, 4vw, 42px);\n}\n\n@container tidal-preview (max-width: 390px) {\n .tidal-scope .notification-preferences-demo {\n gap: 16px;\n padding: 12px;\n }\n}"; const CHANNELS: readonly TidalNotificationChannel[] = [ { id: "in-app", label: "In app", accessibleLabel: "In app", }, { id: "email", label: "Email", accessibleLabel: "Email" }, { id: "mobile", label: "Mobile", accessibleLabel: "Mobile" }, ]; const INITIAL_VALUES: Readonly> = { "assignment:in-app": true, "assignment:email": true, "assignment:mobile": false, "comment:in-app": true, "comment:email": false, "comment:mobile": true, "approval:in-app": true, "approval:email": true, "approval:mobile": true, "weekly:in-app": false, "weekly:email": true, }; export function NotificationPreferencesDemo() { const [values, setValues] = useState>({ ...INITIAL_VALUES, }); const [savedValues, setSavedValues] = useState>({ ...INITIAL_VALUES, }); const [busy, setBusy] = useState(false); const [status, setStatus] = useState("No unsaved changes."); const timerRef = useRef(null); useEffect( () => () => { if (timerRef.current !== null) window.clearTimeout(timerRef.current); }, [], ); function cancelPendingSave() { if (timerRef.current !== null) { window.clearTimeout(timerRef.current); timerRef.current = null; } setBusy(false); } function updateValue(key: string, checked: boolean) { cancelPendingSave(); setValues((current) => ({ ...current, [key]: checked })); setStatus("Unsaved changes."); } const groups = useMemo( () => [ { id: "project", title: "Project activity", description: "Updates about your work.", preferences: [ { id: "assignment", label: "Task assigned", accessibleLabel: "Task assigned", description: "When someone assigns you a task.", controls: CHANNELS.map((channel) => ({ channelId: channel.id, checked: values[`assignment:${channel.id}`] ?? false, inputProps: { name: `assignment-${channel.id}` }, })), }, { id: "comment", label: "New comment", accessibleLabel: "New comment", description: "When a teammate replies to your work.", controls: CHANNELS.map((channel) => ({ channelId: channel.id, checked: values[`comment:${channel.id}`] ?? false, inputProps: { name: `comment-${channel.id}` }, })), }, { id: "approval", label: "Review requested", accessibleLabel: "Review requested", description: "When something needs your approval.", controls: CHANNELS.map((channel) => ({ channelId: channel.id, checked: values[`approval:${channel.id}`] ?? false, inputProps: { name: `approval-${channel.id}` }, })), }, ], }, { id: "summary", title: "Workspace summary", description: "A recap of the week.", preferences: [ { id: "weekly", label: "Weekly activity summary", accessibleLabel: "Weekly activity summary", description: "Your activity, delivered every Monday.", controls: CHANNELS.filter((channel) => channel.id !== "mobile").map( (channel) => ({ channelId: channel.id, checked: values[`weekly:${channel.id}`] ?? false, inputProps: { name: `weekly-${channel.id}` }, }), ), }, ], }, ], [values], ); const dirty = Object.keys(INITIAL_VALUES).some( (key) => values[key] !== savedValues[key], ); return (
updateValue(`${preference.id}:${channel.id}`, checked) } onSave={(event) => { event.preventDefault(); if (busy) return; const nextValues = { ...values }; setBusy(true); setStatus("Saving preferences…"); timerRef.current = window.setTimeout(() => { timerRef.current = null; setSavedValues(nextValues); setBusy(false); setStatus("Preferences updated in this preview."); }, 600); }} action="/local-notification-preferences-preview" method="post" status={ {status} } resetAction={{ label: "Reset", disabled: busy, onAction: () => { cancelPendingSave(); setValues({ ...INITIAL_VALUES }); setSavedValues({ ...INITIAL_VALUES }); setStatus("Preferences reset."); }, }} saveButtonProps={{ "aria-disabled": !dirty, formAction: "/local-notification-preferences-preview", formMethod: "post", }} footer="Sample data. Changes stay in this preview." />
); } export default function AgentExample() { return (
); }