Skip to content

Commit 9bee9f2

Browse files
committed
refactor: remove luxon use, use Intl
1 parent 3b3835c commit 9bee9f2

File tree

5 files changed

+37
-60
lines changed

5 files changed

+37
-60
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"i18next": "^23.6.0",
3737
"lodash.merge": "^4.6.2",
3838
"lodash.shuffle": "^4.2.0",
39-
"luxon": "^3.4.3",
4039
"next": "13.4.8",
4140
"next-i18next": "^14.0.3",
4241
"next-mdx-remote": "^3.0.8",

src/components/CommunityEvents/index.tsx

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DateTime, DateTimeFormatOptions } from "luxon"
21
import { useRouter } from "next/router"
32
import { useTranslation } from "next-i18next"
43
import { FaDiscord } from "react-icons/fa"
@@ -12,6 +11,7 @@ import {
1211
Icon,
1312
} from "@chakra-ui/react"
1413

14+
import type { Lang } from "@/lib/types"
1515
import type { CommunityEvent } from "@/lib/interfaces"
1616

1717
import { ButtonLink } from "@/components/Buttons"
@@ -21,6 +21,7 @@ import Text from "@/components/OldText"
2121
import Translation from "@/components/Translation"
2222

2323
import { trackCustomEvent } from "@/lib/utils/matomo"
24+
import { getLocaleTimestamp } from "@/lib/utils/time"
2425

