All files / packages/ui/src/components/DatePicker/hooks useDatePickerCalendarConfig.tsx

37.5% Statements 15/40
0% Branches 0/48
36.36% Functions 4/11
35.89% Lines 14/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                                                  4x     4x     4x     4x               4x                                                                           35x                                   4x                         93x 35x       93x                                                 93x                                                                           93x 91x               93x          
import { useCallback, useMemo } from "react";
import type { CalendarDay } from "react-native-ui-datepicker";
import { Text, View } from "react-native";
 
import { CalendarNavLeftIcon } from "@repo/ui/icons/CalendarNavLeft";
import { CalendarNavRightIcon } from "@repo/ui/icons/CalendarNavRight";
 
import { DATE_PICKER_MODE, DATE_PICKER_TIME_SLOT } from "../constants";
import { useStyles } from "../styles";
import type { DatePickerMode, DatePickerTimeSlot } from "../types";
import { isSameDay, normalizeDate } from "../utils";
 
type DatePickerStyles = ReturnType<typeof useStyles>;
type HalfSelectionSlot = DatePickerTimeSlot | null;
 
interface UseDatePickerCalendarConfigArgs {
  styles: DatePickerStyles;
  mode: DatePickerMode;
  selectedDays: number;
  showTimeOffSection: boolean;
  disableStyleTimeSlot: boolean;
  tempTimeSlot: DatePickerTimeSlot;
  rangeStartTimeSlot: DatePickerTimeSlot;
  rangeEndTimeSlot: DatePickerTimeSlot;
  normalizedRangeStartDate: Date | null;
  normalizedRangeEndDate: Date | null;
  navIconColor: string;
}
 
interface ResolveHalfSelectionSlotArgs {
  day: CalendarDay;
  mode: DatePickerMode;
  selectedDays: number;
  showTimeOffSection: boolean;
  tempTimeSlot: DatePickerTimeSlot;
  rangeStartTimeSlot: DatePickerTimeSlot;
  rangeEndTimeSlot: DatePickerTimeSlot;
  normalizedRangeStartDate: Date | null;
  normalizedRangeEndDate: Date | null;
}
 
const isDayRangeMarker = (day: CalendarDay) =>
  day.isSelected || day.rangeStart || day.rangeEnd;
 
const isDayHighlighted = (day: CalendarDay) =>
  isDayRangeMarker(day) || day.inMiddle;
 
const shouldShowHalfSelection = (slot: HalfSelectionSlot) =>
  !!slot && slot !== DATE_PICKER_TIME_SLOT.ALL_DAY;
 
const getHalfSelectionMaskStyle = (
  slot: Exclude<HalfSelectionSlot, null>,
  styles: DatePickerStyles,
) =>
  slot === DATE_PICKER_TIME_SLOT.MORNING
    ? styles.halfSelectionMaskRight
    : styles.halfSelectionMaskLeft;
 
const resolveHalfSelectionSlot = ({
  day,
  mode,
  selectedDays,
  showTimeOffSection,
  tempTimeSlot,
  rangeStartTimeSlot,
  rangeEndTimeSlot,
  normalizedRangeStartDate,
  normalizedRangeEndDate,
}: ResolveHalfSelectionSlotArgs): HalfSelectionSlot => {
  if (!showTimeOffSection) {
    return null;
  }
 
  if (mode === DATE_PICKER_MODE.SINGLE) {
    return day.isSelected ? tempTimeSlot : null;
  }
 
  const dayDate = normalizeDate(day.date);
  const isStartDay = isSameDay(dayDate, normalizedRangeStartDate);
  const isEndDay = isSameDay(dayDate, normalizedRangeEndDate);
 
  if (selectedDays <= 1) {
    return isDayRangeMarker(day) || isStartDay ? rangeStartTimeSlot : null;
  }
 
  if (day.rangeStart || isStartDay) {
    return rangeStartTimeSlot;
  }
 
  if (day.rangeEnd || isEndDay) {
    return rangeEndTimeSlot;
  }
 
  return null;
};
 
