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 245 246 247 248 249 250 251 252 | 3x 3x 3x 5x 3x 14x 4x 10x 7x 7x 5x 5x 1x 1x 1x 4x 3x 7x 7x 7x 3x 18x 18x 18x 3x 4x 3x 8x 4x 4x 3x 12x 3x 37x 3x 23x 23x 23x 3x 21x 21x 1x 20x 20x 8x 12x 12x 3x 23x 3x 4x 4x 4x 4x 4x 4x 18x 18x 4x 14x 14x 14x 14x 28x 7x 21x 21x 21x 14x 11x 4x 3x 6x 6x 1x 5x 5x | import {
DATE_PICKER_TIME_SLOT,
type DatePickerTimeSlot,
} from '@repo/ui/components/DatePicker';
import type {
QuickSelectOption,
QuickSelectValue,
} from '@repo/ui/components/QuickSelect';
import { formatDateToYMD, isDisabledDate } from '@repo/utils/date';
import {
normalizeRangeBySlot,
type TimeSlotValue,
} from '@repo/utils/timeSlotRange';
import { type ReallocationShift } from '../ReallocationV2/types';
const MAX_LOOKAHEAD_DAYS = 366;
const QUICK_SELECT_TIME_BOUNDARIES = {
MORNING_START_MINUTES: 8 * 60,
AFTERNOON_START_MINUTES: 13 * 60 + 30,
} as const;
const hasToDate = (value: unknown): value is { toDate: () => Date } => {
return (
typeof value === 'object' &&
value !== null &&
'toDate' in value &&
typeof (value as { toDate?: unknown }).toDate === 'function'
);
};
export const normalizeDateValue = (value: unknown): Date | null => {
if (value instanceof Date && !isNaN(value.getTime())) {
return value;
}
if (typeof value === 'string' || typeof value === 'number') {
const parsed = new Date(value);
if (!isNaN(parsed.getTime())) {
return parsed;
}
}
if (hasToDate(value)) {
const parsed = value.toDate();
Eif (!isNaN(parsed.getTime())) {
return parsed;
}
}
return null;
};
const toStartOfDay = (date: Date): Date => {
const normalized = new Date(date);
normalized.setHours(0, 0, 0, 0);
return normalized;
};
const addDays = (date: Date, offset: number): Date => {
const next = new Date(date);
next.setDate(next.getDate() + offset);
return next;
};
const toMinutes = (date: Date): number =>
date.getHours() * 60 + date.getMinutes();
const shouldSkipTodaySlot = (
timeSlot: DatePickerTimeSlot,
nowMinutes: number,
): boolean => {
switch (timeSlot) {
case DATE_PICKER_TIME_SLOT.MORNING:
return nowMinutes >= QUICK_SELECT_TIME_BOUNDARIES.MORNING_START_MINUTES;
case DATE_PICKER_TIME_SLOT.AFTERNOON:
return nowMinutes >= QUICK_SELECT_TIME_BOUNDARIES.AFTERNOON_START_MINUTES;
default:
return false;
}
};
const getShortWeekday = (date: Date) =>
date.toLocaleDateString('en-US', { weekday: 'short' });
export const toDatePickerSlot = (
shift: ReallocationShift | undefined,
): DatePickerTimeSlot => {
return shift === 'Afternoon'
? DATE_PICKER_TIME_SLOT.AFTERNOON
: DATE_PICKER_TIME_SLOT.MORNING;
};
export const toQuickSelectValue = (
date: Date,
shift: ReallocationShift,
): QuickSelectValue => {
const timeSlot = toDatePickerSlot(shift);
const mappedRange = normalizeRangeBySlot(
date,
date,
timeSlot as TimeSlotValue,
);
return {
startDate: mappedRange.startDate,
endDate: mappedRange.endDate,
timeSlot,
};
};
const buildQuickSelectLabel = ({
candidateDate,
dayOffset,
timeSlot,
}: {
candidateDate: Date;
dayOffset: number;
timeSlot: DatePickerTimeSlot;
}) => {
const isToday = dayOffset === 0;
if (isToday) {
return timeSlot === DATE_PICKER_TIME_SLOT.MORNING
? 'This morning'
: 'This afternoon';
}
const isTomorrow = dayOffset === 1;
if (isTomorrow) {
return timeSlot === DATE_PICKER_TIME_SLOT.MORNING
? 'Tomorrow morning'
: 'Tomorrow afternoon';
}
const weekday = getShortWeekday(candidateDate);
return `${weekday} - ${
timeSlot === DATE_PICKER_TIME_SLOT.MORNING ? 'Morning' : 'Afternoon'
}`;
};
const isDisabledReallocationDate = ({
date,
holidays,
makeUpWorkdays,
startDate,
}: {
date: Date;
holidays: string[];
makeUpWorkdays: string[];
startDate?: Date;
}) => {
return isDisabledDate({
date,
holidays,
makeUpWorkdays,
startDate,
});
};
interface GenerateDynamicQuickSelectOptionsParams {
holidays: string[];
makeUpWorkdays: string[];
now?: Date;
maxBusinessDays?: number;
}
export const generateDynamicQuickSelectOptions = ({
holidays,
makeUpWorkdays,
now = new Date(),
maxBusinessDays = 4,
}: GenerateDynamicQuickSelectOptionsParams): QuickSelectOption[] => {
const options: QuickSelectOption[] = [];
const today = toStartOfDay(now);
const nowMinutes = toMinutes(now);
let selectedBusinessDays = 0;
for (
let dayOffset = 0;
dayOffset < MAX_LOOKAHEAD_DAYS && selectedBusinessDays < maxBusinessDays;
dayOffset += 1
) {
const candidateDate = addDays(today, dayOffset);
if (
isDisabledReallocationDate({
date: candidateDate,
holidays,
makeUpWorkdays,
})
) {
continue;
}
const dateKey = formatDateToYMD(candidateDate);
const timeSlots = [
DATE_PICKER_TIME_SLOT.MORNING,
DATE_PICKER_TIME_SLOT.AFTERNOON,
] as const;
let hasAddedOptionForCurrentDay = false;
timeSlots.forEach(timeSlot => {
if (dayOffset === 0 && shouldSkipTodaySlot(timeSlot, nowMinutes)) {
return;
}
const shift =
timeSlot === DATE_PICKER_TIME_SLOT.MORNING ? 'Morning' : 'Afternoon';
options.push({
label: buildQuickSelectLabel({ candidateDate, dayOffset, timeSlot }),
testID: `quick-select-${dateKey}-${timeSlot}`,
value: toQuickSelectValue(candidateDate, shift),
});
hasAddedOptionForCurrentDay = true;
});
if (hasAddedOptionForCurrentDay) {
selectedBusinessDays += 1;
}
}
return options;
};
export const disabledDateByHolidayCalendar = ({
value,
holidays,
makeUpWorkdays,
disablePastDates = true,
}: {
value: unknown;
holidays: string[];
makeUpWorkdays: string[];
disablePastDates?: boolean;
}) => {
const normalizedDate = normalizeDateValue(value);
if (!normalizedDate) {
return false;
}
const minSelectableDate = disablePastDates
? toStartOfDay(new Date())
: undefined;
return isDisabledReallocationDate({
date: normalizedDate,
holidays,
makeUpWorkdays,
startDate: minSelectableDate,
});
};
|