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 235 236 237 238 239 240 241 242 243 244 | 5x 3x 5x 617x 5x 1728x 1110x 618x 610x 8x 5x 5x 3x 2x 2x 1x 5x 149x 30x 119x 73x 46x 46x 46x 46x 46x 46x 46x 5x 8x 2x 6x 2x 4x 2x 2x 5x 64x 64x 64x 64x 64x 64x 93x 93x 93x 4x 93x 64x 5x 149x 149x 149x 33x 116x 116x 56x 60x 60x 146x 5x 9x 6x 3x 5x 71x 6x 65x 65x 5x 204x 5x 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);
};
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;
};
|