|
|
|
@@ -30,7 +30,7 @@
|
|
|
|
|
import { useEffect, useRef, useState } from 'react'
|
|
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
|
|
|
import { X, Loader2 } from 'lucide-react'
|
|
|
|
|
import { useCalendarStore } from '../store/calendarStore.js'
|
|
|
|
|
import { useCalendarStore, todayIso } from '../store/calendarStore.js'
|
|
|
|
|
import {
|
|
|
|
|
createEvent,
|
|
|
|
|
updateEvent,
|
|
|
|
@@ -44,13 +44,8 @@ import type { CalendarOccurrence } from '../api/client.js'
|
|
|
|
|
|
|
|
|
|
const LAST_CALENDAR_KEY = 'eventForm.lastCalendarUrl'
|
|
|
|
|
|
|
|
|
|
function getDefaultStartDate(): string {
|
|
|
|
|
return new Date().toISOString().slice(0, 10)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDefaultEndDate(): string {
|
|
|
|
|
return new Date().toISOString().slice(0, 10)
|
|
|
|
|
}
|
|
|
|
|
// IN-03: todayIso is now imported from calendarStore (single source of truth).
|
|
|
|
|
// getDefaultStartDate/getDefaultEndDate collapsed to todayIso() calls at use sites.
|
|
|
|
|
|
|
|
|
|
// ── Helpers ────────────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
@@ -80,23 +75,32 @@ function writeLastCalendarUrl(url: string): void {
|
|
|
|
|
/**
|
|
|
|
|
* Parse an ISO date string (possibly with time + offset) into
|
|
|
|
|
* { date: 'YYYY-MM-DD', time: 'HH:MM' }. Falls back to today/09:00 if malformed.
|
|
|
|
|
*
|
|
|
|
|
* WR-05 fix: date and time are derived from the SAME local-accessor family.
|
|
|
|
|
* NEVER mix toISOString().slice(0,10) (UTC date) with getHours() (local time).
|
|
|
|
|
* Both date and time use getFullYear/getMonth/getDate/getHours/getMinutes so
|
|
|
|
|
* the pair describes the same wall-clock consistently in the viewer's zone.
|
|
|
|
|
*/
|
|
|
|
|
function parseDateTime(iso: string): { date: string; time: string } {
|
|
|
|
|
try {
|
|
|
|
|
// Strip IANA bracket suffix e.g. '[America/Toronto]'
|
|
|
|
|
const clean = iso.replace(/\[[^\]]*\]$/, '')
|
|
|
|
|
if (/^\d{4}-\d{2}-\d{2}$/.test(iso)) {
|
|
|
|
|
// All-day date string
|
|
|
|
|
// All-day date string — use as-is (no time component)
|
|
|
|
|
return { date: iso, time: '09:00' }
|
|
|
|
|
}
|
|
|
|
|
const d = new Date(clean)
|
|
|
|
|
if (isNaN(d.getTime())) throw new Error('Invalid date')
|
|
|
|
|
const date = d.toISOString().slice(0, 10)
|
|
|
|
|
// WR-05: use ONLY local accessors so date and time are in the same zone frame.
|
|
|
|
|
// Do NOT use toISOString() here — it returns UTC, which can differ from local time.
|
|
|
|
|
const year = String(d.getFullYear())
|
|
|
|
|
const month = String(d.getMonth() + 1).padStart(2, '0')
|
|
|
|
|
const day = String(d.getDate()).padStart(2, '0')
|
|
|
|
|
const hours = String(d.getHours()).padStart(2, '0')
|
|
|
|
|
const mins = String(d.getMinutes()).padStart(2, '0')
|
|
|
|
|
return { date, time: `${hours}:${mins}` }
|
|
|
|
|
return { date: `${year}-${month}-${day}`, time: `${hours}:${mins}` }
|
|
|
|
|
} catch {
|
|
|
|
|
return { date: getDefaultStartDate(), time: '09:00' }
|
|
|
|
|
return { date: todayIso(), time: '09:00' }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -134,8 +138,8 @@ export function EventForm() {
|
|
|
|
|
|
|
|
|
|
// ── Form state ──────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const initStart = occurrence ? parseDateTime(occurrence.start) : { date: getDefaultStartDate(), time: '09:00' }
|
|
|
|
|
const initEnd = occurrence ? parseDateTime(occurrence.end) : { date: getDefaultEndDate(), time: '10:00' }
|
|
|
|
|
const initStart = occurrence ? parseDateTime(occurrence.start) : { date: todayIso(), time: '09:00' }
|
|
|
|
|
const initEnd = occurrence ? parseDateTime(occurrence.end) : { date: todayIso(), time: '10:00' }
|
|
|
|
|
|
|
|
|
|
const [title, setTitle] = useState(occurrence?.title ?? '')
|
|
|
|
|
const [allDay, setAllDay] = useState(occurrence?.allDay ?? false)
|
|
|
|
@@ -163,22 +167,34 @@ export function EventForm() {
|
|
|
|
|
}
|
|
|
|
|
}, [writableCalendars]) // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
|
|
|
|
|
|
// Reset form when opening (mode may change)
|
|
|
|
|
// Reset form when opening (mode may change) or when occurrence resolves in cache.
|
|
|
|
|
// WR-03: `occurrence` (via occurrence?.uid) is in the dep array so the effect
|
|
|
|
|
// re-runs when the occurrence arrives in the TanStack cache after form open.
|
|
|
|
|
// This prevents the "blank edit form" bug when the form opens before the cache
|
|
|
|
|
// has hydrated the occurrence for the requested UID.
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (eventFormOpen) {
|
|
|
|
|
const startParsed = occurrence ? parseDateTime(occurrence.start) : { date: getDefaultStartDate(), time: '09:00' }
|
|
|
|
|
const endParsed = occurrence ? parseDateTime(occurrence.end) : { date: getDefaultEndDate(), time: '10:00' }
|
|
|
|
|
const startParsed = occurrence ? parseDateTime(occurrence.start) : { date: todayIso(), time: '09:00' }
|
|
|
|
|
const endParsed = occurrence ? parseDateTime(occurrence.end) : { date: todayIso(), time: '10:00' }
|
|
|
|
|
setTitle(occurrence?.title ?? '')
|
|
|
|
|
setAllDay(occurrence?.allDay ?? false)
|
|
|
|
|
setStartDate(startParsed.date)
|
|
|
|
|
setStartTime(startParsed.time)
|
|
|
|
|
setEndDate(endParsed.date)
|
|
|
|
|
setEndTime(endParsed.time)
|
|
|
|
|
setRecurrence('none')
|
|
|
|
|
// WR-03 recurrence: derive from occurrence if present; default 'none' only when
|
|
|
|
|
// genuinely absent. Note: occurrence.recurrence is not in CalendarOccurrence type
|
|
|
|
|
// (the API expand contract does not expose it in v1 — D-03). We cast to any to
|
|
|
|
|
// read it if a future API version adds it, and default to 'none' when not present
|
|
|
|
|
// (WR-03 v1 comment: occurrence edits whose recurrence is not in the cache default
|
|
|
|
|
// to 'none'; this will be addressed when the occurrence/expand contract is extended).
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
const derivedRecurrence = (occurrence as any)?.recurrence as RecurrencePreset | undefined
|
|
|
|
|
setRecurrence(derivedRecurrence ?? 'none')
|
|
|
|
|
setLocation(occurrence?.location ?? '')
|
|
|
|
|
setDescription(occurrence?.description ?? '')
|
|
|
|
|
}
|
|
|
|
|
}, [eventFormOpen, eventFormMode, eventFormUid]) // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [eventFormOpen, eventFormMode, eventFormUid, occurrence?.uid]) // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
|
|
|
|
|
|
// ── Validation state ────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|