Skip to content

Commit efad61a

Browse files
refracto: using intl api
1 parent 2cff8de commit efad61a

File tree

13 files changed

+50
-38
lines changed

13 files changed

+50
-38
lines changed

Diff for: frontend/deno.json

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@std/front-matter": "jsr:@std/front-matter@1",
2727
"@std/semver": "jsr:@std/semver@1",
2828

29-
"@augustinmauroy/twas": "jsr:@augustinmauroy/twas@^1.0.0",
3029
"$imagescript": "https://deno.land/x/[email protected]/mod.ts",
3130

3231
"@deno/gfm": "jsr:@deno/[email protected]",

Diff for: frontend/deno.lock

+1-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: frontend/islands/admin/ScopeEdit.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
22
import type { FullScope } from "../../utils/api_types.ts";
33
import { useState } from "preact/hooks";
4-
import twas from "twas";
4+
import { timeAgo } from "../../utils/timeAgo.ts";
55
import { api, path } from "../../utils/api.ts";
66
import { TableData, TableRow } from "../../components/Table.tsx";
77

@@ -71,7 +71,7 @@ export default function AdminScopeEdit({ scope }: { scope: FullScope }) {
7171
: publishAttemptsPerWeekLimit}
7272
</TableData>
7373
<TableData title={new Date(scope.createdAt).toISOString().slice(0, 10)}>
74-
{twas(new Date(scope.createdAt).getTime())}
74+
{timeAgo(new Date(scope.createdAt))}
7575
</TableData>
7676
<TableData align="right">
7777
{edit

Diff for: frontend/islands/admin/UserEdit.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
22
import { useState } from "preact/hooks";
3-
import twas from "twas";
3+
import { timeAgo } from "../../utils/timeAgo.ts";
44
import { FullUser } from "../../utils/api_types.ts";
55
import { api, path } from "../../utils/api.ts";
66
import { TableData, TableRow } from "../../components/Table.tsx";
@@ -68,7 +68,7 @@ export default function UserEdit({ user }: { user: FullUser }) {
6868
: String(isBlocked)}
6969
</TableData>
7070
<TableData title={new Date(user.createdAt).toISOString().slice(0, 10)}>
71-
{twas(new Date(user.createdAt).getTime())}
71+
{timeAgo(new Date(user.createdAt).getTime())}
7272
</TableData>
7373
<TableData class="relative whitespace-nowrap space-x-3 py-4 pl-3 pr-4 text-right text-sm font-semibold sm:pr-6">
7474
{edit

Diff for: frontend/islands/new.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { Package, Scope } from "../utils/api_types.ts";
99
import { api, path } from "../utils/api.ts";
1010
import { ComponentChildren } from "preact";
11-
import twas from "twas";
11+
import { timeAgo } from "../utils/timeAgo.ts";
1212

1313
interface IconColorProps {
1414
done: Signal<unknown>;
@@ -419,7 +419,7 @@ export function CreatePackage({ scope, name, pkg, fromCli }: {
419419
</p>
420420
<p>{pkg.value.description || <i>No description</i>}</p>
421421
<p class="text-jsr-gray-500">
422-
Created {twas(new Date(pkg.value.createdAt).getTime())}.
422+
Created {timeAgo(pkg.value.createdAt)}.
423423
</p>
424424
{fromCli && (
425425
<p class="mt-2 text-jsr-gray-500">

Diff for: frontend/routes/account/(_components)/AccountLayout.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
22
import { ComponentChildren } from "preact";
3-
import twas from "twas";
3+
import { timeAgo } from "../../../utils/timeAgo.ts";
44
import { AccountNav, AccountNavTab } from "./AccountNav.tsx";
55
import { FullUser, User } from "../../../utils/api_types.ts";
66
import { GitHubUserLink } from "../../../islands/GithubUserLink.tsx";
@@ -25,7 +25,7 @@ export function AccountLayout({ user, active, children }: AccountLayoutProps) {
2525
{user.name}
2626
</h1>
2727
<p class="text-xs text-jsr-gray-600">
28-
Created account {twas(new Date(user.createdAt).getTime())}
28+
Created account {timeAgo(new Date(user.createdAt).getTime())}
2929
</p>
3030
<p class="text-base mt-2">
3131
<GitHubUserLink user={user} />

Diff for: frontend/routes/account/tokens/index.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { define } from "../../../util.ts";
55
import { path } from "../../../utils/api.ts";
66
import { Token } from "../../../utils/api_types.ts";
77
import { AccountLayout } from "../(_components)/AccountLayout.tsx";
8-
import twas from "twas";
8+
import { timeAgo } from "../../../utils/timeAgo.ts";
99
import { RevokeToken } from "./(_islands)/RevokeToken.tsx";
1010
import TbPlus from "@preact-icons/tb/TbPlus";
1111

@@ -105,7 +105,7 @@ function PersonalTokenRow({ token }: { token: Token }) {
105105
<b>Active</b> {expiresAt === null
106106
? "forever"
107107
: `– expires ${
108-
twas(new Date().getTime(), expiresAt.getTime()).replace(
108+
timeAgo(new Date().getTime(), expiresAt.getTime()).replace(
109109
"ago",
110110
"from now",
111111
)
@@ -114,12 +114,12 @@ function PersonalTokenRow({ token }: { token: Token }) {
114114
)
115115
: (
116116
<span class="text-red-600">
117-
<b>Inactive</b> - expired {twas(expiresAt.getTime())}
117+
<b>Inactive</b> - expired {timeAgo(expiresAt.getTime())}
118118
</span>
119119
)}
120120
</p>
121121
<p class="text-sm sm:text-right">
122-
Created {twas(new Date(token.createdAt).getTime())}
122+
Created {timeAgo(new Date(token.createdAt).getTime())}
123123
</p>
124124
</div>
125125
<p class="text-sm text-jsr-gray-600">
@@ -160,7 +160,7 @@ function SessionRow({ token }: { token: Token }) {
160160
<b>Active</b> {expiresAt === null
161161
? "forever"
162162
: `– expires ${
163-
twas(new Date().getTime(), expiresAt.getTime()).replace(
163+
timeAgo(expiresAt.getTime()).replace(
164164
"ago",
165165
"from now",
166166
)
@@ -169,7 +169,7 @@ function SessionRow({ token }: { token: Token }) {
169169
)
170170
: (
171171
<span class="text-red-600">
172-
<b>Inactive</b> - expired {twas(expiresAt.getTime())}
172+
<b>Inactive</b> - expired {timeAgo(expiresAt.getTime())}
173173
</span>
174174
)}
175175

@@ -178,7 +178,7 @@ function SessionRow({ token }: { token: Token }) {
178178
</div>
179179
<div>
180180
<p class="text-sm sm:text-right">
181-
Created {twas(new Date(token.createdAt).getTime())}
181+
Created {timeAgo(new Date(token.createdAt).getTime())}
182182
</p>
183183
</div>
184184
</div>

Diff for: frontend/routes/admin/publishingTasks.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AdminNav } from "./(_components)/AdminNav.tsx";
55
import { path } from "../../utils/api.ts";
66
import { List, PublishingTask } from "../../utils/api_types.ts";
77
import { URLQuerySearch } from "../../components/URLQuerySearch.tsx";
8-
import twas from "twas";
8+
import { timeAgo } from "../../utils/timeAgo.ts";
99
import PublishingTaskRequeue from "../../islands/PublishingTaskRequeue.tsx";
1010

1111
export default define.page<typeof handler>(function PublishingTasks({
@@ -77,15 +77,15 @@ export default define.page<typeof handler>(function PublishingTasks({
7777
10,
7878
)}
7979
>
80-
{twas(new Date(publishingTask.createdAt).getTime())}
80+
{timeAgo(new Date(publishingTask.createdAt).getTime())}
8181
</TableData>
8282
<TableData
8383
title={new Date(publishingTask.updatedAt).toISOString().slice(
8484
0,
8585
10,
8686
)}
8787
>
88-
{twas(new Date(publishingTask.updatedAt).getTime())}
88+
{timeAgo(new Date(publishingTask.updatedAt).getTime())}
8989
</TableData>
9090
<TableData>
9191
<PublishingTaskRequeue publishingTask={publishingTask} />

Diff for: frontend/routes/package/(_components)/PackageHeader.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
TbRosetteDiscountCheck,
1010
} from "@preact-icons/tb";
1111
import { Tooltip } from "../../../components/Tooltip.tsx";
12-
import twas from "twas";
12+
import { timeAgo } from "../../../utils/timeAgo.ts";
1313
import { greaterThan, parse } from "@std/semver";
1414

1515
interface PackageHeaderProps {
@@ -207,7 +207,7 @@ export function PackageHeader({
207207
)}
208208
>
209209
{`${
210-
twas(new Date(selectedVersion.createdAt).getTime())
210+
timeAgo(new Date(selectedVersion.createdAt))
211211
} (${selectedVersion.version})`}
212212
</div>
213213
</div>

Diff for: frontend/routes/package/og.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { HttpError, RouteConfig } from "fresh";
44
import { Image } from "$imagescript";
5-
import twas from "twas";
5+
import { timeAgo } from "../../utils/timeAgo.ts";
66

77
import { packageDataWithVersion } from "../../utils/data.ts";
88
import { define } from "../../util.ts";
@@ -263,7 +263,7 @@ export const handler = define.handlers({
263263
const publishDateText = Image.renderText(
264264
dmmonoFont,
265265
25,
266-
twas(new Date(selectedVersion.createdAt).getTime()),
266+
timeAgo(new Date(selectedVersion.createdAt).getTime()),
267267
COLOR_GRAY,
268268
);
269269
const result = new Image(

Diff for: frontend/routes/package/versions.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from "../../utils/api_types.ts";
88
import { define } from "../../util.ts";
99
import { compare, equals, format, lessThan, parse, SemVer } from "@std/semver";
10-
import twas from "twas";
10+
import { timeAgo } from "../../utils/timeAgo.ts";
1111
import { packageData } from "../../utils/data.ts";
1212
import { PackageHeader } from "./(_components)/PackageHeader.tsx";
1313
import { PackageNav, Params } from "./(_components)/PackageNav.tsx";
@@ -239,7 +239,7 @@ function Version({
239239
{" "}
240240
</>
241241
)}
242-
{twas(new Date(version.createdAt).getTime())}
242+
{timeAgo(new Date(version.createdAt).getTime())}
243243
</div>
244244
)}
245245
</div>
@@ -269,7 +269,7 @@ function Version({
269269
<span>
270270
{ordinalNumber(tasks.length - i)} publishing attempt{" "}
271271
{statusVerb[task.status]}{" "}
272-
{twas(new Date(task.updatedAt).getTime())}
272+
{timeAgo(new Date(task.updatedAt).getTime())}
273273
</span>
274274
<a href={`/status/${task.id}`} class="link justify-self-end z-20">
275275
Details

Diff for: frontend/routes/status.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { path } from "../utils/api.ts";
99
import { packageData } from "../utils/data.ts";
1010
import { PackageHeader } from "./package/(_components)/PackageHeader.tsx";
1111
import { PackageNav } from "./package/(_components)/PackageNav.tsx";
12-
import twas from "twas";
12+
import { timeAgo } from "../utils/timeAgo.ts";
1313
import PublishingTaskRequeue from "../islands/PublishingTaskRequeue.tsx";
1414
import { TbAlertCircle, TbCheck, TbClockHour3 } from "@preact-icons/tb";
1515
import { scopeIAM } from "../utils/iam.ts";
@@ -48,7 +48,7 @@ export default define.page<typeof handler>(function PackageListPage({
4848
</p>
4949
<p>
5050
<span class="font-semibold">Created:</span>{" "}
51-
{twas(new Date(data.publishingTask.createdAt).getTime())}
51+
{timeAgo(new Date(data.publishingTask.createdAt))}
5252
</p>
5353
{data.publishingTask.userId && (
5454
<p>

Diff for: frontend/utils/timeAgo.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export function timeAgo(date: Date | string): string {
2+
const now = new Date();
3+
const past = new Date(date);
4+
const diff = Math.abs(now.getTime() - past.getTime());
5+
6+
const duration = {
7+
years: Math.floor(diff / (1000 * 60 * 60 * 24 * 365)),
8+
months: Math.floor(
9+
(diff % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24 * 30),
10+
),
11+
days: Math.floor(
12+
(diff % (1000 * 60 * 60 * 24 * 30)) / (1000 * 60 * 60 * 24),
13+
),
14+
hours: Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
15+
minutes: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)),
16+
seconds: Math.floor((diff % (1000 * 60)) / 1000),
17+
};
18+
19+
// Force english because JSR is an English-only project
20+
const formatter = new Intl.DurationFormat("en", { style: "long" });
21+
return formatter.format(duration);
22+
}

0 commit comments

Comments
 (0)