Skip to content

Commit

Permalink
start some basic og image generation
Browse files Browse the repository at this point in the history
  • Loading branch information
heybereket committed Jun 2, 2023
1 parent 574edf8 commit c184bde
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pages/api/og/avatar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { NextApiRequest, NextApiResponse } from "next";
import db from "@/lib/db";
import { fetchTeamAvatar } from "../teams/avatar";

export default async function getStatus(
req: NextApiRequest,
res: NextApiResponse
): Promise<void> {
const fetchAvatar = await fetchTeamAvatar(req);
const avatar = fetchAvatar.avatar;

res.setHeader('Content-Type', 'image/jpeg');
res.end(Buffer.from(avatar, 'base64'));
}
77 changes: 77 additions & 0 deletions pages/api/og/image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { ImageResponse } from "next/server";
import { API_URL } from "@/lib/constants";

export const config = {
runtime: "edge",
};

const InterFont = fetch(
new URL("/public/fonts/Inter-Bold.ttf", import.meta.url)
).then((res) => res.arrayBuffer());

export default async function handler(request: Request) {
const url = new URL(request.url);
const team = url.searchParams.get("team");

const { teamData, avatar } = await fetch(
`${API_URL}/api/og/team?team=${team}`
).then((res) => res.json());

return new ImageResponse(
(
<div
style={{
height: "100%",
width: "100%",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#141414",
}}
>
<div tw="flex relative">
<div tw="flex flex-col md:flex-row w-full py-12 px-12 md:items-center justify-between">
<h2 tw="flex flex-col font-bold tracking-tight text-gray-900 text-left">
<div tw="flex">
{/* eslint-disable-next-line @next/next/no-img-element*/}
<img
src={
avatar
? `${API_URL}/api/og/avatar?team=${team}`
: `https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${
teamData.website?.startsWith("https")
? teamData.website
: `https://${teamData.website?.slice(7)}`
}/&size=64`
}
tw="mr-5"
alt="Scout Machine Logo"
height="40"
width="40"
/>
<span tw="text-5xl text-[#a6a6a6] mb-2">
{teamData.team_number}
</span>
</div>
<span tw="text-7xl text-white">
{teamData.nickname}
</span>
</h2>
</div>
</div>
</div>
),
{
width: 1200,
height: 630,
fonts: [
{
name: "Typewriter",
data: await InterFont,
style: "normal",
},
],
}
);
}
19 changes: 19 additions & 0 deletions pages/api/og/team.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { NextApiRequest, NextApiResponse } from "next";
import db from "@/lib/db";
import { fetchTeamAvatar } from "../teams/avatar";

export default async function getStatus(
req: NextApiRequest,
res: NextApiResponse
): Promise<void> {
const { team } = req.query;
const avatar = await fetchTeamAvatar(req);

const teamData = await db.team.findUnique({
where: {
team_number: Number(team),
},
});

res.status(200).json({ teamData, avatar: avatar.avatar });
}
58 changes: 58 additions & 0 deletions pages/team/[team]/wrap.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { GetServerSideProps, GetServerSidePropsContext } from "next";
import { NextRouter, useRouter } from "next/router";
import db from "@/lib/db";

export default function LiveFieldViewPage({ teamMatches }: any) {
const router: NextRouter = useRouter();
console.log(teamMatches);

return <h1>Testing</h1>;
}

export const getServerSideProps: GetServerSideProps = async (
context: GetServerSidePropsContext
): Promise<{ props: any }> => {
const { team }: any = context.params;

const teamMatches = await db.match.findMany({
where: {
event_key: {
contains: "2023",
},
OR: [
{
alliances: {
path: ["red", "team_keys"],
array_contains: `frc${team}`,
},
},
{
alliances: {
path: ["blue", "team_keys"],
array_contains: `frc${team}`,
},
},
],
},
});

const highestScoringMatches = teamMatches
.sort((a: any, b: any) => {
const scoreA = a.alliances.red.score + a.alliances.blue.score;
const scoreB = b.alliances.red.score + b.alliances.blue.score;
return scoreB - scoreA;
})
.slice(0, 2);

return {
props: {
teamMatches: JSON.parse(
JSON.stringify(
highestScoringMatches,
(key: string, value) =>
typeof value === "bigint" ? value.toString() : value // return everything else unchanged
)
),
},
};
};
Binary file added public/fonts/Inter-Black.ttf
Binary file not shown.
Binary file added public/fonts/Inter-Bold.ttf
Binary file not shown.
Binary file added public/fonts/Inter-ExtraBold.ttf
Binary file not shown.
Binary file added public/fonts/Inter-ExtraLight.ttf
Binary file not shown.
Binary file added public/fonts/Inter-Light.ttf
Binary file not shown.
Binary file added public/fonts/Inter-Medium.ttf
Binary file not shown.
Binary file added public/fonts/Inter-Regular.ttf
Binary file not shown.
Binary file added public/fonts/Inter-SemiBold.ttf
Binary file not shown.
Binary file added public/fonts/Inter-Thin.ttf
Binary file not shown.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "pages/api/og/image.tsx"],
"exclude": ["node_modules"],
"typedocOptions": {
"entryPoints": [
Expand Down

0 comments on commit c184bde

Please sign in to comment.