chore: refactor shell to individual files (#16893)

* banners

* useAuthHooks

* fixes to redirect and banner

* extract useIntercom to custom hook

* use app theme

* extract user-dropdown to new component

* Navigation Item

* navigation and profile dropdown

* Fix import

* navigation and sidebar

* fix type errors

* fix banners being an async call

* fix types

* fix banner prop type

* fix mobile nav item import

* fix banner types

* (revert) layout banner render method to fix type error

---------

Co-authored-by: Alex van Andel <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
Co-authored-by: Joe Au-Yeung <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-10-02 15:35:56 +01:00
1 parent b4f1b5aebe
commit 4553455098
16 files changed
+1353 -1152

No files matched your search

@@ -0,0 +1,205 @@
import { useSession } from "next-auth/react";
import { useIsEmbed } from "@calcom/embed-core/embed-iframe";
import UnconfirmedBookingBadge from "@calcom/features/bookings/UnconfirmedBookingBadge";
import { KBarTrigger } from "@calcom/features/kbar/Kbar";
import { classNames } from "@calcom/lib";
import { TeamInviteBadge } from "../TeamInviteBadge";
import type { NavigationItemType } from "./NavigationItem";
import { NavigationItem, MobileNavigationItem, MobileNavigationMoreItem } from "./NavigationItem";
export const MORE_SEPARATOR_NAME = "more";
const navigation: NavigationItemType[] = [
{
name: "event_types_page_title",
href: "/event-types",
icon: "link",
},
{
name: "bookings",
href: "/bookings/upcoming",
icon: "calendar",
badge: <UnconfirmedBookingBadge />,
isCurrent: ({ pathname }) => pathname?.startsWith("/bookings") ?? false,
},
{
name: "availability",
href: "/availability",
icon: "clock",
},
{
name: "teams",
href: "/teams",
icon: "users",
onlyDesktop: true,
badge: <TeamInviteBadge />,
},
{
name: "apps",
href: "/apps",
icon: "grid-3x3",
isCurrent: ({ pathname: path, item }) => {
// During Server rendering path is /v2/apps but on client it becomes /apps(weird..)
return (path?.startsWith(item.href) ?? false) && !(path?.includes("routing-forms/") ?? false);
},
child: [
{
name: "app_store",
href: "/apps",
isCurrent: ({ pathname: path, item }) => {
// During Server rendering path is /v2/apps but on client it becomes /apps(weird..)
return (
(path?.startsWith(item.href) ?? false) &&
!(path?.includes("routing-forms/") ?? false) &&
!(path?.includes("/installed") ?? false)
);
},
},
{
name: "installed_apps",
href: "/apps/installed/calendar",
isCurrent: ({ pathname: path }) =>
(path?.startsWith("/apps/installed/") ?? false) ||
(path?.startsWith("/v2/apps/installed/") ?? false),
},
],
},
{
name: MORE_SEPARATOR_NAME,
href: "/more",
icon: "ellipsis",
},
{
name: "Routing Forms",
href: "/apps/routing-forms/forms",
icon: "file-text",
isCurrent: ({ pathname }) => pathname?.startsWith("/apps/routing-forms/") ?? false,
},
{
name: "workflows",
href: "/workflows",
icon: "zap",
},
{
name: "insights",
href: "/insights",
icon: "chart-bar",
},
];
const platformNavigation: NavigationItemType[] = [
{
name: "Dashboard",
href: "/settings/platform/",
icon: "layout-dashboard",
},
{
name: "Documentation",
href: "https://docs.cal.com/docs/platform",
icon: "chart-bar",
target: "_blank",
},
{
name: "API reference",
href: "https://api.cal.com/v2/docs#/",
icon: "terminal",
target: "_blank",
},
{
name: "Atoms",
href: "https://docs.cal.com/docs/platform#atoms",
icon: "atom",
target: "_blank",
},
{
name: MORE_SEPARATOR_NAME,
href: "https://docs.cal.com/docs/platform/faq",
icon: "ellipsis",
target: "_blank",
},
];
export const getDesktopNavigationItems = (isPlatformNavigation = false) => {
const navigationType = !isPlatformNavigation ? navigation : platformNavigation;
const moreSeparatorIndex = navigationType.findIndex((item) => item.name === MORE_SEPARATOR_NAME);
const { desktopNavigationItems, mobileNavigationBottomItems, mobileNavigationMoreItems } = (
!isPlatformNavigation ? navigation : platformNavigation
).reduce<Record<string, NavigationItemType[]>>(
(items, item, index) => {
// We filter out the "more" separator in` desktop navigation
if (item.name !== MORE_SEPARATOR_NAME) items.desktopNavigationItems.push(item);
// Items for mobile bottom navigation
if (index < moreSeparatorIndex + 1 && !item.onlyDesktop) {
items.mobileNavigationBottomItems.push(item);
} // Items for the "more" menu in mobile navigation
else {
items.mobileNavigationMoreItems.push(item);
}
return items;
},
{ desktopNavigationItems: [], mobileNavigationBottomItems: [], mobileNavigationMoreItems: [] }
);
return { desktopNavigationItems, mobileNavigationBottomItems, mobileNavigationMoreItems };
};
export const Navigation = ({ isPlatformNavigation = false }: { isPlatformNavigation?: boolean }) => {
const { desktopNavigationItems } = getDesktopNavigationItems(isPlatformNavigation);
return (
<nav className="mt-2 flex-1 md:px-2 lg:mt-4 lg:px-0">
{desktopNavigationItems.map((item) => (
<NavigationItem key={item.name} item={item} />
))}
<div className="text-subtle mt-0.5 lg:hidden">
<KBarTrigger />
</div>
</nav>
);
};
export function MobileNavigationContainer({
isPlatformNavigation = false,
}: {
isPlatformNavigation?: boolean;
}) {
const { status } = useSession();
if (status !== "authenticated") return null;
return <MobileNavigation isPlatformNavigation={isPlatformNavigation} />;
}
const MobileNavigation = ({ isPlatformNavigation = false }: { isPlatformNavigation?: boolean }) => {
const isEmbed = useIsEmbed();
const { mobileNavigationBottomItems } = getDesktopNavigationItems(isPlatformNavigation);
return (
<>
<nav
className={classNames(
"pwa:pb-[max(0.625rem,env(safe-area-inset-bottom))] pwa:-mx-2 bg-muted border-subtle fixed bottom-0 left-0 z-30 flex w-full border-t bg-opacity-40 px-1 shadow backdrop-blur-md md:hidden",
isEmbed && "hidden"
)}>
{mobileNavigationBottomItems.map((item) => (
<MobileNavigationItem key={item.name} item={item} />
))}
</nav>
{/* add padding to content for mobile navigation*/}
<div className="block pt-12 md:hidden" />
</>
);
};
export const MobileNavigationMoreItems = () => {
const { mobileNavigationMoreItems } = getDesktopNavigationItems();
return (
<ul className="border-subtle mt-2 rounded-md border">
{mobileNavigationMoreItems.map((item) => (
<MobileNavigationMoreItem key={item.name} item={item} />
))}
</ul>
);
};
@@ -0,0 +1,160 @@
import Link from "next/link";
import { usePathname } from "next/navigation";
import React, { Fragment } from "react";
import { classNames } from "@calcom/lib";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { Tooltip, Icon, SkeletonText } from "@calcom/ui";
import type { IconName } from "@calcom/ui";
import { useShouldDisplayNavigationItem } from "./useShouldDisplayNavigationItem";
export type NavigationItemType = {
name: string;
href: string;
isLoading?: boolean;
onClick?: React.MouseEventHandler<HTMLAnchorElement | HTMLButtonElement>;
target?: HTMLAnchorElement["target"];
badge?: React.ReactNode;
icon?: IconName;
child?: NavigationItemType[];
pro?: true;
onlyMobile?: boolean;
onlyDesktop?: boolean;
isCurrent?: ({
item,
isChild,
pathname,
}: {
item: Pick<NavigationItemType, "href">;
isChild?: boolean;
pathname: string | null;
}) => boolean;
};
const defaultIsCurrent: NavigationItemType["isCurrent"] = ({ isChild, item, pathname }) => {
return isChild ? item.href === pathname : item.href ? pathname?.startsWith(item.href) ?? false : false;
};
export const NavigationItem: React.FC<{
index?: number;
item: NavigationItemType;
isChild?: boolean;
}> = (props) => {
const { item, isChild } = props;
const { t, isLocaleReady } = useLocale();
const pathname = usePathname();
const isCurrent: NavigationItemType["isCurrent"] = item.isCurrent || defaultIsCurrent;
const current = isCurrent({ isChild: !!isChild, item, pathname });
const shouldDisplayNavigationItem = useShouldDisplayNavigationItem(props.item);
if (!shouldDisplayNavigationItem) return null;
return (
<Fragment>
<Tooltip side="right" content={t(item.name)} className="lg:hidden">
<Link
data-test-id={item.name}
href={item.href}
aria-label={t(item.name)}
target={item.target}
className={classNames(
"todesktop:py-[7px] text-default group flex items-center rounded-md px-2 py-1.5 text-sm font-medium transition",
item.child ? `[&[aria-current='page']]:!bg-transparent` : `[&[aria-current='page']]:bg-emphasis`,
isChild
? `[&[aria-current='page']]:text-emphasis [&[aria-current='page']]:bg-emphasis hidden h-8 pl-16 lg:flex lg:pl-11 ${
props.index === 0 ? "mt-0" : "mt-px"
}`
: "[&[aria-current='page']]:text-emphasis mt-0.5 text-sm",
isLocaleReady
? "hover:bg-subtle todesktop:[&[aria-current='page']]:bg-emphasis todesktop:hover:bg-transparent hover:text-emphasis"
: ""
)}
aria-current={current ? "page" : undefined}>
{item.icon && (
<Icon
name={item.isLoading ? "rotate-cw" : item.icon}
className={classNames(
"todesktop:!text-blue-500 mr-2 h-4 w-4 flex-shrink-0 rtl:ml-2 md:ltr:mx-auto lg:ltr:mr-2 [&[aria-current='page']]:text-inherit",
item.isLoading && "animate-spin"
)}
aria-hidden="true"
aria-current={current ? "page" : undefined}
/>
)}
{isLocaleReady ? (
<span
className="hidden w-full justify-between truncate text-ellipsis lg:flex"
data-testid={`${item.name}-test`}>
{t(item.name)}
{item.badge && item.badge}
</span>
) : (
<SkeletonText className="h-[20px] w-full" />
)}
</Link>
</Tooltip>
{item.child &&
isCurrent({ pathname, isChild, item }) &&
item.child.map((item, index) => <NavigationItem index={index} key={item.name} item={item} isChild />)}
</Fragment>
);
};
export const MobileNavigationItem: React.FC<{
item: NavigationItemType;
isChild?: boolean;
}> = (props) => {
const { item, isChild } = props;
const pathname = usePathname();
const { t, isLocaleReady } = useLocale();
const isCurrent: NavigationItemType["isCurrent"] = item.isCurrent || defaultIsCurrent;
const current = isCurrent({ isChild: !!isChild, item, pathname });
const shouldDisplayNavigationItem = useShouldDisplayNavigationItem(props.item);
if (!shouldDisplayNavigationItem) return null;
return (
<Link
key={item.name}
href={item.href}
target={item.target}
className="[&[aria-current='page']]:text-emphasis hover:text-default text-muted relative my-2 min-w-0 flex-1 overflow-hidden rounded-md !bg-transparent p-1 text-center text-xs font-medium focus:z-10 sm:text-sm"
aria-current={current ? "page" : undefined}>
{item.badge && <div className="absolute right-1 top-1">{item.badge}</div>}
{item.icon && (
<Icon
name={item.icon}
className="[&[aria-current='page']]:text-emphasis mx-auto mb-1 block h-5 w-5 flex-shrink-0 text-center text-inherit"
aria-hidden="true"
aria-current={current ? "page" : undefined}
/>
)}
{isLocaleReady ? <span className="block truncate">{t(item.name)}</span> : <SkeletonText />}
</Link>
);
};
export const MobileNavigationMoreItem: React.FC<{
item: NavigationItemType;
isChild?: boolean;
}> = (props) => {
const { item } = props;
const { t, isLocaleReady } = useLocale();
const shouldDisplayNavigationItem = useShouldDisplayNavigationItem(props.item);
if (!shouldDisplayNavigationItem) return null;
return (
<li className="border-subtle border-b last:border-b-0" key={item.name}>
<Link href={item.href} className="hover:bg-subtle flex items-center justify-between p-5 transition">
<span className="text-default flex items-center font-semibold ">
{item.icon && (
<Icon name={item.icon} className="h-5 w-5 flex-shrink-0 ltr:mr-3 rtl:ml-3" aria-hidden="true" />
)}
{isLocaleReady ? t(item.name) : <SkeletonText />}
</span>
<Icon name="arrow-right" className="text-subtle h-5 w-5" />
</Link>
</li>
);
};
@@ -0,0 +1,10 @@
import { useFlagMap } from "@calcom/features/flags/context/provider";
import { isKeyInObject } from "@calcom/lib/isKeyInObject";
import type { NavigationItemType } from "./NavigationItem";
export function useShouldDisplayNavigationItem(item: NavigationItemType) {
const flags = useFlagMap();
if (isKeyInObject(item.name, flags)) return flags[item.name];
return true;
}