2022-02-04 19:00:08 +01:00
|
|
|
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 { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
|
|
|
|
import prisma from '../libs/prisma'
|
|
|
|
|
|
|
|
export const getServerSideProps: GetServerSideProps = async (
|
|
|
|
context: GetServerSidePropsContext
|
|
|
|
) => {
|
2022-02-10 16:23:21 +01:00
|
|
|
let typebot: PublicTypebot | 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]
|
|
|
|
try {
|
|
|
|
if (!context.req.headers.host) return { props: {} }
|
2021-12-29 10:22:26 +01:00
|
|
|
typebot = await getTypebotFromPublicId(context.query.publicId?.toString())
|
2021-12-23 16:31:56 +01:00
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
typebot,
|
|
|
|
isIE,
|
|
|
|
url: `https://${context.req.headers.host}${pathname}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
isIE,
|
|
|
|
url: `https://${context.req.headers.host}${pathname}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getTypebotFromPublicId = async (
|
2021-12-29 10:22:26 +01:00
|
|
|
publicId?: string
|
2022-02-10 16:23:21 +01:00
|
|
|
): Promise<PublicTypebot | null> => {
|
|
|
|
if (!publicId) return null
|
2021-12-23 16:31:56 +01:00
|
|
|
const typebot = await prisma.publicTypebot.findUnique({
|
|
|
|
where: { publicId },
|
|
|
|
})
|
2022-02-10 16:23:21 +01:00
|
|
|
return (typebot as unknown as PublicTypebot) ?? null
|
2021-12-23 16:31:56 +01:00
|
|
|
}
|
|
|
|
|
2022-02-04 19:00:08 +01:00
|
|
|
const App = ({ typebot, ...props }: TypebotPageProps) =>
|
|
|
|
typebot ? <TypebotPage typebot={typebot} {...props} /> : <NotFoundPage />
|
|
|
|
|
2021-12-23 16:31:56 +01:00
|
|
|
export default App
|