Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adjust TimeGutter for DST #2205

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions src/TimeGutter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import React, { useState, useEffect } from 'react'
import React, { useState, useEffect, useCallback, useMemo } from 'react'
import clsx from 'clsx'
import PropTypes from 'prop-types'

import * as TimeSlotUtils from './utils/TimeSlots'
import { getSlotMetrics } from './utils/TimeSlots'
import TimeSlotGroup from './TimeSlotGroup'

/**
* Since the TimeGutter only displays the 'times' of slots in a day, and is separate
* from the Day Columns themselves, we check to see if the range contains an offset difference
* and, if so, change the beginning and end 'date' by a day to properly display the slots times
* used.
*/
function adjustForDST({ min, max, localizer }) {
if (localizer.getTimezoneOffset(min) !== localizer.getTimezoneOffset(max)) {
return {
start: localizer.add(min, -1, 'day'),
end: localizer.add(max, -1, 'day'),
}
}
return { start: min, end: max }
}

const TimeGutter = ({
min,
max,
Expand All @@ -17,10 +33,15 @@ const TimeGutter = ({
getters,
gutterRef,
}) => {
const { start, end } = useMemo(
() => adjustForDST({ min, max, localizer }),
// eslint-disable-next-line react-hooks/exhaustive-deps
[min?.toISOString(), max?.toISOString(), localizer]
)
const [slotMetrics, setSlotMetrics] = useState(
TimeSlotUtils.getSlotMetrics({
min,
max,
getSlotMetrics({
min: start,
max: end,
timeslots,
step,
localizer,
Expand All @@ -31,8 +52,8 @@ const TimeGutter = ({
if (slotMetrics) {
setSlotMetrics(
slotMetrics.update({
min,
max,
min: start,
max: end,
timeslots,
step,
localizer,
Expand All @@ -43,18 +64,21 @@ const TimeGutter = ({
* We don't want this to fire when slotMetrics is updated as it would recursively bomb
*/
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [min, max, timeslots, step])
}, [start?.toISOString(), end?.toISOString(), timeslots, step])

const renderSlot = (value, idx) => {
if (idx) return null // don't return the first (0) idx
const renderSlot = useCallback(
(value, idx) => {
if (idx) return null // don't return the first (0) idx

const isNow = slotMetrics.dateIsInGroup(getNow(), idx)
return (
<span className={clsx('rbc-label', isNow && 'rbc-now')}>
{localizer.format(value, 'timeGutterFormat')}
</span>
)
}
const isNow = slotMetrics.dateIsInGroup(getNow(), idx)
return (
<span className={clsx('rbc-label', isNow && 'rbc-now')}>
{localizer.format(value, 'timeGutterFormat')}
</span>
)
},
[slotMetrics, localizer, getNow]
)

return (
<div className="rbc-time-gutter rbc-time-column" ref={gutterRef}>
Expand Down