chore: decouple ui dialog from AppRouter and Atoms (#20088)
* chore: decouple ui dialog from AppRouter and Atoms * fix type e2e test * fix: import dialog from @calcom/ui/components/dialog * fix: remove log * fix unit test * fix * fix and move data-override-list test to features package --------- Co-authored-by: hbjORbj <[email protected]>
This commit is contained in:
1 parent
b0ed1fd5e6
commit
cd952dc93c
112 files changed
+392
-269
No files matched your search
@@ -2,16 +2,12 @@
|
||||
|
||||
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import type { ForwardRefExoticComponent, ReactElement, ReactNode } from "react";
|
||||
import React, { useMemo, useState } from "react";
|
||||
import type { ForwardRefExoticComponent, ReactNode } from "react";
|
||||
import React from "react";
|
||||
|
||||
import { useIsPlatform } from "@calcom/atoms/hooks/useIsPlatform";
|
||||
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
|
||||
import * as PlatformDialogPrimitives from "../../../platform/atoms/src/components/ui/dialog";
|
||||
import type { ButtonProps } from "../button";
|
||||
import { Button } from "../button";
|
||||
import type { IconName } from "../icon";
|
||||
@@ -34,68 +30,10 @@ const dialogClasses = cva(
|
||||
}
|
||||
);
|
||||
|
||||
export type DialogProps = React.ComponentProps<(typeof DialogPrimitive)["Root"]> & {
|
||||
name?: string;
|
||||
clearQueryParamsOnClose?: string[];
|
||||
};
|
||||
|
||||
const enum DIALOG_STATE {
|
||||
// Dialog is there in the DOM but not visible.
|
||||
CLOSED = "CLOSED",
|
||||
// State from the time b/w the Dialog is dismissed and the time the "dialog" query param is removed from the URL.
|
||||
CLOSING = "CLOSING",
|
||||
// Dialog is visible.
|
||||
OPEN = "OPEN",
|
||||
}
|
||||
export type DialogProps = React.ComponentProps<(typeof DialogPrimitive)["Root"]>;
|
||||
|
||||
export function Dialog(props: DialogProps) {
|
||||
const isPlatform = useIsPlatform();
|
||||
return !isPlatform ? <WebDialog {...props} /> : <PlatformDialogPrimitives.Dialog {...props} />;
|
||||
}
|
||||
|
||||
function WebDialog(props: DialogProps) {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const searchParams = useCompatSearchParams();
|
||||
const newSearchParams = new URLSearchParams(searchParams.toString());
|
||||
const { children, name, ...dialogProps } = props;
|
||||
|
||||
// only used if name is set
|
||||
const [dialogState, setDialogState] = useState(dialogProps.open ? DIALOG_STATE.OPEN : DIALOG_STATE.CLOSED);
|
||||
const shouldOpenDialog = newSearchParams.get("dialog") === name;
|
||||
if (name) {
|
||||
const clearQueryParamsOnClose = ["dialog", ...(props.clearQueryParamsOnClose || [])];
|
||||
dialogProps.onOpenChange = (open) => {
|
||||
if (props.onOpenChange) {
|
||||
props.onOpenChange(open);
|
||||
}
|
||||
|
||||
// toggles "dialog" query param
|
||||
if (open) {
|
||||
newSearchParams.set("dialog", name);
|
||||
} else {
|
||||
clearQueryParamsOnClose.forEach((queryParam) => {
|
||||
newSearchParams.delete(queryParam);
|
||||
});
|
||||
router.push(`${pathname}?${newSearchParams.toString()}`);
|
||||
}
|
||||
setDialogState(open ? DIALOG_STATE.OPEN : DIALOG_STATE.CLOSING);
|
||||
};
|
||||
|
||||
if (dialogState === DIALOG_STATE.CLOSED && shouldOpenDialog) {
|
||||
setDialogState(DIALOG_STATE.OPEN);
|
||||
}
|
||||
|
||||
if (dialogState === DIALOG_STATE.CLOSING && !shouldOpenDialog) {
|
||||
setDialogState(DIALOG_STATE.CLOSED);
|
||||
}
|
||||
|
||||
// allow overriding
|
||||
if (!("open" in dialogProps)) {
|
||||
dialogProps.open = dialogState === DIALOG_STATE.OPEN ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
const { children, ...dialogProps } = props;
|
||||
return <DialogPrimitive.Root {...dialogProps}>{children}</DialogPrimitive.Root>;
|
||||
}
|
||||
|
||||
@@ -129,26 +67,14 @@ export const DialogContent = React.forwardRef<HTMLDivElement, DialogContentProps
|
||||
},
|
||||
forwardedRef
|
||||
) => {
|
||||
const isPlatform = useIsPlatform();
|
||||
const [Portal, Overlay, Content] = useMemo(
|
||||
() =>
|
||||
isPlatform
|
||||
? [
|
||||
({ children }: { children: ReactElement | ReactElement[] }) => <>{children}</>,
|
||||
PlatformDialogPrimitives.DialogOverlay,
|
||||
PlatformDialogPrimitives.DialogContent,
|
||||
]
|
||||
: [DialogPrimitive.Portal, DialogPrimitive.Overlay, DialogPrimitive.Content],
|
||||
[isPlatform]
|
||||
);
|
||||
return (
|
||||
<Portal>
|
||||
<DialogPrimitive.Portal>
|
||||
{forceOverlayWhenNoModal ? (
|
||||
<div className="fadeIn fixed inset-0 z-50 bg-neutral-800 bg-opacity-70 transition-opacity" />
|
||||
) : (
|
||||
<Overlay className="fadeIn fixed inset-0 z-50 bg-neutral-800 bg-opacity-70 transition-opacity dark:bg-opacity-80" />
|
||||
<DialogPrimitive.Overlay className="fadeIn fixed inset-0 z-50 bg-neutral-800 bg-opacity-70 transition-opacity dark:bg-opacity-80" />
|
||||
)}
|
||||
<Content
|
||||
<DialogPrimitive.Content
|
||||
{...props}
|
||||
onPointerDownOutside={(e) => {
|
||||
if (preventCloseOnOutsideClick) {
|
||||
@@ -184,8 +110,8 @@ export const DialogContent = React.forwardRef<HTMLDivElement, DialogContentProps
|
||||
</div>
|
||||
)}
|
||||
{!type && children}
|
||||
</Content>
|
||||
</Portal>
|
||||
</DialogPrimitive.Content>
|
||||
</DialogPrimitive.Portal>
|
||||
);
|
||||
}
|
||||
);
|
||||
@@ -242,25 +168,11 @@ DialogContent.displayName = "DialogContent";
|
||||
export const DialogTrigger: ForwardRefExoticComponent<
|
||||
DialogPrimitive.DialogTriggerProps & React.RefAttributes<HTMLButtonElement>
|
||||
> = React.forwardRef((props, ref) => {
|
||||
const isPlatform = useIsPlatform();
|
||||
return !isPlatform ? (
|
||||
<DialogPrimitive.Trigger {...props} ref={ref} />
|
||||
) : (
|
||||
<PlatformDialogPrimitives.DialogTrigger {...props} ref={ref} />
|
||||
);
|
||||
return <DialogPrimitive.Trigger {...props} ref={ref} />;
|
||||
});
|
||||
|
||||
DialogTrigger.displayName = "DialogTrigger";
|
||||
|
||||
type DialogCloseProps = {
|
||||
"data-testid"?: string;
|
||||
dialogCloseProps?: React.ComponentProps<(typeof DialogPrimitive)["Close"]>;
|
||||
children?: ReactNode;
|
||||
onClick?: (e: React.MouseEvent<HTMLElement, MouseEvent>) => void;
|
||||
disabled?: boolean;
|
||||
color?: ButtonProps["color"];
|
||||
} & React.ComponentProps<typeof Button>;
|
||||
|
||||
export function DialogClose(
|
||||
props: {
|
||||
"data-testid"?: string;
|
||||
@@ -272,16 +184,10 @@ export function DialogClose(
|
||||
} & React.ComponentProps<typeof Button>
|
||||
) {
|
||||
const { t } = useLocale();
|
||||
const isPlatform = useIsPlatform();
|
||||
const Close = useMemo(
|
||||
() => (isPlatform ? PlatformDialogPrimitives.DialogClose : DialogPrimitive.Close),
|
||||
[isPlatform]
|
||||
);
|
||||
|
||||
const { className, ...buttonProps } = props;
|
||||
|
||||
return (
|
||||
<Close asChild {...props.dialogCloseProps}>
|
||||
<DialogPrimitive.Close asChild {...props.dialogCloseProps}>
|
||||
<Button
|
||||
data-testid={props["data-testid"] || "dialog-rejection"}
|
||||
color={props.color || "minimal"}
|
||||
@@ -289,8 +195,6 @@ export function DialogClose(
|
||||
{...buttonProps}>
|
||||
{props.children ? props.children : t("close")}
|
||||
</Button>
|
||||
</Close>
|
||||
</DialogPrimitive.Close>
|
||||
);
|
||||
}
|
||||
|
||||
DialogClose.displayName = "WebDialogClose";
|
||||
@@ -41,7 +41,7 @@ describe("Dialog", () => {
|
||||
preventCloseOnOutsideClick?: boolean;
|
||||
enableOverflow?: boolean;
|
||||
}) => (
|
||||
<Dialog open={props.open} name="test-dialog">
|
||||
<Dialog open={props.open}>
|
||||
<DialogContent
|
||||
type={props.type}
|
||||
title={props.title}
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
import { TooltipProvider } from "@radix-ui/react-tooltip";
|
||||
import { render, renderHook, screen } from "@testing-library/react";
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { noop } from "lodash";
|
||||
import { useFieldArray, useForm, FormProvider } from "react-hook-form";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import DateOverrideList from "@calcom/features/schedules/components/DateOverrideList";
|
||||
import type { TimeRange } from "@calcom/types/schedule";
|
||||
|
||||
type FormValues = {
|
||||
dateOverrides: { ranges: TimeRange[] }[];
|
||||
};
|
||||
|
||||
describe("DateOverrideList", () => {
|
||||
it("renders DateOverrideList component in military time", () => {
|
||||
const { result } = renderHook(() =>
|
||||
useForm<FormValues>({
|
||||
values: {
|
||||
dateOverrides: [
|
||||
{
|
||||
ranges: [
|
||||
{
|
||||
start: dayjs.utc().startOf("day").add(540, "minute").toDate(),
|
||||
end: dayjs.utc().startOf("day").add(1020, "minute").toDate(),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
);
|
||||
const methods = result.current;
|
||||
const { control } = methods;
|
||||
|
||||
const { result: fieldArrayResult } = renderHook(() => useFieldArray({ control, name: "dateOverrides" }));
|
||||
render(
|
||||
<TooltipProvider>
|
||||
<FormProvider {...methods}>
|
||||
<DateOverrideList
|
||||
hour12={false}
|
||||
userTimeFormat={24}
|
||||
fields={fieldArrayResult.current.fields}
|
||||
replace={fieldArrayResult.current.replace}
|
||||
workingHours={[]}
|
||||
handleAvailabilityUpdate={noop}
|
||||
/>
|
||||
</FormProvider>
|
||||
</TooltipProvider>
|
||||
);
|
||||
|
||||
const dateString = new Intl.DateTimeFormat("en", {
|
||||
weekday: "long",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
timeZone: "UTC",
|
||||
}).format(new Date());
|
||||
|
||||
expect(screen.getByText(dateString)).toBeInTheDocument();
|
||||
expect(screen.getByText(`09:00 - 17:00`)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders DateOverrideList component in AM/PM", () => {
|
||||
const { result } = renderHook(() =>
|
||||
useForm<FormValues>({
|
||||
values: {
|
||||
dateOverrides: [
|
||||
{
|
||||
ranges: [
|
||||
{
|
||||
start: dayjs.utc().startOf("day").add(540, "minute").toDate(),
|
||||
end: dayjs.utc().startOf("day").add(1020, "minute").toDate(),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
);
|
||||
const methods = result.current;
|
||||
const { control } = methods;
|
||||
|
||||
const { result: fieldArrayResult } = renderHook(() => useFieldArray({ control, name: "dateOverrides" }));
|
||||
render(
|
||||
<TooltipProvider>
|
||||
<FormProvider {...methods}>
|
||||
<DateOverrideList
|
||||
hour12={true}
|
||||
userTimeFormat={12}
|
||||
fields={fieldArrayResult.current.fields}
|
||||
replace={fieldArrayResult.current.replace}
|
||||
workingHours={[]}
|
||||
handleAvailabilityUpdate={noop}
|
||||
/>
|
||||
</FormProvider>
|
||||
</TooltipProvider>
|
||||
);
|
||||
|
||||
const dateString = new Intl.DateTimeFormat("en", {
|
||||
weekday: "long",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
timeZone: "UTC",
|
||||
}).format(new Date());
|
||||
|
||||
expect(screen.getByText(dateString)).toBeInTheDocument();
|
||||
expect(screen.getByText(`9:00 AM - 5:00 PM`)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user