refactor(files): migrate away from moment.js

Use browser provided API and our shared functions.
This also fixes tests when local language is not English.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
Ferdinand Thiessen 2025-06-11 14:04:41 +02:00
parent 27629fd332
commit 62e8d011f9
2 changed files with 18 additions and 10 deletions

View file

@ -9,6 +9,12 @@ import { deleted, deletedBy, originalLocation } from './columns.ts'
import { trashbinView } from './trashbinView.ts'
import * as ncAuth from '@nextcloud/auth'
vi.mock('@nextcloud/l10n', async (originalModule) => ({
...(await originalModule()),
getLanguage: () => 'en',
getCanonicalLocale: () => 'en-US',
}))
describe('files_trashbin: file list columns', () => {
describe('column: original location', () => {
@ -100,19 +106,19 @@ describe('files_trashbin: file list columns', () => {
})
it('renders a node with deletion date', () => {
const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deletion-time': 1741684522 } })
const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deletion-time': (Date.now() / 1000) - 120 } })
const el: HTMLElement = deleted.render(node, trashbinView)
expect(el).toBeInstanceOf(HTMLElement)
expect(el.textContent).toBe('a minute ago')
expect(el.title).toBe('March 11, 2025 9:15 AM')
expect(el.textContent).toBe('2 minutes ago')
expect(el.title).toBe('March 11, 2025 at 9:14 AM')
})
it('renders a node when deletion date is missing and falls back to mtime', () => {
const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', mtime: new Date(1741684522000) })
const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', mtime: new Date(Date.now() - 60000) })
const el: HTMLElement = deleted.render(node, trashbinView)
expect(el).toBeInstanceOf(HTMLElement)
expect(el.textContent).toBe('a minute ago')
expect(el.title).toBe('March 11, 2025 9:15 AM')
expect(el.textContent).toBe('1 minute ago')
expect(el.title).toBe('March 11, 2025 at 9:15 AM')
})
it('renders a node when deletion date is missing', () => {

View file

@ -3,10 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import moment from '@nextcloud/moment'
import { getCurrentUser } from '@nextcloud/auth'
import { Column, Node } from '@nextcloud/files'
import { getCanonicalLocale, getLanguage, translate as t } from '@nextcloud/l10n'
import { formatRelativeTime, getCanonicalLocale, getLanguage, t } from '@nextcloud/l10n'
import { dirname } from '@nextcloud/paths'
import Vue from 'vue'
@ -67,8 +66,11 @@ export const deleted = new Column({
const deletionTime = node.attributes?.['trashbin-deletion-time'] || ((node?.mtime?.getTime() ?? 0) / 1000)
const span = document.createElement('span')
if (deletionTime) {
span.title = moment.unix(deletionTime).format('LLL')
span.textContent = moment.unix(deletionTime).fromNow()
const formatter = Intl.DateTimeFormat([getCanonicalLocale()], { dateStyle: 'long', timeStyle: 'short' })
const timestamp = new Date(deletionTime * 1000)
span.title = formatter.format(timestamp)
span.textContent = formatRelativeTime(timestamp, { ignoreSeconds: t('files', 'few seconds ago') })
return span
}