diff --git a/apps/admin/src/components/events/EventExpectFields.tsx b/apps/admin/src/components/events/EventExpectFields.tsx index cc69bb12..0becf77a 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 970d7377..dd96efb7 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 d0a5761a..5eb57d31 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 467874e4..b8038994 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 8192d37e..0699756b 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 6aec197c..01ce1e64 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 1fd6b0d4..94267268 100644 --- a/apps/admin/src/components/events/Ticket.tsx +++ b/apps/admin/src/components/events/Ticket.tsx @@ -1,14 +1,17 @@ /** * 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 - * 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. + * 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 +74,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 585aba0c..be6a5370 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/CompanyFace.tsx b/apps/admin/src/components/guests/CompanyFace.tsx new file mode 100644 index 00000000..fcff5416 --- /dev/null +++ b/apps/admin/src/components/guests/CompanyFace.tsx @@ -0,0 +1,22 @@ +/** + * A company's logo filling a white circle with a dashed ring in its colour, as a picked speaker's face shows it on + * the site, or its initial when it has no logo yet. The colour is the company's data, so it is set inline. + */ +import type { Company } from '../../types/company'; + +export function CompanyFace({ company }: { company: Pick }) { + return ( + + + ); +} diff --git a/apps/admin/src/components/guests/CompanyRow.tsx b/apps/admin/src/components/guests/CompanyRow.tsx index 90dfbce1..3745ac04 100644 --- a/apps/admin/src/components/guests/CompanyRow.tsx +++ b/apps/admin/src/components/guests/CompanyRow.tsx @@ -10,6 +10,7 @@ import { messageFor } from '../../lib/server/errors'; import type { Company } from '../../types/company'; import { Button } from '../ui/Button'; import { ConfirmDialog } from '../ui/ConfirmDialog'; +import { CompanyFace } from './CompanyFace'; type CompanyRowProps = { company: Company; @@ -74,21 +75,3 @@ export function CompanyRow({ company, guestCount, onEdit }: CompanyRowProps) { ); } - -/** The company's logo filling a white circle with a dashed ring in its colour, as a picked speaker's face shows it. */ -function CompanyFace({ company }: { company: Company }) { - return ( - - - ); -} diff --git a/apps/admin/src/components/guests/CompanySheet.tsx b/apps/admin/src/components/guests/CompanySheet.tsx index d0b54407..3099bfb2 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/guests/GuestHeadshot.tsx b/apps/admin/src/components/guests/GuestHeadshot.tsx new file mode 100644 index 00000000..d536f57b --- /dev/null +++ b/apps/admin/src/components/guests/GuestHeadshot.tsx @@ -0,0 +1,10 @@ +/** A guest's headshot pasted on like a small print, or their initial when there isn't one. */ +export function GuestHeadshot({ url, name }: { url: string | null; name: string }) { + const shape = 'relative size-14 shrink-0 rounded-md border-3 border-white shadow-(--shadow-cutout) sm:size-16'; + if (url) return ; + return ( + + ); +} diff --git a/apps/admin/src/components/guests/GuestLine.tsx b/apps/admin/src/components/guests/GuestLine.tsx index c48b8066..bfdb82b2 100644 --- a/apps/admin/src/components/guests/GuestLine.tsx +++ b/apps/admin/src/components/guests/GuestLine.tsx @@ -12,6 +12,7 @@ import { PATHS } from '../../paths'; import type { GuestBookLine } from '../../types/guest'; import { BOOK_COLUMNS } from './book-columns'; import { CompanyMark } from './CompanyMark'; +import { GuestHeadshot } from './GuestHeadshot'; import { spokeLine, usesLine } from './guest-words'; const ROUND_ACTION = @@ -44,7 +45,7 @@ export function GuestLine({ line, justSaved, onDelete }: GuestLineProps) { transition={{ duration: 0.6, ease: 'easeOut', delay: 0.2 }} /> )} - +

{guest.name}

