'use client' import { useEffect, useMemo, useState } from 'react' import Link from 'next/link' import { ArrowUpRight } from 'lucide-react' import ClientTime from '../components/ClientTime' import type { DocCollection, DocVersionOption } from './types' interface DocCollectionCardProps { collection: DocCollection meta?: string } function resolveVersionLabel(version?: DocVersionOption | null) { return version?.label ?? 'Latest' } export default function DocCollectionCard({ collection, meta }: DocCollectionCardProps) { const { versions } = collection const defaultVersionId = collection.defaultVersionId ?? versions[0]?.id ?? '' const [selectedVersionId, setSelectedVersionId] = useState(defaultVersionId) useEffect(() => { if (!defaultVersionId) { return } setSelectedVersionId(defaultVersionId) }, [defaultVersionId, collection.slug]) useEffect(() => { if (!versions.length) { return } if (!versions.some((version) => version.id === selectedVersionId)) { const fallback = versions.find((version) => version.id === defaultVersionId) ?? versions[0] if (fallback) { setSelectedVersionId(fallback.id) } } }, [versions, selectedVersionId, defaultVersionId]) const activeVersion = useMemo(() => { if (!versions.length) return undefined return versions.find((version) => version.id === selectedVersionId) ?? versions[0] }, [versions, selectedVersionId]) const activeResource = activeVersion?.resource const description = activeResource?.description ?? collection.description const href = activeVersion ? `/docs/${collection.slug}/${activeVersion.slug}` : `/docs/${collection.slug}` const updatedAt = activeResource?.updatedAt ?? collection.updatedAt const estimatedMinutes = activeResource?.estimatedMinutes ?? collection.estimatedMinutes const tags = activeResource?.tags?.length ? activeResource.tags : collection.tags return (
{meta && ( {meta} )}
{updatedAt && ( Updated )} {estimatedMinutes && {estimatedMinutes} min read}

{collection.title}

{description}

{versions.length > 0 && ( )} {tags && tags.length > 0 && (
{tags.map((tag) => ( {tag} ))}
)}
Open reader {activeVersion && ( {resolveVersionLabel(activeVersion)} )}
Open
) }