Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions apps/admin/src/components/photos/BatchDefaultsFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ export function BatchDefaultsFields({ defaults, events, speakers, onChange }: Ba
onChange={(isTeamPhoto) => onChange({ ...defaults, isTeamPhoto })}
/>
</div>
<div>
<p className="mb-2 font-display text-base font-bold text-white">Event's page</p>
<Switch
checked={defaults.onEventPage}
saving={false}
label="Show these photos on their event's page"
stateText={defaults.onEventPage ? 'On the page' : 'Not shown'}
onChange={(onEventPage) => onChange({ ...defaults, onEventPage })}
/>
</div>
</fieldset>
);
}
Expand Down
8 changes: 5 additions & 3 deletions apps/admin/src/components/photos/BulkBar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* What to do with the selected photos, all at once: a short row of actions floating over the wall, small enough
* to leave the photos in view on a phone. Event, Guest, Describe and Credits each open a sheet with their one
* field; Delete asks first; with one photo selected, Edit opens it. The bar says what changed.
* to leave the photos in view on a phone. Event, Guest, Describe, Credits and On page each open a sheet with their
* one field; Delete asks first; with one photo selected, Edit opens it. The bar says what changed.
*/
import {
CalendarDays,
Clapperboard,
Globe,
PenLine,
Text,
Trash2,
Expand All @@ -30,9 +31,10 @@ const SHEET_ACTIONS: ReadonlyArray<{ action: BulkAction; label: string; icon: Lu
{ action: 'guest', label: 'Guest', icon: UserPlus },
{ action: 'describe', label: 'Describe', icon: Text },
{ action: 'credits', label: 'Credits', icon: Clapperboard },
{ action: 'page', label: 'On page', icon: Globe },
];

// Each action is an icon over its name, so five fit across a phone and each is still a 44px target.
// Each action is an icon over its name, so six fit across a phone and each is still a 44px target.
const ACTION =
'flex min-h-14 min-w-11 flex-1 flex-col items-center justify-center gap-1 rounded-field px-1 font-display text-xs font-bold transition-[background-color,color,scale] duration-(--dur-fast) active:scale-97 motion-reduce:transition-none sm:min-w-20';

Expand Down
24 changes: 23 additions & 1 deletion apps/admin/src/components/photos/BulkSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import { Sheet } from '../ui/Sheet';
import { SpeakersNote } from './SpeakersNote';

/** The actions on the selected photos that open a sheet. Delete asks in its own dialog instead. */
export type BulkAction = 'event' | 'guest' | 'describe' | 'credits';
export type BulkAction = 'event' | 'guest' | 'describe' | 'credits' | 'page';

const TITLES: Readonly<Record<BulkAction, string>> = {
event: 'Set the event',
guest: 'Tag a guest',
describe: 'Describe them',
credits: 'Team credits',
page: "Event's page",
};

type BulkSheetProps = {
Expand Down Expand Up @@ -125,6 +126,27 @@ export function BulkSheet({ action, count, events, speakers, busy, error, onChan
</Button>
</div>
);
case 'page':
return (
<div className="grid gap-3 sm:flex">
<Button
variant="primary"
size="small"
disabled={busy}
onClick={() => onChange({ onEventPage: true }, `Put ${count} on their event's page.`)}
>
Show on the event's page
</Button>
<Button
variant="quiet"
size="small"
disabled={busy}
onClick={() => onChange({ onEventPage: false }, `Took ${count} off their event's page.`)}
>
Take off the page
</Button>
</div>
);
}
})();

