aboutsummaryrefslogtreecommitdiff
path: root/apps/web/components/new/document-cards/website-preview.tsx
blob: 21ae9de22e0c2c07f439d0f690d27f7354025e29 (plain) (blame)
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
"use client"

import { useState } from "react"
import type { DocumentsWithMemoriesResponseSchema } from "@repo/validation/api"
import type { z } from "zod"

type DocumentsResponse = z.infer<typeof DocumentsWithMemoriesResponseSchema>
type DocumentWithMemories = DocumentsResponse["documents"][0]

type OgData = {
	title?: string
	image?: string
}

export function WebsitePreview({
	document,
	ogData,
}: {
	document: DocumentWithMemories
	ogData?: OgData | null
}) {
	const [imageError, setImageError] = useState(false)

	const ogImage = (document as DocumentWithMemories & { ogImage?: string })
		.ogImage
	const displayOgImage = ogImage || ogData?.image

	return (
		<div className="bg-[#0B1017] rounded-[18px] overflow-hidden">
			{displayOgImage && !imageError ? (
				<div className="relative w-full aspect-video bg-gray-100 overflow-hidden">
					<img
						src={displayOgImage}
						alt={document.title || "Website preview"}
						className="w-full h-full object-cover"
						onError={() => setImageError(true)}
						loading="lazy"
					/>
				</div>
			) : null}
		</div>
	)
}