Skip to content

Commit

Permalink
refactor(RelativeTime): folded into DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdvl committed Jan 17, 2024
1 parent e76b83b commit 8e9393a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 34 deletions.
12 changes: 7 additions & 5 deletions dotcom-rendering/src/components/Card/components/CardAge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { textSans, until } from '@guardian/source-foundations';
import { palette } from '../../../palette';
import ClockIcon from '../../../static/icons/clock.svg';
import type { DCRContainerPalette } from '../../../types/front';
import { Island } from '../../Island';
import { RelativeTime } from '../../RelativeTime.importable';
import { DateTime } from '../../DateTime';

type Props = {
format: ArticleFormat;
Expand Down Expand Up @@ -77,9 +76,12 @@ export const CardAge = ({
css={ageStyles(format, containerPalette, isDynamo, isOnwardContent)}
>
{showClock && <ClockIcon />}
<Island priority="enhancement" defer={{ until: 'visible' }}>
<RelativeTime then={new Date(webPublicationDate).getTime()} />
</Island>
<DateTime
date={new Date(webPublicationDate)}
display="relative"
editionId="UK"
show="date"
/>
</span>
);
};
22 changes: 20 additions & 2 deletions dotcom-rendering/src/components/DateTime.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { isString, timeAgo } from '@guardian/libs';
import { type EditionId, getEditionFromId } from '../lib/edition';
import { Island } from './Island';
import { RelativeTime } from './RelativeTime.importable';

type Props = {
date: Date;
editionId: EditionId;
show?: 'date & time' | 'date' | 'time';
display?: 'absolute' | 'relative';
};

const formatDate = (date: Date, locale: string, timeZone: string) =>
Expand All @@ -28,9 +32,23 @@ const formatTime = (date: Date, locale: string, timeZone: string) =>
})
.replace(':', '.');

export const DateTime = ({ date, editionId, show = 'date & time' }: Props) => {
export const DateTime = ({
date,
editionId,
show = 'date & time',
display = 'absolute',
}: Props) => {
const { locale, timeZone } = getEditionFromId(editionId);
return (

const epoch = date.getTime();
const relativeTime = display === 'relative' && timeAgo(epoch);
const isRecent = isString(relativeTime) && relativeTime.endsWith(' ago');

return isRecent ? (
<Island priority="enhancement" defer={{ until: 'visible' }}>
<RelativeTime then={epoch} />
</Island>
) : (
<time
dateTime={date.toISOString()}
data-locale={locale}
Expand Down
15 changes: 7 additions & 8 deletions dotcom-rendering/src/components/FirstPublished.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { joinUrl } from '@guardian/libs';
import { palette, space, textSans } from '@guardian/source-foundations';
import { SvgPinned } from '@guardian/source-react-components';
import { palette as themePalette } from '../palette';
import { Island } from './Island';
import { RelativeTime } from './RelativeTime.importable';
import { DateTime } from './DateTime';

const fallbackDate = (date: Date) =>
[date.getHours(), date.getMinutes()]
Expand Down Expand Up @@ -62,12 +61,12 @@ const FirstPublished = ({
margin-right: ${space[2]}px;
`}
>
<Island
priority="enhancement"
defer={{ until: 'visible' }}
>
<RelativeTime then={firstPublished}></RelativeTime>
</Island>
<DateTime
date={new Date(firstPublished)}
display="relative"
editionId="UK"
show="date"
/>
</span>
)}
<span
Expand Down
11 changes: 6 additions & 5 deletions dotcom-rendering/src/components/KeyEventCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { css } from '@emotion/react';
import { from, space, textSans } from '@guardian/source-foundations';
import { Link } from '@guardian/source-react-components';
import { palette } from '../palette';
import { Island } from './Island';
import { RelativeTime } from './RelativeTime.importable';
import { DateTime } from './DateTime';

interface Props {
id: string;
Expand Down Expand Up @@ -120,9 +119,11 @@ export const KeyEventCard = ({
data-link-name={`key event card | ${cardPosition}`}
>
<div css={timeStyles}>
<Island priority="enhancement" defer={{ until: 'visible' }}>
<RelativeTime then={blockFirstPublished} />
</Island>
<DateTime
date={new Date(blockFirstPublished)}
display="relative"
editionId="UK"
/>
</div>
<div css={textStyles}>{title}</div>
</Link>
Expand Down
12 changes: 9 additions & 3 deletions dotcom-rendering/src/components/LatestLinks.importable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { palette as themePalette } from '../palette';
import type { DCRContainerPalette } from '../types/front';
import { WithLink } from './CardHeadline';
import { ContainerOverrides } from './ContainerOverrides';
import { RelativeTime } from './RelativeTime.importable';
import { DateTime } from './DateTime';

type Props = {
id: string;
Expand Down Expand Up @@ -163,8 +163,14 @@ export const LatestLinks = ({
),
}}
>
<RelativeTime
then={block.publishedDateTime}
<DateTime
date={
new Date(
block.publishedDateTime,
)
}
display="relative"
editionId="UK"
/>
</div>
<span className="show-underline">
Expand Down
19 changes: 8 additions & 11 deletions dotcom-rendering/src/components/PinnedPost.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { css } from '@emotion/react';
import { isUndefined } from '@guardian/libs';
import {
focusHalo,
from,
Expand All @@ -16,8 +17,7 @@ import {
} from '@guardian/source-react-components';
import { decidePalette } from '../lib/decidePalette';
import type { Palette } from '../types/palette';
import { Island } from './Island';
import { RelativeTime } from './RelativeTime.importable';
import { DateTime } from './DateTime';

const pinnedPostContainer = (palette: Palette) => css`
border: 3px solid ${palette.border.pinnedPost};
Expand Down Expand Up @@ -164,17 +164,14 @@ export const PinnedPost = ({ pinnedPost, children, format }: Props) => {
/>
<div css={rowStyles(palette)}>
<SvgPinned />
{pinnedPost.blockFirstPublished !== undefined && (
{!isUndefined(pinnedPost.blockFirstPublished) && (
<div css={timeAgoStyles}>
From{' '}
<Island
priority="enhancement"
defer={{ until: 'visible' }}
>
<RelativeTime
then={pinnedPost.blockFirstPublished}
/>
</Island>
<DateTime
date={new Date(pinnedPost.blockFirstPublished)}
display="relative"
editionId="UK"
/>
</div>
)}
</div>
Expand Down

0 comments on commit 8e9393a

Please sign in to comment.