"use client"; /* eslint-disable @typescript-eslint/no-explicit-any */ import { PortableText, createDataAttribute, stegaClean } from "next-sanity"; import { getFileAsset, getImageAsset, getImageDimensions, SanityFileAsset, } from "@sanity/asset-utils"; import type { BlockContent, Button as SanityButton, SanityImageAsset, } from "@/sanity/sanity.types"; import { client } from "@/sanity/client"; import LinkButton from "./link-button"; import Image from "next/image"; import Link from "next/link"; import { DownloadIcon } from "lucide-react"; import { useDeconstructLink } from "@/lib/link-client"; import { dynamicHeight, generateImageUrl } from "@/lib/image-url"; import { Button } from "./ui/button"; import { sanityConnection } from "@repo/sanity-connection"; const { projectId, dataset } = client.config(); export function Callout(props: any) { if (!props) { return null; } return (

{children}

, h2: ({ children }: any) =>

{children}

, h3: ({ children }: any) =>

{children}

, h4: ({ children }: any) =>

{children}

, h5: ({ children }: any) =>
{children}
, h6: ({ children }: any) =>
{children}
, }, }} />
); } export function PortableButton(value: { value: SanityButton }) { const linkData = useDeconstructLink(value.value.link); return (
); } export function PortableImage({ value, isInline, }: { value: SanityImageAsset; isInline: boolean; }) { const { width, height } = getImageDimensions(value); const imageAsset = getImageAsset(value, { projectId: sanityConnection.projectId ?? "", dataset: sanityConnection.dataset ?? "", }); const attr = createDataAttribute({ id: imageAsset._id, type: imageAsset._type, workspace: "production", }); return ( = 100 ? 100 : width) : width >= 1200 ? 1200 : width } height={dynamicHeight(height, width, isInline)} alt={value.altText || " "} loading="lazy" className="border rounded-md shadow-md" style={{ display: isInline ? "inline-block" : "block", aspectRatio: width / height, maxHeight: "400px", maxWidth: "100%", width: "auto", }} /> ); } export function PortableFile({ value }: { value: SanityFileAsset }) { const fileAsset = getFileAsset(value, { projectId: projectId ?? "", dataset: dataset ?? "", }); const attr = createDataAttribute({ id: fileAsset._id, type: fileAsset._type, workspace: "production", }); const filename: string = fileAsset?.originalFilename || `${fileAsset.extension.charAt(0).toUpperCase() + fileAsset.extension?.slice(1)} herunterladen`; return (
20 ? `${filename.slice(0, 10)}...${filename.slice(-10)}` : filename } />
); } function CustomLink(props: { children: React.ReactNode; value?: any }) { const { value, children } = props; let linkUrl = null; let target = "_self"; if (value?.href) { const href = value.href; if (href.type === "external" && href.url) { linkUrl = href.url; target = href.blank ? "_blank" : "_self"; } else if (href.type === "internal" || href._type === "reference") { const resolved = useDeconstructLink(href); if (resolved) { const ButtonComponent = Button as any; return ( {children} ); } } else if (typeof href === "string") { linkUrl = href; } } if (linkUrl) { const ButtonComponent = Button as any; return ( {children} ); } return <>{children}; } const components = { types: { image: PortableImage, button: PortableButton, callout: Callout, file: PortableFile, }, block: { h1: ({ children }: any) =>

{children}

, h2: ({ children }: any) =>

{children}

, h3: ({ children }: any) =>

{children}

, h4: ({ children }: any) =>

{children}

, h5: ({ children }: any) =>
{children}
, h6: ({ children }: any) =>
{children}
, }, marks: { left: ({ children }: { children: React.ReactNode }) => ( {children} ), center: ({ children }: { children: React.ReactNode }) => ( {children} ), right: ({ children }: { children: React.ReactNode }) => ( {children} ), textColor: ({ children, value = { value: "" }, }: { children: React.ReactNode; value?: { value: string }; }) => {children}, highlightColor: ({ children, value = { value: "" }, }: { children: React.ReactNode; value?: { value: string }; }) => ( {children} ), link: CustomLink, }, }; export default function SanityBlock({ body }: { body: BlockContent }) { if (!body) { return null; } return ; }