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:
sean-brydonandKeith Williams authored and GitHub committed 2025-03-05 11:58:52 -03:00
1 parent 1b111c8add
commit 4cdb8f3bae
30 files changed
+97 -89

No files matched your search

@@ -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} />;
}
@@ -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";