perf: Server-Side Data Fetching in App Router: /availability (#16536)

* wip

* add method to OrganizationRepository

* server side fetching for availability

* fix error

* server side fetching finish

* remove log

* fix

* fix

* fix type

* no need to export

* fix type

* address nit comments
This commit is contained in:
Benny Joo authored and GitHub committed 2024-09-10 19:23:42 -07:00
1 parent 7725adedc8
commit d9777a3011
10 files changed
+208 -92

No files matched your search

@@ -1,8 +1,13 @@
import type { PageProps } from "app/_types";
import { _generateMetadata } from "app/_utils";
import { WithLayout } from "app/layoutHOC";
import { getServerSession } from "next-auth";
import { notFound } from "next/navigation";
import { AUTH_OPTIONS } from "@calcom/features/auth/lib/next-auth-options";
import { ScheduleRepository } from "@calcom/lib/server/repository/schedule";
import { TravelScheduleRepository } from "@calcom/lib/server/repository/travelSchedule";
import { UserRepository } from "@calcom/lib/server/repository/user";
import { AvailabilitySettingsWebWrapper } from "~/availability/[schedule]/schedule-view";
@@ -23,4 +28,50 @@ export const generateMetadata = async ({ params }: PageProps) => {
);
};
export default WithLayout({ getLayout: null, Page: AvailabilitySettingsWebWrapper });
const Page = async ({ params }: PageProps) => {
if (params?.schedule) {
notFound();
}
const scheduleId = Number(params.schedule);
const session = await getServerSession(AUTH_OPTIONS);
const userId = session?.user?.id;
if (!userId) {
notFound();
}
let userData, schedule, travelSchedules;
try {
userData = await UserRepository.findById({
id: userId,
select: {
timeZone: true,
defaultScheduleId: true,
},
});
if (!userData?.timeZone || !userData?.defaultScheduleId) {
throw new Error("timeZone and defaultScheduleId not found");
}
} catch (e) {
notFound();
}
try {
schedule = await ScheduleRepository.findDetailedScheduleById({
scheduleId,
isManagedEventType: false,
userId,
timeZone: userData.timeZone,
defaultScheduleId: userData.defaultScheduleId,
});
} catch (e) {}
try {
travelSchedules = await TravelScheduleRepository.findTravelSchedulesByUserId(userId);
} catch (e) {}
return <AvailabilitySettingsWebWrapper schedule={schedule} travelSchedules={travelSchedules} />;
};
export default WithLayout({ ServerPage: Page });
+25 -1
View File
@@ -1,5 +1,10 @@
import { _generateMetadata } from "app/_utils";
import { WithLayout } from "app/layoutHOC";
import { getServerSession } from "next-auth";
import { notFound } from "next/navigation";
import { AUTH_OPTIONS } from "@calcom/features/auth/lib/next-auth-options";
import { OrganizationRepository } from "@calcom/lib/server/repository/organization";
import AvailabilityPage from "~/availability/availability-view";
@@ -10,4 +15,23 @@ export const generateMetadata = async () => {
);
};
export default WithLayout({ getLayout: null, Page: AvailabilityPage });
const Page = async () => {
const session = await getServerSession(AUTH_OPTIONS);
const userId = session?.user?.id;
const orgId = session?.user?.org?.id;
if (!userId || !orgId) {
notFound();
}
try {
const currentOrg = await OrganizationRepository.findCurrentOrg({
orgId,
userId,
});
return <AvailabilityPage currentOrg={currentOrg} />;
} catch {
notFound();
}
};
export default WithLayout({ ServerPage: Page });
@@ -8,11 +8,21 @@ import { withErrorFromUnknown } from "@calcom/lib/getClientErrorFromUnknown";
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { HttpError } from "@calcom/lib/http-error";
import type { ScheduleRepository } from "@calcom/lib/server/repository/schedule";
import type { TravelScheduleRepository } from "@calcom/lib/server/repository/travelSchedule";
import { trpc } from "@calcom/trpc/react";
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
import { showToast } from "@calcom/ui";
export const AvailabilitySettingsWebWrapper = () => {
type PageProps = {
schedule?: Awaited<ReturnType<typeof ScheduleRepository.findDetailedScheduleById>>;
travelSchedules?: Awaited<ReturnType<typeof TravelScheduleRepository.findTravelSchedulesByUserId>>;
};
export const AvailabilitySettingsWebWrapper = ({
schedule: scheduleProp,
travelSchedules: travelSchedulesProp,
}: PageProps) => {
const searchParams = useCompatSearchParams();
const { t } = useLocale();
const router = useRouter();
@@ -21,14 +31,19 @@ export const AvailabilitySettingsWebWrapper = () => {
const scheduleId = searchParams?.get("schedule") ? Number(searchParams.get("schedule")) : -1;
const fromEventType = searchParams?.get("fromEventType");
const { timeFormat } = me.data || { timeFormat: null };
const { data: schedule, isPending } = trpc.viewer.availability.schedule.get.useQuery(
const { data: scheduleData, isPending: isFetchingPending } = trpc.viewer.availability.schedule.get.useQuery(
{ scheduleId },
{
enabled: !!scheduleId,
enabled: !!scheduleId && !scheduleProp,
}
);
const isPending = isFetchingPending && !scheduleProp;
const schedule = scheduleProp ?? scheduleData;
const { data: travelSchedules } = trpc.viewer.getTravelSchedules.useQuery();
const { data: travelSchedulesData } = trpc.viewer.getTravelSchedules.useQuery(undefined, {
enabled: !travelSchedulesProp,
});
const travelSchedules = travelSchedulesProp ?? travelSchedulesData;
const [isBulkUpdateModalOpen, setIsBulkUpdateModalOpen] = useState(false);
const bulkUpdateDefaultAvailabilityMutation =
@@ -13,6 +13,7 @@ import { AvailabilitySliderTable } from "@calcom/features/timezone-buddy/compone
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { HttpError } from "@calcom/lib/http-error";
import type { OrganizationRepository } from "@calcom/lib/server/repository/organization";
import { MembershipRole } from "@calcom/prisma/enums";
import type { RouterOutputs } from "@calcom/trpc/react";
import { trpc } from "@calcom/trpc/react";
@@ -170,13 +171,18 @@ function AvailabilityListWithQuery() {
);
}
export default function AvailabilityPage() {
type PageProps = {
currentOrg?: Awaited<ReturnType<typeof OrganizationRepository.findCurrentOrg>> | null;
};
export default function AvailabilityPage({ currentOrg }: PageProps) {
const { t } = useLocale();
const searchParams = useCompatSearchParams();
const router = useRouter();
const pathname = usePathname();
const me = useMeQuery();
const { data } = trpc.viewer.organizations.listCurrent.useQuery();
const { data: _data } = trpc.viewer.organizations.listCurrent.useQuery(undefined, { enabled: !currentOrg });
const data = currentOrg ?? _data;
// Get a new searchParams string by merging the current
// searchParams with a provided key/value pair