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

100% Statements 51/51
98.27% Branches 57/58
100% Functions 12/12
100% Lines 47/47

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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227                                                                                    35x   30x   6x 25x   6x       7x       6x                     26x 1x     25x 16x     9x 9x 9x   9x 5x     4x 1x     3x 2x     1x     67x                                   6x                           130x 130x   130x   26x                                             130x   26x 24x 24x 24x 24x 23x 23x         26x 26x   26x   26x     26x             26x                               130x   3x 3x                 130x 123x                 130x          
import { useCallback, useMemo, useRef } from "react";
import type { CalendarDay, CalendarWeek } 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;
  onDisplayedMonthDetected: (firstOfMonth: Date) => void;
}
 
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,
  onDisplayedMonthDetected,
}: UseDatePickerCalendarConfigArgs) => {
  const lastDetectedMonthKeyRef = useRef(0);
  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) => {
      if (day.isCurrentMonth) {
        const dayDate = normalizeDate(day.date);
        Eif (dayDate) {
          const monthKey = dayDate.getFullYear() * 100 + dayDate.getMonth();
          if (monthKey !== lastDetectedMonthKeyRef.current) {
            lastDetectedMonthKeyRef.current = monthKey;
            onDisplayedMonthDetected(new Date(dayDate.getFullYear(), dayDate.getMonth(), 1));
          }
        }
      }
 
      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, onDisplayedMonthDetected, styles],
  );
 
  const renderWeekday = useCallback(
    (weekday: CalendarWeek) => {
      const isWeekend = weekday.index === 0 || weekday.index === 6;
      return (
        <Text style={isWeekend ? styles.calendarWeekdayWeekendLabel : styles.calendarWeekdayLabel}>
          {weekday.name.short}
        </Text>
      );
    },
    [styles],
  );
 
  const calendarComponents = useMemo(
    () => ({
      Day: renderDay,
      Weekday: renderWeekday,
      IconPrev: <CalendarNavLeftIcon color={navIconColor} />,
      IconNext: <CalendarNavRightIcon color={navIconColor} />,
    }),
    [navIconColor, renderDay, renderWeekday],
  );
 
  return {
    calendarBaseStyles,
    calendarComponents,
  };
};