Skip to content

Commit

Permalink
chore: remove extra fetching and get user locale from headers in sett…
Browse files Browse the repository at this point in the history
…ings pages (#18048)

* add settings pages to matcher

* fetch user locale from headers in settings pages
  • Loading branch information
hbjORbj authored Dec 10, 2024
1 parent 5ba676b commit a66eb9d
Show file tree
Hide file tree
Showing 43 changed files with 92 additions and 180 deletions.
9 changes: 9 additions & 0 deletions apps/web/app/_utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export const getFixedT = async (locale: string, ns = "common") => {
return i18n.getFixedT(locale, ns);
};

export const getTranslate = async () => {
const headersList = await headers();
// If "x-locale" does not exist in header,
// ensure that config.matcher in middleware includes the page you are testing
const locale = headersList.get("x-locale");
const t = await getFixedT(locale ?? "en");
return t;
};

export const _generateMetadata = async (
getTitle: (t: TFunction<string, undefined>) => string,
getDescription: (t: TFunction<string, undefined>) => string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { _generateMetadata } from "app/_utils";
import { getFixedT } from "app/_utils";
import { getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import AdminAppsList from "@calcom/features/apps/AdminAppsList";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";

Expand All @@ -12,9 +11,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();

const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

return (
<SettingsHeader title={t("apps")} description={t("admin_apps_description")}>
Expand Down
6 changes: 2 additions & 4 deletions apps/web/app/settings/(admin-layout)/admin/flags/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { _generateMetadata, getFixedT } from "app/_utils";
import { _generateMetadata, getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/features/auth/lib/get-server-session-for-app-dir";
import { FlagListingView } from "@calcom/features/flags/pages/flag-listing-view";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";

Expand All @@ -11,8 +10,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();
return (
<SettingsHeader title={t("feature_flags")} description={t("admin_flags_description")}>
<FlagListingView />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { _generateMetadata } from "app/_utils";
import { getFixedT } from "app/_utils";
import { getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";

import ImpersonationView from "~/settings/admin/impersonation-view";
Expand All @@ -13,9 +12,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();

const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();
return (
<SettingsHeader title={t("admin")} description={t("impersonation")}>
<ImpersonationView />
Expand Down
6 changes: 2 additions & 4 deletions apps/web/app/settings/(admin-layout)/admin/lockedSMS/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { _generateMetadata, getFixedT } from "app/_utils";
import { _generateMetadata, getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/features/auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";

import LockedSMSView from "~/settings/admin/locked-sms-view";
Expand All @@ -12,8 +11,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();
return (
<SettingsHeader title={t("lockedSMS")} description={t("admin_lockedSMS_description")}>
<LockedSMSView />
Expand Down
6 changes: 2 additions & 4 deletions apps/web/app/settings/(admin-layout)/admin/oAuth/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { _generateMetadata, getFixedT } from "app/_utils";
import { _generateMetadata, getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/features/auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";

import LegacyPage from "~/settings/admin/oauth-view";
Expand All @@ -12,8 +11,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();
return (
<SettingsHeader title={t("oAuth")} description={t("admin_oAuth_description")}>
<LegacyPage />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { type Params } from "app/_types";
import { _generateMetadata, getFixedT } from "app/_utils";
import { _generateMetadata, getTranslate } from "app/_utils";
import { notFound } from "next/navigation";
import { z } from "zod";

import { getServerSessionForAppDir } from "@calcom/features/auth/lib/get-server-session-for-app-dir";
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
import { OrgForm } from "@calcom/features/ee/organizations/pages/settings/admin/AdminOrgEditPage";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
Expand Down Expand Up @@ -35,8 +34,7 @@ const Page = async ({ params }: { params: Params }) => {

try {
const org = await OrganizationRepository.adminFindById({ id: input.data.id });
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();
return (
<SettingsHeader
title={`${t("editing_org")}: ${org.name}`}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { _generateMetadata } from "app/_utils";
import { getFixedT } from "app/_utils";
import { getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
import AdminOrgTable from "@calcom/features/ee/organizations/pages/settings/admin/AdminOrgPage";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
Expand All @@ -13,9 +12,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();

const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();
return (
<SettingsHeader title={t("organizations")} description={t("orgs_page_description")}>
<LicenseRequired>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { type Params } from "app/_types";
import { _generateMetadata, getFixedT } from "app/_utils";
import { _generateMetadata, getTranslate } from "app/_utils";
import { notFound } from "next/navigation";
import { z } from "zod";

import { getServerSessionForAppDir } from "@calcom/features/auth/lib/get-server-session-for-app-dir";
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
import { UsersEditView } from "@calcom/features/ee/users/pages/users-edit-view";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
Expand Down Expand Up @@ -37,8 +36,7 @@ const Page = async ({ params }: { params: Params }) => {

try {
const user = await UserRepository.adminFindById(input.data.id);
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

return (
<SettingsHeader title={t("editing_user")} description={t("admin_users_edit_description")}>
Expand Down
6 changes: 2 additions & 4 deletions apps/web/app/settings/(admin-layout)/admin/users/add/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { _generateMetadata, getFixedT } from "app/_utils";
import { _generateMetadata, getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/features/auth/lib/get-server-session-for-app-dir";
import UsersAddView from "@calcom/features/ee/users/pages/users-add-view";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";

Expand All @@ -11,8 +10,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

return (
<SettingsHeader title={t("add_new_user")} description={t("admin_users_add_description")}>
Expand Down
6 changes: 2 additions & 4 deletions apps/web/app/settings/(admin-layout)/admin/users/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { _generateMetadata, getFixedT } from "app/_utils";
import { _generateMetadata, getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/features/auth/lib/get-server-session-for-app-dir";
import UsersListingView from "@calcom/features/ee/users/pages/users-listing-view";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
import { Button } from "@calcom/ui";
Expand All @@ -12,8 +11,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();
return (
<SettingsHeader
title={t("users")}
Expand Down
6 changes: 2 additions & 4 deletions apps/web/app/settings/(settings-layout)/billing/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { _generateMetadata } from "app/_utils";
import { getFixedT } from "app/_utils";
import { getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";

import BillingView from "~/settings/billing/billing-view";
Expand All @@ -13,8 +12,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

return (
<SettingsHeader
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getFixedT, _generateMetadata } from "app/_utils";
import { getTranslate, _generateMetadata } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
import { APP_NAME } from "@calcom/lib/constants";

Expand All @@ -13,10 +12,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
// FIXME: Refactor me once next-auth endpoint is migrated to App Router
const session = await getServerSessionForAppDir();

const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

return (
<SettingsHeader
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { PageProps } from "app/_types";
import { getFixedT, _generateMetadata } from "app/_utils";
import { getTranslate, _generateMetadata } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
import { EditWebhookView } from "@calcom/features/webhooks/pages/webhook-edit-view";
import { APP_NAME } from "@calcom/lib/constants";
Expand All @@ -14,9 +13,7 @@ export const generateMetadata = async () =>
);

const Page = async ({ params }: PageProps) => {
const session = await getServerSessionForAppDir();

const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();
const id = typeof params?.id === "string" ? params.id : undefined;

const webhook = await WebhookRepository.findByWebhookId(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getFixedT, _generateMetadata } from "app/_utils";
import { getTranslate, _generateMetadata } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
import { NewWebhookView } from "@calcom/features/webhooks/pages/webhook-new-view";
import { APP_NAME } from "@calcom/lib/constants";
Expand All @@ -12,9 +11,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();

const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

return (
<SettingsHeader
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { _generateMetadata } from "app/_utils";
import { getFixedT } from "app/_utils";
import { getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";

import AppearancePage from "~/settings/my-account/appearance-view";
Expand All @@ -13,8 +12,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

return (
<SettingsHeader title={t("appearance")} description={t("appearance_description")}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { _generateMetadata } from "app/_utils";
import { getFixedT } from "app/_utils";
import { getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
import { Button } from "@calcom/ui";

Expand All @@ -14,8 +13,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

const AddCalendarButton = () => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { _generateMetadata } from "app/_utils";
import { getFixedT } from "app/_utils";
import { getTranslate } from "app/_utils";

import { ConferencingAppsViewWebWrapper } from "@calcom/atoms/monorepo";
import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";

export const generateMetadata = async () =>
await _generateMetadata(
Expand All @@ -11,8 +10,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();
const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

return (
<ConferencingAppsViewWebWrapper
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { _generateMetadata } from "app/_utils";
import { getFixedT } from "app/_utils";
import { getTranslate } from "app/_utils";
import { revalidatePath } from "next/cache";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";

import GeneralQueryView from "~/settings/my-account/general-view";
Expand All @@ -14,9 +13,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();

const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();
const revalidatePage = async () => {
"use server";
revalidatePath("settings/my-account/general");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { _generateMetadata } from "app/_utils";
import { getFixedT } from "app/_utils";
import { getTranslate } from "app/_utils";

import { getServerSessionForAppDir } from "@calcom/feature-auth/lib/get-server-session-for-app-dir";
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
import CreateNewOutOfOfficeEntryButton from "@calcom/features/settings/outOfOffice/CreateNewOutOfOfficeEntryButton";
import { OutOfOfficeEntriesList } from "@calcom/features/settings/outOfOffice/OutOfOfficeEntriesList";
Expand All @@ -13,9 +12,7 @@ export const generateMetadata = async () =>
);

const Page = async () => {
const session = await getServerSessionForAppDir();

const t = await getFixedT(session?.user.locale || "en");
const t = await getTranslate();

return (
<SettingsHeader
Expand Down
Loading

0 comments on commit a66eb9d

Please sign in to comment.