All files / src/lib project-gallery.ts

100% Statements 9/9
80% Branches 8/10
100% Functions 3/3
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                              6x 3x 1x   6x   5x 2x 2x                                             3x    
import type { Media, Project, Room } from "./api/types";
 
/**
 * Returns the media list to render in the project detail gallery.
 *
 * When the project has rooms with media, returns those media sorted by sortOrder.
 * When there are no room media but the project has a denormalized `coverImage`,
 * returns a single synthesized `Media` so the detail page can render the cover
 * image the listing card is showing (fixes #374/#356 — listing thumbnail visible,
 * detail page said "No photos available").
 */
export function getProjectGalleryMedia(
	project: Pick<Project, "coverImage" | "title"> | null | undefined,
	rooms: Pick<Room, "media">[] | null | undefined,
): Media[] {
	const roomMedia = (rooms ?? [])
		.flatMap((room) => room.media ?? [])
		.sort((a, b) => a.sortOrder - b.sortOrder);
 
	if (roomMedia.length > 0) return roomMedia;
 
	if (project?.coverImage) {
		const now = new Date().toISOString();
		return [
			{
				id: 0,
				roomId: 0,
				mediaType: "image",
				filename: "cover",
				originalFilename: null,
				storageKey: project.coverImage,
				thumbnailKey: null,
				width: null,
				height: null,
				fileSize: null,
				durationSeconds: null,
				caption: null,
				altText: project.title ?? "",
				sortOrder: 0,
				isCover: true,
				dateCreated: now,
				dateUpdated: now,
			},
		];
	}
 
	return [];
}