diff --git a/src/App.tsx b/src/App.tsx index d289c04..17ec7dc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3384,7 +3384,13 @@ function ArrowLeftGlyph() { function CalendarPage(props: { onOpenCard: (card: MediaCard, originRect?: ModalOriginRect | null) => void }) { - const [currentDate, setCurrentDate] = useState(new Date()); + const [timeZoneOverride, setTimeZoneOverride] = useState(readStoredCalendarTimeZone); + const detectedTimeZone = getBrowserTimeZone(); + const timeZone = timeZoneOverride || detectedTimeZone; + const [currentDate, setCurrentDate] = useState(() => { + const [year, month] = getDayKey(new Date(), timeZone).split('-').map(Number); + return new Date(year, month - 1, 1); + }); const [entries, setEntries] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); @@ -3393,9 +3399,11 @@ function CalendarPage(props: { const [monthTransition, setMonthTransition] = useState<'idle' | 'next' | 'prev'>('idle'); useEffect(() => { - const start = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1); - const end = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0); - + // Pad the month by a day on each side so it is complete in any time zone; + // entries are then filtered by their day in the selected zone. + const start = new Date(Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), 0)); + const end = new Date(Date.UTC(currentDate.getFullYear(), currentDate.getMonth() + 1, 2)); + let cancelled = false; setLoading(true); setError(''); @@ -3442,15 +3450,26 @@ function CalendarPage(props: { const now = new Date(); - const filteredEntries = entries.filter(entry => { - const entryDate = new Date(entry.startsAt); - return entryDate.getFullYear() === currentDate.getFullYear() && entryDate.getMonth() === currentDate.getMonth(); - }); + const monthKey = `${currentDate.getFullYear()}-${String(currentDate.getMonth() + 1).padStart(2, '0')}`; + const filteredEntries = entries.filter(entry => getCalendarEntryDayKey(entry, timeZone).startsWith(monthKey)); const nextUpEntry = filteredEntries.find(entry => new Date(entry.startsAt) >= now); const nextUpId = nextUpEntry?.id; - const groups = groupCalendarEntries(filteredEntries); + const groups = groupCalendarEntries(filteredEntries, timeZone); + + function changeTimeZone(nextTimeZone: string | null) { + setTimeZoneOverride(nextTimeZone); + try { + if (nextTimeZone) { + localStorage.setItem(CALENDAR_TIME_ZONE_STORAGE_KEY, nextTimeZone); + } else { + localStorage.removeItem(CALENDAR_TIME_ZONE_STORAGE_KEY); + } + } catch { + // Storage can be unavailable (private mode); the choice still applies until reload. + } + } useLayoutEffect(() => { if (nextUpRef.current && scrollContainerRef.current) { @@ -3489,6 +3508,11 @@ function CalendarPage(props: { +
@@ -3521,7 +3545,7 @@ function CalendarPage(props: {

{entry.detail}

- {formatCalendarTime(entry.startsAt)} + {isDateOnlyCalendarEntry(entry) ? null : {formatCalendarTime(entry.startsAt, timeZone)}}
))} @@ -3542,17 +3566,18 @@ function humanizeDownloadState(state: string) { .replace(/\b\w/g, (match) => match.toUpperCase()) } -function groupCalendarEntries(entries: CalendarEntry[]) { +function groupCalendarEntries(entries: CalendarEntry[], timeZone: string) { + // Day keys are already in the selected zone, so the label is formatted as UTC. const formatter = new Intl.DateTimeFormat('en-US', { weekday: 'long', month: 'long', day: 'numeric', + timeZone: 'UTC', }) const grouped = new Map() for (const entry of entries) { - const date = new Date(entry.startsAt) - const key = date.toISOString().slice(0, 10) + const key = getCalendarEntryDayKey(entry, timeZone) const current = grouped.get(key) if (current) { current.items.push(entry) @@ -3560,23 +3585,207 @@ function groupCalendarEntries(entries: CalendarEntry[]) { } grouped.set(key, { key, - label: formatter.format(date), + label: formatter.format(new Date(`${key}T12:00:00Z`)), items: [entry], }) } - return [...grouped.values()] + return [...grouped.values()].sort((left, right) => left.key.localeCompare(right.key)) } -function formatCalendarTime(startsAt: string) { +function formatCalendarTime(startsAt: string, timeZone: string) { const date = new Date(startsAt) return new Intl.DateTimeFormat('en-US', { hour: 'numeric', minute: '2-digit', hour12: false, + timeZone, }).format(date) } +const CALENDAR_TIME_ZONE_STORAGE_KEY = 'omniarr.calendar.timeZone' + +function getBrowserTimeZone() { + return Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC' +} + +function isValidTimeZone(timeZone: string) { + try { + new Intl.DateTimeFormat('en-US', { timeZone }) + return true + } catch { + return false + } +} + +function readStoredCalendarTimeZone(): string | null { + try { + const stored = localStorage.getItem(CALENDAR_TIME_ZONE_STORAGE_KEY) + return stored && isValidTimeZone(stored) ? stored : null + } catch { + return null + } +} + +function getSupportedTimeZones(detectedTimeZone: string) { + const zones = typeof Intl.supportedValuesOf === 'function' ? Intl.supportedValuesOf('timeZone') : [] + return zones.includes(detectedTimeZone) ? zones : [detectedTimeZone, ...zones] +} + +function getDayKey(date: Date, timeZone: string) { + // en-CA formats dates as YYYY-MM-DD. + return new Intl.DateTimeFormat('en-CA', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone }).format(date) +} + +// Radarr release dates and Sonarr's fallback airDate are calendar days, not +// instants, so they keep their day instead of shifting with the time zone. +function isDateOnlyCalendarEntry(entry: CalendarEntry) { + return entry.kind === 'movie' || !entry.startsAt.includes('T') +} + +function getCalendarEntryDayKey(entry: CalendarEntry, timeZone: string) { + return isDateOnlyCalendarEntry(entry) ? entry.startsAt.slice(0, 10) : getDayKey(new Date(entry.startsAt), timeZone) +} + +function formatTimeZoneCity(timeZone: string) { + return (timeZone.split('/').pop() || timeZone).replace(/_/g, ' ') +} + +function formatTimeZoneOffset(timeZone: string) { + const parts = new Intl.DateTimeFormat('en-US', { timeZone, timeZoneName: 'shortOffset' }).formatToParts(new Date()) + return parts.find((part) => part.type === 'timeZoneName')?.value || '' +} + +function CalendarTimeZonePicker(props: { + detectedTimeZone: string + override: string | null + onChange: (timeZone: string | null) => void +}) { + const [open, setOpen] = useState(false) + const [query, setQuery] = useState('') + const pickerRef = useRef(null) + const searchRef = useRef(null) + const timeZone = props.override || props.detectedTimeZone + + useEffect(() => { + if (!open) { + return + } + + // autoFocus would scroll the page to the menu; focus without moving it. + searchRef.current?.focus({ preventScroll: true }) + + function closeOnOutsidePress(event: PointerEvent) { + if (!pickerRef.current?.contains(event.target as Node)) { + setOpen(false) + } + } + + function closeOnEscape(event: KeyboardEvent) { + if (event.key === 'Escape') { + setOpen(false) + } + } + + window.addEventListener('pointerdown', closeOnOutsidePress) + window.addEventListener('keydown', closeOnEscape) + + return () => { + window.removeEventListener('pointerdown', closeOnOutsidePress) + window.removeEventListener('keydown', closeOnEscape) + } + }, [open]) + + const allZones = getSupportedTimeZones(props.detectedTimeZone) + const normalizedQuery = query.trim().toLowerCase().replace(/\s+/g, '_') + const zones = open ? allZones.filter((zone) => zone.toLowerCase().includes(normalizedQuery)) : [] + + function choose(nextTimeZone: string | null) { + props.onChange(nextTimeZone) + setOpen(false) + setQuery('') + } + + return ( +
+ {/* Phones get the native picker, like the season selector. */} + + + {open ? ( +
+ setQuery(event.target.value)} + placeholder="Search city or zone" + ref={searchRef} + value={query} + /> +
+ {normalizedQuery ? null : ( + + )} + {zones.map((zone) => ( + + ))} + {zones.length === 0 && normalizedQuery ?

No matching time zones

: null} +
+
+ ) : null} +
+ ) +} + +function GlobeGlyph() { + return ( + + ) +} + function AdministrationScreen(props: { config: AppConfig connections: ConnectionStatus[] diff --git a/src/index.css b/src/index.css index 67bfb6e..6cfe911 100644 --- a/src/index.css +++ b/src/index.css @@ -5391,6 +5391,181 @@ img { background-color: rgba(255, 255, 255, 0.1); } +.calendar-header { + position: relative; +} + +/* Spans the header's content box (below its top padding) so the picker + centres on the same line as the month and arrows. */ +.calendar-timezone { + position: absolute; + top: 0.4rem; + right: 0; + bottom: 0; + display: flex; + align-items: center; + z-index: 20; +} + +/* The trigger, menu and options follow the season selector in the title view. */ +.calendar-timezone-native { + display: none; +} + +.calendar-timezone-button { + display: inline-flex; + align-items: center; + gap: 0.45rem; + min-height: 2.1rem; + padding: 0; + border: 0; + background: transparent; + color: #d6d8de; + font-family: inherit; + font-size: 0.9rem; + font-weight: 600; + white-space: nowrap; + cursor: pointer; + outline: none; + transition: color 180ms ease; +} + +.calendar-timezone-button:hover, +.calendar-timezone-button:focus-visible, +.calendar-timezone-button[aria-expanded="true"] { + color: #ffffff; +} + +.calendar-timezone-button .title-season-select-chevron { + margin-left: 0.2rem; +} + +.calendar-timezone-button[aria-expanded="true"] .title-season-select-chevron { + transform: translateY(0.14rem) rotate(225deg); +} + +.calendar-timezone-icon { + width: 0.95rem; + height: 0.95rem; + flex: none; +} + +.calendar-timezone-offset { + color: #72809c; + font-weight: 600; +} + +.calendar-timezone-menu { + position: absolute; + z-index: 8; + top: calc(100% + 0.55rem); + right: 0; + display: grid; + gap: 0.55rem; + width: min(20rem, calc(100vw - 3rem)); + padding: 0.55rem; + border: 1px solid rgba(255, 255, 255, 0.12); + border-radius: 0.8rem; + background: rgba(18, 22, 31, 0.98); + box-shadow: 0 1rem 2.5rem rgba(0, 0, 0, 0.32); +} + +.calendar-timezone-search { + box-sizing: border-box; + padding: 0.62rem 0.85rem; + border-radius: 0.55rem; + font: inherit; + font-size: 0.9rem; + outline: none; +} + +.calendar-timezone-options { + display: grid; + gap: 0.38rem; + max-height: min(18rem, 50vh); + overflow-y: auto; +} + +.calendar-timezone-options button { + display: flex; + justify-content: space-between; + gap: 1rem; + width: 100%; + padding: 0.68rem 0.75rem; + border: 0; + border-radius: 0.55rem; + background: transparent; + color: #b7c1d6; + font: inherit; + font-size: 0.9rem; + font-weight: 600; + line-height: 1.25; + text-align: left; + cursor: pointer; + transition: background 150ms ease, color 150ms ease; +} + +.calendar-timezone-options button:hover, +.calendar-timezone-options button:focus-visible { + background: rgba(255, 255, 255, 0.1); + color: #ffffff; + outline: none; +} + +.calendar-timezone-options button.is-selected { + background: rgba(255, 255, 255, 0.14); + color: #ffffff; +} + +.calendar-timezone-empty { + margin: 0; + padding: 0.68rem 0.75rem; + color: #72809c; + font-size: 0.9rem; + font-weight: 600; +} + +/* On phones the picker mirrors the menu button: a square globe button in + the opposite corner, on the month row. An invisible native select on top + opens the phone's own picker, like the season selector. */ +@media (max-width: 980px) { + .calendar-timezone { + top: 0; + } + + .calendar-timezone-button { + justify-content: center; + width: 2.9rem; + height: 2.9rem; + border: 1px solid rgba(255, 255, 255, 0.16); + border-radius: 0.9rem; + background: rgba(10, 12, 16, 0.86); + box-shadow: 0 0.7rem 1.8rem rgba(0, 0, 0, 0.34); + pointer-events: none; + } + + .calendar-timezone-button > :not(.calendar-timezone-icon), + .calendar-timezone-menu { + display: none; + } + + .calendar-timezone-button .calendar-timezone-icon { + width: 1.2rem; + height: 1.2rem; + color: #f5f7fb; + } + + .calendar-timezone-native { + position: absolute; + inset: 0; + display: block; + width: 100%; + opacity: 0; + font-size: 1rem; + cursor: pointer; + } +} + .calendar-container { flex: 1; min-height: 0;