Skip to content

Commit 07653d5

Browse files
authored
feat: make time a client component to use users timezone (#74)
1 parent 175e3be commit 07653d5

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

src/components/DateTime.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use client';
2+
3+
import {INTL_LOCALE} from '@/lib/utils';
4+
5+
type DateTimeProps = {
6+
options?: Intl.DateTimeFormatOptions;
7+
timestamp: number;
8+
type?: 'date' | 'datetime';
9+
};
10+
11+
export function DateTime({
12+
options,
13+
timestamp,
14+
type = 'datetime',
15+
}: DateTimeProps) {
16+
const date = new Date(timestamp);
17+
18+
const formatted =
19+
type === 'date'
20+
? date.toLocaleDateString(INTL_LOCALE, options)
21+
: date.toLocaleString(INTL_LOCALE, options);
22+
23+
return <time dateTime={date.toISOString()}>{formatted}</time>;
24+
}

src/components/PackageDetails.tsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ import {
1313
} from '@/components/ui/card';
1414
import {fetchPkgbuilds, PkgbuildMap} from '@/lib/github';
1515
import {BriefPackageList, PackageDetails, PackageRepo} from '@/lib/types';
16-
import {
17-
getDownloadMirrorUrl,
18-
getPkgverWithoutBuildnum,
19-
INTL_LOCALE,
20-
} from '@/lib/utils';
16+
import {getDownloadMirrorUrl, getPkgverWithoutBuildnum} from '@/lib/utils';
2117

18+
import {DateTime} from './DateTime';
2219
import {HoverPrefetchLink} from './HoverPrefetchLink';
2320

2421
type PackageDetailsComponentProps = {
@@ -93,14 +90,10 @@ export default async function PackageDetailsComponent({
9390
</DetailRow>
9491
<DetailRow label="Build Date">
9592
{pkg.pkg_builddate ? (
96-
<time
97-
dateTime={new Date(pkg.pkg_builddate * 1000).toISOString()}
98-
suppressHydrationWarning
99-
>
100-
{new Date(pkg.pkg_builddate * 1000).toLocaleString(
101-
INTL_LOCALE
102-
)}
103-
</time>
93+
<DateTime
94+
timestamp={pkg.pkg_builddate * 1000}
95+
type="datetime"
96+
/>
10497
) : (
10598
'unknown'
10699
)}

src/components/PackageTable.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
TableRow,
1818
} from '@/components/ui/table';
1919
import {BriefPackage, BriefPackageList} from '@/lib/types';
20-
import {INTL_LOCALE} from '@/lib/utils';
20+
21+
import {DateTime} from './DateTime';
2122

2223
interface PackageSearchResultsTableProps {
2324
onArchitectureClick?: (arch: string) => void;
@@ -91,15 +92,9 @@ export default function PackageTable({
9192
},
9293
}),
9394
columnHelper.accessor('pkg_builddate', {
94-
cell: ({getValue}) => {
95-
const buildDate = getValue();
96-
const date = new Date(buildDate * 1000);
97-
return (
98-
<time dateTime={date.toISOString()} suppressHydrationWarning>
99-
{date.toLocaleDateString(INTL_LOCALE)}
100-
</time>
101-
);
102-
},
95+
cell: ({getValue}) => (
96+
<DateTime timestamp={getValue() * 1000} type="date" />
97+
),
10398
header: 'Last Updated',
10499
meta: {
105100
headerClassName: 'md:w-[200px]',

0 commit comments

Comments
 (0)