2022-08-24 20:18:42 +00:00
|
|
|
import classNames from "classnames";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { components, OptionProps, SingleValueProps } from "react-select";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-09-06 20:45:46 +00:00
|
|
|
import { DestinationCalendar } from "@calcom/prisma/client";
|
2022-08-24 20:18:42 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Select } from "@calcom/ui";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
onChange: (value: { externalId: string; integration: string }) => void;
|
|
|
|
isLoading?: boolean;
|
|
|
|
hidePlaceholder?: boolean;
|
|
|
|
/** The external Id of the connected calendar */
|
2022-09-06 20:45:46 +00:00
|
|
|
destinationCalendar?: DestinationCalendar | null;
|
2022-08-24 20:18:42 +00:00
|
|
|
value: string | undefined;
|
|
|
|
maxWidth?: number;
|
|
|
|
}
|
|
|
|
|
2022-10-18 18:06:26 +00:00
|
|
|
interface Option {
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
subtitle: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SingleValueComponent = ({ ...props }: SingleValueProps<Option>) => {
|
|
|
|
const { label, subtitle } = props.data;
|
|
|
|
return (
|
|
|
|
<components.SingleValue {...props} className="flex space-x-1">
|
2023-01-12 16:57:43 +00:00
|
|
|
<p>{label}</p> <p className=" text-gray-500">{subtitle}</p>
|
2022-10-18 18:06:26 +00:00
|
|
|
</components.SingleValue>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const OptionComponent = ({ ...props }: OptionProps<Option>) => {
|
|
|
|
const { label } = props.data;
|
|
|
|
return (
|
|
|
|
<components.Option {...props}>
|
|
|
|
<span>{label}</span>
|
|
|
|
</components.Option>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
const DestinationCalendarSelector = ({
|
|
|
|
onChange,
|
|
|
|
isLoading,
|
|
|
|
value,
|
|
|
|
hidePlaceholder,
|
|
|
|
maxWidth,
|
2022-09-06 20:45:46 +00:00
|
|
|
destinationCalendar,
|
2022-08-24 20:18:42 +00:00
|
|
|
}: Props): JSX.Element | null => {
|
|
|
|
const { t } = useLocale();
|
2022-11-10 23:40:01 +00:00
|
|
|
const query = trpc.viewer.connectedCalendars.useQuery();
|
2022-10-18 18:06:26 +00:00
|
|
|
const [selectedOption, setSelectedOption] = useState<{
|
|
|
|
value: string;
|
|
|
|
label: string;
|
|
|
|
subtitle: string;
|
|
|
|
} | null>(null);
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
// Extra styles to show prefixed text in react-select
|
|
|
|
const content = (hidePlaceholder = false) => {
|
|
|
|
if (!hidePlaceholder) {
|
|
|
|
return {
|
|
|
|
alignItems: "center",
|
2022-10-18 18:06:26 +00:00
|
|
|
width: "100%",
|
2022-08-24 20:18:42 +00:00
|
|
|
display: "flex",
|
|
|
|
":before": {
|
|
|
|
content: `'${t("select_destination_calendar")}:'`,
|
|
|
|
display: "block",
|
|
|
|
marginRight: 8,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const selected = query.data?.connectedCalendars
|
|
|
|
.map((connected) => connected.calendars ?? [])
|
|
|
|
.flat()
|
|
|
|
.find((cal) => cal.externalId === value);
|
|
|
|
|
|
|
|
if (selected) {
|
2022-10-18 18:06:26 +00:00
|
|
|
const selectedIntegration = query.data?.connectedCalendars.find((integration) =>
|
|
|
|
integration.calendars?.some((calendar) => calendar.externalId === selected.externalId)
|
|
|
|
);
|
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
setSelectedOption({
|
|
|
|
value: `${selected.integration}:${selected.externalId}`,
|
2022-10-18 18:06:26 +00:00
|
|
|
label: `${selected.name} ` || "",
|
|
|
|
subtitle: `(${selectedIntegration?.integration.title?.replace(/calendar/i, "")} - ${
|
|
|
|
selectedIntegration?.primary?.name
|
|
|
|
})`,
|
2022-08-24 20:18:42 +00:00
|
|
|
});
|
|
|
|
}
|
2022-10-18 18:06:26 +00:00
|
|
|
}, [query.data?.connectedCalendars]);
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
if (!query.data?.connectedCalendars.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const options =
|
|
|
|
query.data.connectedCalendars.map((selectedCalendar) => ({
|
|
|
|
key: selectedCalendar.credentialId,
|
2022-10-18 18:06:26 +00:00
|
|
|
label: `${selectedCalendar.integration.title?.replace(/calendar/i, "")} (${
|
2022-12-20 21:50:20 +00:00
|
|
|
selectedCalendar.primary?.integration === "office365_calendar"
|
|
|
|
? selectedCalendar.primary?.email
|
|
|
|
: selectedCalendar.primary?.name
|
2022-10-18 18:06:26 +00:00
|
|
|
})`,
|
2022-08-24 20:18:42 +00:00
|
|
|
options: (selectedCalendar.calendars ?? [])
|
|
|
|
.filter((cal) => cal.readOnly === false)
|
|
|
|
.map((cal) => ({
|
2022-10-18 18:06:26 +00:00
|
|
|
label: ` ${cal.name} `,
|
|
|
|
subtitle: `(${selectedCalendar?.integration.title?.replace(/calendar/i, "")} - ${
|
|
|
|
selectedCalendar?.primary?.name
|
|
|
|
})`,
|
2022-08-24 20:18:42 +00:00
|
|
|
value: `${cal.integration}:${cal.externalId}`,
|
|
|
|
})),
|
|
|
|
})) ?? [];
|
2022-11-16 20:03:19 +00:00
|
|
|
|
|
|
|
// Get primary calendar, which is shown in the placeholder since this is the calendar that will
|
|
|
|
// be used when no destination calendar is selected.
|
2023-02-08 12:19:02 +00:00
|
|
|
const primaryCalendar = query.data.destinationCalendarEmail;
|
2022-11-16 20:03:19 +00:00
|
|
|
|
2023-02-08 21:13:10 +00:00
|
|
|
const queryDestinationCalendar = query.data.destinationCalendar;
|
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
return (
|
|
|
|
<div className="relative" title={`${t("select_destination_calendar")}: ${selectedOption?.label || ""}`}>
|
|
|
|
<Select
|
|
|
|
name="primarySelectedCalendar"
|
2022-09-06 20:45:46 +00:00
|
|
|
placeholder={
|
|
|
|
!hidePlaceholder ? (
|
|
|
|
`${t("select_destination_calendar")}`
|
|
|
|
) : (
|
2023-02-08 21:13:10 +00:00
|
|
|
<span className="min-w-0 overflow-hidden truncate whitespace-nowrap">
|
|
|
|
{t("default_calendar_selected")}{" "}
|
|
|
|
{queryDestinationCalendar.name &&
|
|
|
|
`(${queryDestinationCalendar?.integration} - ${queryDestinationCalendar.name})`}
|
2022-09-06 20:45:46 +00:00
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
2022-08-24 20:18:42 +00:00
|
|
|
options={options}
|
|
|
|
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)",
|
|
|
|
}),
|
|
|
|
control: (defaultStyles) => {
|
|
|
|
return {
|
|
|
|
...defaultStyles,
|
|
|
|
"@media only screen and (min-width: 640px)": {
|
|
|
|
...(defaultStyles["@media only screen and (min-width: 640px)"] as object),
|
|
|
|
maxWidth,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
isSearchable={false}
|
|
|
|
className={classNames(
|
|
|
|
"mt-1 mb-2 block w-full min-w-0 flex-1 rounded-none rounded-r-sm border-gray-300 text-sm"
|
|
|
|
)}
|
2022-10-18 18:06:26 +00:00
|
|
|
onChange={(newValue) => {
|
|
|
|
setSelectedOption(newValue);
|
|
|
|
if (!newValue) {
|
2022-08-24 20:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split only the first `:`, since Apple uses the full URL as externalId */
|
2022-10-18 18:06:26 +00:00
|
|
|
const [integration, externalId] = newValue.value.split(/:(.+)/);
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
onChange({
|
|
|
|
integration,
|
|
|
|
externalId,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
isLoading={isLoading}
|
|
|
|
value={selectedOption}
|
2022-10-18 18:06:26 +00:00
|
|
|
components={{ SingleValue: SingleValueComponent, Option: OptionComponent }}
|
|
|
|
isMulti={false}
|
2022-08-24 20:18:42 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DestinationCalendarSelector;
|