2021-11-01 23:03:19 -07:00
|
|
|
// This is a copy of our higher-level catch-all page, but with some
|
|
|
|
// extra SSR for outfit sharing meta tags!
|
|
|
|
|
|
|
|
import Head from "next/head";
|
2022-09-15 00:27:49 -07:00
|
|
|
import { NextPageWithLayout } from "../_app";
|
|
|
|
import WardrobePage from "../../src/app/WardrobePage";
|
|
|
|
// @ts-ignore: doesn't understand module.exports
|
2021-11-01 23:03:19 -07:00
|
|
|
import connectToDb from "../../src/server/db";
|
2022-09-15 00:27:49 -07:00
|
|
|
// @ts-ignore: doesn't understand module.exports
|
2021-11-01 23:03:19 -07:00
|
|
|
import { normalizeRow } from "../../src/server/util";
|
2022-09-15 00:27:49 -07:00
|
|
|
import { GetServerSideProps } from "next";
|
2021-11-01 23:03:19 -07:00
|
|
|
|
2022-09-15 00:27:49 -07:00
|
|
|
type Outfit = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
updatedAt: string;
|
|
|
|
};
|
|
|
|
type PageProps = {
|
|
|
|
outfit: Outfit;
|
|
|
|
};
|
2021-11-01 23:03:19 -07:00
|
|
|
|
2022-09-15 00:27:49 -07:00
|
|
|
const WardrobePageWrapper: NextPageWithLayout<PageProps> = ({ outfit }) => {
|
2021-11-01 23:03:19 -07:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Head>
|
|
|
|
<title>{outfit.name || "Untitled outfit"} | Dress to Impress</title>
|
|
|
|
<OutfitMetaTags outfit={outfit} />
|
|
|
|
</Head>
|
2022-09-15 00:27:49 -07:00
|
|
|
<WardrobePage />
|
2021-11-01 23:03:19 -07:00
|
|
|
</>
|
|
|
|
);
|
2022-09-15 00:27:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
WardrobePageWrapper.renderWithLayout = (children) => children;
|
2021-11-01 23:03:19 -07:00
|
|
|
|
2022-09-15 00:27:49 -07:00
|
|
|
function OutfitMetaTags({ outfit }: { outfit: Outfit }) {
|
2021-11-01 23:03:19 -07:00
|
|
|
const updatedAtTimestamp = Math.floor(
|
|
|
|
new Date(outfit.updatedAt).getTime() / 1000
|
|
|
|
);
|
|
|
|
const outfitUrl =
|
|
|
|
`https://impress-2020.openneo.net/outfits` +
|
|
|
|
`/${encodeURIComponent(outfit.id)}`;
|
|
|
|
const imageUrl =
|
|
|
|
`https://impress-outfit-images.openneo.net/outfits` +
|
|
|
|
`/${encodeURIComponent(outfit.id)}` +
|
|
|
|
`/v/${encodeURIComponent(updatedAtTimestamp)}` +
|
|
|
|
`/600.png`;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<meta property="og:title" content={outfit.name || "Untitled outfit"} />
|
|
|
|
<meta property="og:type" content="website" />
|
|
|
|
<meta property="og:image" content={imageUrl} />
|
|
|
|
<meta property="og:url" content={outfitUrl} />
|
|
|
|
<meta property="og:site_name" content="Dress to Impress" />
|
|
|
|
<meta
|
|
|
|
property="og:description"
|
|
|
|
content="A custom Neopets outfit, designed on Dress to Impress!"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-15 00:27:49 -07:00
|
|
|
export const getServerSideProps: GetServerSideProps = async ({ params }) => {
|
|
|
|
const outfitId = params?.outfitId;
|
|
|
|
if (typeof outfitId !== "string") {
|
|
|
|
throw new Error(`assertion failed: outfitId route param is missing`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const outfit = await loadOutfitData(outfitId);
|
2021-11-01 23:03:19 -07:00
|
|
|
if (outfit == null) {
|
|
|
|
return { notFound: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
outfit: {
|
|
|
|
id: outfit.id,
|
|
|
|
name: outfit.name,
|
|
|
|
updatedAt: outfit.updatedAt.toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-09-15 00:27:49 -07:00
|
|
|
};
|
2021-11-01 23:03:19 -07:00
|
|
|
|
2022-09-15 00:27:49 -07:00
|
|
|
async function loadOutfitData(id: string) {
|
2021-11-01 23:03:19 -07:00
|
|
|
const db = await connectToDb();
|
|
|
|
const [rows] = await db.query(`SELECT * FROM outfits WHERE id = ?;`, [id]);
|
|
|
|
if (rows.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalizeRow(rows[0]);
|
|
|
|
}
|
2022-09-15 00:27:49 -07:00
|
|
|
|
|
|
|
export default WardrobePageWrapper;
|