All files / packages/ui/src/components/DatePicker/hooks useDatePickerComputedState.ts

100% Statements 25/25
95.83% Branches 23/24
100% Functions 7/7
100% Lines 25/25

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                                                                        4x                   93x   93x 22x 22x 21x     1x     93x 67x 4x       63x         63x     93x 67x       93x 63x     93x 66x         93x 93x       93x   90x                               93x       93x               93x                  
import { useMemo } from "react";
import type { DateType } from "react-native-ui-datepicker";
 
import { formatDateWithWeekday } from "@repo/utils/date";
 
import {
  DATE_PICKER_MODE,
  DATE_PICKER_NO_DATE_SELECTED,
  DATE_PICKER_TIME_SLOT,
  TIME_SLOT_LABEL,
} from "../constants";
import type {
  DatePickerMode,
  DatePickerTimeSlot,
  RangeSelectionTarget,
  TempDateRangeValue,
} from "../types";
import {
  calculateRangeDaysOff,
  formatDaysOff,
  getSelectedDays,
  getSummaryDate,
  normalizeDate,
} from "../utils";
 
interface UseDatePickerComputedStateArgs {
  activeRangeSelection: RangeSelectionTarget;
  mode: DatePickerMode;
  rangeEndTimeSlot: DatePickerTimeSlot;
  rangeStartTimeSlot: DatePickerTimeSlot;
  textForTimeSlot?: string;
  tempDate: DateType | null;
  tempRange: TempDateRangeValue;
  tempTimeSlot: DatePickerTimeSlot;
}
 
export const useDatePickerComputedState = ({
  activeRangeSelection,
  mode,
  rangeEndTimeSlot,
  rangeStartTimeSlot,
  textForTimeSlot,
  tempDate,
  tempRange,
  tempTimeSlot,
}: UseDatePickerComputedStateArgs) => {
  const normalizedTimeSlotText = textForTimeSlot?.trim();
 
  const formatTimeSlotSummaryLabel = (slot: DatePickerTimeSlot) => {
    const baseLabel = TIME_SLOT_LABEL[slot];
    if (slot === DATE_PICKER_TIME_SLOT.ALL_DAY || !normalizedTimeSlotText) {
      return baseLabel;
    }
 
    return `${baseLabel} ${normalizedTimeSlotText}`;
  };
 
  const summaryDate = useMemo(() => {
    if (mode !== DATE_PICKER_MODE.RANGE) {
      return getSummaryDate(mode, tempDate, tempRange);
    }
 
    const rangeSummaryDate =
      activeRangeSelection === "end"
        ? normalizeDate(tempRange.startDate) || normalizeDate(tempRange.endDate)
        : normalizeDate(tempRange.startDate) ||
          normalizeDate(tempRange.endDate);
 
    return rangeSummaryDate;
  }, [activeRangeSelection, mode, tempDate, tempRange]);
 
  const selectedDays = useMemo(
    () => getSelectedDays(mode, tempDate, tempRange),
    [mode, tempDate, tempRange],
  );
 
  const normalizedRangeStartDate = useMemo(
    () => normalizeDate(tempRange.startDate),
    [tempRange.startDate],
  );
  const normalizedRangeEndDate = useMemo(
    () => normalizeDate(tempRange.endDate),
    [tempRange.endDate],
  );
 
  const isRangeWithEndDate =
    mode === DATE_PICKER_MODE.RANGE && selectedDays > 1;
  const effectiveRangeEndTimeSlot = isRangeWithEndDate
    ? rangeEndTimeSlot
    : DATE_PICKER_TIME_SLOT.ALL_DAY;
 
  const rangeDaysOff = useMemo(
    () =>
      calculateRangeDaysOff(
        {
          startDate: normalizedRangeStartDate,
          endDate: normalizedRangeEndDate,
        },
        rangeStartTimeSlot,
        effectiveRangeEndTimeSlot,
      ),
    [
      effectiveRangeEndTimeSlot,
      normalizedRangeEndDate,
      normalizedRangeStartDate,
      rangeStartTimeSlot,
    ],
  );
 
  const summaryDateText = summaryDate
    ? formatDateWithWeekday(summaryDate.toString())
    : DATE_PICKER_NO_DATE_SELECTED;
  const summaryValueText =
    mode === DATE_PICKER_MODE.RANGE
      ? selectedDays <= 1
        ? rangeStartTimeSlot === DATE_PICKER_TIME_SLOT.ALL_DAY
          ? formatDaysOff(rangeDaysOff)
          : formatTimeSlotSummaryLabel(rangeStartTimeSlot)
        : formatDaysOff(rangeDaysOff)
      : formatTimeSlotSummaryLabel(tempTimeSlot);
 
  return {
    isRangeWithEndDate,
    normalizedRangeEndDate,
    normalizedRangeStartDate,
    selectedDays,
    summaryDateText,
    summaryValueText,
  };
};