const buildCalendarBaseStyles = (styles: DatePickerStyles) => ({
  day: styles.calendarDay,
  day_label: styles.calendarDayLabel,
  disabled: styles.calendarDisabled,
  outside_label: styles.calendarOutsideDayLabel,
  weekday_label: styles.calendarWeekdayLabel,
  header: styles.calendarHeader,
  month_selector: styles.calendarMonthSelector,
  year_selector: styles.calendarYearSelector,
  month_selector_label: styles.calendarMonthLabel,
  year_selector_label: styles.calendarMonthLabel,
  button_prev: styles.calendarNavButtonPrev,
  button_next: styles.calendarNavButtonNext,
  button_prev_image: styles.calendarNavIconPrev,
  button_next_image: styles.calendarNavIconNext,
  disabled_label: styles.calendarDisabledLabel,
});
 
export const useDatePickerCalendarConfig = ({
  styles,
  mode,
  selectedDays,
  showTimeOffSection,
  disableStyleTimeSlot,
  tempTimeSlot,
  rangeStartTimeSlot,
  rangeEndTimeSlot,
  normalizedRangeStartDate,
  normalizedRangeEndDate,
  navIconColor,
}: UseDatePickerCalendarConfigArgs) => {
  const calendarBaseStyles = useMemo(
    () => buildCalendarBaseStyles(styles),
    [styles],
  );
 
  const getHalfSelectionSlot = useCallback(
    (day: CalendarDay) =>
      resolveHalfSelectionSlot({
        day,
        mode,
        selectedDays,
        showTimeOffSection,
        tempTimeSlot,
        rangeStartTimeSlot,
        rangeEndTimeSlot,
        normalizedRangeStartDate,
        normalizedRangeEndDate,
      }),
    [
      mode,
      normalizedRangeEndDate,
      normalizedRangeStartDate,
      rangeEndTimeSlot,
      rangeStartTimeSlot,
      selectedDays,
      showTimeOffSection,
      tempTimeSlot,
    ],
  );
 
  const renderDay = useCallback(
    (day: CalendarDay) => {
      const halfSelectionSlot = getHalfSelectionSlot(day);
      const isDisabledInHighlightedRange =
        day.isDisabled && isDayHighlighted(day);
      const showHalfSelection =
        !disableStyleTimeSlot &&
        shouldShowHalfSelection(halfSelectionSlot) &&
        !day.isDisabled;
      const halfSelectionMaskStyle =
        showHalfSelection && halfSelectionSlot
          ? getHalfSelectionMaskStyle(halfSelectionSlot, styles)
          : null;
      const dayLabelStyles = [
        styles.customDayLabel,
        !day.isCurrentMonth && styles.calendarOutsideDayLabel,
        isDayHighlighted(day) && styles.calendarSelectedLabel,
        day.isDisabled && styles.calendarDisabledLabel,
      ];
 
      return (
        <View style={styles.customDayContent}>
          {isDisabledInHighlightedRange ? (
            <View style={styles.disabledInRangeMask} />
          ) : null}
          {showHalfSelection ? (
            <View
              style={[styles.halfSelectionMask, halfSelectionMaskStyle]}
              testID={`half-selection-${day.text}`}
            />
          ) : null}
          <Text style={dayLabelStyles}>{day.text}</Text>
        </View>
      );
    },
    [disableStyleTimeSlot, getHalfSelectionSlot, styles],
  );
 
  const calendarComponents = useMemo(
    () => ({
      Day: renderDay,
      IconPrev: <CalendarNavLeftIcon color={navIconColor} />,
      IconNext: <CalendarNavRightIcon color={navIconColor} />,
    }),
    [navIconColor, renderDay],
  );
 
  return {
    calendarBaseStyles,
    calendarComponents,
  };
};