Skip to content

Commit

Permalink
🔒 Expose minimum data to NEXT_DATA json
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Aug 14, 2023
1 parent 401efa9 commit de616ea
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 64 deletions.
36 changes: 17 additions & 19 deletions apps/viewer/src/components/TypebotPageV3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ import { BackgroundType, Typebot } from '@typebot.io/schemas'
import { useRouter } from 'next/router'
import { SEO } from './Seo'

export type TypebotPageProps = {
export type TypebotV3PageProps = {
url: string
typebot: Pick<Typebot, 'settings' | 'theme' | 'name' | 'publicId'>
name: string
publicId: string | null
isHideQueryParamsEnabled: boolean | null
background: Typebot['theme']['general']['background']
metadata: Typebot['settings']['metadata']
}

export const TypebotPageV3 = ({ url, typebot }: TypebotPageProps) => {
export const TypebotPageV3 = ({
publicId,
name,
url,
isHideQueryParamsEnabled,
metadata,
background,
}: TypebotV3PageProps) => {
const { asPath, push } = useRouter()

const background = typebot?.theme.general.background

const clearQueryParamsIfNecessary = () => {
const hasQueryParams = asPath.includes('?')
if (
!hasQueryParams ||
!(typebot?.settings.general.isHideQueryParamsEnabled ?? true)
)
return
if (!hasQueryParams || !(isHideQueryParamsEnabled ?? true)) return
push(asPath.split('?')[0], undefined, { shallow: true })
}

Expand All @@ -36,15 +41,8 @@ export const TypebotPageV3 = ({ url, typebot }: TypebotPageProps) => {
: '#fff',
}}
>
<SEO
url={url}
typebotName={typebot.name}
metadata={typebot.settings.metadata}
/>
<Standard
typebot={typebot.publicId}
onInit={clearQueryParamsIfNecessary}
/>
<SEO url={url} typebotName={name} metadata={metadata} />
<Standard typebot={publicId} onInit={clearQueryParamsIfNecessary} />
</div>
)
}
134 changes: 89 additions & 45 deletions apps/viewer/src/pages/[[...publicId]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import { IncomingMessage } from 'http'
import { ErrorPage } from '@/components/ErrorPage'
import { NotFoundPage } from '@/components/NotFoundPage'
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
import {
env,
getViewerUrl,
isDefined,
isNotDefined,
omit,
} from '@typebot.io/lib'
import { env, getViewerUrl, isNotDefined } from '@typebot.io/lib'
import prisma from '../lib/prisma'
import { TypebotPageProps, TypebotPageV2 } from '@/components/TypebotPageV2'
import { TypebotPageV3 } from '@/components/TypebotPageV3'
import { TypebotPageV3, TypebotV3PageProps } from '@/components/TypebotPageV3'

// Browsers that doesn't support ES modules and/or web components
const incompatibleBrowsers = [
Expand Down Expand Up @@ -67,14 +61,11 @@ export const getServerSideProps: GetServerSideProps = async (
const publishedTypebot = isMatchingViewerUrl
? await getTypebotFromPublicId(context.query.publicId?.toString())
: await getTypebotFromCustomDomain(customDomain)
const headCode = publishedTypebot?.settings.metadata.customHeadCode
return {
props: {
publishedTypebot,
incompatibleBrowser,
url: `https://${forwardedHost ?? host}${pathname}`,
customHeadCode:
isDefined(headCode) && headCode !== '' ? headCode : null,
},
}
} catch (err) {
Expand All @@ -88,12 +79,18 @@ export const getServerSideProps: GetServerSideProps = async (
}
}

const getTypebotFromPublicId = async (
publicId?: string
): Promise<TypebotPageProps['publishedTypebot'] | null> => {
const publishedTypebot = await prisma.publicTypebot.findFirst({
const getTypebotFromPublicId = async (publicId?: string) => {
const publishedTypebot = (await prisma.publicTypebot.findFirst({
where: { typebot: { publicId: publicId ?? '' } },
include: {
select: {
variables: true,
settings: true,
theme: true,
version: true,
groups: true,
edges: true,
typebotId: true,
id: true,
typebot: {
select: {
name: true,
Expand All @@ -103,21 +100,39 @@ const getTypebotFromPublicId = async (
},
},
},
})
})) as TypebotPageProps['publishedTypebot'] | null
if (isNotDefined(publishedTypebot)) return null
return omit(
publishedTypebot,
'createdAt',
'updatedAt'
) as TypebotPageProps['publishedTypebot']
return publishedTypebot.version
? ({
name: publishedTypebot.typebot.name,
publicId: publishedTypebot.typebot.publicId ?? null,
background: publishedTypebot.theme.general.background,
isHideQueryParamsEnabled:
publishedTypebot.settings.general.isHideQueryParamsEnabled ?? null,
metadata: publishedTypebot.settings.metadata,
} as Pick<
TypebotV3PageProps,
| 'name'
| 'publicId'
| 'background'
| 'isHideQueryParamsEnabled'
| 'metadata'
>)
: publishedTypebot
}

const getTypebotFromCustomDomain = async (
customDomain: string
): Promise<TypebotPageProps['publishedTypebot'] | null> => {
const publishedTypebot = await prisma.publicTypebot.findFirst({
const getTypebotFromCustomDomain = async (customDomain: string) => {
const publishedTypebot = (await prisma.publicTypebot.findFirst({
where: { typebot: { customDomain } },
include: {
select: {
variables: true,
settings: true,
theme: true,
version: true,
groups: true,
edges: true,
typebotId: true,
id: true,
typebot: {
select: {
name: true,
Expand All @@ -127,13 +142,25 @@ const getTypebotFromCustomDomain = async (
},
},
},
})
})) as TypebotPageProps['publishedTypebot'] | null
if (isNotDefined(publishedTypebot)) return null
return omit(
publishedTypebot,
'createdAt',
'updatedAt'
) as TypebotPageProps['publishedTypebot']
return publishedTypebot.version
? ({
name: publishedTypebot.typebot.name,
publicId: publishedTypebot.typebot.publicId ?? null,
background: publishedTypebot.theme.general.background,
isHideQueryParamsEnabled:
publishedTypebot.settings.general.isHideQueryParamsEnabled ?? null,
metadata: publishedTypebot.settings.metadata,
} as Pick<
TypebotV3PageProps,
| 'name'
| 'publicId'
| 'background'
| 'isHideQueryParamsEnabled'
| 'metadata'
>)
: publishedTypebot
}

const getHost = (
Expand All @@ -147,7 +174,22 @@ const App = ({
publishedTypebot,
incompatibleBrowser,
...props
}: TypebotPageProps & { incompatibleBrowser: string | null }) => {
}: {
isIE: boolean
customHeadCode: string | null
url: string
publishedTypebot:
| TypebotPageProps['publishedTypebot']
| Pick<
TypebotV3PageProps,
| 'name'
| 'publicId'
| 'background'
| 'isHideQueryParamsEnabled'
| 'metadata'
>
incompatibleBrowser: string | null
}) => {
if (incompatibleBrowser)
return (
<ErrorPage
Expand All @@ -158,22 +200,24 @@ const App = ({
}
/>
)
if (!publishedTypebot || publishedTypebot.typebot.isArchived)
if (
!publishedTypebot ||
('typebot' in publishedTypebot && publishedTypebot.typebot.isArchived)
)
return <NotFoundPage />
if (publishedTypebot.typebot.isClosed)
if ('typebot' in publishedTypebot && publishedTypebot.typebot.isClosed)
return <ErrorPage error={new Error('This bot is now closed')} />
return publishedTypebot.version ? (
return 'typebot' in publishedTypebot ? (
<TypebotPageV2 publishedTypebot={publishedTypebot} {...props} />
) : (
<TypebotPageV3
url={props.url}
typebot={{
name: publishedTypebot.typebot.name,
publicId: publishedTypebot.typebot.publicId,
settings: publishedTypebot.settings,
theme: publishedTypebot.theme,
}}
name={publishedTypebot.name}
publicId={publishedTypebot.publicId}
isHideQueryParamsEnabled={publishedTypebot.isHideQueryParamsEnabled}
background={publishedTypebot.background}
metadata={publishedTypebot.metadata}
/>
) : (
<TypebotPageV2 publishedTypebot={publishedTypebot} {...props} />
)
}

Expand Down

3 comments on commit de616ea

@vercel
Copy link

@vercel vercel bot commented on de616ea Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

receita.info
rhino.cr8.ai
sheep.cr8.ai
snake.cr8.ai
svhm.mprs.in
tiger.cr8.ai
video.cr8.ai
webwhats.pro
yoda.riku.ai
zebra.cr8.ai
alvodelas.com
bemestar.club
bot.krdfy.com
cat.hidden.sg
panther.cr7.ai
panther.cr8.ai
pay.sifuim.com
penguin.cr8.ai
segredomeu.com
talk.gocare.io
test.bot.gives
ticketfute.com
unicorn.cr8.ai
whats-app.chat
apo.nigerias.io
app.blogely.com
apr.nigerias.io
aso.nigerias.io
blackcan.cr8.ai
blackvip.online
bot.4display.nl
bot.artiweb.app
bot.devitus.com
bot.reeplai.com
bot.scayver.com
bot.tc-mail.com
carspecs.lam.ee
chat.lalmon.com
chat.sureb4.com
conversawpp.com
eventhub.com.au
feiraododia.com
fitness.riku.ai
games.klujo.com
localove.online
proscale.com.br
sellmycarbr.com
typebot.aloe.do
app-liberado.pro
ask.pemantau.org
batepapo.digital
bot.bjogador.com
bot.contakit.com
bot.imovfast.com
bot.piccinato.co
botc.ceox.com.br
chat.sifucrm.com
chat.syncwin.com
chatonlineja.com
clo.closeer.work
cockroach.cr8.ai
desafioem21d.com
viewer-v2-typebot-io.vercel.app
mdb.assessoria.fernanda.progenbr.com
mdb.assessoria.jbatista.progenbr.com
mdb.assessoria.mauricio.progenbr.com
form.shopmercedesbenzsouthorlando.com
mdb.evento.equipeinterna.progenbr.com
bot.studiotecnicoimmobiliaremerelli.it
mdb.assessoria.boaventura.progenbr.com
mdb.assessoria.jtrebesqui.progenbr.com
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
gabinete.baleia.formulario.progenbr.com
mdb.assessoria.carreirinha.progenbr.com
chrome-os-inquiry-system.itschromeos.com
mdb.assessoria.paulomarques.progenbr.com
viewer-v2-git-main-typebot-io.vercel.app
main-menu-for-itschromeos.itschromeos.com
mdb.assessoria.qrcode.ademir.progenbr.com
mdb.assessoria.qrcode.arthur.progenbr.com
mdb.assessoria.qrcode.danilo.progenbr.com
mdb.assessoria.qrcode.marcao.progenbr.com
mdb.assessoria.qrcode.marcio.progenbr.com
mdb.assessoria.qrcode.aloisio.progenbr.com
mdb.assessoria.qrcode.girotto.progenbr.com
mdb.assessoria.qrcode.marinho.progenbr.com
mdb.assessoria.qrcode.rodrigo.progenbr.com
mdb.assessoria.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.desideri.progenbr.com
mdb.assessoria.qrcode.fernanda.progenbr.com
mdb.assessoria.qrcode.jbatista.progenbr.com
mdb.assessoria.qrcode.mauricio.progenbr.com
mdb.assessoria.fernanda.regional.progenbr.com
mdb.assessoria.qrcode.boaventura.progenbr.com
mdb.assessoria.qrcode.jtrebesqui.progenbr.com
mdb.assessoria.qrcode.carreirinha.progenbr.com
mdb.assessoria.qrcode.paulomarques.progenbr.com
mdb.assessoria.qrcode.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.fernanda.regional.progenbr.com

@vercel
Copy link

@vercel vercel bot commented on de616ea Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on de616ea Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-typebot-io.vercel.app
docs-git-main-typebot-io.vercel.app
docs.typebot.io

Please sign in to comment.