manifold/web/components/SEO.tsx

90 lines
2.2 KiB
TypeScript
Raw Normal View History

import { ReactNode } from 'react'
2021-12-16 18:40:23 +00:00
import Head from 'next/head'
export type OgCardProps = {
question: string
2022-02-18 00:34:11 +00:00
probability?: string
metadata: string
creatorName: string
creatorUsername: string
creatorAvatarUrl?: string
}
function buildCardUrl(props: OgCardProps) {
2022-02-18 00:34:11 +00:00
const probabilityParam =
props.probability === undefined
? ''
: `&probability=${encodeURIComponent(props.probability ?? '')}`
const creatorAvatarUrlParam =
props.creatorAvatarUrl === undefined
? ''
: `&creatorAvatarUrl=${encodeURIComponent(props.creatorAvatarUrl ?? '')}`
// URL encode each of the props, then add them as query params
return (
`https://manifold-og-image.vercel.app/m.png` +
`?question=${encodeURIComponent(props.question)}` +
2022-02-18 00:34:11 +00:00
probabilityParam +
`&metadata=${encodeURIComponent(props.metadata)}` +
`&creatorName=${encodeURIComponent(props.creatorName)}` +
creatorAvatarUrlParam +
`&creatorUsername=${encodeURIComponent(props.creatorUsername)}`
)
}
2021-12-16 18:40:23 +00:00
export function SEO(props: {
title: string
description: string
url?: string
children?: ReactNode
ogCardProps?: OgCardProps
2021-12-16 18:40:23 +00:00
}) {
const { title, description, url, children, ogCardProps } = props
2021-12-16 18:40:23 +00:00
return (
<Head>
<title>{title} | Manifold Markets</title>
2021-12-16 18:40:23 +00:00
<meta
property="og:title"
name="twitter:title"
content={title}
key="title"
/>
<meta name="description" content={description} key="description1" />
<meta
property="og:description"
name="twitter:description"
content={description}
key="description2"
/>
{url && (
<meta
property="og:url"
content={'https://manifold.markets' + url}
2021-12-16 18:40:23 +00:00
key="url"
/>
)}
{ogCardProps && (
<>
<meta
property="og:image"
content={buildCardUrl(ogCardProps)}
key="image1"
/>
<meta name="twitter:card" content="summary_large_image" key="card" />
<meta
name="twitter:image"
content={buildCardUrl(ogCardProps)}
key="image2"
/>
</>
)}
2021-12-16 18:40:23 +00:00
{children}
</Head>
)
}