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

67 lines
2.1 KiB
TypeScript
Raw Normal View History

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 { 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]
try {
if (!context.req.headers.host) return { props: {} }
const viewerUrls = (process.env.NEXT_PUBLIC_VIEWER_URL ?? '').split(',')
typebot = viewerUrls.some((url) =>
context.req.headers.host?.includes(url.split('//')[1])
2022-02-18 14:57:10 +01:00
)
? await getTypebotFromPublicId(context.query.publicId?.toString())
: await getTypebotFromCustomDomain(
2022-03-14 16:54:37 +01:00
`${context.req.headers.host}${pathname === '/' ? '' : pathname}`
2022-02-18 14:57:10 +01:00
)
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}`,
},
}
}
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: { contains: 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 App = ({ typebot, ...props }: TypebotPageProps) =>
isDefined(typebot) ? (
<TypebotPage typebot={typebot} {...props} />
) : (
<NotFoundPage />
)
2021-12-23 16:31:56 +01:00
export default App