File size: 1,222 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Moment } from 'moment';
import { useMemo } from 'react';
import { applySiteOffset } from 'calypso/lib/site/timezone';
import { useSelector } from 'calypso/state';
import getSiteGmtOffset from 'calypso/state/selectors/get-site-gmt-offset';
import getSiteTimezoneValue from 'calypso/state/selectors/get-site-timezone-value';
import getSelectedSiteId from 'calypso/state/ui/selectors/get-selected-site-id';

// Tolerates null settings values, unlike the implementation in `calypso/components/site-offset`;
// I don't want to disturb existing behavior, but we may want to come back later
// and DRY up this bit of code.
export default function useDateWithOffset(
	date: Moment | undefined | null,
	{ keepLocalTime = false } = {}
): Moment | undefined {
	const siteId = useSelector( getSelectedSiteId );

	const timezone = useSelector( ( state ) =>
		siteId ? getSiteTimezoneValue( state, siteId ) : null
	);
	const gmtOffset = useSelector( ( state ) =>
		siteId ? getSiteGmtOffset( state, siteId ) : null
	);

	const dateWithOffset = useMemo(
		() => ( date ? applySiteOffset( date, { timezone, gmtOffset, keepLocalTime } ) : undefined ),
		[ date, timezone, gmtOffset, keepLocalTime ]
	);

	return dateWithOffset;
}