/* ═══════════════════════════════════════════════════════════════ WEBSITE FEEDBACK FORM Posts to /api/feedback, which is the only public write on the site. The server is the authority on validation; the checks here exist to stop someone hitting a 422 they could have avoided, and the two sets are kept deliberately in step (MIN_MESSAGE, and the ids in FEEDBACK_TYPES). The page and section options come from navConfig, so adding a page to the nav adds it to this form too. With fixedType the type is set and neither picker shows. The Privacy page uses it for data requests (DATA_REQUEST), which also need an email, since the answer has to go somewhere. ═══════════════════════════════════════════════════════════════ */ import { CaretDown } from '@phosphor-icons/react'; import { useState } from "react"; import { post, ApiError } from "../../lib/api.ts"; import { PAGE_LINKS } from "../../navConfig.ts"; import { useNavSections, type NavSection } from "../../lib/navSections.ts"; import { DATA_REQUEST, FEEDBACK_TYPES } from "../../data/feedbackTypes.ts"; import { accentText, BRAND } from '../../lib/color.ts' import { focusOnMount } from '../../lib/focus.ts' const MUTED = "var(--color-muted)"; const MAX_CHARS = 1500; const MIN_MESSAGE = 10; // matches the server // Sentinel for "this isn't about one particular page". const SITE_WIDE = "site"; // Sentinel for "this page, but not one section of it". const WHOLE_PAGE = ""; /* ── Shared field chrome ─────────────────────────────────────── */ export const fieldClass = "w-full border border-muted/50 bg-surface px-4 py-3 text-body "+ "placeholder:text-muted outline-none transition-colors " + "focus:border-brand focus:ring-2 focus:ring-brand/25"; export const errorFieldClass = "border-danger focus:border-danger focus:ring-danger/20"; const selectClass = `${fieldClass} appearance-none pr-10`; export function OptionalTag() { return ( Optional ); } /* On a field the send button waits for. */ export function RequiredTag() { return ( Required ); } /* Under a message box: how many more characters it needs, until it has them (the send button stays off until then). */ export function MinimumNote({ length, min }: { length: number; min: number }) { if (length >= min) return null; return ( {length === 0 ? `At least ${min} characters` : `${min - length} more character${min - length === 1 ? "" : "s"} needed`} ); } export function FieldError({ id, children }) { if (!children) return null; return (

{children}

); } // Native select plus a chevron, since appearance-none strips the default one. function Select({ id, label, value, onChange, children }) { return (
); } /* ── Type picker: a tile per option (the Request form's too) ─── */ export function TilePicker({ legend, blurb, name, options, value, onChange, error, }: { legend: string; blurb: string; name: string; options: { id: string; label: string; hint: string }[]; value: string | null; onChange: (id: string) => void; /** Shown under the tiles and read with each one. */ error?: string; }) { const errorId = `${name}-error`; return (
{legend}

{blurb}

{options.map((type) => { const selected = value === type.id; return ( ); })}
{error}
); } /* ── Where on the site ───────────────────────────────────────── */ function LocationPicker({ page, section, onPageChange, onSectionChange }) { const sectionsOf = useNavSections(); const sections = page === SITE_WIDE ? [] : sectionsOf(page); return (

Where did you run into it?

Leave this on "Not page-specific" if it applies to the whole site.

{sections.length > 0 && ( )}
); } // Human-readable version of the picked location, for the thank-you panel. function describeLocation(page, section, sectionsOf: (path: string) => NavSection[]) { if (page === SITE_WIDE) return null; const pageLabel = PAGE_LINKS.find((p) => p.path === page)?.label ?? page; const sectionLabel = sectionsOf(page).find( (s) => s.hash === section, )?.label; return sectionLabel ? `${pageLabel} → ${sectionLabel}` : pageLabel; } /* ── The form ────────────────────────────────────────────────── */ export default function FeedbackForm({ fixedType }: { fixedType?: string } = {}) { const sectionsOf = useNavSections(); const request = fixedType === DATA_REQUEST.id; const [type, setType] = useState(fixedType ?? null); const [page, setPage] = useState(SITE_WIDE); const [section, setSection] = useState(WHOLE_PAGE); const [message, setMessage] = useState(""); const [name, setName] = useState(""); const [email, setEmail] = useState(""); // Honeypot: never shown, never filled by a person. const [website, setWebsite] = useState(""); // idle → sending → sent, or back to idle with an error to show. const [status, setStatus] = useState("idle"); const [formError, setFormError] = useState(null); const [fieldErrors, setFieldErrors] = useState({}); const sending = status === "sending"; const ready = Boolean(type) && message.trim().length >= MIN_MESSAGE && (!request || email.trim() !== ""); async function handleSubmit(event) { event.preventDefault(); if (!ready || sending) return; setStatus("sending"); setFormError(null); setFieldErrors({}); try { await post("/feedback", { feedbackType: type, message: message.trim(), name: name.trim(), email: email.trim(), pagePath: page === SITE_WIDE ? "" : page, sectionId: section, // server strips the leading '#' website, }); setStatus("sent"); } catch (error) { setStatus("idle"); if (error instanceof ApiError) { setFieldErrors(error.fields ?? {}); setFormError( error.fields ? "Have another look at the highlighted fields." : error.message, ); } else { // Network failure, offline, server down. setFormError("Couldn't reach the server. Try again in a moment."); } } } function reset() { setType(fixedType ?? null); setPage(SITE_WIDE); setSection(WHOLE_PAGE); setMessage(""); setName(""); setEmail(""); setWebsite(""); setStatus("idle"); setFormError(null); setFieldErrors({}); } if (status === "sent") { const where = describeLocation(page, section, sectionsOf); return (

Thanks, we've got it

{where && (

Filed against {where}.

)}

{request ? `We'll reply to ${email}. We may ask you to confirm it's you before we change or delete anything.` : email ? `We'll follow up at ${email} if we have questions.` : "You sent this anonymously, so we won't be able to reply, but we read everything that comes in."}

); } return (
{!fixedType && ( <>
)} {/* Message */}

{request ? "Say what you'd like: to see the information we hold about you, to correct it, or to have it deleted. Tell us how you've been in touch with NGU (a retreat, a group, this site) so we can find it." : "What you expected to happen, and what happened instead, is the most useful thing you can give us."}