feat: make calcom UI dumb again (#19658)
* remove create button for teams from calcom ui * migrate timezone select to features * remove trpc from phone and storybook provider * new-yarn.lock * fix imports + test file for select * fix platform timezone select to use Raw timezone select * fix timezone type import * use correct select in timezone-select.tsx - needs trpc version * fix lock and remove test-utils * fix log file * Updated yarn.lock * Fixed types * Fixed tests --------- Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
1 parent
1b111c8add
commit
4cdb8f3bae
30 files changed
+97
-89
No files matched your search
@@ -23,7 +23,7 @@ import { FromToTime } from "../utils/dates";
|
||||
import { useBookerTime } from "./hooks/useBookerTime";
|
||||
|
||||
const WebTimezoneSelect = dynamic(
|
||||
() => import("@calcom/ui/components/form/timezone-select/TimezoneSelect").then((mod) => mod.TimezoneSelect),
|
||||
() => import("@calcom/features/components/timezone-select").then((mod) => mod.TimezoneSelect),
|
||||
{
|
||||
ssr: false,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo, useState } from "react";
|
||||
import type { ITimezoneOption, ITimezone, Props as SelectProps } from "react-timezone-select";
|
||||
import BaseSelect from "react-timezone-select";
|
||||
|
||||
import { CALCOM_VERSION } from "@calcom/lib/constants";
|
||||
import { filterBySearchText, addTimezonesToDropdown, handleOptionLabel } from "@calcom/lib/timezone";
|
||||
import type { Timezones } from "@calcom/lib/timezone";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { getReactSelectProps, inputStyles } from "@calcom/ui";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
|
||||
const SELECT_SEARCH_DATA: Timezones = [
|
||||
{ label: "San Francisco", timezone: "America/Los_Angeles" },
|
||||
{ label: "Sao Francisco do Sul", timezone: "America/Sao_Paulo" },
|
||||
{ label: "San Francisco de Macoris", timezone: "America/Santo_Domingo" },
|
||||
{ label: "San Francisco Gotera", timezone: "America/El_Salvador" },
|
||||
{ label: "Eastern Time - US & Canada", timezone: "America/New_York" },
|
||||
{ label: "Pacific Time - US & Canada", timezone: "America/Los_Angeles" },
|
||||
{ label: "Central Time - US & Canada", timezone: "America/Chicago" },
|
||||
{ label: "Mountain Time - US & Canada", timezone: "America/Denver" },
|
||||
{ label: "Atlantic Time - Canada", timezone: "America/Halifax" },
|
||||
{ label: "Eastern European Time", timezone: "Europe/Bucharest" },
|
||||
{ label: "Central European Time", timezone: "Europe/Berlin" },
|
||||
{ label: "Western European Time", timezone: "Europe/London" },
|
||||
{ label: "Australian Eastern Time", timezone: "Australia/Sydney" },
|
||||
{ label: "Japan Standard Time", timezone: "Asia/Tokyo" },
|
||||
{ label: "India Standard Time", timezone: "Asia/Kolkata" },
|
||||
{ label: "Gulf Standard Time", timezone: "Asia/Dubai" },
|
||||
{ label: "South Africa Standard Time", timezone: "Africa/Johannesburg" },
|
||||
{ label: "Brazil Time", timezone: "America/Sao_Paulo" },
|
||||
{ label: "Hawaii-Aleutian Standard Time", timezone: "Pacific/Honolulu" },
|
||||
];
|
||||
|
||||
export type TimezoneSelectProps = SelectProps & {
|
||||
variant?: "default" | "minimal";
|
||||
timezoneSelectCustomClassname?: string;
|
||||
size?: "sm" | "md";
|
||||
grow?: boolean;
|
||||
};
|
||||
export function TimezoneSelect(props: TimezoneSelectProps) {
|
||||
const { data = [], isPending } = trpc.viewer.timezones.cityTimezones.useQuery(
|
||||
{
|
||||
CalComVersion: CALCOM_VERSION,
|
||||
},
|
||||
{
|
||||
trpc: { context: { skipBatch: true } },
|
||||
}
|
||||
);
|
||||
return (
|
||||
<TimezoneSelectComponent
|
||||
data={data.map(({ city, timezone }) => ({ label: city, timezone }))}
|
||||
isPending={isPending}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export type TimezoneSelectComponentProps = SelectProps & {
|
||||
variant?: "default" | "minimal";
|
||||
isPending: boolean;
|
||||
data?: Timezones;
|
||||
timezoneSelectCustomClassname?: string;
|
||||
size?: "sm" | "md";
|
||||
grow?: boolean;
|
||||
};
|
||||
|
||||
// TODO: I wonder if we move this to ui package, and keep the TRPC version in features
|
||||
export function TimezoneSelectComponent({
|
||||
className,
|
||||
classNames: timezoneClassNames,
|
||||
timezoneSelectCustomClassname,
|
||||
components,
|
||||
variant = "default",
|
||||
isPending,
|
||||
value,
|
||||
size = "md",
|
||||
grow = false,
|
||||
...props
|
||||
}: TimezoneSelectComponentProps) {
|
||||
const data = [...(props.data || []), ...SELECT_SEARCH_DATA];
|
||||
/*
|
||||
* we support multiple timezones for the different labels
|
||||
* e.g. 'Sao Paulo' and 'Brazil Time' both being 'America/Sao_Paulo'
|
||||
* but react-timezone-select does not.
|
||||
*
|
||||
* We make sure to be able to search through both options, and flip the key/value on final display.
|
||||
*/
|
||||
const [additionalTimezones, setAdditionalTimezones] = useState<Timezones>([]);
|
||||
const handleInputChange = (searchText: string) => {
|
||||
if (data) setAdditionalTimezones(filterBySearchText(searchText, data));
|
||||
};
|
||||
|
||||
const reactSelectProps = useMemo(() => {
|
||||
return getReactSelectProps({
|
||||
components: components || {},
|
||||
});
|
||||
}, [components]);
|
||||
|
||||
return (
|
||||
<BaseSelect
|
||||
value={value}
|
||||
className={`${className} ${timezoneSelectCustomClassname}`}
|
||||
aria-label="Timezone Select"
|
||||
isLoading={isPending}
|
||||
isDisabled={isPending}
|
||||
{...reactSelectProps}
|
||||
timezones={{
|
||||
...(props.data ? addTimezonesToDropdown(data) : {}),
|
||||
...addTimezonesToDropdown(additionalTimezones),
|
||||
}}
|
||||
styles={{
|
||||
control: (base) => ({
|
||||
...base,
|
||||
minHeight: size === "sm" ? "28px" : "36px",
|
||||
height: grow ? "h-auto " : size === "sm" ? "28px" : "36px",
|
||||
}),
|
||||
}}
|
||||
onInputChange={handleInputChange}
|
||||
{...props}
|
||||
formatOptionLabel={(option) => (
|
||||
<p className="truncate">{(option as ITimezoneOption).value.replace(/_/g, " ")}</p>
|
||||
)}
|
||||
getOptionLabel={(option) => handleOptionLabel(option as ITimezoneOption, additionalTimezones)}
|
||||
classNames={{
|
||||
...timezoneClassNames,
|
||||
input: (state) =>
|
||||
classNames(
|
||||
"text-emphasis h-6 md:max-w-[145px] max-w-[250px]",
|
||||
timezoneClassNames?.input && timezoneClassNames.input(state)
|
||||
),
|
||||
option: (state) =>
|
||||
classNames(
|
||||
"bg-default flex cursor-pointer justify-between py-2.5 px-3 rounded-md text-default ",
|
||||
state.isFocused && "bg-subtle",
|
||||
state.isDisabled && "bg-muted",
|
||||
state.isSelected && "bg-emphasis text-default",
|
||||
timezoneClassNames?.option && timezoneClassNames.option(state)
|
||||
),
|
||||
placeholder: (state) => classNames("text-muted", state.isFocused && "hidden"),
|
||||
dropdownIndicator: () => "text-default",
|
||||
control: (state) =>
|
||||
classNames(
|
||||
inputStyles({ size }),
|
||||
state.isMulti
|
||||
? state.hasValue
|
||||
? "p-1 h-fit"
|
||||
: "px-3 h-fit"
|
||||
: size === "sm"
|
||||
? "h-7 px-2"
|
||||
: "h-9 px-3",
|
||||
props.isDisabled && "bg-subtle",
|
||||
"rounded-[10px]",
|
||||
timezoneClassNames?.control && timezoneClassNames.control(state)
|
||||
),
|
||||
singleValue: (state) =>
|
||||
classNames(
|
||||
"text-emphasis placeholder:text-muted",
|
||||
timezoneClassNames?.singleValue && timezoneClassNames.singleValue(state)
|
||||
),
|
||||
valueContainer: (state) =>
|
||||
classNames(
|
||||
"text-emphasis placeholder:text-muted flex gap-1",
|
||||
timezoneClassNames?.valueContainer && timezoneClassNames.valueContainer(state)
|
||||
),
|
||||
multiValue: (state) =>
|
||||
classNames(
|
||||
"bg-subtle text-default rounded-md py-1.5 px-2 flex items-center text-sm leading-none",
|
||||
timezoneClassNames?.multiValue && timezoneClassNames.multiValue(state)
|
||||
),
|
||||
menu: (state) =>
|
||||
classNames(
|
||||
"rounded-md bg-default text-sm leading-4 text-default mt-1 border border-subtle",
|
||||
state.selectProps.menuIsOpen && "shadow-dropdown", // Add box-shadow when menu is open
|
||||
timezoneClassNames?.menu && timezoneClassNames.menu(state)
|
||||
),
|
||||
groupHeading: () => "leading-none text-xs uppercase text-default pl-2.5 pt-4 pb-2",
|
||||
menuList: (state) =>
|
||||
classNames(
|
||||
"scroll-bar scrollbar-track-w-20 rounded-md",
|
||||
timezoneClassNames?.menuList && timezoneClassNames.menuList(state)
|
||||
),
|
||||
indicatorsContainer: (state) =>
|
||||
classNames(
|
||||
state.selectProps.menuIsOpen
|
||||
? state.isMulti
|
||||
? "[&>*:last-child]:rotate-180 [&>*:last-child]:transition-transform"
|
||||
: "rotate-180 transition-transform"
|
||||
: "text-default", // Woo it adds another SVG here on multi for some reason
|
||||
timezoneClassNames?.indicatorsContainer && timezoneClassNames.indicatorsContainer(state)
|
||||
),
|
||||
multiValueRemove: () => "text-default py-auto ml-2",
|
||||
noOptionsMessage: () => "h-12 py-2 flex items-center justify-center",
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export type { ITimezone, ITimezoneOption };
|
||||
@@ -0,0 +1,7 @@
|
||||
export { TimezoneSelect, TimezoneSelectComponent } from "./TimezoneSelect";
|
||||
export type {
|
||||
ITimezone,
|
||||
ITimezoneOption,
|
||||
TimezoneSelectProps,
|
||||
TimezoneSelectComponentProps,
|
||||
} from "./TimezoneSelect";
|
||||
@@ -0,0 +1,194 @@
|
||||
/* eslint-disable playwright/missing-playwright-await */
|
||||
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
|
||||
import type { Props as SelectProps } from "react-timezone-select";
|
||||
import { vi } from "vitest";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
|
||||
import { TimezoneSelect } from "./TimezoneSelect";
|
||||
|
||||
const cityTimezonesMock = [
|
||||
{ city: "Dawson City", timezone: "America/Dawson" },
|
||||
{ city: "Honolulu", timezone: "Pacific/Honolulu" },
|
||||
{ city: "Juneau", timezone: "America/Juneau" },
|
||||
{ city: "Toronto", timezone: "America/Toronto" },
|
||||
];
|
||||
|
||||
const runtimeMock = async (isPending: boolean) => {
|
||||
const updatedTrcp = {
|
||||
viewer: {
|
||||
timezones: {
|
||||
cityTimezones: {
|
||||
useQuery() {
|
||||
return {
|
||||
data: cityTimezonesMock,
|
||||
isPending,
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const mockedLib = (await import("@calcom/trpc/react")) as any;
|
||||
mockedLib.trpc = updatedTrcp;
|
||||
};
|
||||
|
||||
const formatOffset = (offset: string) =>
|
||||
offset.replace(/^([-+])(0)(\d):00$/, (_, sign, _zero, hour) => `${sign}${hour}:00`);
|
||||
const formatTimeZoneWithOffset = (timeZone: string) =>
|
||||
`${timeZone} GMT ${formatOffset(dayjs.tz(undefined, timeZone).format("Z"))}`;
|
||||
|
||||
const timezoneMockValues = ["America/Dawson", "Pacific/Honolulu", "America/Juneau", "America/Toronto"];
|
||||
const optionMockValues = timezoneMockValues.map(formatTimeZoneWithOffset);
|
||||
|
||||
const classNames = {
|
||||
singleValue: () => "test1",
|
||||
valueContainer: () => "test2",
|
||||
control: () => "test3",
|
||||
input: () => "test4",
|
||||
option: () => "test5",
|
||||
menuList: () => "test6",
|
||||
menu: () => "test7",
|
||||
multiValue: () => "test8",
|
||||
};
|
||||
|
||||
const onChangeMock = vi.fn();
|
||||
|
||||
const renderSelect = (newProps: SelectProps & { variant?: "default" | "minimal" }) => {
|
||||
render(
|
||||
<form aria-label="test-form">
|
||||
<label htmlFor="test">Test</label>
|
||||
<TimezoneSelect {...newProps} inputId="test" />
|
||||
</form>
|
||||
);
|
||||
};
|
||||
|
||||
const openMenu = async () => {
|
||||
await waitFor(async () => {
|
||||
const element = screen.getByLabelText("Test");
|
||||
element.focus();
|
||||
fireEvent.keyDown(element, { key: "ArrowDown", code: "ArrowDown" });
|
||||
screen.getByText(optionMockValues[0]);
|
||||
});
|
||||
};
|
||||
|
||||
describe("Test TimezoneSelect", () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
describe("Test TimezoneSelect with isPending = false", () => {
|
||||
beforeAll(async () => {
|
||||
// INFO: This needs to be here before the other calls to runtimeMock. For some reason,
|
||||
// when we moved this file from @calcom/ui, the imports started breaking, resulting in
|
||||
// errors of "Cannot set property 'trpc' of [object Module] which has only a getter"
|
||||
// TODO: Update this pattern to be more consistent. Using this direct mocking in the
|
||||
// functions below breaks some tests.
|
||||
vi.mock("@calcom/trpc/react", () => ({
|
||||
trpc: {
|
||||
viewer: {
|
||||
timezones: {
|
||||
cityTimezones: {
|
||||
useQuery() {
|
||||
return {
|
||||
data: cityTimezonesMock,
|
||||
isPending: false,
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
});
|
||||
test("Should render with the correct CSS when provided with classNames prop", async () => {
|
||||
renderSelect({ value: timezoneMockValues[0], classNames });
|
||||
openMenu();
|
||||
|
||||
const dawsonEl = screen.getByText(timezoneMockValues[0]);
|
||||
|
||||
expect(dawsonEl).toBeInTheDocument();
|
||||
|
||||
const singleValueEl = dawsonEl.parentElement;
|
||||
const valueContainerEl = singleValueEl?.parentElement;
|
||||
const controlEl = valueContainerEl?.parentElement;
|
||||
const inputEl = screen.getByRole("combobox", { hidden: true }).parentElement;
|
||||
const optionEl = screen.getByText(optionMockValues[0]).parentElement?.parentElement;
|
||||
const menuListEl = optionEl?.parentElement;
|
||||
const menuEl = menuListEl?.parentElement;
|
||||
|
||||
expect(singleValueEl).toHaveClass(classNames.singleValue());
|
||||
expect(valueContainerEl).toHaveClass(classNames.valueContainer());
|
||||
expect(controlEl).toHaveClass(classNames.control());
|
||||
expect(inputEl).toHaveClass(classNames.input());
|
||||
expect(optionEl).toHaveClass(classNames.option());
|
||||
expect(menuListEl).toHaveClass(classNames.menuList());
|
||||
expect(menuEl).toHaveClass(classNames.menu());
|
||||
|
||||
for (const mockText of optionMockValues) {
|
||||
expect(screen.getByText(mockText)).toBeInTheDocument();
|
||||
}
|
||||
});
|
||||
|
||||
test("Should render with the correct CSS when provided with className prop", async () => {
|
||||
renderSelect({ value: timezoneMockValues[0], className: "test-css" });
|
||||
openMenu();
|
||||
const labelTest = screen.getByText("Test");
|
||||
const timezoneEl = labelTest.nextSibling;
|
||||
expect(timezoneEl).toHaveClass("test-css");
|
||||
});
|
||||
|
||||
test("Should render with the correct CSS when isMulti is enabled", async () => {
|
||||
renderSelect({ value: timezoneMockValues[0], isMulti: true, classNames });
|
||||
openMenu();
|
||||
|
||||
const dawsonEl = screen.getByText(timezoneMockValues[0]);
|
||||
const multiValueEl = dawsonEl.parentElement?.parentElement;
|
||||
expect(multiValueEl).toHaveClass(classNames.multiValue());
|
||||
|
||||
const inputEl = screen.getByRole("combobox", { hidden: true }).parentElement;
|
||||
const menuIsOpenEl = inputEl?.parentElement?.nextSibling;
|
||||
expect(menuIsOpenEl).toHaveClass("[&>*:last-child]:rotate-180 [&>*:last-child]:transition-transform ");
|
||||
});
|
||||
|
||||
test("Should render with the correct CSS when menu is open and onChange is called", async () => {
|
||||
renderSelect({ value: timezoneMockValues[0], onChange: onChangeMock });
|
||||
await waitFor(async () => {
|
||||
const element = screen.getByLabelText("Test");
|
||||
element.focus();
|
||||
fireEvent.keyDown(element, { key: "ArrowDown", code: "ArrowDown" });
|
||||
screen.getByText(optionMockValues[3]);
|
||||
|
||||
const inputEl = screen.getByRole("combobox", { hidden: true }).parentElement;
|
||||
const menuIsOpenEl = inputEl?.parentElement?.nextSibling;
|
||||
expect(menuIsOpenEl).toHaveClass("rotate-180 transition-transform ");
|
||||
const opt = screen.getByText(optionMockValues[3]);
|
||||
fireEvent.click(opt);
|
||||
fireEvent.keyDown(element, { key: "ArrowDown", code: "ArrowDown" });
|
||||
});
|
||||
|
||||
expect(onChangeMock).toBeCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Test TimezoneSelect with isPending = true", () => {
|
||||
beforeAll(async () => {
|
||||
await runtimeMock(true);
|
||||
});
|
||||
test("Should have no options when isPending is true", async () => {
|
||||
renderSelect({ value: timezoneMockValues[0] });
|
||||
await waitFor(async () => {
|
||||
const element = screen.getByLabelText("Test");
|
||||
element.focus();
|
||||
fireEvent.keyDown(element, { key: "ArrowDown", code: "ArrowDown" });
|
||||
});
|
||||
|
||||
for (const mockText of optionMockValues) {
|
||||
const optionEl = screen.queryByText(mockText);
|
||||
expect(optionEl).toBeNull();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import { useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
|
||||
import { TimezoneSelect } from "@calcom/features/components/timezone-select";
|
||||
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
|
||||
import SectionBottomActions from "@calcom/features/settings/SectionBottomActions";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
@@ -21,7 +22,6 @@ import {
|
||||
SkeletonButton,
|
||||
SkeletonContainer,
|
||||
SkeletonText,
|
||||
TimezoneSelect,
|
||||
} from "@calcom/ui";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { TimezoneSelect } from "@calcom/features/components/timezone-select";
|
||||
import type { ITimezone } from "@calcom/features/components/timezone-select";
|
||||
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
|
||||
import { WEBAPP_URL, CURRENT_TIMEZONE } from "@calcom/lib/constants";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { ITimezone } from "@calcom/ui";
|
||||
import { Avatar, DatePicker, Label, Select, TimezoneSelect } from "@calcom/ui";
|
||||
import { Avatar, DatePicker, Label, Select } from "@calcom/ui";
|
||||
|
||||
import TeamAvailabilityTimes from "./TeamAvailabilityTimes";
|
||||
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
"use client";
|
||||
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
|
||||
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import type { ButtonColor } from "@calcom/ui";
|
||||
import {
|
||||
Avatar,
|
||||
Button,
|
||||
Dropdown,
|
||||
DropdownItem,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuLabel,
|
||||
DropdownMenuTrigger,
|
||||
} from "@calcom/ui";
|
||||
|
||||
export interface Option {
|
||||
platform?: boolean;
|
||||
teamId: number | null | undefined; // if undefined, then it's a profile
|
||||
label: string | null;
|
||||
image: string | null;
|
||||
slug: string | null;
|
||||
}
|
||||
|
||||
export type CreateBtnProps = {
|
||||
options: Option[];
|
||||
createDialog?: () => JSX.Element;
|
||||
createFunction?: (teamId?: number, platform?: boolean) => void;
|
||||
subtitle?: string;
|
||||
buttonText?: string;
|
||||
isPending?: boolean;
|
||||
disableMobileButton?: boolean;
|
||||
"data-testid"?: string;
|
||||
color?: ButtonColor;
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated use CreateButtonWithTeamsList instead
|
||||
*/
|
||||
export function CreateButton(props: CreateBtnProps) {
|
||||
const { t } = useLocale();
|
||||
const router = useRouter();
|
||||
const searchParams = useCompatSearchParams();
|
||||
const pathname = usePathname();
|
||||
|
||||
const {
|
||||
createDialog,
|
||||
options,
|
||||
isPending,
|
||||
createFunction,
|
||||
buttonText,
|
||||
disableMobileButton,
|
||||
subtitle,
|
||||
...restProps
|
||||
} = props;
|
||||
const CreateDialog = createDialog ? createDialog() : null;
|
||||
|
||||
const hasTeams = !!options.find((option) => option.teamId);
|
||||
const platform = !!options.find((option) => option.platform);
|
||||
|
||||
// inject selection data into url for correct router history
|
||||
const openModal = (option: Option) => {
|
||||
const _searchParams = new URLSearchParams(searchParams.toString());
|
||||
function setParamsIfDefined(key: string, value: string | number | boolean | null | undefined) {
|
||||
if (value !== undefined && value !== null) _searchParams.set(key, value.toString());
|
||||
}
|
||||
setParamsIfDefined("dialog", "new");
|
||||
setParamsIfDefined("eventPage", option.slug);
|
||||
setParamsIfDefined("teamId", option.teamId);
|
||||
if (!option.teamId) {
|
||||
_searchParams.delete("teamId");
|
||||
}
|
||||
router.push(`${pathname}?${_searchParams.toString()}`);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{!hasTeams && !platform ? (
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() =>
|
||||
!!CreateDialog
|
||||
? openModal(options[0])
|
||||
: createFunction
|
||||
? createFunction(options[0].teamId || undefined)
|
||||
: null
|
||||
}
|
||||
data-testid="create-button"
|
||||
StartIcon="plus"
|
||||
loading={isPending}
|
||||
variant={disableMobileButton ? "button" : "fab"}
|
||||
{...restProps}>
|
||||
{buttonText ? buttonText : t("new")}
|
||||
</Button>
|
||||
) : (
|
||||
<Dropdown>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
variant={disableMobileButton ? "button" : "fab"}
|
||||
StartIcon="plus"
|
||||
size="sm"
|
||||
data-testid="create-button-dropdown"
|
||||
loading={isPending}
|
||||
{...restProps}>
|
||||
{buttonText ? buttonText : t("new")}
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent sideOffset={14} align="end">
|
||||
<DropdownMenuLabel>
|
||||
<div className="w-48 text-left text-xs">{subtitle}</div>
|
||||
</DropdownMenuLabel>
|
||||
{options.map((option, idx) => (
|
||||
<DropdownMenuItem key={option.label}>
|
||||
<DropdownItem
|
||||
type="button"
|
||||
data-testid={`option${option.teamId ? "-team" : ""}-${idx}`}
|
||||
CustomStartIcon={<Avatar alt={option.label || ""} imageSrc={option.image} size="sm" />}
|
||||
onClick={() =>
|
||||
!!CreateDialog
|
||||
? openModal(option)
|
||||
: createFunction
|
||||
? createFunction(option.teamId || undefined, option.platform)
|
||||
: null
|
||||
}>
|
||||
{" "}
|
||||
{/*improve this code */}
|
||||
<span>{option.label}</span>
|
||||
</DropdownItem>
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</Dropdown>
|
||||
)}
|
||||
{searchParams?.get("dialog") === "new" && CreateDialog}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
"use client";
|
||||
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
import type { CreateBtnProps, Option } from "./CreateButton";
|
||||
import { CreateButton } from "./CreateButton";
|
||||
|
||||
export function CreateButtonWithTeamsList(
|
||||
props: Omit<CreateBtnProps, "options"> & {
|
||||
onlyShowWithTeams?: boolean;
|
||||
onlyShowWithNoTeams?: boolean;
|
||||
isAdmin?: boolean;
|
||||
includeOrg?: boolean;
|
||||
}
|
||||
) {
|
||||
const query = trpc.viewer.teamsAndUserProfilesQuery.useQuery({ includeOrg: props.includeOrg });
|
||||
if (!query.data) return null;
|
||||
|
||||
const teamsAndUserProfiles: Option[] = query.data
|
||||
.filter((profile) => !profile.readOnly)
|
||||
.map((profile) => {
|
||||
return {
|
||||
teamId: profile.teamId,
|
||||
label: profile.name || profile.slug,
|
||||
image: profile.image,
|
||||
slug: profile.slug,
|
||||
};
|
||||
});
|
||||
|
||||
if (props.isAdmin) {
|
||||
teamsAndUserProfiles.push({
|
||||
platform: true,
|
||||
label: "Platform",
|
||||
image: null,
|
||||
slug: null,
|
||||
teamId: null,
|
||||
});
|
||||
}
|
||||
|
||||
if (props.onlyShowWithTeams && teamsAndUserProfiles.length < 2) return null;
|
||||
|
||||
if (props.onlyShowWithNoTeams && teamsAndUserProfiles.length > 1) return null;
|
||||
|
||||
return <CreateButton {...props} options={teamsAndUserProfiles} />;
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { render, screen, fireEvent } from "@testing-library/react";
|
||||
import { vi } from "vitest";
|
||||
|
||||
import type { CreateBtnProps } from "./CreateButton";
|
||||
import { CreateButtonWithTeamsList } from "./CreateButtonWithTeamsList";
|
||||
|
||||
const runtimeMock = async (data: Array<any>) => {
|
||||
const updatedTrpc = {
|
||||
viewer: {
|
||||
teamsAndUserProfilesQuery: {
|
||||
useQuery() {
|
||||
return {
|
||||
data: data,
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const mockedLib = (await import("@calcom/trpc/react")) as any;
|
||||
mockedLib.trpc = updatedTrpc;
|
||||
};
|
||||
const renderCreateButton = (
|
||||
props: Omit<CreateBtnProps, "options"> & { onlyShowWithTeams?: boolean; onlyShowWithNoTeams?: boolean }
|
||||
) => {
|
||||
return render(<CreateButtonWithTeamsList {...props} />);
|
||||
};
|
||||
|
||||
describe("Create Button Tests", () => {
|
||||
describe("Create Button Tests With Valid Team", () => {
|
||||
beforeAll(async () => {
|
||||
// INFO: This needs to be here before the other calls to runtimeMock. For some reason,
|
||||
// when we moved this file from @calcom/ui, the imports started breaking, resulting in
|
||||
// errors of "Cannot set property 'trpc' of [object Module] which has only a getter"
|
||||
// TODO: Update this pattern to be more consistent. Using this direct mocking in the
|
||||
// functions below breaks some tests.
|
||||
vi.mock("@calcom/trpc/react", () => ({
|
||||
trpc: {
|
||||
viewer: {
|
||||
teamsAndUserProfilesQuery: {
|
||||
useQuery() {
|
||||
return {
|
||||
data: [
|
||||
{
|
||||
teamId: 1,
|
||||
name: "test",
|
||||
slug: "create-button-test",
|
||||
image: "image",
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
});
|
||||
test("Should render the create-button-dropdown button", () => {
|
||||
const createFunction = vi.fn();
|
||||
renderCreateButton({ createFunction });
|
||||
|
||||
const createButton = screen.getByTestId("create-button-dropdown");
|
||||
expect(createButton).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Create Button Tests With One Null Team", () => {
|
||||
beforeAll(async () => {
|
||||
await runtimeMock([
|
||||
{
|
||||
teamId: null,
|
||||
name: "test",
|
||||
slug: "create-button-test",
|
||||
image: "image",
|
||||
readOnly: false,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test("Should render only the create-button button", () => {
|
||||
const createFunction = vi.fn();
|
||||
renderCreateButton({ createFunction });
|
||||
|
||||
const createButton = screen.getByTestId("create-button");
|
||||
expect(screen.queryByTestId("create-button-dropdown")).not.toBeInTheDocument();
|
||||
expect(createButton).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(createButton);
|
||||
expect(createFunction).toBeCalled();
|
||||
});
|
||||
|
||||
test("Should render nothing when teamsAndUserProfiles is less than 2 and onlyShowWithTeams is true", () => {
|
||||
renderCreateButton({ onlyShowWithTeams: true });
|
||||
|
||||
expect(screen.queryByTestId("create-button")).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId("create-button-dropdown")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
describe("Create Button Tests With Multiple Null Teams", () => {
|
||||
beforeAll(async () => {
|
||||
await runtimeMock([
|
||||
{
|
||||
teamId: null,
|
||||
name: "test",
|
||||
slug: "create-button-test",
|
||||
image: "image",
|
||||
readOnly: false,
|
||||
},
|
||||
{
|
||||
teamId: null,
|
||||
name: "test2",
|
||||
slug: "create-button-test2",
|
||||
image: "image2",
|
||||
readOnly: false,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test("Should render only the create-button button", () => {
|
||||
const createFunction = vi.fn();
|
||||
renderCreateButton({ createFunction });
|
||||
|
||||
const createButton = screen.getByTestId("create-button");
|
||||
expect(screen.queryByTestId("create-button-dropdown")).not.toBeInTheDocument();
|
||||
expect(createButton).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(createButton);
|
||||
expect(createFunction).toBeCalled();
|
||||
});
|
||||
|
||||
test("Should render nothing when teamsAndUserProfiles is greater than 1 and onlyShowWithNoTeams is true", () => {
|
||||
renderCreateButton({ onlyShowWithNoTeams: true });
|
||||
|
||||
expect(screen.queryByTestId("create-button")).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId("create-button-dropdown")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
export { CreateButton } from "./CreateButton";
|
||||
export { CreateButtonWithTeamsList } from "./CreateButtonWithTeamsList";
|
||||
@@ -2,21 +2,12 @@
|
||||
import { noop } from "lodash";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
|
||||
import { TimezoneSelect } from "@calcom/features/components/timezone-select";
|
||||
import { getUserAvatarUrl } from "@calcom/lib/getAvatarUrl";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { defaultLocaleOption, localeOptions } from "@calcom/lib/i18n";
|
||||
import { nameOfDay } from "@calcom/lib/weekday";
|
||||
import {
|
||||
Avatar,
|
||||
Button,
|
||||
EmailField,
|
||||
Form,
|
||||
ImageUploader,
|
||||
Label,
|
||||
Select,
|
||||
TextField,
|
||||
TimezoneSelect,
|
||||
} from "@calcom/ui";
|
||||
import { Avatar, Button, EmailField, Form, ImageUploader, Label, Select, TextField } from "@calcom/ui";
|
||||
|
||||
import type { UserAdminRouterOutputs } from "../server/trpc-router";
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import { CreateButtonWithTeamsList } from "@calcom/features/ee/teams/components/createButton/CreateButtonWithTeamsList";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { IconName } from "@calcom/ui";
|
||||
import { CreateButtonWithTeamsList, EmptyScreen as ClassicEmptyScreen, Icon, showToast } from "@calcom/ui";
|
||||
import { EmptyScreen as ClassicEmptyScreen, Icon, showToast } from "@calcom/ui";
|
||||
|
||||
type WorkflowExampleType = {
|
||||
Icon: IconName;
|
||||
|
||||
@@ -5,6 +5,7 @@ import { useRouter } from "next/navigation";
|
||||
import type { Dispatch, SetStateAction } from "react";
|
||||
import { useState } from "react";
|
||||
|
||||
import { CreateButtonWithTeamsList } from "@calcom/features/ee/teams/components/createButton/CreateButtonWithTeamsList";
|
||||
import Shell, { ShellMain } from "@calcom/features/shell/Shell";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
@@ -12,7 +13,7 @@ import { useRouterQuery } from "@calcom/lib/hooks/useRouterQuery";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import type { WorkflowRepository } from "@calcom/lib/server/repository/workflow";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { AnimatedPopover, Avatar, CreateButtonWithTeamsList, showToast } from "@calcom/ui";
|
||||
import { AnimatedPopover, Avatar, showToast } from "@calcom/ui";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
|
||||
import { FilterResults } from "../../../filters/components/FilterResults";
|
||||
|
||||
@@ -14,6 +14,7 @@ import { AvailableTimes, AvailableTimesHeader } from "@calcom/features/bookings"
|
||||
import { useBookerStore, useInitializeBookerStore } from "@calcom/features/bookings/Booker/store";
|
||||
import { useEvent, useScheduleForEvent } from "@calcom/features/bookings/Booker/utils/event";
|
||||
import DatePicker from "@calcom/features/calendars/DatePicker";
|
||||
import { TimezoneSelect } from "@calcom/features/components/timezone-select";
|
||||
import type { Slot } from "@calcom/features/schedules";
|
||||
import { useNonEmptyScheduleDays } from "@calcom/features/schedules";
|
||||
import { useSlotsForDate } from "@calcom/features/schedules/lib/use-schedule/useSlotsForDate";
|
||||
@@ -38,7 +39,6 @@ import {
|
||||
showToast,
|
||||
Switch,
|
||||
TextField,
|
||||
TimezoneSelect,
|
||||
} from "@calcom/ui";
|
||||
|
||||
import { useBookerTime } from "../bookings/Booker/components/hooks/useBookerTime";
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useFieldArray, useForm, useFormContext } from "react-hook-form";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { TimezoneSelect } from "@calcom/features/components/timezone-select";
|
||||
import { DateOverrideInputDialog, DateOverrideList } from "@calcom/features/schedules";
|
||||
import Schedule from "@calcom/features/schedules/components/Schedule";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
@@ -22,7 +23,6 @@ import {
|
||||
SheetHeader,
|
||||
SheetTitle,
|
||||
showToast,
|
||||
TimezoneSelect,
|
||||
} from "@calcom/ui";
|
||||
|
||||
import type { SliderUser } from "./AvailabilitySliderTable";
|
||||
|
||||
@@ -5,6 +5,7 @@ import { useMemo, useState } from "react";
|
||||
import { Controller, useForm, useFormContext } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
import { TimezoneSelect } from "@calcom/features/components/timezone-select";
|
||||
import { emailSchema } from "@calcom/lib/emailSchema";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
@@ -15,7 +16,6 @@ import {
|
||||
ToggleGroup,
|
||||
InputField,
|
||||
TextAreaField,
|
||||
TimezoneSelect,
|
||||
Label,
|
||||
showToast,
|
||||
Avatar,
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import { CreateButtonWithTeamsList } from "@calcom/features/ee/teams/components/createButton/CreateButtonWithTeamsList";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { CreateButtonWithTeamsList } from "@calcom/ui";
|
||||
|
||||
export const CreateNewWebhookButton = ({ isAdmin }: { isAdmin: boolean }) => {
|
||||
const router = useRouter();
|
||||
|
||||
Reference in new issue
Block a user