All files / apps/timeOff/src/screens/ChooseDate/hooks helpers.ts

81.81% Statements 27/33
56% Branches 28/50
77.77% Functions 7/9
83.87% Lines 26/31

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                                          1x 18x       1x                                 1x         6x           6x             14x         1x   1x       4x 4x                   1x       9x     1x         9x       9x 5x     4x         1x         9x           9x 5x                 4x       4x       4x   4x                
import {
  DATE_PICKER_TIME_SLOT,
  type DateRangeValue,
} from '@repo/ui/components/DatePicker';
import { getTimeSlot } from '@repo/ui/components/DatePicker/utils';
import type { QuickSelectValue } from '@repo/ui/components/QuickSelect';
 
import { getPresetDate } from '@repo/utils/date';
import {
  applyTimeSlotsToRange,
  normalizeRangeBySlot,
  type TimeSlotValue,
} from '@repo/utils/timeSlotRange';
 
export interface DatePickerDraftSlotState {
  activeRangeSelection: 'start' | 'end';
  rangeEndTimeSlot: QuickSelectValue['timeSlot'];
  rangeStartTimeSlot: QuickSelectValue['timeSlot'];
  timeSlot: QuickSelectValue['timeSlot'];
}
 
const isSameDay = (startDate: Date | null, endDate: Date | null) =>
  !!startDate &&
  !!endDate &&
  startDate.toDateString() === endDate.toDateString();
 
export const getFallbackValue = (
  referenceDate = getPresetDate(1),
): QuickSelectValue => {
  const timeSlot = DATE_PICKER_TIME_SLOT.ALL_DAY;
  const mappedRange = applyTimeSlotsToRange(
    { startDate: referenceDate, endDate: referenceDate },
    timeSlot,
    timeSlot,
  );
 
  return {
    startDate: mappedRange.startDate,
    endDate: mappedRange.endDate,
    timeSlot,
  };
};
 
export const toDateSelectionValue = (
  startDate: Date | null,
  endDate: Date | null,
  timeSlot: QuickSelectValue['timeSlot'],
): QuickSelectValue => {
  const normalized = normalizeRangeBySlot(
    startDate,
    endDate,
    (timeSlot || DATE_PICKER_TIME_SLOT.ALL_DAY) as TimeSlotValue,
  );
 
  return {
    startDate: normalized.startDate,
    endDate: normalized.endDate,
    timeSlot,
  };
};
 
export const toDatePickerRange = (value: QuickSelectValue): DateRangeValue => ({
  startDate: value.startDate,
  endDate: value.endDate,
});
 
const toDateTime = (value: Date | null) => (value ? value.getTime() : null);
 
export const isSameQuickSelectValue = (
  left: QuickSelectValue | null,
  right: QuickSelectValue | null,
) => {
  Eif (!left || !right) {
    return left === right;
  }
 
  return (
    toDateTime(left.startDate) === toDateTime(right.startDate) &&
    toDateTime(left.endDate) === toDateTime(right.endDate) &&
    left.timeSlot === right.timeSlot
  );
};
 
export const getTimeSlotOrDefault = (
  startDate: Date | null,
  endDate: Date | null,
) =>
  (startDate && endDate ? getTimeSlot(startDate, endDate) : null) ||
  DATE_PICKER_TIME_SLOT.ALL_DAY;
 
const getDatePickerActiveTimeSlot = (
  startDate: Date | null,
  endDate: Date | null,
  fallbackTimeSlot: QuickSelectValue['timeSlot'],
) => {
  Iif (!startDate || !endDate) {
    return fallbackTimeSlot || DATE_PICKER_TIME_SLOT.ALL_DAY;
  }
 
  if (isSameDay(startDate, endDate)) {
    return getTimeSlotOrDefault(startDate, endDate);
  }
 
  return endDate.getHours() === 12 && endDate.getMinutes() === 0
    ? DATE_PICKER_TIME_SLOT.MORNING
    : DATE_PICKER_TIME_SLOT.ALL_DAY;
};
 
export const getDraftSlotState = (
  startDate: Date | null,
  endDate: Date | null,
  fallbackTimeSlot: QuickSelectValue['timeSlot'],
): DatePickerDraftSlotState => {
  const resolvedTimeSlot = getDatePickerActiveTimeSlot(
    startDate,
    endDate,
    fallbackTimeSlot,
  );
 
  if (!startDate || !endDate || isSameDay(startDate, endDate)) {
    return {
      activeRangeSelection: 'start',
      rangeEndTimeSlot: resolvedTimeSlot,
      rangeStartTimeSlot: resolvedTimeSlot,
      timeSlot: resolvedTimeSlot,
    };
  }
 
  const rangeStartTimeSlot =
    startDate.getHours() === 13 && startDate.getMinutes() === 30
      ? DATE_PICKER_TIME_SLOT.AFTERNOON
      : DATE_PICKER_TIME_SLOT.ALL_DAY;
  const rangeEndTimeSlot =
    endDate.getHours() === 12 && endDate.getMinutes() === 0
      ? DATE_PICKER_TIME_SLOT.MORNING
      : DATE_PICKER_TIME_SLOT.ALL_DAY;
  const activeRangeSelection =
    rangeEndTimeSlot !== DATE_PICKER_TIME_SLOT.ALL_DAY ? 'end' : 'start';
 
  return {
    activeRangeSelection,
    rangeEndTimeSlot,
    rangeStartTimeSlot,
    timeSlot:
      activeRangeSelection === 'start' ? rangeStartTimeSlot : rangeEndTimeSlot,
  };
};