teqo-backup/src/components/conteudos/ContentPieceCatalog.tsx

103 lines
3.5 KiB
TypeScript

'use client'
import { useState } from 'react'
import {
contentPieceBoardBucket,
contentPieceMediaKind,
interleaveCatalogItems,
isCardCatalogItem,
type CardCatalogItem,
type ContentCatalogItem,
type ContentPiecePublicItem,
} from '@/lib/contentPieceCatalog'
import { ContentCatalogCardItem } from './ContentCatalogCardItem'
import { ContentPieceCard } from './ContentPieceCard'
import { ContentPieceShareSheet } from './ContentPieceShareSheet'
/**
* S27 — the catalogue board (artefato: cena 01): the vertical board (video and
* the S38 models) runs four columns from `lg`, audio/link keep a wide two-column
* grid (no framing to preserve) and photo/text keep the compact scan row. One
* piece plays at a time (switching unmounts the previous element) and the share
* sheet is owned here, so every card opens the same vote message.
*
* S38 — the six card models are items of the same board: they take the vertical
* board bucket and the whole item is one link to the studio (`/cards?model=<id>`);
* the old single invite tile is gone and no section groups them.
*/
export const ContentPieceCatalog = ({
items,
themeMode,
}: {
items: readonly ContentCatalogItem[]
/** S28 — the board is in the theme mode: the pieces state their provenance. */
themeMode: boolean
}) => {
const [playingId, setPlayingId] = useState<number | null>(null)
const [sharingItem, setSharingItem] = useState<ContentPiecePublicItem | null>(null)
const videoPieces: ContentPiecePublicItem[] = []
const widePieces: ContentPiecePublicItem[] = []
const compactItems: ContentPiecePublicItem[] = []
const cards: CardCatalogItem[] = []
for (const item of items) {
if (isCardCatalogItem(item)) {
cards.push(item)
continue
}
const bucket = contentPieceBoardBucket(contentPieceMediaKind(item))
if (bucket === 'vertical') videoPieces.push(item)
else if (bucket === 'wide') widePieces.push(item)
else compactItems.push(item)
}
// S38 — the models alternate with the video pieces (never a block of their
// own); the wide and compact rows keep the S27 board below.
const verticalItems = interleaveCatalogItems(videoPieces, cards)
const cardProps = (item: ContentPiecePublicItem) => ({
item,
playing: playingId === item.id,
onToggle: () => setPlayingId((current) => (current === item.id ? null : item.id)),
onEnded: () => setPlayingId((current) => (current === item.id ? null : current)),
onShare: setSharingItem,
themeMode,
})
return (
<>
{verticalItems.length > 0 ? (
<div className="grid items-start gap-5 sm:grid-cols-2 lg:grid-cols-4">
{verticalItems.map((item) =>
isCardCatalogItem(item) ? (
<ContentCatalogCardItem key={`card:${item.modelId}`} item={item} />
) : (
<ContentPieceCard key={item.id} {...cardProps(item)} />
),
)}
</div>
) : null}
{widePieces.length > 0 ? (
<div className="mt-6 grid items-start gap-5 lg:grid-cols-2">
{widePieces.map((item) => (
<ContentPieceCard key={item.id} {...cardProps(item)} />
))}
</div>
) : null}
{compactItems.length > 0 ? (
<div className="mt-6 grid items-start gap-5 sm:grid-cols-2">
{compactItems.map((item) => (
<ContentPieceCard key={item.id} {...cardProps(item)} />
))}
</div>
) : null}
{sharingItem ? (
<ContentPieceShareSheet item={sharingItem} onClose={() => setSharingItem(null)} />
) : null}
</>
)
}