← back to Stayclaim

src/components/FilmsGridServer.tsx

66 lines

import { listPdFilmImages, type PdFilmImage } from '@/lib/db';
import { currentSurface, type SurfaceKey } from '@/lib/site';
import { FilmsGrid, type FilmsGridImage } from './FilmsGrid';

// Per-surface source affinity — same map as HeroCollage so hero + grid stay
// thematically consistent within a surface, and visually distinct across them.
const GRID_SOURCE_PREFERENCE: Record<SurfaceKey, PdFilmImage['source'][]> = {
  flagship:  ['met', 'loc', 'wikimedia'],
  claim:     ['archive_org', 'mhdl'],
  community: ['mhdl'],   // R4 fix: MHDL only — magazine pages, newspaper clippings, scrapbook texture
};

/**
 * Server wrapper around FilmsGrid. Reads PD film images from Postgres
 * and resolves the visible surface (wholivedthere / claimmyaddress / bubbesblock)
 * from the request host so the grid auto-themes itself per domain.
 *
 * Pass a productionId to scope the grid to a single film/TV title;
 * otherwise it shows the latest cached PD images globally.
 */
export async function FilmsGridServer({
  heading,
  kicker,
  limit = 60,
  productionId,
  productionIds,
  defaultCols,
}: {
  heading: string;
  kicker: string;
  limit?: number;
  productionId?: string;
  productionIds?: string[];
  defaultCols?: number;
}) {
  const site = await currentSurface();
  // Apply per-surface source preference UNLESS scoped to a specific production
  // (in which case all sources are valid — production_id is the dominant filter).
  const sources = (productionId || productionIds?.length) ? undefined : GRID_SOURCE_PREFERENCE[site.key];
  const preferred = await listPdFilmImages({ limit, productionId, productionIds, sources });
  // Fallback if surface-preferred pool is too thin
  const rows = preferred.length >= Math.min(limit, 12)
    ? preferred
    : await listPdFilmImages({ limit, productionId, productionIds });

  const images: FilmsGridImage[] = rows.map((r: PdFilmImage) => ({
    id: r.id,
    source: r.source,
    title: r.title,
    year: r.year,
    image_url: r.image_url,
    thumb_url: r.thumb_url,
    source_url: r.source_url,
  }));

  return (
    <FilmsGrid
      images={images}
      surfaceKey={site.key}
      heading={heading}
      kicker={kicker}
      defaultCols={defaultCols}
    />
  );
}