From ff802cd92453d09e7511db10e864a8813b9a9ddd Mon Sep 17 00:00:00 2001 From: ByteAtATime Date: Sun, 22 Dec 2024 13:55:49 -0800 Subject: [PATCH] feat: dynamic og image --- .../nextjs/app/view/[id]/opengraph-image.tsx | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 packages/nextjs/app/view/[id]/opengraph-image.tsx diff --git a/packages/nextjs/app/view/[id]/opengraph-image.tsx b/packages/nextjs/app/view/[id]/opengraph-image.tsx new file mode 100644 index 0000000..015de5f --- /dev/null +++ b/packages/nextjs/app/view/[id]/opengraph-image.tsx @@ -0,0 +1,194 @@ +import { ImageResponse } from "next/og"; +import { eq } from "drizzle-orm"; +import { db } from "~~/services/db"; +import { MessageType, messagesTable, signaturesTable } from "~~/services/db/schema"; + +export const alt = "About Acme"; +export const size = { + width: 1200, + height: 630, +}; + +export const contentType = "image/png"; + +export default async function Image({ params }: { params: { id: string } }) { + const { id } = params; + const [message] = await db.select().from(messagesTable).where(eq(messagesTable.id, id)).execute(); + + if (!message) { + return new ImageResponse( + ( +
+
+ Message Not Found +
+
+ ), + { + ...size, + }, + ); + } + + const signatures = await db.select().from(signaturesTable).where(eq(signaturesTable.message, id)).execute(); + + const formatMessage = (message: string, type: MessageType) => { + if (type === "typed_data") { + try { + const parsed = JSON.parse(message); + // Return a simplified representation of the typed data + return `${parsed.domain.name} - ${parsed.primaryType}`; + } catch { + return "Invalid JSON message"; + } + } + return message.length > 200 ? message.substring(0, 200) + "..." : message; + }; + + return new ImageResponse( + ( +
+ {/* Header */} +
+
+ {new Date(message.createdAt).toLocaleDateString()} +
+
+ {message.type === "typed_data" ? "Typed Message" : "Text"} +
+
+ + {/* Message Content */} +
+
+ {formatMessage(message.message, message.type)} +
+ + {message.type === "typed_data" && ( +
+ Structured data signature request +
+ )} +
+ + {/* Footer remains the same */} +
+
+ Created by: {message.creator.substring(0, 6)}...{message.creator.substring(38)} +
+
+
+ {signatures.length} {signatures.length === 1 ? "Signature" : "Signatures"} +
+
+
+
+
+ ), + { + ...size, + }, + ); +}