From de616ea649f1e12599ef2c216d4e46588e6846ad Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Mon, 14 Aug 2023 16:55:47 +0200 Subject: [PATCH] :lock: Expose minimum data to NEXT_DATA json --- apps/viewer/src/components/TypebotPageV3.tsx | 36 +++-- apps/viewer/src/pages/[[...publicId]].tsx | 134 ++++++++++++------- 2 files changed, 106 insertions(+), 64 deletions(-) diff --git a/apps/viewer/src/components/TypebotPageV3.tsx b/apps/viewer/src/components/TypebotPageV3.tsx index e16a31a1e..520c58f43 100644 --- a/apps/viewer/src/components/TypebotPageV3.tsx +++ b/apps/viewer/src/components/TypebotPageV3.tsx @@ -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 + 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 }) } @@ -36,15 +41,8 @@ export const TypebotPageV3 = ({ url, typebot }: TypebotPageProps) => { : '#fff', }} > - - + + ) } diff --git a/apps/viewer/src/pages/[[...publicId]].tsx b/apps/viewer/src/pages/[[...publicId]].tsx index 54a442610..cb3c591f9 100644 --- a/apps/viewer/src/pages/[[...publicId]].tsx +++ b/apps/viewer/src/pages/[[...publicId]].tsx @@ -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 = [ @@ -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) { @@ -88,12 +79,18 @@ export const getServerSideProps: GetServerSideProps = async ( } } -const getTypebotFromPublicId = async ( - publicId?: string -): Promise => { - 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, @@ -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 => { - 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, @@ -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 = ( @@ -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 ( ) - if (!publishedTypebot || publishedTypebot.typebot.isArchived) + if ( + !publishedTypebot || + ('typebot' in publishedTypebot && publishedTypebot.typebot.isArchived) + ) return - if (publishedTypebot.typebot.isClosed) + if ('typebot' in publishedTypebot && publishedTypebot.typebot.isClosed) return - return publishedTypebot.version ? ( + return 'typebot' in publishedTypebot ? ( + + ) : ( - ) : ( - ) }