2022-06-01 17:24:41 +00:00
|
|
|
import classNames from "classnames";
|
2022-01-21 21:35:31 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import Select from "react-select";
|
|
|
|
|
2022-06-01 17:24:41 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-03-16 23:36:43 +00:00
|
|
|
|
2022-01-21 21:35:31 +00:00
|
|
|
import { trpc } from "@lib/trpc";
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
onChange: (value: { externalId: string; integration: string }) => void;
|
|
|
|
isLoading?: boolean;
|
|
|
|
hidePlaceholder?: boolean;
|
|
|
|
/** The external Id of the connected calendar */
|
|
|
|
value: string | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DestinationCalendarSelector = ({
|
|
|
|
onChange,
|
|
|
|
isLoading,
|
|
|
|
value,
|
|
|
|
hidePlaceholder,
|
|
|
|
}: Props): JSX.Element | null => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const query = trpc.useQuery(["viewer.connectedCalendars"]);
|
|
|
|
const [selectedOption, setSelectedOption] = useState<{ value: string; label: string } | null>(null);
|
|
|
|
|
2022-06-01 17:24:41 +00:00
|
|
|
// Extra styles to show prefixed text in react-select
|
|
|
|
const content = (hidePlaceholder = false) => {
|
|
|
|
if (!hidePlaceholder) {
|
|
|
|
return {
|
|
|
|
alignItems: "center",
|
|
|
|
display: "flex",
|
|
|
|
":before": {
|
|
|
|
content: `'${t("select_destination_calendar")}:'`,
|
|
|
|
display: "block",
|
|
|
|
marginRight: 8,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
2022-01-21 21:35:31 +00:00
|
|
|
useEffect(() => {
|
2022-03-31 17:26:26 +00:00
|
|
|
const selected = query.data?.connectedCalendars
|
|
|
|
.map((connected) => connected.calendars ?? [])
|
|
|
|
.flat()
|
|
|
|
.find((cal) => cal.externalId === value);
|
2022-01-21 21:35:31 +00:00
|
|
|
|
2022-03-31 17:26:26 +00:00
|
|
|
if (selected) {
|
|
|
|
setSelectedOption({
|
|
|
|
value: `${selected.integration}:${selected.externalId}`,
|
|
|
|
label: selected.name || "",
|
|
|
|
});
|
2022-01-21 21:35:31 +00:00
|
|
|
}
|
2022-03-31 17:26:26 +00:00
|
|
|
}, [query.data?.connectedCalendars, value]);
|
2022-01-21 21:35:31 +00:00
|
|
|
|
|
|
|
if (!query.data?.connectedCalendars.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const options =
|
|
|
|
query.data.connectedCalendars.map((selectedCalendar) => ({
|
|
|
|
key: selectedCalendar.credentialId,
|
|
|
|
label: `${selectedCalendar.integration.title} (${selectedCalendar.primary?.name})`,
|
2022-07-14 00:16:24 +00:00
|
|
|
options: (selectedCalendar.calendars ?? [])
|
|
|
|
.filter((cal) => cal.readOnly === false)
|
|
|
|
.map((cal) => ({
|
|
|
|
label: cal.name || "",
|
|
|
|
value: `${cal.integration}:${cal.externalId}`,
|
|
|
|
})),
|
2022-01-21 21:35:31 +00:00
|
|
|
})) ?? [];
|
|
|
|
return (
|
2022-03-24 16:43:07 +00:00
|
|
|
<div className="relative" title={`${t("select_destination_calendar")}: ${selectedOption?.label || ""}`}>
|
2022-01-21 21:35:31 +00:00
|
|
|
<Select
|
2022-07-12 17:50:04 +00:00
|
|
|
name="primarySelectedCalendar"
|
2022-01-21 21:35:31 +00:00
|
|
|
placeholder={!hidePlaceholder ? `${t("select_destination_calendar")}:` : undefined}
|
|
|
|
options={options}
|
2022-06-01 17:24:41 +00:00
|
|
|
styles={{
|
|
|
|
placeholder: (styles) => ({ ...styles, ...content(hidePlaceholder) }),
|
|
|
|
singleValue: (styles) => ({ ...styles, ...content(hidePlaceholder) }),
|
|
|
|
option: (defaultStyles, state) => ({
|
|
|
|
...defaultStyles,
|
|
|
|
backgroundColor: state.isSelected
|
|
|
|
? state.isFocused
|
|
|
|
? "var(--brand-color)"
|
|
|
|
: "var(--brand-color)"
|
|
|
|
: state.isFocused
|
|
|
|
? "var(--brand-color-dark-mode)"
|
|
|
|
: "var(--brand-text-color)",
|
|
|
|
}),
|
2022-06-09 20:49:09 +00:00
|
|
|
control: (defaultStyles) => {
|
|
|
|
return {
|
|
|
|
...defaultStyles,
|
|
|
|
borderRadius: "2px",
|
2022-06-20 17:33:07 +00:00
|
|
|
"@media only screen and (min-width: 640px)": {
|
|
|
|
...(defaultStyles["@media only screen and (min-width: 640px)"] as object),
|
|
|
|
maxWidth: "320px",
|
|
|
|
},
|
2022-06-09 20:49:09 +00:00
|
|
|
};
|
|
|
|
},
|
2022-06-01 17:24:41 +00:00
|
|
|
}}
|
2022-01-21 21:35:31 +00:00
|
|
|
isSearchable={false}
|
2022-06-01 17:24:41 +00:00
|
|
|
className={classNames(
|
2022-06-09 20:49:09 +00:00
|
|
|
"mt-1 mb-2 block w-full min-w-0 flex-1 rounded-none rounded-r-sm border-gray-300 sm:text-sm",
|
2022-06-01 17:24:41 +00:00
|
|
|
!hidePlaceholder && "font-medium"
|
|
|
|
)}
|
2022-01-21 21:35:31 +00:00
|
|
|
onChange={(option) => {
|
|
|
|
setSelectedOption(option);
|
|
|
|
if (!option) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split only the first `:`, since Apple uses the full URL as externalId */
|
|
|
|
const [integration, externalId] = option.value.split(/:(.+)/);
|
|
|
|
|
|
|
|
onChange({
|
|
|
|
integration,
|
|
|
|
externalId,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
isLoading={isLoading}
|
|
|
|
value={selectedOption}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DestinationCalendarSelector;
|