// Todo: This relies on NextJS import { ImageResponse } from 'next/og'; import { P, match } from 'ts-pattern'; import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app'; import type { ShareHandlerAPIResponse } from '../api+/share'; import type { Route } from './+types/share.$slug.opengraph'; export const runtime = 'edge'; const CARD_OFFSET_TOP = 173; const CARD_OFFSET_LEFT = 307; const CARD_WIDTH = 590; const CARD_HEIGHT = 337; const IMAGE_SIZE = { width: 1200, height: 630, }; export const loader = async ({ params }: Route.LoaderArgs) => { const { slug } = params; const baseUrl = NEXT_PUBLIC_WEBAPP_URL(); const [interSemiBold, interRegular, caveatRegular, shareFrameImage] = await Promise.all([ fetch(new URL(`${baseUrl}/fonts/inter-semibold.ttf`, import.meta.url)).then(async (res) => res.arrayBuffer(), ), fetch(new URL(`${baseUrl}/fonts/inter-regular.ttf`, import.meta.url)).then(async (res) => res.arrayBuffer(), ), fetch(new URL(`${baseUrl}/fonts/caveat-regular.ttf`, import.meta.url)).then(async (res) => res.arrayBuffer(), ), fetch(new URL(`${baseUrl}/static/og-share-frame2.png`, import.meta.url)).then(async (res) => res.arrayBuffer(), ), ]); const recipientOrSender: ShareHandlerAPIResponse = await fetch( new URL(`/api/share?slug=${slug}`, baseUrl), ).then(async (res) => res.json()); if ('error' in recipientOrSender) { return Response.json({ error: 'Not found' }, { status: 404 }); } const isRecipient = 'Signature' in recipientOrSender; const signatureImage = match(recipientOrSender) .with({ signatures: P.array(P._) }, (recipient) => { return recipient.signatures?.[0]?.signatureImageAsBase64 || null; }) .otherwise((sender) => { return sender.signature || null; }); const signatureName = match(recipientOrSender) .with({ signatures: P.array(P._) }, (recipient) => { return recipient.name || recipient.email; }) .otherwise((sender) => { return sender.name || sender.email; }); return new ImageResponse( (
{/* @ts-expect-error Lack of typing from ImageResponse */} og-share-frame {signatureImage ? (
signature
) : (

{signatureName}

)}

{isRecipient ? 'Document Signed!' : 'Document Sent!'}

), { ...IMAGE_SIZE, fonts: [ { name: 'Caveat', data: caveatRegular, style: 'italic', }, { name: 'Inter', data: interRegular, style: 'normal', weight: 400, }, { name: 'Inter', data: interSemiBold, style: 'normal', weight: 600, }, ], headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, OPTIONS', }, }, ); };