{guest.title &&

{guest.title}

} @@ -91,14 +92,3 @@ export function GuestLine({ line, justSaved, onDelete }: GuestLineProps) { ); } - -/** Their headshot pasted onto the line like a small print, or their initial when there isn't one. */ -function Headshot({ url, name }: { url: string | null; name: string }) { - const shape = 'relative size-14 shrink-0 rounded-md border-3 border-white shadow-(--shadow-cutout) sm:size-16'; - if (url) return ; - return ( - - ); -} diff --git a/apps/admin/src/components/guests/ImportCompanyCard.tsx b/apps/admin/src/components/guests/ImportCompanyCard.tsx new file mode 100644 index 00000000..8f5047ea --- /dev/null +++ b/apps/admin/src/components/guests/ImportCompanyCard.tsx @@ -0,0 +1,26 @@ +/** + * A company in a CSV import, drawn as its face on the site in its colour, with its name and its new or updated + * label. Logos aren't in a CSV, so a new company shows its initial until a logo is framed on Companies. + */ +import type { Company, CompanyFields } from '../../types/company'; +import { IMPORT_LABELS } from '../ui/import-labels'; +import type { ImportEntry } from '../ui/import-view'; +import { Tag } from '../ui/Tag'; +import { CompanyFace } from './CompanyFace'; +import { importedCompany } from './company-import'; + +export function ImportCompanyCard({ entry }: { entry: ImportEntry }) { + const company = importedCompany(entry); + const logoUrl = entry.kind === 'updated' ? entry.item.after.logoUrl : null; + const label = IMPORT_LABELS[entry.kind]; + return ( +
+ +
+

{company.name}

+

{company.colour}

+
+ {label.text} +
+ ); +} diff --git a/apps/admin/src/components/guests/ImportGuestLine.tsx b/apps/admin/src/components/guests/ImportGuestLine.tsx new file mode 100644 index 00000000..6a3c110c --- /dev/null +++ b/apps/admin/src/components/guests/ImportGuestLine.tsx @@ -0,0 +1,36 @@ +/** + * A guest in a CSV import, drawn as their line in the guest book with its new or updated label. Headshots aren't + * in a CSV, so a new guest shows their initial until one is added on their page. + */ +import type { Company } from '../../types/company'; +import type { Guest, GuestFields } from '../../types/guest'; +import { IMPORT_LABELS } from '../ui/import-labels'; +import type { ImportEntry } from '../ui/import-view'; +import { Tag } from '../ui/Tag'; +import { CompanyMark } from './CompanyMark'; +import { GuestHeadshot } from './GuestHeadshot'; +import { importedGuest } from './guest-import'; + +type ImportGuestLineProps = { + entry: ImportEntry; + /** The company the guest will work at, or null for none. */ + company: Company | null; +}; + +export function ImportGuestLine({ entry, company }: ImportGuestLineProps) { + const guest = importedGuest(entry); + const label = IMPORT_LABELS[entry.kind]; + return ( +
+ +
+

{guest.name}

+ {guest.title &&

{guest.title}

} +

+ +

+
+ {label.text} +
+ ); +} diff --git a/apps/admin/src/components/guests/company-import.ts b/apps/admin/src/components/guests/company-import.ts new file mode 100644 index 00000000..3ef91bae --- /dev/null +++ b/apps/admin/src/components/guests/company-import.ts @@ -0,0 +1,53 @@ +/** + * How companies read in a CSV import: a company's name, every field in its details, and its lines on the print + * order. Its card itself is `ImportCompanyCard`. + */ +import type { Company, CompanyFields } from '../../types/company'; +import type { FieldChange } from '../ui/FieldChanges'; +import type { ImportEntry, ImportView, OrderItem } from '../ui/import-view'; + +type CompanyEntry = ImportEntry; + +/** The company as it will be once imported. */ +export function importedCompany(entry: CompanyEntry): CompanyFields { + return entry.kind === 'new' ? entry.item.fields : entry.item.after; +} + +// What each column is called on the print order, and how its new value reads there. +const ORDER_LINES: Readonly OrderItem>> = { + name: (after) => ({ label: 'name', value: `→ ${after}` }), + colour: (after) => ({ label: 'colour', value: `→ ${after}` }), + link: (after) => ({ label: 'link', value: after ? 'new link' : 'removed' }), +}; + +/** A field as the details show it. `before` is left out for a new company, which has nothing to compare. */ +function field(label: string, before: string | undefined, after: string): FieldChange { + return { label, before: before ?? null, after }; +} + +export const COMPANY_IMPORT = { + noun: { one: 'company', many: 'companies' }, + piece: 'card', + arrangement: 'cards', + nameOf: (entry) => importedCompany(entry).name, + fields: (entry) => { + const was = entry.kind === 'updated' ? entry.item.before : undefined; + const will = importedCompany(entry); + return { + short: [ + field('Name', was?.name, will.name), + field('Colour', was?.colour, will.colour), + field('Link', was && (was.link ?? 'None'), will.link ?? 'None'), + ], + }; + }, + orderItems: (entry) => { + if (entry.kind === 'updated') { + return entry.item.cells.flatMap(({ column, after }) => { + const line = ORDER_LINES[column]; + return line ? [line(after)] : []; + }); + } + return [{ label: entry.item.fields.colour, value: '' }]; + }, +} satisfies Omit, 'front'>; diff --git a/apps/admin/src/components/guests/guest-import.ts b/apps/admin/src/components/guests/guest-import.ts new file mode 100644 index 00000000..02ca8488 --- /dev/null +++ b/apps/admin/src/components/guests/guest-import.ts @@ -0,0 +1,63 @@ +/** + * How guests read in a CSV import: a guest's name, every field in their details, and their lines on the print + * order. Their line itself is `ImportGuestLine`. A guest's company is saved as its id, so the view is made with the + * companies, to say each one's name. + */ +import type { Company } from '../../types/company'; +import type { Guest, GuestFields } from '../../types/guest'; +import type { FieldChange } from '../ui/FieldChanges'; +import type { ImportEntry, ImportView, OrderItem } from '../ui/import-view'; + +type GuestEntry = ImportEntry; + +/** The guest as they will be once imported. */ +export function importedGuest(entry: GuestEntry): GuestFields { + return entry.kind === 'new' ? entry.item.fields : entry.item.after; +} + +// What each column is called on the print order, and how its new value reads there. +const ORDER_LINES: Readonly OrderItem>> = { + name: (after) => ({ label: 'name', value: `→ ${after}` }), + title: (after) => ({ label: 'title', value: `→ ${after || 'none'}` }), + company: (after) => ({ label: 'company', value: `→ ${after || 'none'}` }), + link_url: (after) => ({ label: 'link', value: after ? 'new link' : 'removed' }), +}; + +/** A field as the details show it. `before` is left out for a new guest, who has nothing to compare. */ +function field(label: string, before: string | undefined, after: string): FieldChange { + return { label, before: before ?? null, after }; +} + +/** The guests' import view, without the line itself. `companies` name each guest's company. */ +export function guestImport(companies: readonly Company[]): Omit, 'front'> { + const byId = new Map(companies.map((company) => [company.id, company])); + const companyName = (guest: GuestFields) => (guest.companyId ? (byId.get(guest.companyId)?.name ?? null) : null); + return { + noun: { one: 'guest', many: 'guests' }, + piece: 'line', + arrangement: 'rows', + nameOf: (entry) => importedGuest(entry).name, + fields: (entry) => { + const was = entry.kind === 'updated' ? entry.item.before : undefined; + const will = importedGuest(entry); + return { + short: [ + field('Name', was?.name, will.name), + field('Title', was && (was.title ?? 'None'), will.title ?? 'None'), + field('Company', was && (companyName(was) ?? 'None'), companyName(will) ?? 'None'), + field('Link', was && (was.linkUrl ?? 'None'), will.linkUrl ?? 'None'), + ], + }; + }, + orderItems: (entry) => { + if (entry.kind === 'updated') { + return entry.item.cells.flatMap(({ column, after }) => { + const line = ORDER_LINES[column]; + return line ? [line(after)] : []; + }); + } + const guest = entry.item.fields; + return [{ label: [guest.title, companyName(guest)].filter(Boolean).join(' · '), value: '' }]; + }, + }; +} diff --git a/apps/admin/src/components/ui/CsvImportPage.tsx b/apps/admin/src/components/ui/CsvImportPage.tsx new file mode 100644 index 00000000..f8c2f1aa --- /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 ( +
@@ -119,6 +128,17 @@ function GuestBookResults({ savedName }: { savedName: string | null }) { } } +/** Saves every guest as a CSV file, with each one's company by name. */ +function ExportGuestsButton() { + const guests = useGuests(); + const companies = useCompanies(); + const getFile = + guests.status === 'success' && companies.status === 'success' + ? () => csvFile(guestsCsv(companies.data), guests.data, clubToday()) + : null; + return ; +} + /** Grey lines in the shape of the guest book's, shown while it loads. */ function GuestBookSkeleton() { return ( diff --git a/apps/admin/src/pages/ImportCompaniesPage.tsx b/apps/admin/src/pages/ImportCompaniesPage.tsx new file mode 100644 index 00000000..5d154363 --- /dev/null +++ b/apps/admin/src/pages/ImportCompaniesPage.tsx @@ -0,0 +1,27 @@ +/** + * Importing companies from a CSV file, as a print order: the file is checked against the saved companies, the new + * and updated ones come out of the press as their faces on the site, and Import saves all of them or none. + */ +import { COMPANY_IMPORT } from '../components/guests/company-import'; +import { ImportCompanyCard } from '../components/guests/ImportCompanyCard'; +import { CsvImport } from '../components/ui/CsvImport'; +import { CsvImportPage } from '../components/ui/CsvImportPage'; +import type { ImportView } from '../components/ui/import-view'; +import { useCompanies, useImportCompanies } from '../hooks/useCompanies'; +import { COMPANIES_CSV } from '../lib/csv/companies-csv'; +import { PATHS } from '../paths'; +import type { Company, CompanyFields } from '../types/company'; + +const BACK = { to: PATHS.companies, section: 'Companies' }; + +const VIEW: ImportView = { ...COMPANY_IMPORT, front: (entry) => }; + +export function ImportCompaniesPage() { + const companies = useCompanies(); + const saver = useImportCompanies(); + return ( + + {companies.data && } + + ); +} diff --git a/apps/admin/src/pages/ImportEventsPage.tsx b/apps/admin/src/pages/ImportEventsPage.tsx index e01ef99c..19add7b6 100644 --- a/apps/admin/src/pages/ImportEventsPage.tsx +++ b/apps/admin/src/pages/ImportEventsPage.tsx @@ -1,18 +1,16 @@ /** - * Importing events from a CSV file, as a print order: the file is checked against the saved events, the new and - * updated ones come out of the press as the tickets they will be, and Import saves all of them or none. + * Importing events from a CSV file, as a print order: the file is checked against the saved events and the + * buildings, the new and updated ones come out of the press as the tickets they will be, and Import saves all of + * them or none. */ -import { RotateCw } from 'lucide-react'; import { EVENT_IMPORT } from '../components/events/event-import'; import { ImportTicket } from '../components/events/ImportTicket'; -import { Button } from '../components/ui/Button'; import { CsvImport } from '../components/ui/CsvImport'; -import { FormPageHeading } from '../components/ui/FormPageHeading'; +import { CsvImportPage } from '../components/ui/CsvImportPage'; import type { ImportView } from '../components/ui/import-view'; -import { StateBlock } from '../components/ui/StateBlock'; +import { useBuildings } from '../hooks/useBuildings'; import { useEvents, useImportEvents } from '../hooks/useEvents'; -import { EVENTS_CSV } from '../lib/csv/csv-formats'; -import { messageFor } from '../lib/server/errors'; +import { eventsCsv } from '../lib/csv/events-csv'; import { PATHS } from '../paths'; import type { Event, EventFields } from '../types/event'; @@ -21,50 +19,14 @@ const BACK = { to: PATHS.events, section: 'Events' }; const VIEW: ImportView = { ...EVENT_IMPORT, front: (entry) => }; export function ImportEventsPage() { - return ( -
- -
- -
-
- ); -} - -/** The import once the saved events have loaded, since the file is checked against them. */ -function EventsImport() { const events = useEvents(); + const buildings = useBuildings(); const saver = useImportEvents(); - - switch (events.status) { - case 'pending': - return
; - case 'error': - return ( -