Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/components/BuildEnvironment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defineProps<{
footer?: boolean
}>()

const { locale } = useI18n()
const buildInfo = useAppConfig().buildInfo
</script>

Expand All @@ -14,7 +13,7 @@ const buildInfo = useAppConfig().buildInfo
style="animation-delay: 0.05s"
>
<i18n-t keypath="built_at" scope="global">
<NuxtTime :datetime="buildInfo.time" :locale="locale" relative />
<DateTimeButton :datetime="buildInfo.time" />
</i18n-t>
<span>&middot;</span>
<NuxtLink
Expand Down
2 changes: 1 addition & 1 deletion app/components/Compare/FacetCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function getShortName(header: string): string {
<template v-else>
<span class="font-mono text-sm tabular-nums" :class="getStatusClass(value.status)">
<!-- Date values use DateTime component for i18n and user settings -->
<DateTime
<DateTimeButton
v-if="value.type === 'date'"
:datetime="value.display"
date-style="medium"
Expand Down
12 changes: 10 additions & 2 deletions app/components/Compare/FacetRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ function isCellLoading(index: number): boolean {
:data-status="value.status"
>
<!-- Date values use DateTime component for i18n and user settings -->
<DateTime v-if="value.type === 'date'" :datetime="value.display" date-style="medium" />
<DateTimeButton
v-if="value.type === 'date'"
:datetime="value.display"
date-style="medium"
/>
<template v-else>
<span dir="auto">{{ value.display }}</span>
</template>
Expand All @@ -147,7 +151,11 @@ function isCellLoading(index: number): boolean {
:data-status="value.status"
>
<!-- Date values use DateTime component for i18n and user settings -->
<DateTime v-if="value.type === 'date'" :datetime="value.display" date-style="medium" />
<DateTimeButton
v-if="value.type === 'date'"
:datetime="value.display"
date-style="medium"
/>
<template v-else>
<span dir="auto">{{ value.display }}</span>
</template>
Expand Down
6 changes: 3 additions & 3 deletions app/components/DateTime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
const props = withDefaults(
defineProps<{
/** The datetime value (ISO string or Date) */
datetime: string | Date
/** The datetime value (ISO string or Date or timestamp) */
datetime: string | Date | number
/** Override title (defaults to datetime) */
title?: string
/** Date style for absolute display */
Expand All @@ -31,7 +31,7 @@ const props = withDefaults(

const { locale } = useI18n()

const relativeDates = useRelativeDates()
const { relativeDates } = useRelativeDates()

const dateFormatter = new Intl.DateTimeFormat(locale.value, {
month: 'short',
Expand Down
28 changes: 28 additions & 0 deletions app/components/DateTimeButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script setup lang="ts">
/**
* refer to DateTime
*/
defineProps<{
datetime: string | Date | number
title?: string
dateStyle?: 'full' | 'long' | 'medium' | 'short'
year?: 'numeric' | '2-digit'
month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow'
day?: 'numeric' | '2-digit'
}>()

const { relativeDates, toggleRelativeDates } = useRelativeDates()
</script>

<template>
<button type="button" :aria-pressed="relativeDates" @click="toggleRelativeDates">
<DateTime
:datetime="datetime"
:title="title"
:date-style="dateStyle"
:year="year"
:month="month"
:day="day"
/>
</button>
</template>
2 changes: 1 addition & 1 deletion app/components/Package/TableRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const allMaintainersText = computed(() => {
v-if="isColumnVisible('updated')"
class="py-2 px-3 font-mono text-end text-xs text-fg-muted"
>
<DateTime
<DateTimeButton
v-if="updatedDate"
:datetime="updatedDate"
year="numeric"
Expand Down
12 changes: 6 additions & 6 deletions app/components/Package/Versions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ function getTagVersions(tag: string): VersionDisplay[] {
</div>
</div>
<div class="flex items-center gap-2 shrink-0">
<DateTime
<DateTimeButton
v-if="row.primaryVersion.time"
:datetime="row.primaryVersion.time"
year="numeric"
Expand Down Expand Up @@ -444,7 +444,7 @@ function getTagVersions(tag: string): VersionDisplay[] {
</span>
</NuxtLink>
<div class="flex items-center gap-2 shrink-0">
<DateTime
<DateTimeButton
v-if="v.time"
:datetime="v.time"
class="text-[10px] text-fg-subtle"
Expand Down Expand Up @@ -551,7 +551,7 @@ function getTagVersions(tag: string): VersionDisplay[] {
</span>
</NuxtLink>
<div class="flex items-center gap-2 shrink-0 pe-2">
<DateTime
<DateTimeButton
v-if="row.primaryVersion.time"
:datetime="row.primaryVersion.time"
class="text-[10px] text-fg-subtle"
Expand Down Expand Up @@ -630,7 +630,7 @@ function getTagVersions(tag: string): VersionDisplay[] {
</NuxtLink>
</div>
<div class="flex items-center gap-2 shrink-0 pe-2">
<DateTime
<DateTimeButton
v-if="group.versions[0]?.time"
:datetime="group.versions[0]?.time"
class="text-[10px] text-fg-subtle"
Expand Down Expand Up @@ -693,7 +693,7 @@ function getTagVersions(tag: string): VersionDisplay[] {
</NuxtLink>
</div>
<div class="flex items-center gap-2 shrink-0 pe-2">
<DateTime
<DateTimeButton
v-if="group.versions[0]?.time"
:datetime="group.versions[0]?.time"
class="text-[10px] text-fg-subtle"
Expand Down Expand Up @@ -751,7 +751,7 @@ function getTagVersions(tag: string): VersionDisplay[] {
</span>
</NuxtLink>
<div class="flex items-center gap-2 shrink-0 pe-2">
<DateTime
<DateTimeButton
v-if="v.time"
:datetime="v.time"
class="text-[10px] text-fg-subtle"
Expand Down
10 changes: 9 additions & 1 deletion app/composables/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ export function useSettings() {
*/
export function useRelativeDates() {
const { settings } = useSettings()
return computed(() => settings.value.relativeDates)

function toggleRelativeDates() {
settings.value.relativeDates = !settings.value.relativeDates
}

return {
relativeDates: computed(() => settings.value.relativeDates),
toggleRelativeDates,
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/pages/package/[[org]]/[name].vue
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ onKeyStroke(
{{ $t('package.stats.published') }}
</dt>
<dd class="font-mono text-sm text-fg">
<DateTime :datetime="pkg.time[resolvedVersion]!" date-style="medium" />
<DateTimeButton :datetime="pkg.time[resolvedVersion]!" date-style="medium" />
</dd>
</div>
</dl>
Expand Down
4 changes: 3 additions & 1 deletion test/nuxt/components/DateTime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import DateTime from '~/components/DateTime.vue'
// Mock the useRelativeDates composable
const mockRelativeDates = shallowRef(false)
vi.mock('~/composables/useSettings', () => ({
useRelativeDates: () => mockRelativeDates,
useRelativeDates: () => ({
relativeDates: mockRelativeDates,
}),
Comment on lines +8 to +10
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add toggleRelativeDates to the mock so clicks actually toggle.

DateTime now calls toggleRelativeDates on click, but the mock only provides relativeDates, so the new interaction test cannot flip state and will fail.

✅ Suggested fix
 vi.mock('~/composables/useSettings', () => ({
   useRelativeDates: () => ({
     relativeDates: mockRelativeDates,
+    toggleRelativeDates: () => {
+      mockRelativeDates.value = !mockRelativeDates.value
+    },
   }),
   useSettings: () => ({
     settings: ref({ relativeDates: mockRelativeDates.value }),
   }),
   useAccentColor: () => ({}),
 }))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useRelativeDates: () => ({
relativeDates: mockRelativeDates,
}),
useRelativeDates: () => ({
relativeDates: mockRelativeDates,
toggleRelativeDates: () => {
mockRelativeDates.value = !mockRelativeDates.value
},
}),

useSettings: () => ({
settings: ref({ relativeDates: mockRelativeDates.value }),
}),
Expand Down
36 changes: 36 additions & 0 deletions test/nuxt/components/DateTimeButton.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import DateTimeButton from '~/components/DateTimeButton.vue'

// Mock the useRelativeDates composable
const mockRelativeDates = shallowRef(false)
vi.mock('~/composables/useSettings', () => ({
useRelativeDates: () => ({
relativeDates: mockRelativeDates,
}),
useSettings: () => ({
settings: ref({ relativeDates: mockRelativeDates.value }),
}),
useAccentColor: () => ({}),
}))

describe('DateTimeButton', () => {
const testDate = '2024-01-15T12:00:00.000Z'

beforeEach(() => {
mockRelativeDates.value = false
})

it('clicking button to toggle the relative dates settings', async () => {
const component = await mountSuspended(DateTimeButton, {
props: { datetime: testDate },
})
const button = component.find('button')

await button.trigger('click')
expect(button.attributes('aria-pressed')).toBe('true')

await button.trigger('click')
expect(button.attributes('aria-pressed')).toBe('false')
})
})
Loading