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 | 1x 1x 1x 1x 1x 7x 15x 2x 13x 8x 4x 1x 1x 6x 6x 1x 13x 13x 13x 13x 2x 6x 6x 1x 8x 10x 14x 14x 5x 3x 2x 9x 8x 3x 2x 1x 5x 5x 2x 3x 1x 2x 1x 1x 3x 3x 3x 2x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 370x 370x 105x 265x 265x 265x 265x 2x 265x 265x 265x 265x 273x 3x 270x 270x 265x 5x 5x 5x 5x 2x 265x 3x 3x | import { DATE_PICKER_TIME_SLOT } from "@repo/constants";
import type { DatePickerTimeSlot, QuickSelectOption, QuickSelectValue } from "@repo/types";
import { addDays, formatDateToYMD, isDisabledDate, toMinutes, toStartOfDay } from "./date";
import { applyTimeSlotsToRange } from "./timeSlotRange";
export const QUICK_SELECT_TIME_BOUNDARIES = {
MORNING_START_MINUTES: 8 * 60,
AFTERNOON_START_MINUTES: 13 * 60 + 30,
ALL_DAY_END_MINUTES: 17 * 60 + 30,
} as const;
export const QUICK_SELECT_SLOT_SEQUENCE = [
DATE_PICKER_TIME_SLOT.ALL_DAY,
DATE_PICKER_TIME_SLOT.MORNING,
DATE_PICKER_TIME_SLOT.AFTERNOON,
] as const;
export const shouldSkipTodaySlot = (
timeSlot: DatePickerTimeSlot,
nowMinutes: number,
isAllDayOnly: boolean = false,
): boolean => {
if (isAllDayOnly) {
return nowMinutes >= QUICK_SELECT_TIME_BOUNDARIES.ALL_DAY_END_MINUTES;
}
switch (timeSlot) {
case DATE_PICKER_TIME_SLOT.ALL_DAY:
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;
}
};
export const toQuickSelectValue = (date: Date, timeSlot: DatePickerTimeSlot): QuickSelectValue => {
const mappedRange = applyTimeSlotsToRange({ startDate: date, endDate: date }, timeSlot, timeSlot);
return {
startDate: mappedRange.startDate,
endDate: mappedRange.endDate,
timeSlot,
};
};
export const getWeekStart = (date: Date): Date => {
const normalized = toStartOfDay(date);
const mondayIndex = (normalized.getDay() + 6) % 7;
normalized.setDate(normalized.getDate() - mondayIndex);
return normalized;
};
export const getWeekPrefix = (day: Date, baseDay: Date): "This" | "Next" => {
const sameWeek = getWeekStart(day).getTime() === getWeekStart(baseDay).getTime();
return sameWeek ? "This" : "Next";
};
export const getShortWeekday = (date: Date) =>
date.toLocaleDateString("en-US", { weekday: "short" });
export interface QuickSelectLabelParams {
day: Date;
isToday: boolean;
isNextWorkingDay: boolean;
isCalendarTomorrow: boolean;
timeSlot: DatePickerTimeSlot;
baseDay: Date;
}
export const getDynamicQuickSelectLabel = (params: QuickSelectLabelParams): string => {
const { day, isToday, isNextWorkingDay, isCalendarTomorrow, timeSlot, baseDay } = params;
if (isToday) {
if (timeSlot === DATE_PICKER_TIME_SLOT.MORNING) return "This morning";
if (timeSlot === DATE_PICKER_TIME_SLOT.AFTERNOON) return "This afternoon";
return "Today";
}
if (isNextWorkingDay) {
if (isCalendarTomorrow) {
if (timeSlot === DATE_PICKER_TIME_SLOT.MORNING) return "Tomorrow morning";
if (timeSlot === DATE_PICKER_TIME_SLOT.AFTERNOON) return "Tomorrow afternoon";
return "Tomorrow";
}
const shortWeekday = getShortWeekday(day);
if (timeSlot === DATE_PICKER_TIME_SLOT.MORNING) {
return `${shortWeekday} morning`;
}
if (timeSlot === DATE_PICKER_TIME_SLOT.AFTERNOON) {
return `${shortWeekday} afternoon`;
}
return `${getWeekPrefix(day, baseDay)} ${shortWeekday}`;
}
const shortWeekday = getShortWeekday(day);
return `${getWeekPrefix(day, baseDay)} ${shortWeekday}`;
};
export const getShortDateQuickSelectLabel = (params: QuickSelectLabelParams): string => {
const { day, isToday, isCalendarTomorrow, baseDay } = params;
if (isToday) return "Today";
if (isCalendarTomorrow) return "Tomorrow";
const shortWeekday = getShortWeekday(day);
return `${getWeekPrefix(day, baseDay)} ${shortWeekday}`;
};
export interface GenerateDynamicQuickSelectOptionsParams {
holidays: string[];
makeUpWorkdays: string[];
now?: Date;
limit?: number;
maxBusinessDays?: number;
slotSequence?: readonly DatePickerTimeSlot[];
fallbackSlotSequence?: readonly DatePickerTimeSlot[];
labelFormatter?: (params: QuickSelectLabelParams) => string;
}
const MAX_LOOKAHEAD_DAYS = 366;
export const generateDynamicQuickSelectOptions = ({
holidays,
makeUpWorkdays,
now = new Date(),
limit = 4,
maxBusinessDays,
slotSequence = QUICK_SELECT_SLOT_SEQUENCE,
fallbackSlotSequence = [DATE_PICKER_TIME_SLOT.ALL_DAY],
labelFormatter = getDynamicQuickSelectLabel,
}: GenerateDynamicQuickSelectOptionsParams): QuickSelectOption[] => {
const options: QuickSelectOption[] = [];
const seenLabels = new Set<string>();
const today = toStartOfDay(now);
const nowMinutes = toMinutes(now);
let businessDaysCount = 0;
let nextWorkingDayKey: string | null = null;
const isAllDayOnly =
slotSequence.length === 1 && slotSequence[0] === DATE_PICKER_TIME_SLOT.ALL_DAY;
for (
let dayOffset = 0;
dayOffset < MAX_LOOKAHEAD_DAYS &&
options.length < limit &&
(maxBusinessDays === undefined || businessDaysCount < maxBusinessDays);
dayOffset += 1
) {
const candidateDate = addDays(today, dayOffset);
if (
isDisabledDate({
date: candidateDate,
holidays,
makeUpWorkdays,
})
) {
continue;
}
const candidateDateKey = formatDateToYMD(candidateDate);
const isToday = dayOffset === 0;
const isCalendarTomorrow = dayOffset === 1;
if (!isToday && !nextWorkingDayKey) {
nextWorkingDayKey = candidateDateKey;
}
const isNextWorkingDay = !isToday && nextWorkingDayKey === candidateDateKey;
const timeSlotsForDay = isToday || isNextWorkingDay ? slotSequence : fallbackSlotSequence;
let hasAddedOptionForCurrentDay = false;
for (const timeSlot of timeSlotsForDay) {
if (isToday && shouldSkipTodaySlot(timeSlot, nowMinutes, isAllDayOnly)) {
continue;
}
const label = labelFormatter({
day: candidateDate,
isToday,
isNextWorkingDay,
isCalendarTomorrow,
timeSlot,
baseDay: today,
});
if (seenLabels.has(label)) {
continue;
}
seenLabels.add(label);
options.push({
label,
testID: `quick-select-${candidateDateKey}-${timeSlot}`,
value: toQuickSelectValue(candidateDate, timeSlot),
});
hasAddedOptionForCurrentDay = true;
if (options.length >= limit) {
break;
}
}
if (hasAddedOptionForCurrentDay) {
businessDaysCount += 1;
}
}
return options;
};
|