import { Fragment, useCallback, useEffect, useMemo, useRef, useState } from "react"; import type { ArrayPath, Control, ControllerRenderProps, FieldArrayWithId, FieldPath, FieldPathValue, FieldValues, UseFieldArrayRemove, } from "react-hook-form"; import { Controller, useFieldArray, useFormContext } from "react-hook-form"; import type { GroupBase, Props } from "react-select"; import type { AvailabilityFormValues } from "@calcom/atoms/availability/types"; import type { ConfigType } from "@calcom/dayjs"; import dayjs from "@calcom/dayjs"; import { defaultDayRange as DEFAULT_DAY_RANGE } from "@calcom/lib/availability"; import classNames from "@calcom/lib/classNames"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { weekdayNames } from "@calcom/lib/weekday"; import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery"; import type { TimeRange } from "@calcom/types/schedule"; import { Button, CheckboxField, Dropdown, DropdownMenuContent, DropdownMenuTrigger, Select, SkeletonText, Switch, } from "@calcom/ui"; export type { TimeRange }; export type ScheduleLabelsType = { addTime: string; copyTime: string; deleteTime: string; }; export type FieldPathByValue = { [Key in FieldPath]: FieldPathValue extends TValue ? Key : never; }[FieldPath]; export const ScheduleDay = ({ name, weekday, control, handleSubmit, CopyButton, disabled, labels, userTimeFormat, className, }: { name: ArrayPath; weekday: string; control: Control; handleSubmit?: (data: AvailabilityFormValues) => Promise; CopyButton: JSX.Element; disabled?: boolean; labels?: ScheduleLabelsType; userTimeFormat: number | null; className?: { scheduleDay?: string; dayRanges?: string; timeRangeField?: string; labelAndSwitchContainer?: string; scheduleContainer?: string; }; }) => { const { watch, setValue, getValues } = useFormContext(); const watchDayRange = watch(name); return (
{/* Label & switch container */}
<> {!watchDayRange && } {watchDayRange.length > 0 && (
{!disabled &&
{CopyButton}
}
)}
); }; const CopyButton = ({ getValuesFromDayRange, weekStart, labels, handleSubmit, }: { getValuesFromDayRange: string; weekStart: number; labels?: ScheduleLabelsType; handleSubmit?: (data: AvailabilityFormValues) => Promise; }) => { const { t } = useLocale(); const [open, setOpen] = useState(false); const fieldArrayName = getValuesFromDayRange.substring(0, getValuesFromDayRange.lastIndexOf(".")); const { setValue, getValues } = useFormContext(); return (