mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
refactor(vacay): share the calendar logging logic between desktop and phone
The log mode and its half and comp modifiers, the entry, shared and company holiday maps, the trip dots, the day click rules, the default person and the day count format lived in both the desktop calendar and the phone screen. They now live in useVacayCalendarLogic and vacayFormat; the phone keeps reloading trip dots only on a new window shape.
This commit is contained in:
@@ -10,9 +10,7 @@ import MVacayInviteSheet from './MVacayInviteSheet'
|
||||
import MVacaySettingsSheet from './MVacaySettingsSheet'
|
||||
import MVacayShareSheet from './MVacayShareSheet'
|
||||
import { FALLBACK_PERSON_COLOR } from './vacayDayModel'
|
||||
|
||||
/** Half days (#552) make the balance fractional; one decimal is exact and never drifts. */
|
||||
const fmtDays = (n: number) => (Number.isInteger(n) ? String(n) : n.toFixed(1))
|
||||
import { fmtDays } from '../../../components/Vacay/vacayFormat'
|
||||
|
||||
const WEEKDAY_KEYS_MONDAY = ['vacay.mon', 'vacay.tue', 'vacay.wed', 'vacay.thu', 'vacay.fri', 'vacay.sat', 'vacay.sun'] as const
|
||||
const WEEKDAY_KEYS_SUNDAY = ['vacay.sun', 'vacay.mon', 'vacay.tue', 'vacay.wed', 'vacay.thu', 'vacay.fri', 'vacay.sat'] as const
|
||||
|
||||
@@ -5,15 +5,15 @@ import { useVacayStore } from '../../../store/vacayStore'
|
||||
import { useAuthStore } from '../../../store/authStore'
|
||||
import { useTranslation } from '../../../i18n'
|
||||
import { useToast } from '../../../components/shared/Toast'
|
||||
import { tripsApi } from '../../../api/client'
|
||||
import { isWeekend } from '../../../components/Vacay/holidays'
|
||||
import { currentPeriodYear, inGridWindow, windowMonths } from '../../../vacay/yearWindow'
|
||||
import { currentPeriodYear, windowMonths } from '../../../vacay/yearWindow'
|
||||
import { FALLBACK_PERSON_COLOR, localDateStr, type DayVisualContext } from './vacayDayModel'
|
||||
import { getApiErrorMessage, type Trip } from '../../../types'
|
||||
import { companyHolidaySets, leaveFractionFor } from '../../../components/Vacay/companyHolidays'
|
||||
import { getApiErrorMessage } from '../../../types'
|
||||
import {
|
||||
useDefaultVacayPerson, useVacayCalendarLogic, vacayWindowShape, type VacayMode,
|
||||
} from '../../../components/Vacay/useVacayCalendarLogic'
|
||||
|
||||
export type MVacayView = 'grid' | 'edit'
|
||||
export type MVacayMode = 'vacation' | 'company'
|
||||
export type MVacayMode = VacayMode
|
||||
export type MVacaySheet = 'invite' | 'settings' | 'share' | null
|
||||
|
||||
/**
|
||||
@@ -32,10 +32,10 @@ export function useMVacay() {
|
||||
handleAddNextYear, handleAddPrevYear,
|
||||
} = useVacay()
|
||||
const {
|
||||
entries, companyHolidays, stats, users, holidays,
|
||||
stats, users, holidays,
|
||||
selectedUserId, setSelectedUserId, isFused,
|
||||
toggleEntry, toggleCompanyHoliday, updateVacationDays,
|
||||
incomingShares, sharedCalendars, setShareHidden, yearSettings,
|
||||
updateVacationDays,
|
||||
incomingShares, setShareHidden, yearSettings,
|
||||
} = useVacayStore()
|
||||
const currentUser = useAuthStore(s => s.user)
|
||||
|
||||
@@ -43,89 +43,30 @@ export function useMVacay() {
|
||||
// Index into the window's twelve months (#737), not a calendar month — with a
|
||||
// fiscal year starting in July, slot 0 is July and slot 11 is the following June.
|
||||
const [monthSlot, setMonthSlot] = useState(0)
|
||||
const [mode, setMode] = useState<MVacayMode>('vacation')
|
||||
// Half-day modifier: when on, taps log the selected person's day as 0.5 (#552).
|
||||
const [halfDay, setHalfDay] = useState(false)
|
||||
// Comp/Flex day modifier (#1074): when on, taps log the day as kind='comp' (free).
|
||||
const [compDay, setCompDay] = useState(false)
|
||||
// Log mode plus the half-day (#552) and comp/flex (#1074) tap modifiers, the
|
||||
// per-day maps and the trip-overlap dots come from the shared calendar logic.
|
||||
const {
|
||||
mode, setMode, halfDay, setHalfDay, compDay, setCompDay, tripDates, entryMap, sharedMap,
|
||||
companyHolidaySet, companyHalfSet, blockWeekends, weekendDays, companyHolidaysEnabled, logDay,
|
||||
} = useVacayCalendarLogic({ selectedYear, plan, tripsFollowWindowShape: true })
|
||||
const [sheet, setSheet] = useState<MVacaySheet>(null)
|
||||
const [tripDates, setTripDates] = useState<Set<string>>(new Set())
|
||||
|
||||
// The leave-year window's shape as a primitive. loadAll() hands back a fresh
|
||||
// (deep-equal) settings object every refresh, so effects key on this instead of
|
||||
// the object and stop re-firing when nothing about the window actually changed.
|
||||
const windowShape = `${yearSettings.year_type}|${yearSettings.year_start_month}|${yearSettings.year_start_day}|${yearSettings.hire_date}`
|
||||
const windowShape = vacayWindowShape(yearSettings)
|
||||
|
||||
// Default the active person to the current user (same as the persons panel).
|
||||
useEffect(() => {
|
||||
if (!selectedUserId && currentUser) setSelectedUserId(currentUser.id)
|
||||
}, [currentUser, selectedUserId, setSelectedUserId])
|
||||
useDefaultVacayPerson()
|
||||
|
||||
// Trip-overlap dots: collect every day of the year covered by an own trip.
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
;void (async () => {
|
||||
try {
|
||||
const data = await tripsApi.list()
|
||||
const dates = new Set<string>()
|
||||
for (const trip of (data.trips || []) as Trip[]) {
|
||||
if (!trip.start_date || !trip.end_date) continue
|
||||
const start = new Date(trip.start_date + 'T00:00:00')
|
||||
const end = new Date(trip.end_date + 'T00:00:00')
|
||||
for (let d = new Date(start); d <= end; d.setDate(d.getDate() + 1)) {
|
||||
// The grid covers the leave-year window (#737), which is only the
|
||||
// calendar year while the window is unshifted.
|
||||
const date = localDateStr(d.getFullYear(), d.getMonth(), d.getDate())
|
||||
if (inGridWindow(date, selectedYear, yearSettings)) dates.add(date)
|
||||
}
|
||||
}
|
||||
if (!cancelled) setTripDates(dates)
|
||||
} catch { /* ignore */ }
|
||||
})()
|
||||
return () => { cancelled = true }
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedYear, windowShape])
|
||||
|
||||
const blockWeekends = plan?.block_weekends !== false
|
||||
const companyHolidaysEnabled = plan?.company_holidays_enabled !== false
|
||||
const holidaysEnabled = plan?.holidays_enabled === true
|
||||
const weekStart = plan?.week_start ?? 1
|
||||
const weekendDays = useMemo<number[]>(
|
||||
() => (plan?.weekend_days ? String(plan.weekend_days).split(',').map(Number) : [0, 6]),
|
||||
[plan?.weekend_days],
|
||||
)
|
||||
|
||||
// Whole company holidays close a day to leave, half ones leave half of it (#2439).
|
||||
const { full: companyHolidaySet, half: companyHalfSet } = useMemo(() => companyHolidaySets(companyHolidays), [companyHolidays])
|
||||
|
||||
const entryMap = useMemo(() => {
|
||||
const map: DayVisualContext['entryMap'] = {}
|
||||
entries.forEach(e => {
|
||||
if (!map[e.date]) map[e.date] = []
|
||||
map[e.date].push(e)
|
||||
})
|
||||
return map
|
||||
}, [entries])
|
||||
|
||||
const todayStr = useMemo(() => {
|
||||
const d = new Date()
|
||||
return localDateStr(d.getFullYear(), d.getMonth(), d.getDate())
|
||||
}, [])
|
||||
|
||||
// Shared read-only calendars (#444/#667) overlay as rings; hidden ones stay out.
|
||||
const sharedMap = useMemo(() => {
|
||||
const map: Record<string, { color: string }[]> = {}
|
||||
sharedCalendars.filter(c => !c.hidden).forEach(cal => {
|
||||
const push = (date: string) => {
|
||||
if (!map[date]) map[date] = []
|
||||
map[date].push({ color: cal.color })
|
||||
}
|
||||
cal.entries.forEach(e => push(e.date))
|
||||
cal.companyHolidays.forEach(h => push(h.date))
|
||||
})
|
||||
return map
|
||||
}, [sharedCalendars])
|
||||
|
||||
const dayCtx = useMemo<DayVisualContext>(() => ({
|
||||
todayStr, entryMap, companyHolidaySet, companyHalfSet, companyHolidaysEnabled, holidays, weekendDays, sharedMap,
|
||||
}), [todayStr, entryMap, companyHolidaySet, companyHalfSet, companyHolidaysEnabled, holidays, weekendDays, sharedMap])
|
||||
@@ -172,7 +113,7 @@ export function useMVacay() {
|
||||
setSelectedUserId(id)
|
||||
setMode('vacation')
|
||||
}
|
||||
}, [isFused, currentUser?.id, setSelectedUserId])
|
||||
}, [isFused, currentUser?.id, setSelectedUserId, setMode])
|
||||
|
||||
// Zoom the year overview into one month's editor (#1811). The overview used to
|
||||
// swallow every tap, leaving the pen FAB as the only way in; it is a way in
|
||||
@@ -193,25 +134,8 @@ export function useMVacay() {
|
||||
if (slot >= 0) openMonthSlot(slot)
|
||||
return
|
||||
}
|
||||
if (mode === 'company') {
|
||||
if (!companyHolidaysEnabled) return
|
||||
await toggleCompanyHoliday(dateStr, halfDay ? 0.5 : 1)
|
||||
return
|
||||
}
|
||||
if (blockWeekends && isWeekend(dateStr, weekendDays)) {
|
||||
// A day already logged when the weekend config changed under it (#1897) keeps
|
||||
// counting against the entitlement, so clearing it stays possible — with the
|
||||
// entry's own fraction/kind, since the server only allows the delete on a
|
||||
// blocked day, not a conversion. Logging a new one stays blocked.
|
||||
const own = entryMap[dateStr]?.find(e => e.user_id === (selectedUserId ?? currentUser?.id))
|
||||
if (!own) return
|
||||
await toggleEntry(dateStr, selectedUserId || undefined, (own.fraction ?? 1) === 0.5 ? 0.5 : 1, own.kind ?? 'vacation')
|
||||
return
|
||||
}
|
||||
if (companyHolidaysEnabled && companyHolidaySet.has(dateStr)) return
|
||||
const fraction = companyHolidaysEnabled ? leaveFractionFor(dateStr, companyHalfSet, halfDay ? 0.5 : 1) : (halfDay ? 0.5 : 1)
|
||||
await toggleEntry(dateStr, selectedUserId || undefined, fraction, compDay ? 'comp' : 'vacation')
|
||||
}, [view, months, openMonthSlot, mode, halfDay, compDay, companyHolidaysEnabled, blockWeekends, weekendDays, companyHolidaySet, companyHalfSet, toggleEntry, toggleCompanyHoliday, selectedUserId, currentUser?.id, entryMap])
|
||||
await logDay(dateStr)
|
||||
}, [view, months, openMonthSlot, logDay])
|
||||
|
||||
// Entitlement stepper: never below what is already used this year
|
||||
// (carried-over days cover the difference when used > entitlement).
|
||||
|
||||
Reference in New Issue
Block a user