Expand Down
11 changes: 11 additions & 0 deletions apps/admin/src/components/photos/PhotoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function PhotoForm({ photo, events, guests, speakersAt, saving, error, sa
description: photo.description,
eventId: photo.eventId,
isTeamPhoto: photo.isTeamPhoto,
onEventPage: photo.onEventPage,
focus: photo.focus,
guestIds: guestIdsOf(photo),
});
Expand Down Expand Up @@ -145,6 +146,16 @@ export function PhotoForm({ photo, events, guests, speakersAt, saving, error, sa
onChange={(isTeamPhoto) => change({ isTeamPhoto })}
/>
</div>
<div>
<p className="mb-2 font-display text-base font-bold text-white">Event's page</p>
<Switch
checked={edits.onEventPage}
saving={false}
label="Show this photo on its event's page"
stateText={edits.onEventPage ? 'On the page' : 'Not shown'}
onChange={(onEventPage) => change({ onEventPage })}
/>
</div>
<div className="flex flex-wrap items-center gap-3">
<Button type="submit" variant="primary" size="small" disabled={saving}>
{saving ? 'Saving…' : 'Save photo'}
Expand Down
1 change: 1 addition & 0 deletions apps/admin/src/components/photos/PhotoTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function PhotoTile({ photo, selected, selecting, guests, rowHeight, onTog
className="pointer-events-none absolute top-1.5 right-1.5 flex items-center gap-1.5 sm:top-2.5 sm:right-2.5"
>
{photo.isTeamPhoto && <Tag tone="info">Team</Tag>}
{photo.onEventPage && <Tag tone="highlight">On page</Tag>}
{faces.map((guest, index) => (
<Face key={guestIds[index]} guest={guest} />
))}
Expand Down
66 changes: 34 additions & 32 deletions apps/admin/src/lib/data/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Reading, adding, changing, importing and deleting events in Supabase v2, with cover photos. Rows are checked
* and turned into `Event`s here, once, so pages get typed data.
* Reading, adding, changing, importing and deleting events in Supabase v2, with cover photos. The cover is the
* event's poster, kept in `events.poster_path`, the same column the site reads. Rows are checked and turned into
* `Event`s here, once, so pages get typed data.
*/
import { z } from 'zod';
import type { Database } from '../../types/database.types';
Expand All @@ -14,11 +15,10 @@ import { getSupabase } from '../server/supabase';
type EventRow = Database['v2']['Tables']['events']['Row'];
type EventInsert = Database['v2']['Tables']['events']['Insert'];
type EventUpdate = Database['v2']['Tables']['events']['Update'];
type PhotoRow = Pick<Database['v2']['Tables']['event_photos']['Row'], 'id' | 'path' | 'is_cover'>;

// One literal, not joined pieces: supabase-js reads the row's type from this exact text.
const COLUMNS =
'id, title, description, date, start_time, end_time, category, slug, format, building_code, room, location, meeting_url, stream_url, highlights, dress_code, primary_colour, secondary_colour, event_photos(id, path, is_cover)';
'id, title, description, date, start_time, end_time, category, slug, format, building_code, room, location, meeting_url, stream_url, highlights, dress_code, primary_colour, secondary_colour, poster_path';

// Postgres' code for a broken unique rule. On events the only unique column we write is the slug.
const UNIQUE_VIOLATION = '23505';
Expand Down Expand Up @@ -69,7 +69,7 @@ export async function createEvent(fields: EventFields, picture: PictureChange):
const eventId = data[0]?.id;
if (!eventId) throw new UnavailableError(`"${fields.title}" couldn't be saved.`);
try {
await applyCoverPhoto(eventId, picture, { coverPath: null, coverPhotoId: null });
await applyCoverPhoto(eventId, picture, null);
} catch (cause) {
await v2().from('events').delete().eq('id', eventId);
throw cause;
Expand All @@ -86,11 +86,11 @@ export async function updateEvent(event: Event, changes: Partial<EventFields>, p
const { data, error } = await v2().from('events').update(toUpdate(changes)).eq('id', event.id).select('id');
if (error || data.length === 0) throw saveFailure("Your changes couldn't be saved.", error);
}
await applyCoverPhoto(event.id, picture, event);
await applyCoverPhoto(event.id, picture, event.coverPath);
}

/**
* Deletes an event (its cover photo row goes with it), then the file in storage when it is ours.
* Deletes an event, then its cover photo's file in storage.
* @throws UnavailableError when the event could not be deleted (Supabase down, or the delete was refused).
*/
export async function deleteEvent(event: Event): Promise<void> {
Expand Down Expand Up @@ -119,30 +119,33 @@ export async function importEvents({ added, changed }: ImportRows<EventFields>):
}
}

async function applyCoverPhoto(
eventId: string,
picture: PictureChange,
current: Readonly<Pick<Event, 'coverPath' | 'coverPhotoId'>>,
): Promise<void> {
/**
* Points the event's poster at a newly uploaded file, or at nothing, then deletes the file it pointed at before.
* `currentPath` is that file, or null when there was none.
*/
async function applyCoverPhoto(eventId: string, picture: PictureChange, currentPath: string | null): Promise<void> {
if (picture.kind === 'keep') return;
const folder = `${eventId}/photos`;
if (picture.kind === 'replace') {
const path = await uploadImage(BUCKETS.events, picture.image, folder);
const { error } = current.coverPhotoId
? await v2().from('event_photos').update({ path, is_cover: true }).eq('id', current.coverPhotoId)
: await v2().from('event_photos').insert({ event_id: eventId, path, is_cover: true, sort_order: 0 });
if (error) {
const path = await uploadImage(BUCKETS.events, picture.image, `${eventId}/photos`);
try {
await savePosterPath(eventId, path, "The picture couldn't be saved.");
} catch (cause) {
await removeImage(BUCKETS.events, path);
throw new UnavailableError("The picture couldn't be saved.", { cause: error });
throw cause;
}
if (current.coverPath) await removeImage(BUCKETS.events, current.coverPath);
return;
} else {
await savePosterPath(eventId, null, "The picture couldn't be removed.");
}
if (current.coverPhotoId) {
const { error } = await v2().from('event_photos').delete().eq('id', current.coverPhotoId);
if (error) throw new UnavailableError("The picture couldn't be removed.", { cause: error });
}
if (current.coverPath) await removeImage(BUCKETS.events, current.coverPath);
if (currentPath) await removeImage(BUCKETS.events, currentPath);
}

/**
* Saves the poster's path on the event.
* @throws UnavailableError with `failure` when Supabase refuses it or no event was changed.
*/
async function savePosterPath(eventId: string, path: string | null, failure: string): Promise<void> {
const { data, error } = await v2().from('events').update({ poster_path: path }).eq('id', eventId).select('id');
if (error || data.length === 0) throw new UnavailableError(failure, { cause: error });
}

function toInsert(fields: EventFields): EventInsert {
Expand Down Expand Up @@ -190,7 +193,7 @@ function toUpdate(fields: Partial<EventFields>): EventUpdate {
};
}

type EventRowWithPhotos = Pick<
type EventReadRow = Pick<
EventRow,
| 'id'
| 'title'
Expand All @@ -210,11 +213,11 @@ type EventRowWithPhotos = Pick<
| 'dress_code'
| 'primary_colour'
| 'secondary_colour'
> & { event_photos: readonly PhotoRow[] | null };
| 'poster_path'
>;

function toEvent(row: EventRowWithPhotos): Event | null {
const cover = (row.event_photos ?? []).find((photo) => photo.is_cover) ?? row.event_photos?.[0];
const coverPath = cover?.path ?? null;
function toEvent(row: EventReadRow): Event | null {
const coverPath = row.poster_path;
const parsed = EventSchema.safeParse({
id: row.id,
title: row.title,
Expand All @@ -235,7 +238,6 @@ function toEvent(row: EventRowWithPhotos): Event | null {
primaryColour: row.primary_colour,
secondaryColour: row.secondary_colour,
coverPath,
coverPhotoId: cover?.id ?? null,
pictureUrl: coverPath ? publicUrl(BUCKETS.events, coverPath) : null,
});
if (parsed.success) return parsed.data;
Expand Down
22 changes: 16 additions & 6 deletions apps/admin/src/lib/data/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getSupabase } from '../server/supabase';
import type { PreparedPhoto } from '../ui/prepare-photo';

// One template string, not pieces joined with +, so supabase-js can read the columns and type the rows from it.
const COLUMNS = `id, path, alt, event_id, is_team_photo, width, height, focus_x, focus_y, sort_order, team_sort_order,
const COLUMNS = `id, path, alt, event_id, is_team_photo, on_event_page, width, height, focus_x, focus_y, sort_order, team_sort_order,
created_at, photo_guests(guest_id, sort_order)`;

type PhotoRow = Pick<
Expand All @@ -29,6 +29,7 @@ type PhotoRow = Pick<
| 'alt'
| 'event_id'
| 'is_team_photo'
| 'on_event_page'
| 'width'
| 'height'
| 'focus_x'
Expand All @@ -52,8 +53,9 @@ export async function listPhotos(): Promise<readonly Photo[]> {
}

/**
* Uploads one photo and saves it with the batch's event, team setting and guest. If the row or the guest tag can't
* be saved, everything for this photo is undone, so a failed photo leaves nothing behind and can be tried again.
* Uploads one photo and saves it with the batch's event, team and event-page settings and guest. If the row or the
* guest tag can't be saved, everything for this photo is undone, so a failed photo leaves nothing behind and can be
* tried again.
* @throws UnavailableError when the file or the row could not be saved.
*/
export async function uploadPhoto(photo: PreparedPhoto, defaults: BatchDefaults): Promise<void> {
Expand All @@ -66,6 +68,7 @@ export async function uploadPhoto(photo: PreparedPhoto, defaults: BatchDefaults)
height: photo.height,
event_id: defaults.eventId,
is_team_photo: defaults.isTeamPhoto,
on_event_page: defaults.onEventPage,
})
.select('id')
.single();
Expand Down Expand Up @@ -107,7 +110,12 @@ export async function getPhoto(id: string): Promise<Photo | null> {
export async function updatePhotos(ids: readonly string[], changes: PhotoChanges): Promise<void> {
const { data, error } = await v2()
.from('photos')
.update({ alt: changes.description, event_id: changes.eventId, is_team_photo: changes.isTeamPhoto })
.update({
alt: changes.description,
event_id: changes.eventId,
is_team_photo: changes.isTeamPhoto,
on_event_page: changes.onEventPage,
})
.in('id', ids)
.select('id');
if (error || data.length !== ids.length) throw new UnavailableError("The photos couldn't be changed.", { cause: error });
Expand All @@ -128,8 +136,8 @@ export async function tagGuest(ids: readonly string[], guestId: string): Promise
}

/**
* Saves everything changed on one photo: its description, event, team setting and focus point, then the guests
* added and taken off.
* Saves everything changed on one photo: its description, event, team and event-page settings and focus point,
* then the guests added and taken off.
* @throws UnavailableError when any part couldn't be saved. What saved before the failure stays saved.
*/
export async function savePhoto(photo: Photo, edits: PhotoEdits): Promise<void> {
Expand All @@ -139,6 +147,7 @@ export async function savePhoto(photo: Photo, edits: PhotoEdits): Promise<void>
alt: edits.description,
event_id: edits.eventId,
is_team_photo: edits.isTeamPhoto,
on_event_page: edits.onEventPage,
focus_x: edits.focus.x,
focus_y: edits.focus.y,
})
Expand Down Expand Up @@ -199,6 +208,7 @@ function toPhoto(row: PhotoRow): Photo | null {
description: row.alt,
eventId: row.event_id,
isTeamPhoto: row.is_team_photo,
onEventPage: row.on_event_page,
width: row.width,
height: row.height,
focus: { x: row.focus_x, y: row.focus_y },
Expand Down
3 changes: 3 additions & 0 deletions apps/admin/src/types/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ export type Database = {
height: number
id: string
is_team_photo: boolean
on_event_page: boolean
path: string
sort_order: number
team_sort_order: number
Expand All @@ -586,6 +587,7 @@ export type Database = {
height: number
id?: string
is_team_photo?: boolean
on_event_page?: boolean
path: string
sort_order?: number
team_sort_order?: number
Expand All @@ -600,6 +602,7 @@ export type Database = {
height?: number
id?: string
is_team_photo?: boolean
on_event_page?: boolean
path?: string
sort_order?: number
team_sort_order?: number
Expand Down
3 changes: 1 addition & 2 deletions apps/admin/src/types/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ export const EventSchema = z.object({
/** The event's own colours; null uses the LCS default (yellow and pink). */
primaryColour: HexColourSchema.nullable(),
secondaryColour: HexColourSchema.nullable(),
/** Path in the `event-media` bucket for the cover photo, when there is one. */
/** Path in the `event-media` bucket for the cover photo (the event's poster), when there is one. */
coverPath: z.string().nullable(),
coverPhotoId: z.uuid().nullable(),
pictureUrl: z.url().nullable(),
});
export type Event = Readonly<z.infer<typeof EventSchema>>;
Expand Down
Loading
Loading