Skip to content

Commit

Permalink
Make calendar max dates sensitive to site timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
apata committed Nov 1, 2024
1 parent 2624d70 commit b5f462e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
16 changes: 9 additions & 7 deletions assets/js/dashboard/datepicker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @format */
import React, { useState, useEffect, useRef, useCallback, useMemo } from 'react'
import { formatDateRange, formatISO } from './util/date'
import { formatDateRange, formatISO, nowForSite } from './util/date'
import {
shiftQueryPeriod,
getDateForShiftedPeriod,
Expand Down Expand Up @@ -317,14 +317,16 @@ export default function QueryPeriodPicker() {
() => getCompareLinkItem({ site, query }),
[site, query]
)
const groups = useMemo(() => {

const datePeriodGroups = useMemo(() => {
const groups = getDatePeriodGroups(site)
// add Custom Range link to the last group
groups[groups.length - 1].push(customRangeLink)

if (COMPARISON_DISABLED_PERIODS.includes(query.period)) {
return groups
}
// maybe ass Compare link as another group to the very end
// maybe add Compare link as another group to the very end
return groups.concat([[compareLink]])
}, [site, query, customRangeLink, compareLink])

Expand Down Expand Up @@ -357,15 +359,15 @@ export default function QueryPeriodPicker() {
}}
>
{menuVisible === 'datemenu' && (
<QueryPeriodsMenu groups={groups} closeMenu={closeMenu} />
<QueryPeriodsMenu groups={datePeriodGroups} closeMenu={closeMenu} />
)}
{menuVisible === 'datemenu-calendar' && (
<DateRangeCalendar
onCloseWithSelection={(selection) =>
navigate({ search: getSearchToApplyCustomDates(selection) })
}
minDate={site.statsBegin}
maxDate={new Date().toISOString()}
maxDate={formatISO(nowForSite(site))}
defaultDates={
query.to && query.from
? [formatISO(query.from), formatISO(query.to)]
Expand Down Expand Up @@ -407,7 +409,7 @@ export default function QueryPeriodPicker() {
})
}
minDate={site.statsBegin}
maxDate={new Date().toISOString()}
maxDate={formatISO(nowForSite(site))}
defaultDates={
query.compare_from && query.compare_to
? [
Expand All @@ -425,7 +427,7 @@ export default function QueryPeriodPicker() {
<>
<ArrowKeybind keyboardKey="ArrowLeft" />
<ArrowKeybind keyboardKey="ArrowRight" />
{groups
{datePeriodGroups
.concat([[last6MonthsLinkItem]])
.flatMap((group) =>
group
Expand Down
7 changes: 6 additions & 1 deletion assets/js/dashboard/query-time-periods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,12 @@ export const getDatePeriodGroups = (
export const last6MonthsLinkItem: LinkItem = [
['Last 6 months', 'S'],
{
search: (s) => ({ ...s, period: QueryPeriod['6mo'], keybindHint: 'S' }),
search: (s) => ({
...s,
...clearedDateSearch,
period: QueryPeriod['6mo'],
keybindHint: 'S'
}),
isActive: ({ query }) => query.period === QueryPeriod['6mo']
}
]
Expand Down
15 changes: 15 additions & 0 deletions assets/js/dashboard/util/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import { formatISO, nowForSite, shiftMonths, yesterday } from './date'

jest.useFakeTimers()

describe(`${nowForSite.name} and ${formatISO.name}`, () => {
/* prettier-ignore */
const cases = [
[ 'Los Angeles/America', -3600 * 6, '2024-11-01T20:00:00.000Z', '2024-11-01' ],
[ 'Sydney/Australia', 3600 * 6, '2024-11-01T20:00:00.000Z', '2024-11-02' ]
]
test.each(cases)(
'in timezone of %s (offset %p) at %s, today is %s',
(_tz, offset, utcTime, expectedToday) => {
jest.setSystemTime(new Date(utcTime))
expect(formatISO(nowForSite({ offset }))).toEqual(expectedToday)
}
)
})

/* prettier-ignore */
const dstChangeOverDayEstonia = [
// system time today yesterday today-2mo today+2mo today-12mo offset
Expand Down

0 comments on commit b5f462e

Please sign in to comment.