Skip to content

Commit

Permalink
Fold RelativeTime into DateTime (#10178)
Browse files Browse the repository at this point in the history
* refactor(RelativeTime): folded into DateTime

* fix(DateTime): allow not showing weekday

* fix: remove time from KeyEventCard

* fix: remove time from PinnedPost
  • Loading branch information
mxdvl authored Jan 18, 2024
1 parent 268a2a4 commit d897763
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 47 deletions.
14 changes: 9 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,14 @@ 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"
showWeekday={false}
showDate={true}
showTime={false}
/>
</span>
);
};
8 changes: 7 additions & 1 deletion dotcom-rendering/src/components/CricketScoreboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ export const CricketScoreboard = ({
return (
<div css={containerStyle}>
<h2 css={screenReaderOnlyStyle}>
<DateTime date={date} editionId={editionId} show="date" />
<DateTime
date={date}
editionId={editionId}
showWeekday={false}
showDate={true}
showTime={false}
/>
{match.competitionName}, {match.venueName}
</h2>
<table css={tableStyle}>
Expand Down
58 changes: 52 additions & 6 deletions dotcom-rendering/src/components/DateTime.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,73 @@ export const UK: Story = {
};

export const TimeOnly: Story = {
args: { date, editionId: 'UK', show: 'time' },
args: {
date,
editionId: 'UK',
showWeekday: false,
showDate: false,
showTime: true,
},
};

export const DateOnly: Story = {
args: { date, editionId: 'UK', show: 'date' },
args: {
date,
editionId: 'UK',
showWeekday: false,
showDate: true,
showTime: false,
},
};

export const WeekdayDateOnly: Story = {
args: {
date,
editionId: 'UK',
showWeekday: true,
showDate: true,
showTime: false,
},
};

export const US: Story = {
args: { date, editionId: 'US' },
args: {
date,
editionId: 'US',
showWeekday: true,
showDate: true,
showTime: true,
},
};

export const AU: Story = {
args: { date, editionId: 'AU' },
args: {
date,
editionId: 'AU',
showWeekday: true,
showDate: true,
showTime: true,
},
};

export const EUR: Story = {
args: { date, editionId: 'EUR' },
args: {
date,
editionId: 'EUR',
showWeekday: true,
showDate: true,
showTime: true,
},
};

export const INT: Story = {
args: { date, editionId: 'INT' },
args: {
date,
editionId: 'INT',
showWeekday: true,
showDate: true,
showTime: true,
},
};

export default meta;
44 changes: 38 additions & 6 deletions dotcom-rendering/src/components/DateTime.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
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';
showWeekday: boolean;
showDate: boolean;
showTime: boolean;
display?: 'absolute' | 'relative';
};

const formatWeekday = (date: Date, locale: string, timeZone: string) =>
date.toLocaleDateString(locale, {
weekday: 'short',
timeZone,
});

const formatDate = (date: Date, locale: string, timeZone: string) =>
date
.toLocaleDateString(locale, {
weekday: 'short',
day: 'numeric',
month: 'short',
year: 'numeric',
Expand All @@ -28,9 +39,25 @@ const formatTime = (date: Date, locale: string, timeZone: string) =>
})
.replace(':', '.');

export const DateTime = ({ date, editionId, show = 'date & time' }: Props) => {
export const DateTime = ({
date,
editionId,
showWeekday,
showDate,
showTime,
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 All @@ -45,8 +72,13 @@ export const DateTime = ({ date, editionId, show = 'date & time' }: Props) => {
timeZone,
})}
>
{show.includes('date') && formatDate(date, locale, timeZone)}{' '}
{show.includes('time') && formatTime(date, locale, timeZone)}
{[
showWeekday && formatWeekday(date, locale, timeZone),
showDate && formatDate(date, locale, timeZone),
showTime && formatTime(date, locale, timeZone),
]
.filter(isString)
.join(' ')}
</time>
);
};
17 changes: 9 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,14 @@ 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"
showWeekday={false}
showDate={true}
showTime={false}
/>
</span>
)}
<span
Expand Down
14 changes: 9 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,14 @@ 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"
showWeekday={false}
showDate={true}
showTime={false}
/>
</div>
<div css={textStyles}>{title}</div>
</Link>
Expand Down
4 changes: 3 additions & 1 deletion dotcom-rendering/src/components/LastUpdated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const LastUpdated = ({
<DateTime
date={new Date(lastUpdated)}
editionId={editionId}
show="time"
showWeekday={false}
showDate={false}
showTime={true}
/>
</div>
);
Expand Down
15 changes: 12 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,17 @@ export const LatestLinks = ({
),
}}
>
<RelativeTime
then={block.publishedDateTime}
<DateTime
date={
new Date(
block.publishedDateTime,
)
}
display="relative"
editionId="UK"
showWeekday={false}
showDate={true}
showTime={false}
/>
</div>
<span className="show-underline">
Expand Down
22 changes: 11 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,17 @@ 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"
showWeekday={false}
showDate={true}
showTime={false}
/>
</div>
)}
</div>
Expand Down
4 changes: 3 additions & 1 deletion dotcom-rendering/src/components/WitnessBlockComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ const WitnessWrapper = ({
<DateTime
date={new Date(dateCreated)}
editionId={editionId}
show="date"
showWeekday={false}
showDate={true}
showTime={false}
/>
</p>
</div>
Expand Down

0 comments on commit d897763

Please sign in to comment.