Skip to content

Commit f2b5f33

Browse files
committed
refactor: unify DateTime
1 parent dc47efe commit f2b5f33

3 files changed

Lines changed: 29 additions & 38 deletions

File tree

app/components/BuildEnvironment.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ defineProps<{
33
footer?: boolean
44
}>()
55
6-
const { locale } = useI18n()
76
const buildInfo = useAppConfig().buildInfo
87
</script>
98

@@ -14,7 +13,7 @@ const buildInfo = useAppConfig().buildInfo
1413
style="animation-delay: 0.05s"
1514
>
1615
<i18n-t keypath="built_at" scope="global">
17-
<NuxtTime :datetime="buildInfo.time" :locale="locale" relative />
16+
<DateTime :datetime="buildInfo.time" />
1817
</i18n-t>
1918
<span>&middot;</span>
2019
<NuxtLink

app/components/DateTime.vue

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
*/
1010
const props = withDefaults(
1111
defineProps<{
12-
/** The datetime value (ISO string or Date) */
13-
datetime: string | Date
12+
/** The datetime value (ISO string or Date or timestamp) */
13+
datetime: string | Date | number
1414
/** Override title (defaults to datetime) */
1515
title?: string
1616
/** Date style for absolute display */
@@ -32,7 +32,6 @@ const props = withDefaults(
3232
const { locale } = useI18n()
3333
3434
const { settings } = useSettings()
35-
const relativeDates = useRelativeDates()
3635
3736
const dateFormatter = new Intl.DateTimeFormat(locale.value, {
3837
month: 'short',
@@ -49,21 +48,34 @@ const titleValue = computed(() => {
4948
const date = typeof props.datetime === 'string' ? new Date(props.datetime) : props.datetime
5049
return dateFormatter.format(date)
5150
})
51+
52+
function toggleRelativeDates() {
53+
settings.value.relativeDates = !settings.value.relativeDates
54+
}
5255
</script>
5356

5457
<template>
55-
<button @click="settings.relativeDates = !settings.relativeDates">
56-
<span>
57-
<ClientOnly>
58-
<NuxtTime
59-
v-if="relativeDates"
60-
:datetime="datetime"
61-
:title="titleValue"
62-
relative
63-
:locale="locale"
64-
/>
58+
<button @click="toggleRelativeDates">
59+
<ClientOnly>
60+
<NuxtTime
61+
v-if="settings.relativeDates"
62+
:datetime="datetime"
63+
:title="titleValue"
64+
relative
65+
:locale="locale"
66+
/>
67+
<NuxtTime
68+
v-else
69+
:datetime="datetime"
70+
:title="titleValue"
71+
:date-style="dateStyle"
72+
:year="year"
73+
:month="month"
74+
:day="day"
75+
:locale="locale"
76+
/>
77+
<template #fallback>
6578
<NuxtTime
66-
v-else
6779
:datetime="datetime"
6880
:title="titleValue"
6981
:date-style="dateStyle"
@@ -72,18 +84,7 @@ const titleValue = computed(() => {
7284
:day="day"
7385
:locale="locale"
7486
/>
75-
<template #fallback>
76-
<NuxtTime
77-
:datetime="datetime"
78-
:title="titleValue"
79-
:date-style="dateStyle"
80-
:year="year"
81-
:month="month"
82-
:day="day"
83-
:locale="locale"
84-
/>
85-
</template>
86-
</ClientOnly>
87-
</span>
87+
</template>
88+
</ClientOnly>
8889
</button>
8990
</template>

app/composables/useSettings.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ export function useSettings() {
6262
}
6363
}
6464

65-
/**
66-
* Composable for accessing just the relative dates setting.
67-
* Useful for components that only need to read this specific setting.
68-
*/
69-
export function useRelativeDates() {
70-
const { settings } = useSettings()
71-
return computed(() => settings.value.relativeDates)
72-
}
73-
7465
/**
7566
* Composable for managing accent color.
7667
*/

0 commit comments

Comments
 (0)