2526
const matomoEvent = (buttonType: string) => {
2627
trackCustomEvent({
@@ -30,30 +31,15 @@ const matomoEvent = (buttonType: string) => {
3031
})
3132
}
3233

33-
const renderEventDateTime = (
34-
date: string,
35-
language: string,
36-
params: DateTimeFormatOptions = {
37-
year: "numeric",
38-
month: "long",
39-
day: "numeric",
40-
hour12: false,
41-
hour: "numeric",
42-
minute: "numeric",
43-
}
44-
) => {
45-
return DateTime.fromISO(date).setLocale(language).toLocaleString(params)
46-
}
47-
48-
interface EventProps {
34+
type EventProps = {
4935
event: CommunityEvent
50-
language: string
5136
type: "upcoming" | "past"
5237
}
5338

54-
const Event = ({ event, language, type }: EventProps) => {
39+
const Event = ({ event, type }: EventProps) => {
40+
const { locale } = useRouter()
5541
const { date, title, calendarLink } = event
56-
const params: DateTimeFormatOptions = {
42+
const options: Intl.DateTimeFormatOptions = {
5743
year: "numeric",
5844
month: "short",
5945
day: "numeric",
@@ -63,7 +49,7 @@ const Event = ({ event, language, type }: EventProps) => {
6349
<Grid gap={6} templateColumns="auto 1fr" mb={4}>
6450
<GridItem>
6551
<Text color="body.medium" m={0}>
66-
{renderEventDateTime(date, language, params)}
52+
{getLocaleTimestamp(locale! as Lang, date, options)}
6753
</Text>
6854
</GridItem>
6955
<GridItem>
@@ -130,9 +116,17 @@ const CommunityEvents = ({ events }: CommunityEventsProps) => {
130116
{reversedUpcomingEventData[0].title}
131117
</Text>
132118
<Text m={0} fontSize="xl">
133-
{renderEventDateTime(
119+
{getLocaleTimestamp(
120+
locale! as Lang,
134121
reversedUpcomingEventData[0].date,
135-
locale!
122+
{
123+
year: "numeric",
124+
month: "long",
125+
day: "numeric",
126+
hour12: false,
127+
hour: "numeric",
128+
minute: "numeric",
129+
}
136130
)}
137131
</Text>
138132
<Text color="body.medium" fontSize="md">
@@ -177,14 +171,7 @@ const CommunityEvents = ({ events }: CommunityEventsProps) => {
177171
<Divider mb={4} />
178172
{reversedUpcomingEventData.slice(1).length ? (
179173
reversedUpcomingEventData.slice(1).map((item, idx) => {
180-
return (
181-
<Event
182-
key={idx}
183-
event={item}
184-
language={locale!}
185-
type="upcoming"
186-
/>
187-
)
174+
return <Event key={idx} event={item} type="upcoming" />
188175
})
189176
) : (
190177
<Text mx="auto">
@@ -197,9 +184,7 @@ const CommunityEvents = ({ events }: CommunityEventsProps) => {
197184
<Divider mb={4} />
198185
{reversedPastEventData.length ? (
199186
reversedPastEventData.map((item, idx) => {
200-
return (
201-
<Event key={idx} event={item} language={locale!} type="past" />
202-
)
187+
return <Event key={idx} event={item} type="past" />
203188
})
204189
) : (
205190
<Text mx="auto">

src/lib/utils/time.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
// TODO: we are using `luxon` package just for this util, should consider rewrite using native JS Date API
2-
import { DateTime } from "luxon"
3-
41
import { Lang } from "../types"
52

6-
export const INVALID_DATETIME = "Invalid DateTime"
7-
8-
export const getLocaleTimestamp = (locale: Lang, timestamp: string) => {
9-
let localeTimestamp = DateTime.fromSQL(timestamp)
10-
.setLocale(locale)
11-
.toLocaleString(DateTime.DATE_FULL)
12-
13-
// Fallback to ISO
14-
if (localeTimestamp === INVALID_DATETIME) {
15-
localeTimestamp = DateTime.fromISO(timestamp)
16-
.setLocale(locale)
17-
.toLocaleString(DateTime.DATE_FULL)
18-
}
19-
return localeTimestamp
3+
export const getLocaleTimestamp = (
4+
locale: Lang,
5+
timestamp: string,
6+
options?: Intl.DateTimeFormatOptions
7+
) => {
8+
const opts =
9+
options ||
10+
({
11+
year: "numeric",
12+
month: "long",
13+
day: "numeric",
14+
} as Intl.DateTimeFormatOptions)
15+
const date = new Date(timestamp)
16+
return new Intl.DateTimeFormat(locale, opts).format(date)
2017
}

src/pages/developers/tutorials.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { existsNamespace } from "@/lib/utils/existsNamespace"
3333
import { getLastDeployDate } from "@/lib/utils/getLastDeployDate"
3434
import { trackCustomEvent } from "@/lib/utils/matomo"
3535
import { getTutorialsData } from "@/lib/utils/md"
36-
import { getLocaleTimestamp, INVALID_DATETIME } from "@/lib/utils/time"
36+
import { getLocaleTimestamp } from "@/lib/utils/time"
3737
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
3838
import {
3939
filterTutorialsByLang,
@@ -126,9 +126,10 @@ export interface ITutorial {
126126

127127
const published = (locale: string, published: string) => {
128128
const localeTimestamp = getLocaleTimestamp(locale as Lang, published)
129-
return localeTimestamp !== INVALID_DATETIME ? (
129+
return localeTimestamp !== "Invalid Date" ? (
130130
<span>
131-
<Emoji text=":calendar:" fontSize="sm" ms={2} me={2} /> {localeTimestamp}
131+
<Emoji text=":calendar:" fontSize="sm" ms={2} me={2} />
132+
{localeTimestamp}
132133
</span>
133134
) : null
134135
}

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9561,11 +9561,6 @@ lru-cache@^6.0.0:
95619561
dependencies:
95629562
yallist "^4.0.0"
95639563

9564-
luxon@^3.4.3:
9565-
version "3.4.4"
9566-
resolved "https://registry.yarnpkg.com/luxon/-/luxon-3.4.4.tgz#cf20dc27dc532ba41a169c43fdcc0063601577af"
9567-
integrity sha512-zobTr7akeGHnv7eBOXcRgMeCP6+uyYsczwmeRCauvpvaAltgNyTbLH/+VaEAPUeWBT+1GuNmz4wC/6jtQzbbVA==
9568-
95699564
lz-string@^1.5.0:
95709565
version "1.5.0"
95719566
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.5.0.tgz#c1ab50f77887b712621201ba9fd4e3a6ed099941"

0 commit comments

Comments
 (0)