2023-08-02 09:35:48 +00:00
|
|
|
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
2023-02-27 07:24:43 +00:00
|
|
|
|
2023-07-13 13:52:21 +00:00
|
|
|
import { useBookerUrl } from "@calcom/lib/hooks/useBookerUrl";
|
2023-02-27 07:24:43 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import {
|
|
|
|
Avatar,
|
|
|
|
Button,
|
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
|
|
|
DropdownMenuLabel,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
} from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { Plus } from "@calcom/ui/components/icon";
|
2023-02-27 07:24:43 +00:00
|
|
|
|
|
|
|
export interface Option {
|
|
|
|
teamId: number | null | undefined; // if undefined, then it's a profile
|
|
|
|
label: string | null;
|
|
|
|
image?: string | null;
|
2023-03-06 10:47:57 +00:00
|
|
|
slug: string | null;
|
2023-02-27 07:24:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
export type CreateBtnProps = {
|
2023-02-27 07:24:43 +00:00
|
|
|
options: Option[];
|
|
|
|
createDialog?: () => JSX.Element;
|
|
|
|
createFunction?: (teamId?: number) => void;
|
|
|
|
subtitle?: string;
|
|
|
|
buttonText?: string;
|
|
|
|
isLoading?: boolean;
|
|
|
|
disableMobileButton?: boolean;
|
2023-06-15 08:58:07 +00:00
|
|
|
"data-testid"?: string;
|
|
|
|
};
|
2023-02-27 07:24:43 +00:00
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
/**
|
|
|
|
* @deprecated use CreateButtonWithTeamsList instead
|
|
|
|
*/
|
2023-02-27 07:24:43 +00:00
|
|
|
export function CreateButton(props: CreateBtnProps) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
2023-08-02 09:35:48 +00:00
|
|
|
const searchParams = useSearchParams();
|
|
|
|
const pathname = usePathname();
|
2023-07-13 13:52:21 +00:00
|
|
|
const bookerUrl = useBookerUrl();
|
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
const {
|
|
|
|
createDialog,
|
|
|
|
options,
|
|
|
|
isLoading,
|
|
|
|
createFunction,
|
|
|
|
buttonText,
|
|
|
|
disableMobileButton,
|
|
|
|
subtitle,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
const CreateDialog = createDialog ? createDialog() : null;
|
2023-02-27 07:24:43 +00:00
|
|
|
|
2023-06-15 08:58:07 +00:00
|
|
|
const hasTeams = !!options.find((option) => option.teamId);
|
2023-02-27 07:24:43 +00:00
|
|
|
|
|
|
|
// inject selection data into url for correct router history
|
|
|
|
const openModal = (option: Option) => {
|
2023-08-02 09:35:48 +00:00
|
|
|
const _searchParams = new URLSearchParams(searchParams);
|
|
|
|
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);
|
2023-02-27 07:24:43 +00:00
|
|
|
if (!option.teamId) {
|
2023-08-02 09:35:48 +00:00
|
|
|
_searchParams.delete("teamId");
|
2023-02-27 07:24:43 +00:00
|
|
|
}
|
2023-08-02 09:35:48 +00:00
|
|
|
router.push(`${pathname}?${_searchParams.toString()}`);
|
2023-02-27 07:24:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{!hasTeams ? (
|
|
|
|
<Button
|
|
|
|
onClick={() =>
|
|
|
|
!!CreateDialog
|
2023-06-15 08:58:07 +00:00
|
|
|
? openModal(options[0])
|
|
|
|
: createFunction
|
|
|
|
? createFunction(options[0].teamId || undefined)
|
2023-02-27 07:24:43 +00:00
|
|
|
: null
|
|
|
|
}
|
2023-07-21 15:36:39 +00:00
|
|
|
data-testid="create-button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Plus}
|
2023-06-15 08:58:07 +00:00
|
|
|
loading={isLoading}
|
|
|
|
variant={disableMobileButton ? "button" : "fab"}
|
|
|
|
{...restProps}>
|
|
|
|
{buttonText ? buttonText : t("new")}
|
2023-02-27 07:24:43 +00:00
|
|
|
</Button>
|
|
|
|
) : (
|
|
|
|
<Dropdown>
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
<Button
|
2023-06-15 08:58:07 +00:00
|
|
|
variant={disableMobileButton ? "button" : "fab"}
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Plus}
|
2023-07-21 15:36:39 +00:00
|
|
|
data-testid="create-button-dropdown"
|
2023-06-15 08:58:07 +00:00
|
|
|
loading={isLoading}
|
|
|
|
{...restProps}>
|
|
|
|
{buttonText ? buttonText : t("new")}
|
2023-02-27 07:24:43 +00:00
|
|
|
</Button>
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent sideOffset={14} align="end">
|
|
|
|
<DropdownMenuLabel>
|
2023-06-15 08:58:07 +00:00
|
|
|
<div className="w-48 text-left text-xs">{subtitle}</div>
|
2023-02-27 07:24:43 +00:00
|
|
|
</DropdownMenuLabel>
|
2023-06-15 08:58:07 +00:00
|
|
|
{options.map((option, idx) => (
|
2023-02-27 07:24:43 +00:00
|
|
|
<DropdownMenuItem key={option.label}>
|
|
|
|
<DropdownItem
|
|
|
|
type="button"
|
2023-04-13 02:10:23 +00:00
|
|
|
data-testid={`option${option.teamId ? "-team" : ""}-${idx}`}
|
2023-02-27 07:24:43 +00:00
|
|
|
StartIcon={(props) => (
|
|
|
|
<Avatar
|
|
|
|
alt={option.label || ""}
|
2023-07-13 13:52:21 +00:00
|
|
|
imageSrc={option.image || `${bookerUrl}/${option.label}/avatar.png`} // if no image, use default avatar
|
2023-02-27 07:24:43 +00:00
|
|
|
size="sm"
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
onClick={() =>
|
|
|
|
!!CreateDialog
|
|
|
|
? openModal(option)
|
2023-06-15 08:58:07 +00:00
|
|
|
: createFunction
|
|
|
|
? createFunction(option.teamId || undefined)
|
2023-02-27 07:24:43 +00:00
|
|
|
: null
|
|
|
|
}>
|
|
|
|
{" "}
|
|
|
|
{/*improve this code */}
|
|
|
|
<span>{option.label}</span>
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
))}
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</Dropdown>
|
|
|
|
)}
|
2023-08-02 09:35:48 +00:00
|
|
|
{searchParams.get("dialog") === "new" && CreateDialog}
|
2023-02-27 07:24:43 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|