From 680e967a8cba838acaa3ea312874475ef3b448b9 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Mon, 27 Feb 2023 09:17:15 +0100 Subject: [PATCH] :adhesive_bandage: (viewer) Add path where old engine is forced --- apps/viewer/src/pages/old/[[...publicId]].tsx | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 apps/viewer/src/pages/old/[[...publicId]].tsx diff --git a/apps/viewer/src/pages/old/[[...publicId]].tsx b/apps/viewer/src/pages/old/[[...publicId]].tsx new file mode 100644 index 000000000..27edf605c --- /dev/null +++ b/apps/viewer/src/pages/old/[[...publicId]].tsx @@ -0,0 +1,70 @@ +import { IncomingMessage } from 'http' +import { ErrorPage } from '@/components/ErrorPage' +import { NotFoundPage } from '@/components/NotFoundPage' +import { GetServerSideProps, GetServerSidePropsContext } from 'next' +import { isDefined, isNotDefined, omit } from 'utils' +import prisma from '../../lib/prisma' +import { TypebotPageProps, TypebotPageV2 } from '@/components/TypebotPageV2' + +export const getServerSideProps: GetServerSideProps = async ( + context: GetServerSidePropsContext +) => { + const pathname = context.resolvedUrl.split('?')[0] + const { host, forwardedHost } = getHost(context.req) + try { + if (!host) return { props: {} } + const publishedTypebot = await getTypebotFromPublicId( + context.query.publicId?.toString() + ) + const headCode = publishedTypebot?.settings.metadata.customHeadCode + return { + props: { + publishedTypebot, + url: `https://${forwardedHost ?? host}${pathname}`, + customHeadCode: + isDefined(headCode) && headCode !== '' ? headCode : null, + }, + } + } catch (err) { + console.error(err) + } + return { + props: { + url: `https://${forwardedHost ?? host}${pathname}`, + }, + } +} + +const getTypebotFromPublicId = async ( + publicId?: string +): Promise => { + const publishedTypebot = await prisma.publicTypebot.findFirst({ + where: { typebot: { publicId: publicId ?? '' } }, + include: { + typebot: { select: { name: true, isClosed: true, isArchived: true } }, + }, + }) + if (isNotDefined(publishedTypebot)) return null + return omit( + publishedTypebot, + 'createdAt', + 'updatedAt' + ) as TypebotPageProps['publishedTypebot'] +} + +const getHost = ( + req?: IncomingMessage +): { host?: string; forwardedHost?: string } => ({ + host: req?.headers ? req.headers.host : window.location.host, + forwardedHost: req?.headers['x-forwarded-host'] as string | undefined, +}) + +const App = ({ publishedTypebot, ...props }: TypebotPageProps) => { + if (!publishedTypebot || publishedTypebot.typebot.isArchived) + return + if (publishedTypebot.typebot.isClosed) + return + return +} + +export default App