import type { LibraryBlockProps, TestimonialItem } from '../../context.js' import type { BaseBlockData } from '../../data.js' import { MEDIA_SIZES } from '../../../media/mediaProps.js' import { blockTexts, fillText } from '../../texts.js' import { Buttons, headingId } from '../../ui/Buttons.js' import { cx, parts } from '../../ui/cx.js' export type TestimonialsBlockData = { entries?: null | unknown[]; items?: null | TestimonialItem[]; source?: null | string } & BaseBlockData export const TESTIMONIALS_PARTS = { author: 'font-medium text-fg-strong', figure: 'm-0 flex h-full flex-col gap-4 p-6', grid: 'grid gap-gap md:grid-cols-2 lg:grid-cols-3', photo: 'h-12 w-12 rounded-full object-cover', quote: 'text-lead text-fg', rating: 'text-accent', role: 'text-sm text-fg-muted', single: 'mx-auto grid max-w-narrow gap-gap', } const str = (v: unknown) => (typeof v === 'string' && v.trim() ? v : undefined) /** Default mapping of a testimonials-collection document. */ export function defaultTestimonial(doc: Record): null | TestimonialItem { const quote = str(doc.quote) ?? str(doc.text) const heading = str(doc.heading) ?? str(doc.author) ?? str(doc.name) if (!quote || !heading) {return null} return { id: str(doc.id), heading, media: doc.media ?? doc.photo, quote, rating: typeof doc.rating === 'number' ? doc.rating : null, text: str(doc.role) ?? str(doc.company) ?? null } } /** Quotes as
/
/
; a rating has a text equivalent for screen readers. */ export function TestimonialsBlock(props: LibraryBlockProps) { const { classNames, ctx, entries, header, isFirst, items, links, locale, settings, source, ui, variant } = props const map = ctx.mapTestimonial ?? defaultTestimonial const list = source === 'collection' ? (entries ?? []).flatMap((e) => (e && typeof e === 'object' ? [map(e as Record)] : [])).filter((x): x is TestimonialItem => !!x) : (items ?? []).filter((i) => i.quote && i.heading) if (!list.length) {return null} const c = parts(TESTIMONIALS_PARTS, classNames) const texts = blockTexts(locale, ctx.texts) const id = headingId(props) return (
    {list.map((t, i) => (
  • {t.rating ? (

    {'★'.repeat(Math.round(t.rating))} {fillText(texts.rating, t.rating)}

    ) : null}
    {t.quote}
    {t.heading} {t.text ? {t.text} : null}
  • ))}
) }