Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

challenge OG image (soon™️) (#671)

PickleNik3ce4c94738f1
diff --git a/apps/og-image/app/api/challenge/route.tsx b/apps/og-image/app/api/challenge/route.tsx
index 370dcad6..e63327ad 100644
--- a/apps/og-image/app/api/challenge/route.tsx
+++ b/apps/og-image/app/api/challenge/route.tsx
@@ -1,10 +1,18 @@
import { ImageResponse } from '@vercel/og';
import { challengeParam } from 'utils/zodParams';
-import { Logo } from 'app/components/logo';
+import { fetchFont } from 'utils/fetchFont';
+import { Logo3d } from 'app/components/logo3d';
+import { Grid } from 'app/components/grid';
export const runtime = 'edge';
export async function GET(req: Request) {
+ const [inter900, inter700, inter400] = await Promise.all([
+ fetchFont('Inter', 900),
+ fetchFont('Inter', 700),
+ fetchFont('Inter', 400),
+ ]);
+
const parsed = challengeParam.decodeRequest(req);
if (!parsed.success) {
@@ -13,23 +21,90 @@ export async function GET(req: Request) {
const props = parsed.data.input;
+ const COLORS_BY_DIFFICULTY = {
+ BEGINNER: 'bg-[#86cae9]',
+ EASY: 'bg-[#85efac]',
+ MEDIUM: 'bg-[#f19c41]',
+ HARD: 'bg-[#f87272]',
+ EXTREME: 'bg-[#d8b4fe]',
+ };
+
+ const HOST =
+ process.env.NODE_ENV === 'production' ? 'https://og.typehero.dev' : 'http://localhost:4200';
+
return new ImageResponse(
(
- <div tw="bg-zinc-900 h-full w-full text-white bg-cover flex flex-col p-14">
- <div tw="flex flex-col justify-center items-center w-full h-full">
- <Logo />
- <h1 tw="text-6xl pt-3">{props.title}</h1>
- <p tw="text-center text-3xl text-zinc-300">{props.description}</p>
- <p tw="text-3xl">
- <span tw="mr-2">created by</span>
- <span tw="text-blue-500">{props.username}</span>
- </p>
+ <div tw="bg-black h-full w-full text-white bg-cover flex flex-col pt-14 px-14">
+ <Grid />
+ <img
+ tw="absolute right-2 -bottom-10 h-32 opacity-30"
+ style={{ transform: 'rotate(30deg)' }}
+ src={`${HOST}/amoguwuowoaahhh.png?cache-bust=${new Date().getDate()}`}
+ alt="OG"
+ />
+ <div tw="flex flex-col items-start h-full overflow-hidden rounded-t-[3.5rem] border-zinc-700 border-2 border-b-0 to-black relative">
+ <svg
+ // @ts-ignore
+ tw="absolute h-full w-full"
+ viewBox="0 0 512 512"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <rect width="512" height="512" fill="url(#paint0_linear_970_280)" />
+ <defs>
+ <linearGradient
+ id="paint0_linear_970_280"
+ x1="4.54647"
+ y1="-5.28981e-05"
+ x2="616.249"
+ y2="213.967"
+ gradientUnits="userSpaceOnUse"
+ >
+ <stop stop-color="#27272A" />
+ <stop offset="1" stop-color="#0E0E0F" />
+ </linearGradient>
+ </defs>
+ </svg>
+ <div tw="flex flex-col p-10 w-full">
+ <div tw="flex absolute top-10 right-10">
+ <Logo3d />
+ </div>
+
+ <p tw="flex items-center -mt-3">
+ <p tw="text-2xl font-bold bg-zinc-600 rounded-full px-3 py-1.5">
+ <span>@</span>
+ <span>{props.username}</span>
+ </p>
+ <p tw="text-2xl ml-4">{props.date}</p>
+ </p>
+ <h1
+ tw="text-7xl font-bold truncate pr-48 -mt-2 overflow-ellipsis"
+ style={{ fontWeight: '800 !important' }}
+ >
+ {props.title}
+ </h1>
+ <p tw="flex items-center -mt-3">
+ <p
+ tw={`${
+ COLORS_BY_DIFFICULTY[props.difficulty]
+ } text-black font-bold text-2xl px-6 py-1.5 rounded-full`}
+ >
+ {props.difficulty}
+ </p>
+ </p>
+ <p tw="text-3xl text-zinc-300">{props.description}</p>
+ </div>
</div>
</div>
),
{
width: 1200,
height: 600,
+ fonts: [
+ { name: 'Inter', data: inter900, weight: 900 },
+ { name: 'Inter', data: inter700, weight: 700 },
+ { name: 'Inter', data: inter400, weight: 400 },
+ ],
},
);
}
diff --git a/apps/og-image/app/api/font/route.tsx b/apps/og-image/app/api/font/route.tsx
new file mode 100644
index 00000000..1641db1c
--- /dev/null
+++ b/apps/og-image/app/api/font/route.tsx
@@ -0,0 +1,49 @@
+/**
+ * Originally from Vercel's Satori project.
+ * @link https://github.com/vercel/satori/blob/main/playground/pages/api/font.ts
+ */
+import type { NextRequest } from 'next/server';
+import { fontParams } from 'utils/zodParams';
+
+export const runtime = 'edge';
+
+export async function GET(req: NextRequest) {
+ if (req.nextUrl.pathname !== '/api/font') return;
+ const url = new URL(req.url);
+
+ const parsed = fontParams.decodeRequest(req);
+ if (!parsed.success) {
+ return new Response(parsed.error.toString(), { status: 400 });
+ }
+ const props = parsed.data.input;
+
+ let API = `https://fonts.googleapis.com/css2?family=${props.family}:wght@${props.weight}`;
+ if (props.text) {
+ // allow font optimization if we pass text => only getting the characters we need
+ API += `&text=${encodeURIComponent(props.text)}`;
+ }
+
+ const css = await (
+ await fetch(API, {
+ headers: {
+ // Make sure it returns TTF.
+ 'User-Agent':
+ 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1',
+ },
+ })
+ ).text();
+
+ const resource = /src: url\((.+)\) format\('(opentype|truetype)'\)/.exec(css);
+
+ if (!resource || !resource[1]) return;
+
+ const res = await fetch(resource[1]);
+
+ // Make sure not to mess it around with compression when developing it locally.
+ if (url.hostname === 'localhost') {
+ res.headers.delete('content-encoding');
+ res.headers.delete('content-length');
+ }
+
+ return res;
+};
diff --git a/apps/og-image/app/components/grid.tsx b/apps/og-image/app/components/grid.tsx
new file mode 100644
index 00000000..a04c834e
--- /dev/null
+++ b/apps/og-image/app/components/grid.tsx
@@ -0,0 +1,198 @@
+export function Grid() {
+ return (
+ <svg
+ // @ts-ignore
+ tw="absolute h-[150%] w-[150%]"
+ viewBox="0 0 3272 1764"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <g opacity="0.08">
+ <path d="M-757.85 83.708H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 237.22H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 390.732H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 544.243H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 697.757H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 851.268H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1004.78H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1158.29H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1311.8H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1465.32H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1618.83H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path
+ d="M69.6211 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M290.279 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M510.938 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M731.602 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path d="M952.26 -525.542V1930.65" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path
+ d="M1172.92 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M1393.58 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M1614.24 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path d="M1834.9 -525.542V1930.65" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path
+ d="M2055.55 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M2276.22 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M2496.88 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M2717.53 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M2938.19 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M3158.85 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ </g>
+ <g opacity="0.08">
+ <path d="M-757.85 83.708H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 237.22H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 390.732H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 544.243H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 697.757H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 851.268H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1004.78H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1158.29H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1311.8H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1465.32H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path d="M-757.85 1618.83H4648.3" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path
+ d="M69.6211 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M290.279 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M510.938 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M731.602 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path d="M952.26 -525.542V1930.65" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path
+ d="M1172.92 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M1393.58 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M1614.24 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path d="M1834.9 -525.542V1930.65" stroke="white" stroke-width="3" stroke-dasharray="1 1" />
+ <path
+ d="M2055.55 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M2276.22 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M2496.88 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M2717.53 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M2938.19 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ <path
+ d="M3158.85 -525.542V1930.65"
+ stroke="white"
+ stroke-width="3"
+ stroke-dasharray="1 1"
+ />
+ </g>
+ </svg>
+ );
+}
diff --git a/apps/og-image/app/components/logo.tsx b/apps/og-image/app/components/logo.tsx
index aec7448b..1f432a5c 100644
--- a/apps/og-image/app/components/logo.tsx
+++ b/apps/og-image/app/components/logo.tsx
@@ -1,8 +1,8 @@
export function Logo() {
return (
<svg
- height="128"
- width="128"
+ height="144"
+ width="144"
viewBox="0 0 512 512"
fill="none"
xmlns="http://www.w3.org/2000/svg"
diff --git a/apps/og-image/app/components/logo3d.tsx b/apps/og-image/app/components/logo3d.tsx
new file mode 100644
index 00000000..244921e2
--- /dev/null
+++ b/apps/og-image/app/components/logo3d.tsx
@@ -0,0 +1,65 @@
+export function Logo3d() {
+ return (
+ <svg
+ width="144"
+ height="144"
+ viewBox="0 0 638 638"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <g filter="url(#filter0_ii_284_103)">
+ <rect width="637.029" height="637.029" rx="68.1096" fill="#3178C6" />
+ </g>
+ <path
+ fill-rule="evenodd"
+ clip-rule="evenodd"
+ d="M275.704 313.881H354.636V262.692H134.631V313.881H213.176V541.802H275.704V313.881Z"
+ fill="white"
+ />
+ <path
+ d="M354.635 541.802V262.692H413.185V378.363H499.529V262.692H558.221V541.802H499.529V425.724H413.185V541.802H354.635Z"
+ fill="white"
+ />
+ <defs>
+ <filter
+ id="filter0_ii_284_103"
+ x="-49"
+ y="-49"
+ width="736.029"
+ height="736.029"
+ filterUnits="userSpaceOnUse"
+ color-interpolation-filters="sRGB"
+ >
+ <feFlood flood-opacity="0" result="BackgroundImageFix" />
+ <feBlend mode="normal" in="SourceGraphic" in2="BackgroundImageFix" result="shape" />
+ <feColorMatrix
+ in="SourceAlpha"
+ type="matrix"
+ values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"
+ result="hardAlpha"
+ />
+ <feOffset dx="-49" dy="-49" />
+ <feGaussianBlur stdDeviation="74.5" />
+ <feComposite in2="hardAlpha" operator="arithmetic" k2="-1" k3="1" />
+ <feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5 0" />
+ <feBlend mode="normal" in2="shape" result="effect1_innerShadow_284_103" />
+ <feColorMatrix
+ in="SourceAlpha"
+ type="matrix"
+ values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"
+ result="hardAlpha"
+ />
+ <feOffset dx="50" dy="50" />
+ <feGaussianBlur stdDeviation="57.5" />
+ <feComposite in2="hardAlpha" operator="arithmetic" k2="-1" k3="1" />
+ <feColorMatrix type="matrix" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0.12 0" />
+ <feBlend
+ mode="normal"
+ in2="effect1_innerShadow_284_103"
+ result="effect2_innerShadow_284_103"
+ />
+ </filter>
+ </defs>
+ </svg>
+ );
+}
diff --git a/apps/og-image/app/page.tsx b/apps/og-image/app/page.tsx
index 126dd465..ac488bc3 100644
--- a/apps/og-image/app/page.tsx
+++ b/apps/og-image/app/page.tsx
@@ -1,3 +1,4 @@
+/* eslint-disable @next/next/no-img-element */
'use client';
import Head from 'next/head';
@@ -34,15 +35,19 @@ export default function Page() {
<img
alt="ayo"
src={`/api/challenge?${challengeParam.toSearchString({
- description: 'This is a test card that has a has a nice description',
- title: 'Inferring types is fun',
+ description:
+ 'This is a test card that has a has a nice description lorem40 ipsum dolor sit amet, consectetur adipiscing elit. lorem more text here s aothesntohks anatoh esn uthsanotedhusn hwanoejt hsaont hesnathdoneitsdhsas asnoehusnatohesunthaoenst',
+ title: 'Inferring types is fun, but not really',
username: 'Hacksore',
+ date: '84 years ago',
+ difficulty: 'EASY',
})}&random=${nonce}`}
/>
</div>
<div>
<h2>User</h2>
+
<img
alt="ayo"
src={`/api/user?${userParam.toSearchString({
diff --git a/apps/og-image/public/amoguwuowoaahhh.png b/apps/og-image/public/amoguwuowoaahhh.png
new file mode 100644
index 00000000..60ab61e5
Binary files /dev/null and b/apps/og-image/public/amoguwuowoaahhh.png differ
diff --git a/apps/og-image/utils/fetchFont.ts b/apps/og-image/utils/fetchFont.ts
index fe82eccb..3fe5be6c 100644
--- a/apps/og-image/utils/fetchFont.ts
+++ b/apps/og-image/utils/fetchFont.ts
@@ -1,8 +1,9 @@
import { fontParams } from './zodParams';
-const baseUrl = process.env.VERCEL
- ? 'https://' + process.env.VERCEL_URL
- : 'http://localhost:3001';
+const baseUrl = process.env.VERCEL_URL
+ ? `https://${process.env.VERCEL_URL}`
+ : 'http://localhost:4200';
+// process.env.NODE_ENV === 'production' ? 'https://og.typehero.dev' : 'http://localhost:4200';
export const fetchFont = (family: string, weight?: number, text?: string) =>
fetch(
diff --git a/apps/og-image/utils/zodParams.ts b/apps/og-image/utils/zodParams.ts
index 50e81ddb..ca6d9e69 100644
--- a/apps/og-image/utils/zodParams.ts
+++ b/apps/og-image/utils/zodParams.ts
@@ -55,6 +55,8 @@ export const challengeParam = zodParams(
title: truncatedWordSchema({ maxCharacters: 70 }),
description: truncatedWordSchema({ maxCharacters: 145 }),
username: z.string(),
+ difficulty: z.enum(['BEGINNER', 'EASY', 'MEDIUM', 'HARD', 'EXTREME']),
+ date: z.string().optional(),
}),
);
diff --git a/apps/web/src/app/[locale]/challenge/[id]/page.tsx b/apps/web/src/app/[locale]/challenge/[id]/page.tsx
index 85ad5ce3..60da3321 100644
--- a/apps/web/src/app/[locale]/challenge/[id]/page.tsx
+++ b/apps/web/src/app/[locale]/challenge/[id]/page.tsx
@@ -3,6 +3,7 @@ import { Description } from '../_components/description';
import { Comments } from '../_components/comments';
import { getChallengeRouteData } from './getChallengeRouteData';
import { buildMetaForChallenge } from '~/app/metadata';
+import { getRelativeTime } from '~/utils/relativeTime';
interface Props {
params: {
@@ -16,6 +17,8 @@ export async function generateMetadata({ params: { id } }: Props) {
title: `${challenge.name} | TypeHero`,
description: `${challenge.shortDescription} Can you solve it?`,
username: challenge.user.name,
+ difficulty: challenge.difficulty,
+ date: getRelativeTime(challenge.createdAt),
});
}
diff --git a/apps/web/src/app/[locale]/challenge/[id]/solutions/page.tsx b/apps/web/src/app/[locale]/challenge/[id]/solutions/page.tsx
index 3e4b73d2..dddb58b8 100644
--- a/apps/web/src/app/[locale]/challenge/[id]/solutions/page.tsx
+++ b/apps/web/src/app/[locale]/challenge/[id]/solutions/page.tsx
@@ -2,7 +2,7 @@ import { getServerAuthSession } from '@repo/auth/server';
import { notFound } from 'next/navigation';
import { Solutions } from './_components';
import { getChallengeRouteData } from '../getChallengeRouteData';
-import { buildMetaForChallenge } from '~/app/metadata';
+import { buildMetaForDefault } from '~/app/metadata';
import { getSolutionsRouteData } from './getSolutionRouteData';
interface Props {
@@ -13,10 +13,9 @@ interface Props {
export async function generateMetadata({ params: { id } }: Props) {
const challenge = await getChallengeRouteData(id, null);
- return buildMetaForChallenge({
+ return buildMetaForDefault({
title: `Solutions to ${challenge.name} | TypeHero`,
description: challenge.shortDescription,
- username: challenge.user.name,
});
}
diff --git a/apps/web/src/app/metadata.ts b/apps/web/src/app/metadata.ts
index 0eccf1b4..6b63b05c 100644
--- a/apps/web/src/app/metadata.ts
+++ b/apps/web/src/app/metadata.ts
@@ -46,18 +46,26 @@ const baseMetadata: Metadata = {
},
};
+// TODO: infer from ZOD
interface MetaParamsForChallenge {
title: string;
description: string;
username: string;
+ difficulty: 'BEGINNER' | 'EASY' | 'EXTREME' | 'HARD' | 'MEDIUM';
+ date: string;
}
+interface MetaParamsForUser {
+ title: string;
+ description: string;
+ username: string;
+}
/** Helper to build opengraph metadata for a user, you should call this in generateMetadata() next function */
export const buildMetaForUser = async ({
title,
description,
username,
-}: MetaParamsForChallenge): Promise<Metadata> => {
+}: MetaParamsForUser): Promise<Metadata> => {
const params = `${userParam.toSearchString({
username,
})}`;
@@ -76,11 +84,15 @@ export const buildMetaForChallenge = async ({
title,
description,
username,
+ difficulty,
+ date,
}: MetaParamsForChallenge): Promise<Metadata> => {
const params = `${challengeParam.toSearchString({
description,
title,
username,
+ difficulty,
+ date,
})}`;
const ogImageUrl = `${OG_URL}/api/challenge?${params}`;