feat: update settings page to manage reminder times and retention policies

Revamped the settings page to allow coordinators to configure reminder email timings and the duration for retaining volunteer signup details. Removed the privacy text field from settings, reflecting the decision to not include unapproved privacy wording. Enhanced the user interface with clearer instructions and previews for reminder settings. Updated related tests to ensure proper functionality and validation of new settings.
This commit is contained in:
blake committed 2026-10-30 14:52:04 -05:00
1 parent e1bd85f879
commit caaeded467
12 files changed
+461 -175

No files matched your search

-1
View File
@@ -726,7 +726,6 @@ function settingsFormFromBody(body: Record<string, unknown>): SettingsForm {
reminderNear: textField(body, 'reminder_near'),
replyTo: textField(body, 'reply_to'),
retentionMonths: textField(body, 'retention_months'),
privacyText: textField(body, 'privacy_text'),
};
}
+152 -57
View File
@@ -2,7 +2,7 @@ import { Temporal } from '@js-temporal/polyfill';
import { DESK_WINDOW_DAYS, type AdminEvent, type HomeEvent, type HomeShift, type AdminShift, type AdminShiftSummary, type AdminTemplate, type MoveTarget, type RosterPerson } from './admin/store.js';
import { spotsSentence } from './schedule.js';
import type { EventValues, ShiftValues, TemplateValues } from './admin/forms.js';
import type { SettingsErrors, SettingsForm } from './settings.js';
import { MAX_REMINDER_HOURS, MAX_RETENTION_MONTHS, type SettingsErrors, type SettingsForm } from './settings.js';
import type { AppConfig } from './config.js';
import {
CONTACT_LIST_PAGE,
@@ -337,6 +337,13 @@ export function AdminHome(props: {
);
}
const FIRST_REMINDER_HOURS = [168, 72, 48, 24] as const;
const SECOND_REMINDER_HOURS = [24, 12, 4, 2, 1] as const;
const RETENTION_MONTHS = [6, 12, 24, 36, 60, 120] as const;
const USUAL_FIRST_HOURS = 48;
const USUAL_SECOND_HOURS = 2;
const USUAL_RETENTION_MONTHS = 24;
export function SettingsPage(props: {
config: AppConfig;
csrf: string;
@@ -345,50 +352,149 @@ export function SettingsPage(props: {
notice?: string;
}) {
const errors = props.errors ?? {};
const reminderNote = reminderPreview(props.values.reminderFar, props.values.reminderNear);
const keepNote = retentionPreview(props.values.retentionMonths);
return (
<>
<Crumb items={[{ href: appPath(props.config, '/admin'), label: 'Coordinator' }, { label: 'Settings' }]} />
<h1>Settings</h1>
<header class="settings-intro">
<h1>Settings</h1>
<p>Choose when volunteers get a reminder, where their replies go, and how long signup details stay here. This applies to email that has not gone out yet.</p>
</header>
<Notice notice={props.notice} />
<form method="post" action={appPath(props.config, '/admin/settings')} class="signup-form">
<form method="post" action={appPath(props.config, '/admin/settings')} class="settings-form" data-settings>
<input type="hidden" name="csrf" value={props.csrf} />
<p>These apply to reminders and mail that have not been queued yet. Mail that was already accepted cannot be recalled.</p>
<label>
First reminder, hours before the shift
<input name="reminder_far" type="number" min="1" max="336" required value={props.values.reminderFar} />
<span class="hint">The default is 48. Use a whole number from 1 to 336.</span>
{errors.reminderFar ? <span class="error">{errors.reminderFar}</span> : null}
</label>
<label>
Second reminder, hours before the shift
<input name="reminder_near" type="number" min="1" max="336" required value={props.values.reminderNear} />
<span class="hint">The default is 2. The two times have to be different. A reminder is not sent after the shift starts.</span>
{errors.reminderNear ? <span class="error">{errors.reminderNear}</span> : null}
</label>
<label>
Reply-to address
<input name="reply_to" type="email" required value={props.values.replyTo} />
<span class="hint">Replies to volunteer and sign-in email go here. The sending address stays in the server configuration.</span>
{errors.replyTo ? <span class="error">{errors.replyTo}</span> : null}
</label>
<label>
Keep signups for, months
<input name="retention_months" type="number" min="1" max="120" required value={props.values.retentionMonths} />
<span class="hint">The default is 24, counted after the shift ends. After 1:00 AM Central Time, signups older than this lose their name, email, phone, and notes in this app. A spreadsheet already downloaded is left as it is.</span>
{errors.retentionMonths ? <span class="error">{errors.retentionMonths}</span> : null}
</label>
<label>
Privacy page wording
<textarea name="privacy_text">{props.values.privacyText}</textarea>
<span class="hint">Leave this blank until the pantry approves a notice. Saved wording is what the public privacy page shows. Saving it does not by itself make it an approved privacy policy. Start a new line for a new paragraph.</span>
{errors.privacyText ? <span class="error">{errors.privacyText}</span> : null}
</label>
<button type="submit">Save settings</button>
<section class="settings-card" aria-labelledby="reminders-heading">
<div class="settings-card-copy">
<h2 id="reminders-heading">Reminder emails</h2>
<p>Each person who is signed up gets two emails before the shift, so they remember to come. A reminder is not sent after the shift starts.</p>
</div>
<div class="settings-fields">
<label>
First reminder
<select name="reminder_far" required>
<ReminderOptions selected={props.values.reminderFar} choices={FIRST_REMINDER_HOURS} usual={USUAL_FIRST_HOURS} />
</select>
{errors.reminderFar ? <span class="error">{errors.reminderFar}</span> : null}
</label>
<label>
Second reminder
<select name="reminder_near" required>
<ReminderOptions selected={props.values.reminderNear} choices={SECOND_REMINDER_HOURS} usual={USUAL_SECOND_HOURS} />
</select>
{errors.reminderNear ? <span class="error">{errors.reminderNear}</span> : null}
</label>
</div>
{reminderNote ? <p class="settings-preview" data-reminder-preview>{reminderNote}</p> : null}
</section>
<section class="settings-card" aria-labelledby="replies-heading">
<div class="settings-card-copy">
<h2 id="replies-heading">Where replies go</h2>
<p>When a volunteer replies to a pantry email, it comes to this address. Use an inbox someone at the pantry checks.</p>
</div>
<label>
Email address
<input name="reply_to" type="email" autocomplete="email" required value={props.values.replyTo} />
<span class="hint">This does not change the address the email is sent from.</span>
{errors.replyTo ? <span class="error">{errors.replyTo}</span> : null}
</label>
</section>
<section class="settings-card" aria-labelledby="keep-heading">
<div class="settings-card-copy">
<h2 id="keep-heading">How long we keep signup details</h2>
<p>After a shift ends, that person's name, email, and phone stay on the signup for a while, then they are removed.</p>
</div>
<label>
Keep them for
<select name="retention_months" required>
<RetentionOptions selected={props.values.retentionMonths} />
</select>
<span class="hint">A spreadsheet already downloaded is left as it is. People stay on the contact list.</span>
{errors.retentionMonths ? <span class="error">{errors.retentionMonths}</span> : null}
</label>
{keepNote ? <p class="settings-preview" data-retention-preview>{keepNote}</p> : null}
</section>
<div class="settings-save">
<button type="submit">Save these choices</button>
</div>
</form>
<script src={assetHref(props.config, '/js/settings.js')} defer />
</>
);
}
function ReminderOptions(props: { selected: string; choices: readonly number[]; usual: number }) {
return listedHours(props.selected, props.choices).map((hours) => (
<option value={String(hours)} key={hours} selected={String(hours) === props.selected.trim() ? true : undefined}>
{`${aheadChoice(hours)}${hours === props.usual ? ' (usual)' : ''}`}
</option>
));
}
function RetentionOptions(props: { selected: string }) {
return listedMonths(props.selected).map((months) => (
<option value={String(months)} key={months} selected={String(months) === props.selected.trim() ? true : undefined}>
{`${keepPhrase(months)}${months === USUAL_RETENTION_MONTHS ? ' (usual)' : ''}`}
</option>
));
}
function listedHours(selected: string, choices: readonly number[]): number[] {
const current = wholeInRange(selected, 1, MAX_REMINDER_HOURS);
if (current === null || choices.includes(current)) return [...choices];
return [...choices, current].sort((a, b) => b - a);
}
function listedMonths(selected: string): number[] {
const current = wholeInRange(selected, 1, MAX_RETENTION_MONTHS);
if (current === null || (RETENTION_MONTHS as readonly number[]).includes(current)) return [...RETENTION_MONTHS];
return [...RETENTION_MONTHS, current].sort((a, b) => a - b);
}
function reminderPreview(far: string, near: string): string | null {
const first = wholeInRange(far, 1, MAX_REMINDER_HOURS);
const second = wholeInRange(near, 1, MAX_REMINDER_HOURS);
if (first === null || second === null) return null;
if (first === second) return 'Choose two different times. One email should go out earlier than the other.';
const earlier = Math.max(first, second);
const closer = Math.min(first, second);
return `Volunteers get an email ${aheadChoice(earlier)} the shift, and another ${aheadChoice(closer)} the shift.`;
}
function retentionPreview(months: string): string | null {
const count = wholeInRange(months, 1, MAX_RETENTION_MONTHS);
if (count === null) return null;
return `Names, email addresses, and phone numbers come off a signup ${keepPhrase(count)} after the shift ends.`;
}
function aheadChoice(hours: number): string {
if (hours === 1) return '1 hour before';
if (hours % 168 === 0) {
const weeks = hours / 168;
return weeks === 1 ? '1 week before' : `${weeks} weeks before`;
}
if (hours % 24 === 0) {
const days = hours / 24;
return days === 1 ? '1 day before' : `${days} days before`;
}
return `${hours} hours before`;
}
function keepPhrase(months: number): string {
if (months % 12 === 0) {
const years = months / 12;
return years === 1 ? '1 year' : `${years} years`;
}
return months === 1 ? '1 month' : `${months} months`;
}
function wholeInRange(value: string, min: number, max: number): number | null {
const trimmed = value.trim();
if (!/^[1-9][0-9]{0,2}$/.test(trimmed)) return null;
const count = Number(trimmed);
return count >= min && count <= max ? count : null;
}
export function AdminsPage(props: {
config: AppConfig;
csrf: string;
@@ -796,7 +902,7 @@ export function EventEditor(props: {
Edit details
<span class="edit-chevron" aria-hidden="true" />
</summary>
<EventForm action={props.action} csrf={props.csrf} values={props.values} error={props.error} submitLabel={props.submitLabel} wide />
<EventForm action={props.action} csrf={props.csrf} values={props.values} error={props.error} submitLabel={props.submitLabel} wide panel />
</details>
</section>
) : (
@@ -1659,41 +1765,30 @@ function EventForm(props: {
heading?: string;
headingId?: string;
wide?: boolean;
panel?: boolean;
}) {
const formClass = ['signup-form', props.wide ? 'event-details-form' : '', props.panel ? 'event-fields' : ''].filter(Boolean).join(' ');
return (
<form method="post" action={props.action} class={props.wide ? 'signup-form event-details-form' : 'signup-form'}>
<form method="post" action={props.action} class={formClass}>
{props.heading ? <h2 id={props.headingId}>{props.heading}</h2> : null}
<input type="hidden" name="csrf" value={props.csrf} />
{props.creating ? <input type="hidden" name="tz" value={props.values.tz} /> : null}
<input type="hidden" name="tz" value={props.values.tz} />
{props.values.slug ? <input type="hidden" name="slug" value={props.values.slug} /> : null}
{props.error ? <p class="error">{props.error}</p> : null}
<label>
Title
<input name="title" type="text" required value={props.values.title} />
</label>
{props.creating ? null : (
<label>
Public address
<input name="slug" type="text" value={props.values.slug} />
<span class="hint">Letters, numbers, and hyphens. Leave this blank to build it from the title. This becomes /e/this-address.</span>
</label>
)}
<label>
Description
<textarea name="description">{props.values.description}</textarea>
</label>
{props.creating ? null : (
<>
<label>
Location
<input name="location" type="text" value={props.values.location} />
<span class="hint">Changing the location emails people on shifts that have not ended.</span>
</label>
<label>
Time zone
<input name="tz" type="text" required value={props.values.tz} />
<span class="hint">Use an IANA time zone, such as America/Chicago.</span>
</label>
</>
<label>
Location
<input name="location" type="text" value={props.values.location} />
<span class="hint">Changing the location emails people on shifts that have not ended.</span>
</label>
)}
<label class="check">
<input type="checkbox" name="published" value="yes" checked={props.values.published ? true : undefined} />
+1 -1
View File
@@ -51,7 +51,7 @@ export function parseEvent(values: EventValues): { ok: true; value: EventInput }
if (title.length === 0) return { ok: false, error: 'Enter a title.' };
if (title.length > 120) return { ok: false, error: 'Use a shorter title.' };
const slug = values.slug.trim() ? normalizeSlug(values.slug) : normalizeSlug(title);
if (!validSlug(slug)) return { ok: false, error: 'Use a public address with lowercase letters, numbers, and hyphens.' };
if (!validSlug(slug)) return { ok: false, error: 'Use a title with a letter or number.' };
const description = values.description.trim();
if (description.length > 2000) return { ok: false, error: 'Use a shorter description.' };
const location = values.location.trim();
+2 -2
View File
@@ -349,7 +349,7 @@ export function createEvent(
return { ok: true as const, id, mailed: 0 };
}).immediate();
} catch (err) {
if (isUniqueConstraint(err)) return { ok: false, error: 'That public address is already used.' };
if (isUniqueConstraint(err)) return { ok: false, error: 'Another event already uses that name.' };
throw err;
}
}
@@ -386,7 +386,7 @@ export function updateEvent(
return { ok: true as const, id, mailed };
}).immediate();
} catch (err) {
if (isUniqueConstraint(err)) return { ok: false, error: 'That public address is already used.' };
if (isUniqueConstraint(err)) return { ok: false, error: 'Another event already uses that name.' };
throw err;
}
}
+1 -7
View File
@@ -5,9 +5,8 @@ import type { Child } from 'hono/jsx';
import { registerAdmin } from './admin-http.js';
import { readSession } from './auth/session.js';
import type { AppConfig } from './config.js';
import { EmptySchedulePage, EventPage, Layout, NotFoundPage, PrivacyPage, ProblemPage } from './pages.js';
import { EmptySchedulePage, EventPage, Layout, NotFoundPage, ProblemPage } from './pages.js';
import { getPublishedEvent } from './schedule.js';
import { privacyParagraphs, readPantrySettings } from './settings.js';
import { registerReminderOff } from './reminder-http.js';
import { registerPublicSignup } from './signup-http.js';
import { securityHeaders } from './security.js';
@@ -80,11 +79,6 @@ export function createApp(
app.get(`${config.basePath}/`, (c) => c.redirect(config.basePath, 302));
}
app.get(appPath(config, '/privacy'), (c) => {
const paragraphs = privacyParagraphs(readPantrySettings(db, config.mail.replyTo).privacyText);
return page(c, config, db, now(), 'Privacy · Hope Volunteer', <PrivacyPage paragraphs={paragraphs} />);
});
app.get(appPath(config, '/e/:slug'), (c) => {
const at = now();
const slug = c.req.param('slug') ?? '';
-18
View File
@@ -484,24 +484,6 @@ export function ManagePage(props: {
);
}
// TODO(D3): The pantry still has to approve this wording, including retention and copies.
// A blank setting keeps the unapproved message. Saved wording is shown as written, escaped, and is not a policy by itself.
export function PrivacyPage(props: { paragraphs: string[] }) {
return (
<>
<h1>Privacy</h1>
{props.paragraphs.length === 0 ? (
<>
<p>Hope Volunteer does not have an approved privacy notice yet.</p>
<p>When the pantry approves the wording, it will appear on this page. This page is not a privacy policy.</p>
</>
) : (
props.paragraphs.map((paragraph, index) => <p key={index}>{paragraph}</p>)
)}
</>
);
}
export function SwitchAlreadyPage() {
return (
<>
+10 -44
View File
@@ -7,18 +7,15 @@ export const DEFAULT_REMINDER_HOURS = [48, 2] as const;
export const DEFAULT_RETENTION_MONTHS = 24;
export const MAX_REMINDER_HOURS = 336;
export const MAX_RETENTION_MONTHS = 120;
export const MAX_PRIVACY_TEXT = 8000;
const HOUR_KEY = 'reminder_hours';
const REPLY_KEY = 'reply_to';
const PRIVACY_KEY = 'privacy_text';
const RETENTION_KEY = 'retention_months';
export type PantrySettings = {
/** Larger hour count first, so the first reminder is the earlier one. */
reminderHours: readonly [number, number];
replyTo: string;
privacyText: string;
retentionMonths: number;
};
@@ -27,7 +24,6 @@ export type SettingsForm = {
reminderNear: string;
replyTo: string;
retentionMonths: string;
privacyText: string;
};
export type SettingsErrors = Partial<Record<keyof SettingsForm, string>>;
@@ -40,7 +36,6 @@ export function readPantrySettings(db: Database.Database, fallbackReplyTo: strin
return {
reminderHours: storedHours(db),
replyTo: storedReplyTo(db) ?? fallbackReplyTo,
privacyText: storedPrivacy(db),
retentionMonths: storedRetention(db),
};
}
@@ -58,20 +53,12 @@ export function readReplyTo(db: Database.Database, fallback: string): string {
return storedReplyTo(db) ?? fallback;
}
export function privacyParagraphs(text: string): string[] {
return text
.split(/\n+/)
.map((line) => line.trim())
.filter((line) => line.length > 0);
}
export function settingsFormFrom(settings: PantrySettings): SettingsForm {
return {
reminderFar: String(settings.reminderHours[0]),
reminderNear: String(settings.reminderHours[1]),
replyTo: settings.replyTo,
retentionMonths: String(settings.retentionMonths),
privacyText: settings.privacyText,
};
}
@@ -79,18 +66,16 @@ export function parseSettingsForm(input: SettingsForm): { ok: true; value: Pantr
const errors: SettingsErrors = {};
const earlier = parseHour(input.reminderFar);
const closer = parseHour(input.reminderNear);
if (earlier === null) errors.reminderFar = `Enter a whole number of hours from 1 to ${MAX_REMINDER_HOURS}.`;
if (closer === null) errors.reminderNear = `Enter a whole number of hours from 1 to ${MAX_REMINDER_HOURS}.`;
if (earlier === null) errors.reminderFar = 'Choose when the first reminder goes out.';
if (closer === null) errors.reminderNear = 'Choose when the second reminder goes out.';
if (earlier !== null && closer !== null && earlier === closer) {
errors.reminderNear = 'The two reminder times have to be different.';
errors.reminderNear = 'Choose two different times.';
}
const reply = parseReply(input.replyTo);
if (!reply.ok) errors.replyTo = reply.error;
const months = parseMonths(input.retentionMonths);
if (months === null) errors.retentionMonths = `Enter a whole number of months from 1 to ${MAX_RETENTION_MONTHS}.`;
const privacy = parsePrivacy(input.privacyText);
if (!privacy.ok) errors.privacyText = privacy.error;
if (earlier === null || closer === null || earlier === closer || !reply.ok || months === null || !privacy.ok) {
if (months === null) errors.retentionMonths = 'Choose how long to keep signup details.';
if (earlier === null || closer === null || earlier === closer || !reply.ok || months === null) {
return { ok: false, errors };
}
const reminderHours: [number, number] = earlier > closer ? [earlier, closer] : [closer, earlier];
@@ -99,15 +84,14 @@ export function parseSettingsForm(input: SettingsForm): { ok: true; value: Pantr
value: {
reminderHours,
replyTo: reply.value,
privacyText: privacy.value,
retentionMonths: months,
},
};
}
/**
* Saves the four coordinator settings. Retention is stored only.
* This does not anonymize signups, and it does not change mail that is already queued.
* Saves reminder times, the reply address, and how long signup details are kept.
* This does not remove signup details, and it does not change mail that is already queued.
*/
export function savePantrySettings(
db: Database.Database,
@@ -116,16 +100,15 @@ export function savePantrySettings(
fallbackReplyTo: string,
now: string,
): { changed: boolean } {
db.prepare(`DELETE FROM settings WHERE key = 'privacy_text'`).run();
const current = readPantrySettings(db, fallbackReplyTo);
const replyChanged = current.replyTo !== next.replyTo;
const privacyChanged = current.privacyText !== next.privacyText;
const hoursChanged = current.reminderHours[0] !== next.reminderHours[0] || current.reminderHours[1] !== next.reminderHours[1];
const retentionChanged = current.retentionMonths !== next.retentionMonths;
if (!replyChanged && !privacyChanged && !hoursChanged && !retentionChanged) return { changed: false };
if (!replyChanged && !hoursChanged && !retentionChanged) return { changed: false };
db.transaction(() => {
writeValue(db, HOUR_KEY, [next.reminderHours[0], next.reminderHours[1]], now);
writeValue(db, REPLY_KEY, next.replyTo, now);
writeValue(db, PRIVACY_KEY, next.privacyText, now);
writeValue(db, RETENTION_KEY, next.retentionMonths, now);
writeAudit(
db,
@@ -136,7 +119,6 @@ export function savePantrySettings(
reminderCloser: next.reminderHours[1],
retentionMonths: next.retentionMonths,
replyChanged: replyChanged ? 1 : 0,
privacyChanged: privacyChanged ? 1 : 0,
},
now,
);
@@ -163,13 +145,6 @@ function storedReplyTo(db: Database.Database): string | null {
}
}
function storedPrivacy(db: Database.Database): string {
const value = readValue(db, PRIVACY_KEY);
if (typeof value !== 'string') return '';
const parsed = parsePrivacy(value);
return parsed.ok ? parsed.value : '';
}
function storedRetention(db: Database.Database): number {
const value = readValue(db, RETENTION_KEY);
return isMonth(value) ? value : DEFAULT_RETENTION_MONTHS;
@@ -208,7 +183,7 @@ function parseMonths(value: string): number | null {
function parseReply(value: string): { ok: true; value: string } | { ok: false; error: string } {
if (value.includes('<') || value.includes('>')) {
return { ok: false, error: 'Enter an email address without a display name.' };
return { ok: false, error: 'Enter just the email address, without a name in front of it.' };
}
try {
return { ok: true, value: normalizeEmail(value) };
@@ -217,15 +192,6 @@ function parseReply(value: string): { ok: true; value: string } | { ok: false; e
}
}
function parsePrivacy(value: string): { ok: true; value: string } | { ok: false; error: string } {
const cleaned = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n').replace(/\t/g, ' ').trim();
if (cleaned.length > MAX_PRIVACY_TEXT) return { ok: false, error: 'Keep the privacy wording under 8,000 characters.' };
if (/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]/.test(cleaned)) {
return { ok: false, error: 'Remove the unusual character in the privacy wording.' };
}
return { ok: true, value: cleaned };
}
function isHour(value: unknown): value is number {
return typeof value === 'number' && Number.isInteger(value) && value >= 1 && value <= MAX_REMINDER_HOURS;
}