🦴 Add viewer backbone
This commit is contained in:
5
apps/viewer/pages/404.tsx
Normal file
5
apps/viewer/pages/404.tsx
Normal file
@ -0,0 +1,5 @@
|
||||
import React from 'react'
|
||||
import { NotFoundPage } from '../layouts/NotFoundPage'
|
||||
|
||||
const NotFoundErrorPage = () => <NotFoundPage />
|
||||
export default NotFoundErrorPage
|
44
apps/viewer/pages/[publicId].tsx
Normal file
44
apps/viewer/pages/[publicId].tsx
Normal file
@ -0,0 +1,44 @@
|
||||
import { PublicTypebot } from 'bot-engine'
|
||||
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
|
||||
import { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
|
||||
import prisma from '../libs/prisma'
|
||||
|
||||
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 getTypebotFromPublicId(context.query.publicId.toString())
|
||||
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}`,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const getTypebotFromPublicId = async (
|
||||
publicId: string
|
||||
): Promise<PublicTypebot | undefined> => {
|
||||
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
|
15
apps/viewer/pages/_app.tsx
Normal file
15
apps/viewer/pages/_app.tsx
Normal file
@ -0,0 +1,15 @@
|
||||
import React from 'react'
|
||||
import '../assets/styles.css'
|
||||
|
||||
type Props = {
|
||||
Component: React.ComponentType
|
||||
pageProps: {
|
||||
[key: string]: unknown
|
||||
}
|
||||
}
|
||||
|
||||
export default function MyApp({ Component, pageProps }: Props): JSX.Element {
|
||||
const { ...componentProps } = pageProps
|
||||
|
||||
return <Component {...componentProps} />
|
||||
}
|
@ -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
|
||||
|
Reference in New Issue
Block a user