Files
sign/apps/web/src/app/(share)/share/[slug]/opengraph/route.tsx

159 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-10-27 15:14:04 +11:00
import { ImageResponse } from 'next/og';
import { NextResponse } from 'next/server';
2023-09-21 00:51:02 +00:00
import { P, match } from 'ts-pattern';
import { Logo } from '~/components/branding/logo';
import { ShareHandlerAPIResponse } from '~/pages/api/share';
export const runtime = 'edge';
2023-08-31 15:36:09 +10:00
const CARD_OFFSET_TOP = 152;
const CARD_OFFSET_LEFT = 350;
const CARD_WIDTH = 500;
const CARD_HEIGHT = 250;
const IMAGE_SIZE = {
width: 1200,
height: 630,
};
type SharePageOpenGraphImageProps = {
2023-09-21 00:51:02 +00:00
params: { slug: string };
};
2023-09-27 07:52:24 +10:00
export async function GET(_request: Request, { params: { slug } }: SharePageOpenGraphImageProps) {
const [interSemiBold, interRegular, caveatRegular, shareFrameImage] = await Promise.all([
fetch(new URL('@documenso/assets/fonts/inter-semibold.ttf', import.meta.url)).then(
async (res) => res.arrayBuffer(),
),
fetch(new URL('@documenso/assets/fonts/inter-regular.ttf', import.meta.url)).then(async (res) =>
res.arrayBuffer(),
),
fetch(new URL('@documenso/assets/fonts/caveat-regular.ttf', import.meta.url)).then(
async (res) => res.arrayBuffer(),
),
fetch(new URL('@documenso/assets/static/og-share-frame.png', import.meta.url)).then(
async (res) => res.arrayBuffer(),
),
]);
const baseUrl = process.env.NEXT_PUBLIC_WEBAPP_URL || 'http://localhost:3000';
2023-09-21 00:51:02 +00:00
const recipientOrSender: ShareHandlerAPIResponse = await fetch(
new URL(`/api/share?slug=${slug}`, baseUrl),
).then(async (res) => res.json());
if ('error' in recipientOrSender) {
2023-09-27 07:52:24 +10:00
return NextResponse.json({ error: 'Not found' }, { status: 404 });
2023-09-21 00:51:02 +00:00
}
const isRecipient = 'Signature' in recipientOrSender;
2023-09-21 00:51:02 +00:00
const signatureImage = match(recipientOrSender)
.with({ Signature: P.array(P._) }, (recipient) => {
return recipient.Signature?.[0]?.signatureImageAsBase64 || null;
})
.otherwise((sender) => {
return sender.signature || null;
});
2023-08-31 15:36:09 +10:00
2023-09-21 00:51:02 +00:00
const signatureName = match(recipientOrSender)
.with({ Signature: P.array(P._) }, (recipient) => {
return recipient.name || recipient.email;
})
.otherwise((sender) => {
return sender.name || sender.email;
});
return new ImageResponse(
(
<div tw="relative flex h-full w-full bg-white">
2023-08-31 15:36:09 +10:00
{/* @ts-expect-error Lack of typing from ImageResponse */}
<img src={shareFrameImage} alt="og-share-frame" tw="absolute inset-0 w-full h-full" />
<div tw="absolute top-20 flex w-full items-center justify-center">
{/* @ts-expect-error Lack of typing from ImageResponse */}
<Logo tw="h-8 w-60" />
</div>
2023-09-21 00:51:02 +00:00
{signatureImage ? (
<div
tw="absolute py-6 px-12 flex items-center justify-center text-center"
2023-09-21 00:51:02 +00:00
style={{
top: `${CARD_OFFSET_TOP}px`,
left: `${CARD_OFFSET_LEFT}px`,
width: `${CARD_WIDTH}px`,
height: `${CARD_HEIGHT}px`,
}}
>
<img src={signatureImage} alt="signature" tw="opacity-60 h-full max-w-[100%]" />
2023-09-21 00:51:02 +00:00
</div>
) : (
<p
tw="absolute py-6 px-12 -mt-2 flex items-center justify-center text-center text-slate-500"
2023-09-21 00:51:02 +00:00
style={{
fontFamily: 'Caveat',
fontSize: `${Math.max(
Math.min((CARD_WIDTH * 1.5) / signatureName.length, 80),
36,
)}px`,
top: `${CARD_OFFSET_TOP}px`,
left: `${CARD_OFFSET_LEFT}px`,
width: `${CARD_WIDTH}px`,
height: `${CARD_HEIGHT}px`,
}}
>
{signatureName}
</p>
)}
2023-08-31 15:36:09 +10:00
<div
tw="absolute flex flex-col items-center justify-center pt-12 w-full"
2023-08-31 15:36:09 +10:00
style={{
top: `${CARD_OFFSET_TOP + CARD_HEIGHT}px`,
}}
>
<h2
tw="text-3xl text-slate-500"
2023-08-31 15:36:09 +10:00
style={{
fontFamily: 'Inter',
fontWeight: 600,
}}
>
{isRecipient
? 'I just signed with Documenso and you can too!'
: 'I just sent a document with Documenso and you can too!'}
2023-08-31 15:36:09 +10:00
</h2>
</div>
</div>
),
{
...IMAGE_SIZE,
fonts: [
{
name: 'Caveat',
2023-08-31 15:36:09 +10:00
data: caveatRegular,
style: 'italic',
},
{
name: 'Inter',
2023-08-31 15:36:09 +10:00
data: interRegular,
style: 'normal',
weight: 400,
},
{
name: 'Inter',
data: interSemiBold,
style: 'normal',
2023-08-31 15:36:09 +10:00
weight: 600,
},
],
2023-11-04 13:14:20 +11:00
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
},
},
);
}