2
0
Files
bot/apps/viewer/pages/[[...publicId]].tsx

95 lines
3.1 KiB
TypeScript
Raw Normal View History

import { IncomingMessage } from 'http'
import { NotFoundPage } from 'layouts/NotFoundPage'
2022-01-06 09:40:56 +01:00
import { PublicTypebot } from 'models'
2021-12-23 16:31:56 +01:00
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
import sanitizeHtml from 'sanitize-html'
import { isDefined, isNotDefined, omit } from 'utils'
2021-12-23 16:31:56 +01:00
import { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
import prisma from '../libs/prisma'
export const getServerSideProps: GetServerSideProps = async (
context: GetServerSidePropsContext
) => {
let typebot: Omit<PublicTypebot, 'createdAt' | 'updatedAt'> | null
2021-12-23 16:31:56 +01:00
const isIE = /MSIE|Trident/.test(context.req.headers['user-agent'] ?? '')
const pathname = context.resolvedUrl.split('?')[0]
const { host, forwardedHost } = getHost(context.req)
2021-12-23 16:31:56 +01:00
try {
if (!host) return { props: {} }
const viewerUrls = (process.env.NEXT_PUBLIC_VIEWER_URL ?? '').split(',')
const isMatchingViewerUrl = viewerUrls.some(
(url) =>
host.split(':')[0].includes(url.split('//')[1].split(':')[0]) ||
(forwardedHost &&
forwardedHost
.split(':')[0]
.includes(url.split('//')[1].split(':')[0]))
2022-02-18 14:57:10 +01:00
)
const customDomain = `${forwardedHost ?? host}${
pathname === '/' ? '' : pathname
}`
typebot = isMatchingViewerUrl
2022-02-18 14:57:10 +01:00
? await getTypebotFromPublicId(context.query.publicId?.toString())
: await getTypebotFromCustomDomain(customDomain)
if (!typebot)
console.log(
isMatchingViewerUrl
? `Couldn't find publicId: ${context.query.publicId?.toString()}`
: `Couldn't find customDomain: ${customDomain}`
)
const headCode = typebot?.settings.metadata.customHeadCode
2021-12-23 16:31:56 +01:00
return {
props: {
typebot,
isIE,
url: `https://${forwardedHost ?? host}${pathname}`,
customHeadCode:
isDefined(headCode) && headCode !== ''
? sanitizeHtml(headCode, { allowedTags: ['script', 'meta'] })
: null,
2021-12-23 16:31:56 +01:00
},
}
} catch (err) {
console.error(err)
}
return {
props: {
isIE,
url: `https://${forwardedHost ?? host}${pathname}`,
2021-12-23 16:31:56 +01:00
},
}
}
2022-02-18 14:57:10 +01:00
const getTypebotFromPublicId = async (publicId?: string) => {
if (!publicId) return null
2021-12-23 16:31:56 +01:00
const typebot = await prisma.publicTypebot.findUnique({
where: { publicId },
})
if (isNotDefined(typebot)) return null
2022-03-09 15:12:00 +01:00
return omit(typebot as unknown as PublicTypebot, 'createdAt', 'updatedAt')
2021-12-23 16:31:56 +01:00
}
2022-02-18 14:57:10 +01:00
const getTypebotFromCustomDomain = async (customDomain: string) => {
2022-03-14 16:54:37 +01:00
const typebot = await prisma.publicTypebot.findFirst({
where: { customDomain },
2022-02-18 14:57:10 +01:00
})
if (isNotDefined(typebot)) return null
2022-03-09 15:12:00 +01:00
return omit(typebot as unknown as PublicTypebot, 'createdAt', 'updatedAt')
2022-02-18 14:57:10 +01:00
}
const getHost = (
req?: IncomingMessage
): { host?: string; forwardedHost?: string } => ({
host: req?.headers ? req.headers.host : window.location.host,
forwardedHost: req?.headers['x-forwarded-host'] as string | undefined,
})
const App = ({ typebot, ...props }: TypebotPageProps) =>
isDefined(typebot) ? (
<TypebotPage typebot={typebot} {...props} />
) : (
<NotFoundPage />
)
2021-12-23 16:31:56 +01:00
export default App