Skip to content

Commit 3e7a848

Browse files
authored
refactor: extract back button logic into dedicated wrapper component (calcom#25093)
* refactor: extract back button logic into dedicated wrapper component * Added changeset
1 parent e6e6784 commit 3e7a848

6 files changed

Lines changed: 59 additions & 23 deletions

File tree

.changeset/happy-buckets-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@calcom/atoms": minor
3+
---
4+
5+
Refactor SettingsHeader to accept onBackButtonClick callback instead of using useRouter directly.

apps/web/app/(use-page-wrapper)/settings/(settings-layout)/developer/webhooks/[id]/page.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { PageProps } from "app/_types";
2-
import { getTranslate, _generateMetadata } from "app/_utils";
2+
import { _generateMetadata, getTranslate } from "app/_utils";
33

4-
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
4+
import SettingsHeaderWithBackButton from "@calcom/features/settings/appDir/SettingsHeaderWithBackButton";
55
import { WebhookRepository } from "@calcom/features/webhooks/lib/repository/WebhookRepository";
66
import { EditWebhookView } from "@calcom/features/webhooks/pages/webhook-edit-view";
77
import { APP_NAME } from "@calcom/lib/constants";
@@ -24,13 +24,12 @@ const Page = async ({ params: _params }: PageProps) => {
2424
const webhook = await webhookRepository.findByWebhookId(id);
2525

2626
return (
27-
<SettingsHeader
27+
<SettingsHeaderWithBackButton
2828
title={t("edit_webhook")}
2929
description={t("add_webhook_description", { appName: APP_NAME })}
30-
borderInShellHeader={true}
31-
backButton>
30+
borderInShellHeader={true}>
3231
<EditWebhookView webhook={webhook} />
33-
</SettingsHeader>
32+
</SettingsHeaderWithBackButton>
3433
);
3534
};
3635

packages/features/settings/appDir/SettingsHeader.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@ import classNames from "@calcom/ui/classNames";
66
import { Icon } from "@calcom/ui/components/icon";
77
import { Button } from "@calcom/ui/components/button";
88
import { useLocale } from "@calcom/lib/hooks/useLocale";
9-
import { useRouter } from "next/navigation";
109

11-
interface HeaderProps {
10+
type HeaderPropsBase = {
1211
children: React.ReactNode;
1312
title?: string;
1413
description?: string;
1514
CTA?: React.ReactNode;
1615
ctaClassName?: string;
1716
borderInShellHeader?: boolean;
18-
backButton?: boolean;
19-
}
17+
};
18+
19+
type HeaderPropsWithBackButton = HeaderPropsBase & {
20+
backButton: true;
21+
onBackButtonClick: () => void;
22+
};
23+
24+
type HeaderPropsWithoutBackButton = HeaderPropsBase & {
25+
backButton?: false;
26+
onBackButtonClick?: never;
27+
};
28+
29+
type HeaderProps = HeaderPropsWithBackButton | HeaderPropsWithoutBackButton;
2030

2131
export default function Header({
2232
children,
@@ -26,8 +36,8 @@ export default function Header({
2636
ctaClassName,
2737
borderInShellHeader,
2838
backButton,
39+
onBackButtonClick,
2940
}: HeaderProps) {
30-
const router = useRouter();
3141
const { t } = useLocale();
3242

3343
return (
@@ -40,12 +50,12 @@ export default function Header({
4050
)}>
4151
<div className="flex w-full items-center justify-between gap-2">
4252
<div className="flex items-center">
43-
{backButton && (
53+
{backButton && onBackButtonClick && (
4454
<Button
4555
variant="icon"
4656
size="sm"
4757
color="minimal"
48-
onClick={() => router.back()}
58+
onClick={onBackButtonClick}
4959
className="rounded-md ltr:mr-2 rtl:ml-2"
5060
StartIcon="arrow-left"
5161
aria-label={t("go_back")}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use client";
2+
3+
import { useRouter } from "next/navigation";
4+
import type { ReactNode } from "react";
5+
6+
import SettingsHeader from "./SettingsHeader";
7+
8+
type SettingsHeaderWithBackButtonProps = {
9+
children: ReactNode;
10+
title?: string;
11+
description?: string;
12+
CTA?: ReactNode;
13+
ctaClassName?: string;
14+
borderInShellHeader?: boolean;
15+
};
16+
17+
export default function SettingsHeaderWithBackButton(props: SettingsHeaderWithBackButtonProps) {
18+
const router = useRouter();
19+
20+
return (
21+
<SettingsHeader {...props} backButton={true} onBackButtonClick={() => router.back()} />
22+
);
23+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
"use client";
22

3-
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
3+
import SettingsHeaderWithBackButton from "@calcom/features/settings/appDir/SettingsHeaderWithBackButton";
44
import { APP_NAME } from "@calcom/lib/constants";
55
import { useLocale } from "@calcom/lib/hooks/useLocale";
66
import { SkeletonText, SkeletonContainer } from "@calcom/ui/components/skeleton";
77

88
export const SkeletonLoader = () => {
99
const { t } = useLocale();
10+
1011
return (
11-
<SettingsHeader
12+
<SettingsHeaderWithBackButton
1213
title={t("add_webhook")}
1314
description={t("add_webhook_description", { appName: APP_NAME })}
14-
borderInShellHeader={true}
15-
backButton={true}>
15+
borderInShellHeader={true}>
1616
<SkeletonContainer>
1717
<div className="divide-subtle border-subtle space-y-6 rounded-b-lg border border-t-0 px-6 py-4">
1818
<SkeletonText className="h-8 w-full" />
1919
<SkeletonText className="h-8 w-full" />
2020
</div>
2121
</SkeletonContainer>
22-
</SettingsHeader>
22+
</SettingsHeaderWithBackButton>
2323
);
2424
};

packages/features/webhooks/pages/webhook-new-view.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useSession } from "next-auth/react";
44
import { useRouter } from "next/navigation";
55

6-
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
6+
import SettingsHeaderWithBackButton from "@calcom/features/settings/appDir/SettingsHeaderWithBackButton";
77
import { APP_NAME } from "@calcom/lib/constants";
88
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
99
import { useLocale } from "@calcom/lib/hooks/useLocale";
@@ -76,17 +76,16 @@ export const NewWebhookView = ({ webhooks, installedApps }: Props) => {
7676
};
7777

7878
return (
79-
<SettingsHeader
79+
<SettingsHeaderWithBackButton
8080
title={t("add_webhook")}
8181
description={t("add_webhook_description", { appName: APP_NAME })}
82-
borderInShellHeader={true}
83-
backButton={true}>
82+
borderInShellHeader={true}>
8483
<WebhookForm
8584
noRoutingFormTriggers={false}
8685
onSubmit={onCreateWebhook}
8786
apps={installedApps?.items.map((app) => app.slug)}
8887
/>
89-
</SettingsHeader>
88+
</SettingsHeaderWithBackButton>
9089
);
9190
};
9291

0 commit comments

Comments
 (0)