2023-02-27 07:24:43 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
|
|
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-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) => {
|
|
|
|
const query = {
|
|
|
|
...router.query,
|
|
|
|
dialog: "new",
|
2023-03-06 10:47:57 +00:00
|
|
|
eventPage: option.slug,
|
2023-02-27 07:24:43 +00:00
|
|
|
teamId: option.teamId,
|
|
|
|
};
|
|
|
|
if (!option.teamId) {
|
|
|
|
delete query.teamId;
|
|
|
|
}
|
|
|
|
router.push(
|
|
|
|
{
|
|
|
|
pathname: router.pathname,
|
|
|
|
query,
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
{ shallow: true }
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
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-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-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 || ""}
|
|
|
|
imageSrc={option.image || `${WEBAPP_URL}/${option.label}/avatar.png`} // if no image, use default avatar
|
|
|
|
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>
|
|
|
|
)}
|
|
|
|
{router.query.dialog === "new" && CreateDialog}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|