From 6fe824088aca34100a30fc9dd989623ae0219cee Mon Sep 17 00:00:00 2001 From: hariombalhara Date: Fri, 11 Feb 2022 20:04:12 +0530 Subject: [PATCH] Performance improvements (lazy loading) for booking pages (#1762) --- apps/web/components/ui/AvatarSSR.tsx | 28 +++++ apps/web/pages/[user].tsx | 158 +++++++++++++-------------- apps/web/prisma/schema.prisma | 2 +- package.json | 4 +- 4 files changed, 110 insertions(+), 82 deletions(-) create mode 100644 apps/web/components/ui/AvatarSSR.tsx diff --git a/apps/web/components/ui/AvatarSSR.tsx b/apps/web/components/ui/AvatarSSR.tsx new file mode 100644 index 0000000000..1516d51de3 --- /dev/null +++ b/apps/web/components/ui/AvatarSSR.tsx @@ -0,0 +1,28 @@ +import { User } from "@prisma/client"; + +import classNames from "@lib/classNames"; +import { defaultAvatarSrc } from "@lib/profile"; + +export type AvatarProps = { + user: Pick & { emailMd5?: string }; + className?: string; + size?: number; + title?: string; + alt: string; +}; + +// An SSR Supported version of Avatar component. +// FIXME: title support is missing +export function AvatarSSR(props: AvatarProps) { + const { user, size } = props; + const nameOrUsername = user.name || user.username || ""; + const className = classNames("rounded-full", props.className, size && `h-${size} w-${size}`); + let imgSrc; + const alt = props.alt || nameOrUsername; + if (user.avatar) { + imgSrc = user.avatar; + } else if (user.emailMd5) { + imgSrc = defaultAvatarSrc({ md5: user.emailMd5 }); + } + return imgSrc ? {alt} : null; +} diff --git a/apps/web/pages/[user].tsx b/apps/web/pages/[user].tsx index 81f87aaaaa..62765205f6 100644 --- a/apps/web/pages/[user].tsx +++ b/apps/web/pages/[user].tsx @@ -1,6 +1,8 @@ import { ArrowRightIcon } from "@heroicons/react/outline"; import { BadgeCheckIcon } from "@heroicons/react/solid"; +import crypto from "crypto"; import { GetServerSidePropsContext } from "next"; +import dynamic from "next/dynamic"; import Link from "next/link"; import { useRouter } from "next/router"; import React, { useState } from "react"; @@ -9,24 +11,23 @@ import { JSONObject } from "superjson/dist/types"; import { useLocale } from "@lib/hooks/useLocale"; import useTheme from "@lib/hooks/useTheme"; -import showToast from "@lib/notification"; import prisma from "@lib/prisma"; import { inferSSRProps } from "@lib/types/inferSSRProps"; -import EventTypeDescription from "@components/eventtype/EventTypeDescription"; -import { HeadSeo } from "@components/seo/head-seo"; -import Avatar from "@components/ui/Avatar"; +import { AvatarSSR } from "@components/ui/AvatarSSR"; import { ssrInit } from "@server/lib/ssr"; -import CryptoSection from "../ee/components/web3/CryptoSection"; +const EventTypeDescription = dynamic(() => import("@components/eventtype/EventTypeDescription")); +const HeadSeo = dynamic(() => import("@components/seo/head-seo").then((mod) => mod.HeadSeo)); +const CryptoSection = dynamic(() => import("../ee/components/web3/CryptoSection")); interface EvtsToVerify { [evtId: string]: boolean; } export default function User(props: inferSSRProps) { - const { isReady, Theme } = useTheme(props.user.theme); + const { Theme } = useTheme(props.user.theme); const { user, eventTypes } = props; const { t } = useLocale(); const router = useRouter(); @@ -35,7 +36,6 @@ export default function User(props: inferSSRProps) { const nameOrUsername = user.name || user.username || ""; const [evtsToVerify, setEvtsToVerify] = useState({}); - return ( <> @@ -46,80 +46,75 @@ export default function User(props: inferSSRProps) { username={(user.username as string) || ""} // avatar={user.avatar || undefined} /> - {isReady && ( -
-
-
- -

- {nameOrUsername} - {user.verified && ( - - )} -

-

{user.bio}

-
- - {eventTypes.length === 0 && ( -
-
-

- {t("uh_oh")} -

-

{t("no_event_types_have_been_setup")}

+
+
+
+ +

+ {nameOrUsername} + {user.verified && ( + + )} +

+

{user.bio}

+
+ + {eventTypes.length === 0 && ( +
+
+

+ {t("uh_oh")} +

+

{t("no_event_types_have_been_setup")}

- )} -
- -
- )} +
+ )} +
+ +
); } @@ -224,7 +219,10 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => return { props: { - user, + user: { + ...user, + emailMd5: crypto.createHash("md5").update(user.email).digest("hex"), + }, eventTypes, trpcState: ssr.dehydrate(), }, diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma index 89a0dbcc45..7e0e95cc5f 100644 --- a/apps/web/prisma/schema.prisma +++ b/apps/web/prisma/schema.prisma @@ -245,7 +245,7 @@ model Booking { payment Payment[] destinationCalendar DestinationCalendar? cancellationReason String? - rejectionReason String? + rejectionReason String? } model Schedule { diff --git a/package.json b/package.json index 1fa8dfbabb..a9d2d52578 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,9 @@ "husky": "^7.0.1", "lint-staged": "^11.1.2", "prettier": "^2.5.1", - "prettier-plugin-tailwindcss": "^0.1.6", + "prettier-plugin-tailwindcss": "^0.1.6" + }, + "dependencies": { "turbo": "latest" }, "lint-staged": {