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 228 229 230 231 232 233 234 | 8x 3x 8x 725x 8x 1948x 1222x 726x 685x 41x 38x 38x 3x 2x 2x 1x 8x 154x 31x 123x 77x 46x 46x 46x 46x 46x 46x 46x 8x 8x 2x 6x 2x 4x 2x 2x 8x 68x 68x 68x 68x 68x 68x 93x 93x 93x 4x 93x 68x 8x 154x 154x 154x 34x 120x 120x 56x 64x 64x 151x 8x 9x 6x 3x 8x 76x 6x 70x 70x 8x 209x 8x 47x 47x 47x 47x 8x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 3x 2x | import type { DateType } from "react-native-ui-datepicker";
import { calculateLeaveDays, formatDaysOff, getStartOfDayTime, isSameDay } from "@repo/utils/date";
import { applyTimeSlotsToRange as applyTimeSlotsToRangeShared } from "@repo/utils/timeSlotRange";
import { DATE_PICKER_MODE, DATE_PICKER_TIME_SLOT, DAY_IN_MS } from "./constants";
import type {
DatePickerMode,
DatePickerTimeSlot,
DateRangeValue,
TempDateRangeValue,
} from "./types";
export { formatDaysOff, getStartOfDayTime, isSameDay };
const hasToDate = (value: unknown): value is { toDate: () => Date } =>
typeof value === "object" &&
value !== null &&
"toDate" in value &&
typeof (value as { toDate?: unknown }).toDate === "function";
const isValidDate = (value: Date | null | undefined): value is Date =>
!!value && !Number.isNaN(value.getTime());
export const normalizeDate = (value: DateType | null | undefined): Date | null => {
if (!value) {
return null;
}
if (value instanceof Date) {
return isValidDate(value) ? value : null;
}
if (typeof value === "string" || typeof value === "number") {
const parsedDate = new Date(value);
return isValidDate(parsedDate) ? parsedDate : null;
}
if (hasToDate(value)) {
const parsedDate = value.toDate();
return isValidDate(parsedDate) ? parsedDate : null;
}
return null;
};
export const getInclusiveDaysInRange = (startDate: Date | null, endDate: Date | null): number => {
if (!startDate && !endDate) {
return 0;
}
if (!startDate || !endDate) {
return 1;
}
const start = new Date(startDate);
const end = new Date(endDate);
start.setHours(0, 0, 0, 0);
end.setHours(0, 0, 0, 0);
const from = Math.min(start.getTime(), end.getTime());
const to = Math.max(start.getTime(), end.getTime());
return Math.floor((to - from) / DAY_IN_MS) + 1;
};
export const calculateDaysOff = (selectedDays: number, timeSlot: DatePickerTimeSlot): number => {
if (selectedDays <= 0) {
return 0;
}
if (timeSlot === DATE_PICKER_TIME_SLOT.ALL_DAY) {
return selectedDays;
}
if (selectedDays <= 1) {
return 0.5;
}
return selectedDays - 0.5;
};
/**
* Count how many weekdays (Mon–Fri) between startDate and endDate (inclusive)
* satisfy the given `isDisabled` predicate. Used to subtract user-disabled
* dates (e.g. public holidays) from the days-off total.
*/
export const countDisabledWeekdaysInRange = (
startDate: Date,
endDate: Date,
isDisabled: (date: DateType) => boolean,
): number => {
let count = 0;
const current = new Date(startDate);
current.setHours(0, 0, 0, 0);
const end = new Date(endDate);
end.setHours(0, 0, 0, 0);
while (current.getTime() <= end.getTime()) {
const dayOfWeek = current.getDay();
const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
if (!isWeekend && isDisabled(new Date(current))) {
count++;
}
current.setTime(current.getTime() + DAY_IN_MS);
}
return count;
};
export const calculateRangeDaysOff = (
range: DateRangeValue,
startTimeSlot: DatePickerTimeSlot,
endTimeSlot: DatePickerTimeSlot,
isDateDisabled?: (date: DateType) => boolean,
): number => {
const normalizedRange =
range.startDate && range.endDate
? getStartOfDayTime(range.startDate) <= getStartOfDayTime(range.endDate)
? range
: {
startDate: range.endDate,
endDate: range.startDate,
}
: range;
const rangeWithTime = applyTimeSlotsToRange(normalizedRange, startTimeSlot, endTimeSlot);
if (!rangeWithTime.startDate || !rangeWithTime.endDate) {
return 0;
}
const rawDays = calculateLeaveDays(
rangeWithTime.startDate.toISOString(),
rangeWithTime.endDate.toISOString(),
);
if (!isDateDisabled) {
return rawDays;
}
// Subtract disabled weekdays (e.g. public holidays) from the raw count.
// calculateLeaveDays already excludes weekends, so we only count weekday
// disabled dates here to avoid double-subtracting weekend days.
const disabledCount = countDisabledWeekdaysInRange(
normalizedRange.startDate!,
normalizedRange.endDate!,
isDateDisabled,
);
return Math.max(0, rawDays - disabledCount);
};
export const toRangeValue = (value: TempDateRangeValue): DateRangeValue => ({
startDate: normalizeDate(value.startDate),
endDate: normalizeDate(value.endDate),
});
export const getSummaryDate = (
mode: DatePickerMode,
tempDate: DateType | null,
tempRange: TempDateRangeValue,
): Date | null => {
if (mode === DATE_PICKER_MODE.SINGLE) {
return normalizeDate(tempDate);
}
return normalizeDate(tempRange.startDate) || normalizeDate(tempRange.endDate);
};
export const getSelectedDays = (
mode: DatePickerMode,
tempDate: DateType | null,
tempRange: TempDateRangeValue,
): number => {
if (mode === DATE_PICKER_MODE.SINGLE) {
return normalizeDate(tempDate) ? 1 : 0;
}
const rangeValue = toRangeValue(tempRange);
return getInclusiveDaysInRange(rangeValue.startDate, rangeValue.endDate);
};
export const applyTimeSlotsToRange = (
range: DateRangeValue,
startSlot: DatePickerTimeSlot,
endSlot: DatePickerTimeSlot,
): DateRangeValue => {
return applyTimeSlotsToRangeShared(range, startSlot, endSlot);
};
/**
* Number of 7-day rows needed to render a full month grid, matching
* react-native-ui-datepicker's showOutsideDays padding logic:
* it always pads to at least 35 cells (5 rows), going to 42 (6 rows)
* only when the month's leading + current days overflow 35.
*/
export const getCalendarRowCount = (date: Date, firstDayOfWeek = 0): number => {
const firstOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
const daysInMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
const leadingDays = (firstOfMonth.getDay() - firstDayOfWeek + 7) % 7;
return leadingDays + daysInMonth > 35 ? 6 : 5;
};
export const getTimeSlot = (startDate: Date, endDate: Date) => {
const startHour = startDate.getHours();
const startMinute = startDate.getMinutes();
const endHour = endDate.getHours();
const endMinute = endDate.getMinutes();
const startInMinutes = startHour * 60 + startMinute;
const endInMinutes = endHour * 60 + endMinute;
const MORNING_START = 8 * 60; // 08:00
const MORNING_END = 12 * 60; // 12:00
const AFTERNOON_START = 13 * 60 + 30; // 13:30
const AFTERNOON_END = 17 * 60 + 30; // 17:30
const isMorning = startInMinutes === MORNING_START && endInMinutes === MORNING_END;
const isAfternoon = startInMinutes === AFTERNOON_START && endInMinutes === AFTERNOON_END;
const isAllDay = startInMinutes === MORNING_START && endInMinutes === AFTERNOON_END;
if (isAllDay) return DATE_PICKER_TIME_SLOT.ALL_DAY;
if (isMorning) return DATE_PICKER_TIME_SLOT.MORNING;
if (isAfternoon) return DATE_PICKER_TIME_SLOT.AFTERNOON;
return null;
};
|