From 0e34bf9cab4e122cb7fb795e20003c9c178daeef Mon Sep 17 00:00:00 2001 From: b4sleal Date: Tue, 1 Dec 2026 19:26:55 -0500 Subject: [PATCH 1/6] feat(supabase): all-or-nothing CSV imports for events, team, guests and companies, and a step that drops events.location --- supabase/v2/03_populate.sql | 7 +- supabase/v2/18_csv_imports.sql | 141 +++++++++++++++++++++++++ supabase/v2/19_drop_event_location.sql | 27 +++++ supabase/v2/README.md | 11 +- 4 files changed, 177 insertions(+), 9 deletions(-) create mode 100644 supabase/v2/18_csv_imports.sql create mode 100644 supabase/v2/19_drop_event_location.sql diff --git a/supabase/v2/03_populate.sql b/supabase/v2/03_populate.sql index 85ac5db..65c120e 100644 --- a/supabase/v2/03_populate.sql +++ b/supabase/v2/03_populate.sql @@ -33,7 +33,8 @@ on conflict (id) do update set is_public = excluded.is_public; -- Events: the free-text `time` ("7:30PM", "7:30 PM - 9:00 PM", "TBD") becomes start_time and end_time. A time --- that can't be read is left null; 05_verify.sql lists those so someone can fill them in. +-- that can't be read is left null; 05_verify.sql lists those so someone can fill them in. The old free-text +-- `location` isn't copied: v2 keeps a building and room instead, picked in the admin. with parsed as ( select e.*, @@ -41,7 +42,7 @@ with parsed as ( '^\s*(\d{1,2}):(\d{2})\s*(AM|PM)(?:\s*-\s*(\d{1,2}):(\d{2})\s*(AM|PM))?\s*$') as m from public.events as e ) -insert into v2.events (id, title, description, date, start_time, end_time, location, category, poster_path, created_at) +insert into v2.events (id, title, description, date, start_time, end_time, category, poster_path, created_at) select p.id, p.title, @@ -49,7 +50,6 @@ select p.date, case when p.m is not null then to_timestamp(p.m[1] || ':' || p.m[2] || ' ' || p.m[3], 'HH12:MI AM')::time end, case when p.m[4] is not null then to_timestamp(p.m[4] || ':' || p.m[5] || ' ' || p.m[6], 'HH12:MI AM')::time end, - nullif(nullif(trim(p.location), ''), 'TBD'), -- Category from the old `kind`, with the calls made on 2026-11-25 for events it doesn't cover. Anything new -- that still doesn't match falls back to social; fix it in the admin. case @@ -71,7 +71,6 @@ on conflict (id) do update set date = excluded.date, start_time = excluded.start_time, end_time = excluded.end_time, - location = excluded.location, poster_path = coalesce(v2.events.poster_path, excluded.poster_path); commit; diff --git a/supabase/v2/18_csv_imports.sql b/supabase/v2/18_csv_imports.sql new file mode 100644 index 0000000..e5c87ee --- /dev/null +++ b/supabase/v2/18_csv_imports.sql @@ -0,0 +1,141 @@ +-- v2 step 19: saving a CSV import from the admin in one go, for events, the team, guests and companies. Each +-- function saves every new row and every changed row, or none of them: a function runs as one transaction, so an +-- error anywhere in it (a refused insert, a changed row that no longer exists, a category the table doesn't accept) +-- undoes everything it did. +-- +-- They run as the person calling them (security invoker), so the row-level security policies still decide: an +-- admin's import goes through, anyone else's insert is refused and their updates match no rows. Files are never +-- touched (posters, headshots, logos); a CSV has no file columns. +-- +-- Each takes two JSON arrays of rows named like the table's columns: `new_rows` are inserted first, then +-- `changed_rows` update the rows with their `id`. Rows missing from the file are left alone, never deleted. +-- +-- Run once. Swap `commit` for `rollback` at the bottom for a dry run. + +begin; + +create function v2.import_events(new_rows jsonb, changed_rows jsonb) + returns void + language plpgsql + security invoker + set search_path = '' +as $$ +declare + updated integer; +begin + -- Reading the rows as v2.events gives each column its real type (a date stays a date, highlights a text array). + -- A new row with no slug gets one from the title and year (events_fill_slug). + insert into v2.events ( + title, date, description, category, slug, start_time, end_time, format, building_code, room, meeting_url, + stream_url, highlights, dress_code, primary_colour, secondary_colour + ) + select + r.title, r.date, r.description, r.category, r.slug, r.start_time, r.end_time, r.format, r.building_code, r.room, + r.meeting_url, r.stream_url, coalesce(r.highlights, '{}'), r.dress_code, r.primary_colour, r.secondary_colour + from jsonb_populate_recordset(null::v2.events, new_rows) as r; + + update v2.events as e + set title = r.title, date = r.date, description = r.description, category = r.category, slug = r.slug, + start_time = r.start_time, end_time = r.end_time, format = r.format, building_code = r.building_code, + room = r.room, meeting_url = r.meeting_url, stream_url = r.stream_url, + highlights = coalesce(r.highlights, '{}'), dress_code = r.dress_code, primary_colour = r.primary_colour, + secondary_colour = r.secondary_colour + from jsonb_populate_recordset(null::v2.events, changed_rows) as r + where e.id = r.id; + + -- Row-level security refuses an update by changing nothing, the same as an id that is gone, so count the rows. + get diagnostics updated = row_count; + if updated <> jsonb_array_length(changed_rows) then + raise exception 'Only % of % changed events could be saved.', updated, jsonb_array_length(changed_rows); + end if; +end; +$$; + +create function v2.import_team(new_rows jsonb, changed_rows jsonb) + returns void + language plpgsql + security invoker + set search_path = '' +as $$ +declare + updated integer; +begin + insert into v2.team (name, role, department, is_public, github_url, linkedin_url) + select r.name, r.role, r.department, coalesce(r.is_public, false), r.github_url, r.linkedin_url + from jsonb_populate_recordset(null::v2.team, new_rows) as r; + + update v2.team as t + set name = r.name, role = r.role, department = r.department, is_public = coalesce(r.is_public, false), + github_url = r.github_url, linkedin_url = r.linkedin_url + from jsonb_populate_recordset(null::v2.team, changed_rows) as r + where t.id = r.id; + + get diagnostics updated = row_count; + if updated <> jsonb_array_length(changed_rows) then + raise exception 'Only % of % changed team members could be saved.', updated, jsonb_array_length(changed_rows); + end if; +end; +$$; + +create function v2.import_guests(new_rows jsonb, changed_rows jsonb) + returns void + language plpgsql + security invoker + set search_path = '' +as $$ +declare + updated integer; +begin + insert into v2.guests (name, title, company_id, link_url) + select r.name, r.title, r.company_id, r.link_url + from jsonb_populate_recordset(null::v2.guests, new_rows) as r; + + update v2.guests as g + set name = r.name, title = r.title, company_id = r.company_id, link_url = r.link_url + from jsonb_populate_recordset(null::v2.guests, changed_rows) as r + where g.id = r.id; + + get diagnostics updated = row_count; + if updated <> jsonb_array_length(changed_rows) then + raise exception 'Only % of % changed guests could be saved.', updated, jsonb_array_length(changed_rows); + end if; +end; +$$; + +create function v2.import_companies(new_rows jsonb, changed_rows jsonb) + returns void + language plpgsql + security invoker + set search_path = '' +as $$ +declare + updated integer; +begin + insert into v2.companies (name, colour, link) + select r.name, r.colour, r.link + from jsonb_populate_recordset(null::v2.companies, new_rows) as r; + + update v2.companies as c + set name = r.name, colour = r.colour, link = r.link + from jsonb_populate_recordset(null::v2.companies, changed_rows) as r + where c.id = r.id; + + get diagnostics updated = row_count; + if updated <> jsonb_array_length(changed_rows) then + raise exception 'Only % of % changed companies could be saved.', updated, jsonb_array_length(changed_rows); + end if; +end; +$$; + +-- Supabase lets every API role run new functions by default. Only signed-in people may call these; the policies +-- then decide whether what they send is saved. +revoke execute on function + v2.import_events(jsonb, jsonb), v2.import_team(jsonb, jsonb), v2.import_guests(jsonb, jsonb), + v2.import_companies(jsonb, jsonb) + from public, anon; +grant execute on function + v2.import_events(jsonb, jsonb), v2.import_team(jsonb, jsonb), v2.import_guests(jsonb, jsonb), + v2.import_companies(jsonb, jsonb) + to authenticated; + +commit; diff --git a/supabase/v2/19_drop_event_location.sql b/supabase/v2/19_drop_event_location.sql new file mode 100644 index 0000000..1fe2f4c --- /dev/null +++ b/supabase/v2/19_drop_event_location.sql @@ -0,0 +1,27 @@ +-- v2 step 20: drop the old free-text `events.location`. Every event now says where it is with a building and room, +-- or is online; step 14 copied the old text into those. +-- +-- Run it only once the admin and the site that no longer read `location` are deployed: the versions before them +-- select the column and would fail to load events. It stops if any in-person event still has only its old text +-- and no building, so no place is lost. +-- +-- Run once. Swap `commit` for `rollback` at the bottom for a dry run. + +begin; + +set local lock_timeout = '5s'; + +do $$ +begin + if exists ( + select 1 from v2.events + where location is not null and format = 'in_person' and building_code is null + ) then + raise exception 'An in-person event has only its old location text. Pick its building in the admin first.'; + end if; +end; +$$; + +alter table v2.events drop column location; + +commit; diff --git a/supabase/v2/README.md b/supabase/v2/README.md index 83d9e67..6801105 100644 --- a/supabase/v2/README.md +++ b/supabase/v2/README.md @@ -52,7 +52,6 @@ erDiagram date date time start_time "null = TBD" time end_time "nullable" - text location "nullable" text category "review | social | career | workshop | hackathon | meet_the_pros" text stream_url "nullable" timestamptz created_at @@ -215,7 +214,8 @@ check the current admin uses) can insert, update or delete. Nobody can truncate | Today (`public`) | v2 | | --- | --- | -| `events.id`, `title`, `description`, `date`, `location`, `created_at` | Same, same ids; a location of `"TBD"` becomes null | +| `events.id`, `title`, `description`, `date`, `created_at` | Same, same ids | +| `events.location` | Not copied; each event gets a building and room (or goes online) in the admin | | `events.kind` | `category`, plus a few events placed by hand (see `03_populate.sql`). Re-runs keep whatever the admin set | | `events.time` (text) | `start_time` and `end_time`. `"7:30PM"`, `"7:30 PM"` and `"7:30 PM - 9:00 PM"` are read; anything else (like `"TBD"`) becomes null and is listed by `05_verify.sql` | | `events.picture_url` (full URL in `events-images`) | `events.poster_path`; the file is copied to `event-media//photos/` | @@ -248,14 +248,16 @@ done in the dashboard (`16_drop_event_photos.sql` is step 17). | 16 | `15_photos_on_event_page.sql` | `photos.on_event_page`: the photos an exec picks for their event's page (off until picked), with an index for reading them in order | No | | 17 | `16_drop_event_photos.sql` | Checked every old cover against `events.poster_path`, confirmed no non-cover rows remained, then dropped the obsolete table on 2026-11-30 | No | | 18 | `17_company_links.sql` | `companies.link`: a company's own page (Instagram, Linktree, website), opened from its chip in "In collaboration with"; empty means the chip isn't a link. Already applied on the live project; safe to run again | No | +| 19 | `18_csv_imports.sql` | `import_events`, `import_team`, `import_guests` and `import_companies`: the admin's CSV imports, each saved all or nothing as the person importing (so the access rules still decide). Files are never touched | No | +| 20 | `19_drop_event_location.sql` | Drops the old free-text `events.location` once the admin and site that don't read it are deployed. Stops if an in-person event has only that text and no building | No | | undo | `99_rollback.sql` | Drops `v2` and the new bucket rules (empty and delete the two buckets in the dashboard) | No | Until the new site goes live, the current admin still writes to `public`. **Re-run steps 3 to 5 right before launch** to bring v2 up to date. Re-running updates existing rows from `public` but never overwrites v2-only fields (category, streaming, hosts, documents, related events, extra photos, and from step 14 the link name, building, room, format, highlights, colours, topics and partners). A newly copied event gets its link name -from the title and year; its building and room stay empty until an exec picks them, and the site shows its old -`location` text until then. Events deleted from +from the title and year; its building and room stay empty until an exec picks them, and the site shows "Location +TBA" until then. Events deleted from `public` aren't deleted from v2 automatically; `05_verify.sql` lists them. Once it's been reviewed and applied, the SQL files can be turned into dated files in `migrations/` so the @@ -268,4 +270,3 @@ rollback leaving `public` as it was. ## Later - Moving the admin to v2, then retiring `public.events`, `public.team` and `events-images`. -- Dropping `events.location` remains a separate follow-up after the live unmapped-location check is empty. From 1c7a7f279ee5e1fb529c9a4314246e8a958045df Mon Sep 17 00:00:00 2001 From: b4sleal Date: Tue, 1 Dec 2026 19:26:55 -0500 Subject: [PATCH 2/6] refactor(site): read where an event is from its building and room only --- apps/site/src/lib/places.ts | 8 +++----- apps/site/tests/fixtures/event-page.ts | 1 - apps/site/tests/lib/club-events.test.ts | 1 - apps/site/tests/lib/places.test.ts | 6 +++--- apps/site/tests/lib/speakers.test.ts | 2 +- 5 files changed, 7 insertions(+), 11 deletions(-) diff --git a/apps/site/src/lib/places.ts b/apps/site/src/lib/places.ts index e043791..afb9ab8 100644 --- a/apps/site/src/lib/places.ts +++ b/apps/site/src/lib/places.ts @@ -1,11 +1,10 @@ /** - * Where an event is, read from the events table: online, or a building and room. Events saved before buildings - * existed only have the old free-text `location`, which is used until an exec picks a building. + * Where an event is, read from the events table: online, or a building and room. */ import { z } from 'zod'; /** The columns that say where an event is. Select these with every event the site shows a place for. */ -export const PLACE_COLUMNS = 'format, building_code, room, location'; +export const PLACE_COLUMNS = 'format, building_code, room'; const optionalText = z .string() @@ -18,7 +17,6 @@ export const PlaceRowSchema = z.object({ format: z.enum(['in_person', 'online']), building_code: optionalText, room: optionalText, - location: optionalText, }); export type PlaceRow = z.infer; @@ -28,7 +26,7 @@ export type PlaceRow = z.infer; */ export function placeLabel(place: PlaceRow): string | null { if (place.format === 'online') return 'Online'; - if (place.building_code === null) return place.location; + if (place.building_code === null) return null; if (place.room === null) return place.building_code; // A room number joins its building the way students write it ("BA201", "P110A/B"); words get a dot. return /^\d\S*$/.test(place.room) ? `${place.building_code}${place.room}` : `${place.building_code} · ${place.room}`; diff --git a/apps/site/tests/fixtures/event-page.ts b/apps/site/tests/fixtures/event-page.ts index 18e5990..73b3c96 100644 --- a/apps/site/tests/fixtures/event-page.ts +++ b/apps/site/tests/fixtures/event-page.ts @@ -30,7 +30,6 @@ export function eventRow(overrides: Record = {}) { format: 'in_person', building_code: 'LH', room: '3094', - location: null, meeting_url: null, stream_url: 'https://youtube.com/live/abc', highlights: ['food', 'speaker_series'], diff --git a/apps/site/tests/lib/club-events.test.ts b/apps/site/tests/lib/club-events.test.ts index 82ed6c0..413ef1c 100644 --- a/apps/site/tests/lib/club-events.test.ts +++ b/apps/site/tests/lib/club-events.test.ts @@ -13,7 +13,6 @@ const ROW = { format: 'in_person', building_code: 'N', room: '2085', - location: null, }; describe('toClubEvent', () => { diff --git a/apps/site/tests/lib/places.test.ts b/apps/site/tests/lib/places.test.ts index 7c30893..86bf93e 100644 --- a/apps/site/tests/lib/places.test.ts +++ b/apps/site/tests/lib/places.test.ts @@ -1,16 +1,16 @@ import { describe, expect, it } from 'vitest'; import { mapsEmbed, mapsSearch, placeLabel, type PlaceRow } from '../../src/lib/places'; -const inPerson: PlaceRow = { format: 'in_person', building_code: null, room: null, location: null }; +const inPerson: PlaceRow = { format: 'in_person', building_code: null, room: null }; describe('placeLabel', () => { it.each([ ['a building and a room number', { ...inPerson, building_code: 'BA', room: '201' }, 'BA201'], ['a building and a room in words', { ...inPerson, building_code: 'LH', room: '3rd floor staircase' }, 'LH · 3rd floor staircase'], ['a building with no room', { ...inPerson, building_code: 'AC' }, 'AC'], - ['only the old free-text place', { ...inPerson, location: 'Arts 1E1' }, 'Arts 1E1'], - ['an online event', { ...inPerson, format: 'online' as const, location: 'Discord' }, 'Online'], + ['an online event', { ...inPerson, format: 'online' as const }, 'Online'], ['nothing known yet', inPerson, null], + ['a room with no building picked yet', { ...inPerson, room: '201' }, null], ])('shows %s', (_, place, label) => { expect(placeLabel(place)).toBe(label); }); diff --git a/apps/site/tests/lib/speakers.test.ts b/apps/site/tests/lib/speakers.test.ts index 2d84522..b087cc5 100644 --- a/apps/site/tests/lib/speakers.test.ts +++ b/apps/site/tests/lib/speakers.test.ts @@ -34,7 +34,7 @@ const EVENT: EditionEvent = { edition: 'Fall 2026', room: 'LH3094', posterPath: describe('toEditionEvent', () => { it('reads the event into its term and room, keeping its hosts for toSpeaker', () => { const hosts = [host()]; - const place = { format: 'in_person', building_code: 'LH', room: '3094', location: null }; + const place = { format: 'in_person', building_code: 'LH', room: '3094' }; const row = { date: '2026-11-19', ...place, poster_path: 'mtp/poster.png', event_hosts: hosts }; expect(toEditionEvent(row)).toEqual({ ...EVENT, hosts }); }); From 1b19efca628a28858d54bae588de813fbd20cee3 Mon Sep 17 00:00:00 2001 From: b4sleal Date: Tue, 1 Dec 2026 19:26:55 -0500 Subject: [PATCH 3/6] feat(admin): v2 CSV formats for every list saved all or nothing, the kind of event on tickets, and no old free-text location --- .../components/events/EventExpectFields.tsx | 2 +- .../admin/src/components/events/EventForm.tsx | 1 - .../components/events/EventPlaceFields.tsx | 12 +- .../components/events/EventResourcesPanel.tsx | 2 +- .../src/components/events/EventWhenFields.tsx | 2 +- .../src/components/events/ImportTicket.tsx | 16 +- apps/admin/src/components/events/Ticket.tsx | 14 +- .../src/components/events/event-import.ts | 53 +++++- .../src/components/guests/CompanySheet.tsx | 21 +-- .../admin/src/components/ui/CsvImportPage.tsx | 72 ++++++++ apps/admin/src/lib/csv/companies-csv.ts | 23 +++ apps/admin/src/lib/csv/csv-formats.ts | 126 +++---------- apps/admin/src/lib/csv/csv-import.ts | 3 + apps/admin/src/lib/csv/events-csv.ts | 142 +++++++++++++++ apps/admin/src/lib/csv/guests-csv.ts | 45 +++++ apps/admin/src/lib/csv/team-csv.ts | 40 ++++ apps/admin/src/lib/data/companies.ts | 24 ++- apps/admin/src/lib/data/events.ts | 31 ++-- apps/admin/src/lib/data/guests.ts | 18 +- apps/admin/src/lib/data/team.ts | 23 +-- apps/admin/src/lib/form/company-form.ts | 23 +++ apps/admin/src/lib/form/event-form.ts | 23 +-- apps/admin/src/lib/index.ts | 4 + apps/admin/src/lib/ui/accents.ts | 18 +- .../events => lib/ui}/event-labels.ts | 0 apps/admin/src/pages/EventsPage.tsx | 5 +- apps/admin/src/pages/ImportEventsPage.tsx | 66 ++----- apps/admin/src/pages/ImportTeamPage.tsx | 57 +----- apps/admin/src/pages/TeamPage.tsx | 3 +- apps/admin/src/types/database.types.ts | 19 +- apps/admin/src/types/event.ts | 20 +- apps/admin/tests/fixtures/events.ts | 1 - .../admin/tests/lib/csv/companies-csv.test.ts | 46 +++++ apps/admin/tests/lib/csv/csv-formats.test.ts | 166 ++--------------- apps/admin/tests/lib/csv/csv-import.test.ts | 125 ++++++------- apps/admin/tests/lib/csv/csv-test-helpers.ts | 9 + apps/admin/tests/lib/csv/events-csv.test.ts | 165 +++++++++++++++++ apps/admin/tests/lib/csv/guests-csv.test.ts | 64 +++++++ apps/admin/tests/lib/csv/team-csv.test.ts | 57 ++++++ apps/admin/tests/lib/data/companies.test.ts | 33 +++- apps/admin/tests/lib/data/events.test.ts | 57 ++++-- apps/admin/tests/lib/data/guests.test.ts | 25 ++- apps/admin/tests/lib/data/team.test.ts | 25 ++- apps/admin/tests/lib/form/event-form.test.ts | 20 +- .../tests/lib/server/supabase-v2-mock.ts | 7 + .../admin/tests/pages/CsvImportPages.test.tsx | 171 ++++++++++++++++-- .../admin/tests/pages/EventFormPages.test.tsx | 9 +- apps/admin/tests/pages/EventsPage.test.tsx | 10 +- apps/admin/tests/types/event.test.ts | 5 +- 49 files changed, 1309 insertions(+), 594 deletions(-) create mode 100644 apps/admin/src/components/ui/CsvImportPage.tsx create mode 100644 apps/admin/src/lib/csv/companies-csv.ts create mode 100644 apps/admin/src/lib/csv/events-csv.ts create mode 100644 apps/admin/src/lib/csv/guests-csv.ts create mode 100644 apps/admin/src/lib/csv/team-csv.ts create mode 100644 apps/admin/src/lib/form/company-form.ts rename apps/admin/src/{components/events => lib/ui}/event-labels.ts (100%) create mode 100644 apps/admin/tests/lib/csv/companies-csv.test.ts create mode 100644 apps/admin/tests/lib/csv/csv-test-helpers.ts create mode 100644 apps/admin/tests/lib/csv/events-csv.test.ts create mode 100644 apps/admin/tests/lib/csv/guests-csv.test.ts create mode 100644 apps/admin/tests/lib/csv/team-csv.test.ts diff --git a/apps/admin/src/components/events/EventExpectFields.tsx b/apps/admin/src/components/events/EventExpectFields.tsx index cc69bb1..0becf77 100644 --- a/apps/admin/src/components/events/EventExpectFields.tsx +++ b/apps/admin/src/components/events/EventExpectFields.tsx @@ -8,7 +8,7 @@ import type { EventFormValues } from '../../lib/form/event-form'; import { EVENT_HIGHLIGHTS } from '../../types/event'; import { FIELD_INPUT } from '../ui/field-styles'; import { FormField } from '../ui/FormField'; -import { HIGHLIGHT_NAMES } from './event-labels'; +import { HIGHLIGHT_NAMES } from '../../lib/ui/event-labels'; type EventExpectFieldsProps = { register: UseFormRegister; diff --git a/apps/admin/src/components/events/EventForm.tsx b/apps/admin/src/components/events/EventForm.tsx index 970d737..dd96efb 100644 --- a/apps/admin/src/components/events/EventForm.tsx +++ b/apps/admin/src/components/events/EventForm.tsx @@ -80,7 +80,6 @@ export function EventForm({ kind, defaultValues, unreadableTime, pictureUrl, sav register={register} errors={errors} format={values.format} - oldLocation={defaultValues.buildingCode === '' ? defaultValues.location : ''} onFormatChange={(next) => setValue('format', next, { shouldDirty: true })} /> diff --git a/apps/admin/src/components/events/EventPlaceFields.tsx b/apps/admin/src/components/events/EventPlaceFields.tsx index d0a5761..5eb57d3 100644 --- a/apps/admin/src/components/events/EventPlaceFields.tsx +++ b/apps/admin/src/components/events/EventPlaceFields.tsx @@ -1,7 +1,7 @@ /** * Where an event is, on the event form: a switch for an online event, the building and room for an in-person * one, and the meeting link for an online one. Switching online greys the building and room out, since an online - * event has neither. An old event with no building yet shows its free-text location under the picker. + * event has neither. */ import { Link2 } from 'lucide-react'; import type { FieldErrors, UseFormRegister } from 'react-hook-form'; @@ -16,19 +16,13 @@ type EventPlaceFieldsProps = { register: UseFormRegister; errors: FieldErrors; format: EventFormat; - /** The saved free-text location of an event that has no building yet, or empty. */ - oldLocation: string; onFormatChange: (format: EventFormat) => void; }; -export function EventPlaceFields({ register, errors, format, oldLocation, onFormatChange }: EventPlaceFieldsProps) { +export function EventPlaceFields({ register, errors, format, onFormatChange }: EventPlaceFieldsProps) { const buildings = useBuildings(); const online = format === 'online'; - const buildingHint = buildings.isError - ? "The buildings couldn't be loaded. Reload the page to try again." - : !online && oldLocation - ? `Saved as "${oldLocation}". Pick its building to replace it.` - : null; + const buildingHint = buildings.isError ? "The buildings couldn't be loaded. Reload the page to try again." : null; return (
diff --git a/apps/admin/src/components/events/EventResourcesPanel.tsx b/apps/admin/src/components/events/EventResourcesPanel.tsx index 467874e..b803899 100644 --- a/apps/admin/src/components/events/EventResourcesPanel.tsx +++ b/apps/admin/src/components/events/EventResourcesPanel.tsx @@ -16,7 +16,7 @@ import { ConfirmDialog } from '../ui/ConfirmDialog'; import { FIELD_INPUT } from '../ui/field-styles'; import { FormField } from '../ui/FormField'; import { Tag } from '../ui/Tag'; -import { DOCUMENT_KIND_NAMES } from './event-labels'; +import { DOCUMENT_KIND_NAMES } from '../../lib/ui/event-labels'; import { ExtraPanel } from './ExtraPanel'; export function EventResourcesPanel({ eventId }: { eventId: string }) { diff --git a/apps/admin/src/components/events/EventWhenFields.tsx b/apps/admin/src/components/events/EventWhenFields.tsx index 8192d37..0699756 100644 --- a/apps/admin/src/components/events/EventWhenFields.tsx +++ b/apps/admin/src/components/events/EventWhenFields.tsx @@ -6,7 +6,7 @@ import { type EventFormValues, lengthOptions } from '../../lib/form/event-form'; import { EVENT_CATEGORIES } from '../../types/event'; import { FIELD_INPUT } from '../ui/field-styles'; import { FormField } from '../ui/FormField'; -import { CATEGORY_NAMES } from './event-labels'; +import { CATEGORY_NAMES } from '../../lib/ui/event-labels'; type EventWhenFieldsProps = { register: UseFormRegister; diff --git a/apps/admin/src/components/events/ImportTicket.tsx b/apps/admin/src/components/events/ImportTicket.tsx index 6aec197..01ce1e6 100644 --- a/apps/admin/src/components/events/ImportTicket.tsx +++ b/apps/admin/src/components/events/ImportTicket.tsx @@ -9,14 +9,24 @@ import type { ImportEntry } from '../ui/import-view'; import { importedEvent } from './event-import'; import { Ticket } from './Ticket'; -// The values on a ticket's face that a CSV can change; a description is only in the details. -const FACE_FIELDS: readonly (keyof TicketFace)[] = ['title', 'date', 'time', 'location']; +// The values on a ticket's face that a CSV can change, and the columns behind each. The rest (a description, the +// colours) are only in the details. +const FACE_FIELDS = ['title', 'category', 'date', 'time', 'location'] as const satisfies readonly (keyof TicketFace)[]; +const FACE_COLUMNS: Readonly> = { + title: ['title'], + category: ['category'], + date: ['date'], + time: ['start_time', 'end_time'], + location: ['format', 'building_code', 'room'], +}; export function ImportTicket({ entry }: { entry: ImportEntry }) { const event = importedEvent(entry); const pictureUrl = entry.kind === 'updated' ? entry.item.after.pictureUrl : null; const face = toTicketFace({ ...event, pictureUrl }); - const marked = new Set(entry.kind === 'updated' ? FACE_FIELDS.filter((key) => entry.item.cells.some((cell) => cell.column === key)) : []); + const marked = new Set( + entry.kind === 'updated' ? FACE_FIELDS.filter((key) => entry.item.cells.some((cell) => FACE_COLUMNS[key].includes(cell.column))) : [], + ); // "Next up" depends on every other event, so an imported one is upcoming or past, like the form's preview. const status = isUpcoming(event, clubToday()) ? 'upcoming' : 'past'; return ; diff --git a/apps/admin/src/components/events/Ticket.tsx b/apps/admin/src/components/events/Ticket.tsx index 1fd6b0d..1476af2 100644 --- a/apps/admin/src/components/events/Ticket.tsx +++ b/apps/admin/src/components/events/Ticket.tsx @@ -1,14 +1,18 @@ /** * An event drawn as a ticket: a date stub coloured by its status, a perforated tear line, the picture, title, - * when and where, and a barcode end. It lays itself out by its own width, not the screen's: a band across the - * top when narrow (a phone, or the ticket printer), a stub on the left when wide. `actions` go at the end of + * its kind (a dot in the kind's accent), when and where, and a barcode end. It lays itself out by its own width, + * not the screen's: a band across the top when narrow (a phone, or the ticket printer), a stub on the left when + * wide. `actions` go at the end of * the body, like Edit and Delete on the Events list. In a CSV import a ticket also shows its `importKind` (new * or updated) and puts a highlighter over the `marked` values that will change. */ import { Calendar, Clock, ImageIcon, MapPin } from 'lucide-react'; import type { ReactNode } from 'react'; import { formatEventDay } from '../../lib/time/dates'; +import { CATEGORY_ACCENTS } from '../../lib/ui/accents'; +import { CATEGORY_NAMES } from '../../lib/ui/event-labels'; import type { EventStatus, TicketFace } from '../../types/event'; +import { ACCENT_FILLS } from '../ui/accent-styles'; import type { ImportKind } from '../ui/import-labels'; import { Tag, type TagTone } from '../ui/Tag'; import { TicketStub } from './TicketStub'; @@ -71,6 +75,12 @@ export function Ticket({ face, status, actions, importKind, marked = NOTHING_MAR {look.label}
+ diff --git a/apps/admin/src/components/events/event-import.ts b/apps/admin/src/components/events/event-import.ts index 585aba0..be6a537 100644 --- a/apps/admin/src/components/events/event-import.ts +++ b/apps/admin/src/components/events/event-import.ts @@ -2,8 +2,9 @@ * How events read in a CSV import: an event's name, every field in its details, and its lines on the print * order. The ticket itself is `ImportTicket`. */ -import { shortEventDay } from '../../lib/time/dates'; import { eventTimeLabel } from '../../lib/form/event-form'; +import { shortEventDay } from '../../lib/time/dates'; +import { CATEGORY_NAMES, HIGHLIGHT_NAMES } from '../../lib/ui/event-labels'; import { type Event, type EventFields, placeLabel } from '../../types/event'; import type { FieldChange } from '../ui/FieldChanges'; import type { ImportEntry, ImportView, OrderItem } from '../ui/import-view'; @@ -15,8 +16,22 @@ export function importedEvent(entry: EventEntry): EventFields { return entry.kind === 'new' ? entry.item.fields : entry.item.after; } -// What each column is called on the print order, in the words the ticket uses. -const ORDER_LABELS: Readonly> = { title: 'title', date: 'date', time: 'time', location: 'room', description: 'description' }; +// What each column is called on the print order, in the words the event form uses. +const ORDER_LABELS: Readonly> = { + slug: 'link name', + start_time: 'starts', + end_time: 'ends', + format: 'in person or online', + building_code: 'building', + meeting_url: 'meeting link', + stream_url: 'stream link', + dress_code: 'dress code', + primary_colour: 'main colour', + secondary_colour: 'second colour', +}; + +// Columns too long for a receipt line; the event's details show what changed in them. +const LONG_COLUMNS: ReadonlySet = new Set(['description', 'meeting_url', 'stream_url']); /** "→ BA201"; an emptied value reads "→ none". */ function becomes(value: string): string { @@ -26,11 +41,21 @@ function becomes(value: string): string { function changeLines(entry: Extract): readonly OrderItem[] { return entry.item.cells.map(({ column, after }) => ({ label: ORDER_LABELS[column] ?? column, - // A description is too long for a receipt line; the event's details show what changed in it. - value: column === 'description' ? 'edited' : becomes(column === 'date' ? shortEventDay(after) : after), + value: LONG_COLUMNS.has(column) ? (after ? 'edited' : 'removed') : becomes(column === 'date' ? shortEventDay(after) : after), })); } +/** "Food, Drinks", or "None". */ +function highlightsOf(event: Pick): string { + return event.highlights.map((highlight) => HIGHLIGHT_NAMES[highlight]).join(', ') || 'None'; +} + +/** "#ff9770 and #ffd670", or "LCS yellow and pink" when the event uses the default. */ +function coloursOf(event: Pick): string { + if (!event.primaryColour && !event.secondaryColour) return 'LCS yellow and pink'; + return [event.primaryColour ?? 'LCS yellow', event.secondaryColour ?? 'LCS pink'].join(' and '); +} + /** A field as the details show it. `before` is left out for a new event, which has nothing to compare. */ function field(label: string, before: string | undefined, after: string): FieldChange { return { label, before: before ?? null, after }; @@ -49,7 +74,14 @@ export const EVENT_IMPORT = { field('Title', was?.title, will.title), field('Date', was && shortEventDay(was.date), shortEventDay(will.date)), field('Time', was && (eventTimeLabel(was) ?? 'None'), eventTimeLabel(will) ?? 'None'), - field('Room', was && (placeLabel(was) ?? 'None'), placeLabel(will) ?? 'None'), + field('Category', was && CATEGORY_NAMES[was.category], CATEGORY_NAMES[will.category]), + field('Where', was && (placeLabel(was) ?? 'None'), placeLabel(will) ?? 'None'), + field('Meeting link', was && (was.meetingUrl ?? 'None'), will.meetingUrl ?? 'None'), + field('Stream link', was && (was.streamUrl ?? 'None'), will.streamUrl ?? 'None'), + field('Highlights', was && highlightsOf(was), highlightsOf(will)), + field('Dress code', was && (was.dressCode ?? 'None'), will.dressCode ?? 'None'), + field('Colours', was && coloursOf(was), coloursOf(will)), + field('Link name', was?.slug, will.slug ?? 'Made from the title'), ], // Only an event with a description, before or after, has one to show. long: @@ -61,6 +93,13 @@ export const EVENT_IMPORT = { orderItems: (entry) => { if (entry.kind === 'updated') return changeLines(entry); const event = entry.item.fields; - return [{ label: [shortEventDay(event.date), eventTimeLabel(event), placeLabel(event)].filter(Boolean).join(' · '), value: '' }]; + return [ + { + label: [CATEGORY_NAMES[event.category], shortEventDay(event.date), eventTimeLabel(event), placeLabel(event)] + .filter(Boolean) + .join(' · '), + value: '', + }, + ]; }, } satisfies Omit, 'front'>; diff --git a/apps/admin/src/components/guests/CompanySheet.tsx b/apps/admin/src/components/guests/CompanySheet.tsx index d0b5440..3099bfb 100644 --- a/apps/admin/src/components/guests/CompanySheet.tsx +++ b/apps/admin/src/components/guests/CompanySheet.tsx @@ -7,28 +7,17 @@ import { zodResolver } from '@hookform/resolvers/zod'; import { useState } from 'react'; import { useForm, useWatch } from 'react-hook-form'; -import { z } from 'zod'; import { useCreateCompany, useUpdateCompany } from '../../hooks/useCompanies'; +import { CompanyFormSchema, type CompanyFormValues, toCompanyFields } from '../../lib/form/company-form'; import { messageFor } from '../../lib/server/errors'; import type { PictureChange } from '../../lib/ui/images'; -import { type Company, COMPANY_LINK, CompanySchema, type LogoChanges } from '../../types/company'; +import type { Company, LogoChanges } from '../../types/company'; import { Button } from '../ui/Button'; import { FIELD_INPUT } from '../ui/field-styles'; import { FormField } from '../ui/FormField'; import { Sheet } from '../ui/Sheet'; import { LogoField } from './LogoField'; -const CompanyFormSchema = CompanySchema.pick({ name: true, colour: true }).extend({ - // Empty means no link. - link: z - .string() - .trim() - .refine( - (value) => value === '' || COMPANY_LINK.safeParse(value).success, - "That isn't a whole link. Paste the full address, like https://linktr.ee/their-club.", - ), -}); - const KEEP_BOTH: LogoChanges = { face: { kind: 'keep' }, card: { kind: 'keep' } }; // A neutral starting colour; most companies' own colour is on their logo or website. @@ -47,7 +36,7 @@ export function CompanySheet({ company, onDone, onClose }: CompanySheetProps) { const update = useUpdateCompany(); const saving = create.isPending || update.isPending; const failed = create.isError ? create.error : update.isError ? update.error : null; - const { register, handleSubmit, control, formState } = useForm>({ + const { register, handleSubmit, control, formState } = useForm({ resolver: zodResolver(CompanyFormSchema), defaultValues: { name: company?.name ?? '', @@ -58,8 +47,8 @@ export function CompanySheet({ company, onDone, onClose }: CompanySheetProps) { const colour = useWatch({ control, name: 'colour' }); const [logos, setLogos] = useState>(KEEP_BOTH); - const save = handleSubmit(({ name, colour: chosen, link }) => { - const fields = { name: name.trim(), colour: chosen, link: link || null }; + const save = handleSubmit((values) => { + const fields = toCompanyFields(values); if (company) { update.mutate({ company, fields, logos }, { onSuccess: onDone }); } else { diff --git a/apps/admin/src/components/ui/CsvImportPage.tsx b/apps/admin/src/components/ui/CsvImportPage.tsx new file mode 100644 index 0000000..f8c2f1a --- /dev/null +++ b/apps/admin/src/components/ui/CsvImportPage.tsx @@ -0,0 +1,72 @@ +/** + * The page around a CSV import: its heading with the way back to the list, and the lists the file is checked + * against, loading or failed, before the import itself shows. Each list's import page gives its own queries and + * its `CsvImport`. + */ +import { RotateCw } from 'lucide-react'; +import type { ReactNode } from 'react'; +import { messageFor } from '../../lib/server/errors'; +import { Button } from './Button'; +import { FormPageHeading } from './FormPageHeading'; +import { StateBlock } from './StateBlock'; + +/** What the page needs to know about each list the file is checked against. */ +type Loaded = Readonly<{ isPending: boolean; error: Error | null; refetch: () => Promise }>; + +type CsvImportPageProps = { + title: string; + /** The list's page and name, for the way back. */ + back: Readonly<{ to: string; section: string }>; + /** What is loading, for screen readers: "Loading the events". */ + loadingLabel: string; + /** Every list the file is checked against. The import shows once they've all loaded. */ + queries: readonly Loaded[]; + /** The import, given once every list has loaded. */ + children: ReactNode; +}; + +export function CsvImportPage({ title, back, loadingLabel, queries, children }: CsvImportPageProps) { + return ( +
+ +
+ + {children} + +
+
+ ); +} + +function Loading({ label, queries, children }: Readonly<{ label: string; queries: readonly Loaded[]; children: ReactNode }>) { + const failed = queries.find((query) => query.error !== null); + if (failed?.error) { + return ( +