Skip to content

Commit 32fb0b7

Browse files
committed
fixup! fixup! fixup! refactor(NcDateTimePicker): adjust for updated library
1 parent fa0c031 commit 32fb0b7

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

src/components/NcDateTimePicker/NcDateTimePicker.vue

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ import { watch } from 'vue'
190190
import NcIconSvgWrapper from '../NcIconSvgWrapper/NcIconSvgWrapper.vue'
191191
import NcTimezonePicker from '../NcTimezonePicker/NcTimezonePicker.vue'
192192
import { t } from '../../l10n.ts'
193-
import { logger } from '../../utils/logger.ts'
193+
import { loadDateFnsLocale } from '../../utils/date-fns.ts'
194194
import NcButton from '../NcButton/index.ts'
195195
196196
type VueDatePickerProps = InstanceType<typeof VueDatePicker>['$props']
@@ -333,7 +333,9 @@ const targetElement = useTemplateRef('target')
333333
const pickerInstance = useTemplateRef('picker')
334334
335335
const localeObject = ref<Locale>()
336-
watch(() => props.locale, loadLocale, { immediate: true })
336+
watch(() => props.locale, async () => {
337+
localeObject.value = await loadDateFnsLocale(props.locale)
338+
}, { immediate: true })
337339
338340
/**
339341
* Mapping of the model-value prop to the format expected by the library.
@@ -602,26 +604,6 @@ function selectDate() {
602604
function cancelSelection() {
603605
pickerInstance.value!.closeMenu()
604606
}
605-
606-
/**
607-
* Load the locale object from date-fns dynamically.
608-
*
609-
* @param locale - The locale to load
610-
*/
611-
async function loadLocale(locale: string) {
612-
try {
613-
const { default: dateFnLocale } = await import(`date-fns/locale/${locale}`)
614-
localeObject.value = dateFnLocale
615-
} catch (error) {
616-
if (locale.includes('-')) {
617-
locale = locale.split('-')[0]!
618-
logger.debug('Try loading fallback locale for NcDateTimePicker', { locale })
619-
await loadLocale(locale)
620-
} else {
621-
logger.error('Failed to load locale for NcDateTimePicker', { locale, error })
622-
}
623-
}
624-
}
625607
</script>
626608

627609
<template>

src/utils/date-fns.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*!
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import type { Locale } from 'date-fns'
7+
8+
import { logger } from './logger.ts'
9+
10+
// this is a fix only for the styleguide as webpack does not have glob import
11+
let modules: Record<string, () => Promise<{ default: Locale }>>
12+
try {
13+
modules = import.meta.glob('/node_modules/date-fns/locale/*.js')
14+
} catch {
15+
modules = {}
16+
}
17+
18+
/**
19+
* Load a date-fns locale module.
20+
*
21+
* @param locale - The canonical locale to load
22+
*/
23+
export async function loadDateFnsLocale(locale: string): Promise<Locale | undefined> {
24+
logger.debug(`Loading date-fns locale for '${locale}'`)
25+
const modulePath = `/node_modules/date-fns/locale/${locale}.js`
26+
if (modules[modulePath]) {
27+
logger.debug(`Import date-fns locale for '${locale}'`)
28+
const mod = await modules[modulePath]()
29+
return mod.default
30+
}
31+
32+
if (locale.includes('-')) {
33+
// Try without region
34+
const shortLocale = locale.split('-')[0]!
35+
return await loadDateFnsLocale(shortLocale)
36+
}
37+
38+
logger.debug(`No date-fns locale found for '${locale}'`)
39+
}

0 commit comments

Comments
 (0)