2
0

🦴 Add viewer backbone

This commit is contained in:
Baptiste Arnaud
2021-12-23 16:31:56 +01:00
parent 9a78a341d2
commit d369b4d941
24 changed files with 576 additions and 182 deletions

View File

@ -1,7 +1,46 @@
import React from 'react'
import { PublicTypebot } from 'bot-engine'
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
import { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
import prisma from '../libs/prisma'
const HomePage = () => {
return <div>Welcome to "Viewer"!</div>
export const getServerSideProps: GetServerSideProps = async (
context: GetServerSidePropsContext
) => {
let typebot: PublicTypebot | undefined
const isIE = /MSIE|Trident/.test(context.req.headers['user-agent'] ?? '')
const pathname = context.resolvedUrl.split('?')[0]
try {
if (!context.req.headers.host) return { props: {} }
typebot = await getTypebotFromUrl(context.req.headers.host)
if (!typebot) return { props: {} }
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}`,
},
}
}
export default HomePage
const getTypebotFromUrl = async (
hostname: string
): Promise<PublicTypebot | undefined> => {
const publicId = hostname.split('.').shift()
if (!publicId) return
const typebot = await prisma.publicTypebot.findUnique({
where: { publicId },
})
return (typebot as unknown as PublicTypebot | undefined) ?? undefined
}
const App = (props: TypebotPageProps) => <TypebotPage {...props} />
export default App