Skip to content

Commit 0c56b8a

Browse files
authored
Merge pull request #14660 from guardian/jm/server-time
Get server time in enhance methods and prop drill to `DateTime`
2 parents abc6f19 + 7e8cc87 commit 0c56b8a

File tree

102 files changed

+528
-751
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+528
-751
lines changed

dotcom-rendering/src/components/ArticleBody.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Props = {
5454
lang?: string;
5555
isRightToLeftLang?: boolean;
5656
shouldHideAds: boolean;
57+
serverTime?: number;
5758
};
5859

5960
const globalOlStyles = () => css`
@@ -137,6 +138,7 @@ export const ArticleBody = ({
137138
isRightToLeftLang = false,
138139
editionId,
139140
shouldHideAds,
141+
serverTime,
140142
}: Props) => {
141143
const isInteractiveContent =
142144
format.design === ArticleDesign.Interactive ||
@@ -205,6 +207,7 @@ export const ArticleBody = ({
205207
keywordIds={keywordIds}
206208
editionId={editionId}
207209
shouldHideAds={shouldHideAds}
210+
serverTime={serverTime}
208211
/>
209212
</div>
210213
);

dotcom-rendering/src/components/Card/Card.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const basicCardProps: CardProps = {
4747
imageLoading: 'eager',
4848
discussionApiUrl: 'https://discussion.theguardian.com/discussion-api/',
4949
showVideo: true,
50-
absoluteServerTimes: true,
5150
headlinePosition: 'inner',
5251
};
5352

dotcom-rendering/src/components/Card/Card.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export type Position = 'inner' | 'outer' | 'none';
7979
export type Props = {
8080
linkTo: string;
8181
format: ArticleFormat;
82-
absoluteServerTimes: boolean;
82+
serverTime?: number;
8383
headlineText: string;
8484
headlineSizes?: ResponsiveFontSize;
8585
showQuotedHeadline?: boolean;
@@ -388,7 +388,7 @@ export const Card = ({
388388
liveUpdatesPosition = 'inner',
389389
onwardsSource,
390390
showVideo = true,
391-
absoluteServerTimes,
391+
serverTime,
392392
isTagPage = false,
393393
aspectRatio,
394394
index = 0,
@@ -450,7 +450,7 @@ export const Card = ({
450450
isWithinTwelveHours: withinTwelveHours,
451451
}}
452452
showClock={showClock}
453-
absoluteServerTimes={absoluteServerTimes}
453+
serverTime={serverTime}
454454
isTagPage={isTagPage}
455455
/>
456456
);
@@ -1261,9 +1261,7 @@ export const Card = ({
12611261
: supportingContentAlignment
12621262
}
12631263
containerPalette={containerPalette}
1264-
absoluteServerTimes={
1265-
absoluteServerTimes
1266-
}
1264+
serverTime={serverTime}
12671265
displayHeader={isFlexibleContainer}
12681266
directionOnMobile={
12691267
isFlexibleContainer
@@ -1318,7 +1316,7 @@ export const Card = ({
13181316
: supportingContentAlignment
13191317
}
13201318
containerPalette={containerPalette}
1321-
absoluteServerTimes={absoluteServerTimes}
1319+
serverTime={serverTime}
13221320
displayHeader={isFlexibleContainer}
13231321
directionOnMobile={'horizontal'}
13241322
></LatestLinks>

dotcom-rendering/src/components/Card/components/CardAge.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const ageStyles = (colour: string) => {
2222
};
2323

2424
type Props = {
25-
absoluteServerTimes: boolean;
25+
serverTime?: number;
2626
webPublication: {
2727
date: string;
2828
isWithinTwelveHours: boolean;
@@ -33,7 +33,7 @@ type Props = {
3333
};
3434

3535
export const CardAge = ({
36-
absoluteServerTimes,
36+
serverTime,
3737
webPublication,
3838
isTagPage,
3939
showClock,
@@ -58,7 +58,7 @@ export const CardAge = ({
5858
<DateTime
5959
date={new Date(webPublication.date)}
6060
display={'relative'}
61-
absoluteServerTimes={absoluteServerTimes}
61+
serverTime={serverTime}
6262
showWeekday={false}
6363
showDate={true}
6464
showTime={false}

dotcom-rendering/src/components/Carousel.importable.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Props = {
4343
onwardsSource: OnwardsSource;
4444
leftColSize: LeftColSize;
4545
discussionApiUrl: string;
46-
absoluteServerTimes: boolean;
46+
serverTime?: number;
4747
renderingTarget: RenderingTarget;
4848
};
4949

@@ -449,7 +449,7 @@ type CarouselCardProps = {
449449
linkTo: string;
450450
headlineText: string;
451451
webPublicationDate: string;
452-
absoluteServerTimes: boolean;
452+
serverTime?: number;
453453
imageLoading: Loading;
454454
kickerText?: string;
455455
image?: DCRFrontImage;
@@ -483,7 +483,7 @@ const CarouselCard = ({
483483
imageLoading,
484484
discussionApiUrl,
485485
isOnwardContent,
486-
absoluteServerTimes,
486+
serverTime,
487487
starRating,
488488
index,
489489
}: CarouselCardProps) => {
@@ -527,7 +527,7 @@ const CarouselCard = ({
527527
isOnwardContent={isOnwardContent}
528528
mediaPositionOnDesktop={cardImagePosition}
529529
mediaPositionOnMobile={cardImagePosition}
530-
absoluteServerTimes={absoluteServerTimes}
530+
serverTime={serverTime}
531531
starRating={starRating}
532532
index={index}
533533
showTopBarDesktop={!isOnwardContent}
@@ -751,7 +751,7 @@ export const Carousel = ({
751751
leftColSize,
752752
discussionApiUrl,
753753
isOnwardContent = true,
754-
absoluteServerTimes,
754+
serverTime,
755755
renderingTarget,
756756
...props
757757
}: ArticleProps | FrontProps) => {
@@ -973,7 +973,7 @@ export const Carousel = ({
973973
linkTo={linkTo}
974974
headlineText={headlineText}
975975
webPublicationDate={webPublicationDate}
976-
absoluteServerTimes={absoluteServerTimes}
976+
serverTime={serverTime}
977977
image={image}
978978
kickerText={kickerText}
979979
dataLinkName={`carousel-small-card-position-${i}`}

dotcom-rendering/src/components/Carousel.stories.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ export const Headlines: StoryObj = ({ format }: StoryProps) => {
269269
format={format}
270270
leftColSize="compact"
271271
discussionApiUrl={discussionApiUrl}
272-
absoluteServerTimes={true}
273272
renderingTarget="Web"
274273
/>
275274
</Section>
@@ -295,7 +294,6 @@ export const SingleItemCarousel = () => {
295294
format={defaultFormat}
296295
leftColSize="compact"
297296
discussionApiUrl={discussionApiUrl}
298-
absoluteServerTimes={true}
299297
renderingTarget="Web"
300298
/>
301299
</Section>
@@ -350,7 +348,6 @@ export const SingleOpinionCarousel = () => {
350348
}}
351349
leftColSize="compact"
352350
discussionApiUrl={discussionApiUrl}
353-
absoluteServerTimes={true}
354351
renderingTarget="Web"
355352
/>
356353
</Section>
@@ -381,7 +378,6 @@ export const Immersive = () => {
381378
}}
382379
leftColSize="compact"
383380
discussionApiUrl={discussionApiUrl}
384-
absoluteServerTimes={true}
385381
renderingTarget="Web"
386382
/>
387383
</Section>
@@ -427,7 +423,6 @@ export const SpecialReportAlt = () => {
427423
format={specialReportAltFormat}
428424
leftColSize="compact"
429425
discussionApiUrl={discussionApiUrl}
430-
absoluteServerTimes={true}
431426
renderingTarget="Web"
432427
/>
433428
</Section>
@@ -643,7 +638,6 @@ export const AllCards = () => {
643638
leftColSize="compact"
644639
discussionApiUrl={discussionApiUrl}
645640
format={defaultFormat}
646-
absoluteServerTimes={true}
647641
renderingTarget="Web"
648642
/>
649643
</Section>

dotcom-rendering/src/components/DateTime.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isString } from '@guardian/libs';
1+
import { isString, isUndefined } from '@guardian/libs';
22
import { getEditionFromId } from '../lib/edition';
33
import { useConfig } from './ConfigContext';
44
import { Island } from './Island';
@@ -14,11 +14,11 @@ type Props = {
1414
type DisplayProps =
1515
| {
1616
display?: 'absolute';
17-
absoluteServerTimes?: never;
17+
serverTime?: never;
1818
}
1919
| {
2020
display: 'relative';
21-
absoluteServerTimes: boolean;
21+
serverTime?: number;
2222
};
2323

2424
const formatWeekday = (date: Date, locale: string, timeZone: string) =>
@@ -49,31 +49,35 @@ const formatTime = (date: Date, locale: string, timeZone: string) =>
4949
.replace(':', '.');
5050

5151
const ONE_MINUTE = 60_000;
52-
/** https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date */
5352
const MAX_DATE = 8.64e15;
54-
/** Rounded down to the previous minute, to ensure relative times rarely go backwards */
55-
const getServerTime = () => Math.floor(Date.now() / ONE_MINUTE) * ONE_MINUTE;
5653

5754
export const DateTime = ({
5855
date,
5956
showWeekday,
6057
showDate,
6158
showTime,
6259
display = 'absolute',
63-
absoluteServerTimes = true,
60+
serverTime,
6461
}: Props & DisplayProps) => {
6562
const { editionId } = useConfig();
6663
const { dateLocale, timeZone } = getEditionFromId(editionId);
6764

65+
/**
66+
* Server time (if set) is rounded down to the previous minute to ensure
67+
* relative times rarely go backwards. If unset we use the maximum value
68+
* that can be represented by a `Date` object [1] to force display as an
69+
* absolute date due to `then` being far in the past.
70+
*
71+
* [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date
72+
*/
73+
const now = isUndefined(serverTime)
74+
? MAX_DATE
75+
: Math.floor(serverTime / ONE_MINUTE) * ONE_MINUTE;
6876
const then = date.getTime();
6977

7078
return display === 'relative' ? (
7179
<Island priority="enhancement" defer={{ until: 'visible' }}>
72-
<RelativeTime
73-
then={then}
74-
now={absoluteServerTimes ? MAX_DATE : getServerTime()}
75-
editionId={editionId}
76-
/>
80+
<RelativeTime then={then} now={now} editionId={editionId} />
7781
</Island>
7882
) : (
7983
<time

0 commit comments

Comments
 (0)