import { useCallback, useState } from "react";
import { sdkActionManager } from "@calcom/embed-core/embed-iframe";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { useRefreshData } from "@calcom/lib/hooks/useRefreshData";
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry";
import type { RecurringEvent } from "@calcom/types/Calendar";
import { Button, Icon, Label, TextArea, Select } from "@calcom/ui";
interface InternalNotePresetsSelectProps {
internalNotePresets: { id: number; name: string }[];
onPresetSelect: (
option: {
value: number | string;
label: string;
} | null
) => void;
setCancellationReason: (reason: string) => void;
}
const InternalNotePresetsSelect = ({
internalNotePresets,
onPresetSelect,
setCancellationReason,
}: InternalNotePresetsSelectProps) => {
const { t } = useLocale();
const [showOtherInput, setShowOtherInput] = useState(false);
if (!internalNotePresets?.length) {
return null;
}
const handleSelectChange = (option: { value: number | string; label: string } | null) => {
if (option?.value === "other") {
setShowOtherInput(true);
setCancellationReason("");
} else {
setShowOtherInput(false);
onPresetSelect && onPresetSelect(option);
}
};
return (
);
};
type Props = {
booking: {
title?: string;
uid?: string;
id?: number;
};
profile: {
name: string | null;
slug: string | null;
};
recurringEvent: RecurringEvent | null;
team?: string | null;
teamId?: number;
setIsCancellationMode: (value: boolean) => void;
theme: string | null;
allRemainingBookings: boolean;
seatReferenceUid?: string;
currentUserEmail?: string;
bookingCancelledEventProps: {
booking: unknown;
organizer: {
name: string;
email: string;
timeZone?: string;
};
eventType: unknown;
};
isHost: boolean;
internalNotePresets: { id: number; name: string; cancellationReason: string | null }[];
};
export default function CancelBooking(props: Props) {
const [cancellationReason, setCancellationReason] = useState("");
const { t } = useLocale();
const refreshData = useRefreshData();
const {
booking,
allRemainingBookings,
seatReferenceUid,
bookingCancelledEventProps,
currentUserEmail,
teamId,
} = props;
const [loading, setLoading] = useState(false);
const telemetry = useTelemetry();
const [error, setError] = useState(booking ? null : t("booking_already_cancelled"));
const [internalNote, setInternalNote] = useState<{ id: number; name: string } | null>(null);
const cancelBookingRef = useCallback((node: HTMLTextAreaElement) => {
if (node !== null) {
// eslint-disable-next-line @calcom/eslint/no-scroll-into-view-embed -- CancelBooking is not usually used in embed mode
node.scrollIntoView({ behavior: "smooth" });
node.focus();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<>
{error && (
)}
{!error && (
{props.isHost && props.internalNotePresets.length > 0 && (
<>
{
if (!option) return;
if (option.value === "other") {
setInternalNote({ id: -1, name: option.label });
} else {
const foundInternalNote = props.internalNotePresets.find(
(preset) => preset.id === Number(option.value)
);
if (foundInternalNote) {
setInternalNote(foundInternalNote);
setCancellationReason(foundInternalNote.cancellationReason || "");
}
}
}}
/>
>
)}
)}
>
);
}