2023-03-02 20:42:41 +00:00
|
|
|
import { useMemo, useState } from "react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { ITimezoneOption, ITimezone, Props as SelectProps } from "react-timezone-select";
|
|
|
|
import BaseSelect, { allTimezones } from "react-timezone-select";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2023-03-20 11:18:23 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
2023-03-02 20:42:41 +00:00
|
|
|
import { filterByCities, addCitiesToDropdown, handleOptionLabel } from "@calcom/lib/timezone";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
|
|
|
|
2023-01-05 12:04:28 +00:00
|
|
|
import { getReactSelectProps } from "../select";
|
2022-10-10 18:50:43 +00:00
|
|
|
|
2023-03-02 20:42:41 +00:00
|
|
|
export interface ICity {
|
|
|
|
city: string;
|
|
|
|
timezone: string;
|
|
|
|
}
|
|
|
|
|
2023-03-26 19:19:13 +00:00
|
|
|
export function TimezoneSelect({
|
|
|
|
className,
|
|
|
|
components,
|
|
|
|
variant = "default",
|
|
|
|
...props
|
|
|
|
}: SelectProps & { variant?: "default" | "minimal" }) {
|
2023-03-02 20:42:41 +00:00
|
|
|
const [cities, setCities] = useState<ICity[]>([]);
|
|
|
|
const { data, isLoading } = trpc.viewer.public.cityTimezones.useQuery(undefined, {
|
|
|
|
trpc: { context: { skipBatch: true } },
|
|
|
|
});
|
|
|
|
const handleInputChange = (tz: string) => {
|
|
|
|
if (data) setCities(filterByCities(tz, data));
|
|
|
|
};
|
|
|
|
|
2022-10-10 18:50:43 +00:00
|
|
|
const reactSelectProps = useMemo(() => {
|
2023-03-20 11:18:23 +00:00
|
|
|
return getReactSelectProps({
|
|
|
|
components: components || {},
|
|
|
|
});
|
|
|
|
}, [components]);
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<BaseSelect
|
2023-03-20 11:18:23 +00:00
|
|
|
className={className}
|
2023-03-02 20:42:41 +00:00
|
|
|
isLoading={isLoading}
|
|
|
|
isDisabled={isLoading}
|
2022-10-10 18:50:43 +00:00
|
|
|
{...reactSelectProps}
|
2022-08-24 20:18:42 +00:00
|
|
|
timezones={{
|
|
|
|
...allTimezones,
|
2023-03-02 20:42:41 +00:00
|
|
|
...addCitiesToDropdown(cities),
|
2022-08-24 20:18:42 +00:00
|
|
|
"America/Asuncion": "Asuncion",
|
|
|
|
}}
|
2023-03-02 20:42:41 +00:00
|
|
|
onInputChange={handleInputChange}
|
2022-08-24 20:18:42 +00:00
|
|
|
{...props}
|
2023-02-08 12:08:00 +00:00
|
|
|
formatOptionLabel={(option) => <p className="truncate">{(option as ITimezoneOption).value}</p>}
|
2023-03-02 20:42:41 +00:00
|
|
|
getOptionLabel={(option) => handleOptionLabel(option as ITimezoneOption, cities)}
|
2023-03-20 11:18:23 +00:00
|
|
|
classNames={{
|
2023-04-05 18:14:46 +00:00
|
|
|
input: () => classNames("text-emphasis", props.classNames?.input),
|
2023-03-20 11:18:23 +00:00
|
|
|
option: (state) =>
|
|
|
|
classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"bg-default flex cursor-pointer justify-between py-2.5 px-3 rounded-none text-default ",
|
|
|
|
state.isFocused && "bg-subtle",
|
|
|
|
state.isSelected && "bg-emphasis text-default",
|
2023-03-20 11:18:23 +00:00
|
|
|
props.classNames?.option
|
|
|
|
),
|
2023-04-05 18:14:46 +00:00
|
|
|
placeholder: (state) => classNames("text-muted", state.isFocused && "hidden"),
|
|
|
|
dropdownIndicator: () => "text-default",
|
2023-03-26 19:19:13 +00:00
|
|
|
control: (state) =>
|
|
|
|
classNames(
|
|
|
|
variant === "default"
|
2023-04-09 11:16:33 +00:00
|
|
|
? "px-3 py-2 bg-default border-default !min-h-9 text-sm leading-4 placeholder:text-sm placeholder:font-normal focus-within:ring-2 focus-within:ring-emphasis hover:border-emphasis rounded-md border gap-1"
|
|
|
|
: "text-sm gap-1",
|
2023-04-05 18:14:46 +00:00
|
|
|
props.classNames?.control
|
2023-03-20 11:18:23 +00:00
|
|
|
),
|
2023-04-05 18:14:46 +00:00
|
|
|
singleValue: () => classNames("text-emphasis placeholder:text-muted", props.classNames?.singleValue),
|
|
|
|
valueContainer: () =>
|
|
|
|
classNames("text-emphasis placeholder:text-muted flex gap-1", props.classNames?.valueContainer),
|
|
|
|
multiValue: () =>
|
2023-03-20 11:18:23 +00:00
|
|
|
classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"bg-subtle text-default rounded-md py-1.5 px-2 flex items-center text-sm leading-none",
|
|
|
|
props.classNames?.multiValue
|
2023-03-20 11:18:23 +00:00
|
|
|
),
|
2023-04-05 18:14:46 +00:00
|
|
|
menu: () =>
|
2023-03-20 11:18:23 +00:00
|
|
|
classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"rounded-md bg-default text-sm leading-4 text-default mt-1 border border-subtle",
|
|
|
|
props.classNames?.menu
|
2023-03-20 11:18:23 +00:00
|
|
|
),
|
2023-04-05 18:14:46 +00:00
|
|
|
groupHeading: () => "leading-none text-xs uppercase text-default pl-2.5 pt-4 pb-2",
|
|
|
|
menuList: () => classNames("scroll-bar scrollbar-track-w-20 rounded-md", props.classNames?.menuList),
|
2023-03-20 11:18:23 +00:00
|
|
|
indicatorsContainer: (state) =>
|
|
|
|
classNames(
|
|
|
|
state.selectProps.menuIsOpen
|
|
|
|
? state.isMulti
|
|
|
|
? "[&>*:last-child]:rotate-180 [&>*:last-child]:transition-transform"
|
|
|
|
: "rotate-180 transition-transform"
|
2023-04-05 18:14:46 +00:00
|
|
|
: "text-default" // Woo it adds another SVG here on multi for some reason
|
2023-03-20 11:18:23 +00:00
|
|
|
),
|
2023-04-05 18:14:46 +00:00
|
|
|
multiValueRemove: () => "text-default py-auto ml-2",
|
|
|
|
...props.classNames,
|
2023-03-20 11:18:23 +00:00
|
|
|
}}
|
2022-08-24 20:18:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export type { ITimezone, ITimezoneOption };
|