trypost/resources/js/components/ui/date-range-picker/DateRangePicker.vue
Paulo Castellano 1af705f3fd
Localize calendar date pickers to the active UI locale (#234)
* Localize calendar date pickers to the active UI locale.

Pass the Inertia locale into Reka calendars, translate a11y labels, and use dayjs LL/LLL for DatePicker display text.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Localize remaining dayjs date displays across the UI.

Route list/calendar/preview timestamps through localized LL/LLL helpers so formats follow the active UI locale instead of English or Portuguese-locked patterns.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Use scheduled-or-now timestamps in platform previews.

Drive preview labels from the post schedule when set, localize Discord without dayjs calendar(), and expose a single formatPreviewPostedAt helper.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix preview schedule wiring and Discord/Facebook timestamp labels.

Restore hasPickedTime from any saved scheduled_at, keep time on non-today Discord labels, and use a localized just-now fallback for unschedled Facebook previews.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix API key expiry day shift and tighten calendar range titles.

Format date-only expiry in UTC calendar days, and use compact dayjs titles for calendar day/week headers.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Replace frontend date source greps with Inertia API key assertions.

Drop the Vue/TS file scanner and assert expiry calendar days through ApiKeyController props instead.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Localize calendar week titles with day-first dayjs formats.

Avoid English month-day order in week headers and chart axis labels so locales like pt-BR keep natural day-first dates.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Split preview timestamp formatting into named platform helpers.

Replace the style-switch formatPreviewPostedAt with clear per-platform helpers so call sites read as formatDiscordPreview / formatXPreview / etc.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-05 21:02:34 -03:00

177 lines
5.8 KiB
Vue

<script setup lang="ts">
import type { DateRange } from "reka-ui"
import type { Ref } from "vue"
import {
CalendarDate,
getLocalTimeZone,
} from "@internationalized/date"
import { IconCalendar } from "@tabler/icons-vue"
import { trans } from "laravel-vue-i18n"
import { computed, nextTick, ref, watch } from "vue"
import { useWindowSize } from "@vueuse/core"
import { Button } from "@/components/ui/button"
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover"
import { RangeCalendar } from "@/components/ui/range-calendar"
import { useCalendarLocale } from "@/composables/useCalendarLocale"
import { cn } from "@/lib/utils"
import dayjs from "@/dayjs"
const props = defineProps<{
modelValue: { start: Date, end: Date }
triggerClass?: string
}>()
const emit = defineEmits<{
'update:modelValue': [value: { start: Date, end: Date }]
}>()
const calendarLocale = useCalendarLocale()
const toCalendarDate = (dateValue: Date) => {
return new CalendarDate(
dateValue.getFullYear(),
dateValue.getMonth() + 1,
dateValue.getDate(),
)
}
const toDate = (calendarDate: any) => {
if (!calendarDate) return new Date()
return calendarDate.toDate(getLocalTimeZone())
}
const value = ref({
start: toCalendarDate(props.modelValue.start),
end: toCalendarDate(props.modelValue.end),
}) as Ref<DateRange>
const isUpdating = ref(false)
const isOpen = ref(false)
const { width } = useWindowSize()
const numberOfMonths = computed(() => width.value < 640 ? 1 : 2)
const range = (start: dayjs.Dayjs, end: dayjs.Dayjs) => ({
start: toCalendarDate(start.toDate()),
end: toCalendarDate(end.toDate()),
})
const presetGroups = computed(() => [
[
{ label: trans('common.date_range_picker.today'), getValue: () => range(dayjs(), dayjs()) },
{ label: trans('common.date_range_picker.yesterday'), getValue: () => range(dayjs().subtract(1, "day"), dayjs().subtract(1, "day")) },
],
[
{ label: trans('common.date_range_picker.last_7_days'), getValue: () => range(dayjs().subtract(6, "day"), dayjs()) },
{ label: trans('common.date_range_picker.last_30_days'), getValue: () => range(dayjs().subtract(29, "day"), dayjs()) },
{ label: trans('common.date_range_picker.last_3_months'), getValue: () => range(dayjs().subtract(3, "month"), dayjs()) },
{ label: trans('common.date_range_picker.last_6_months'), getValue: () => range(dayjs().subtract(6, "month"), dayjs()) },
{ label: trans('common.date_range_picker.last_12_months'), getValue: () => range(dayjs().subtract(12, "month").add(1, "day"), dayjs()) },
],
[
{ label: trans('common.date_range_picker.this_month'), getValue: () => range(dayjs().startOf("month"), dayjs().endOf("month")) },
{ label: trans('common.date_range_picker.last_month'), getValue: () => range(dayjs().subtract(1, "month").startOf("month"), dayjs().subtract(1, "month").endOf("month")) },
{ label: trans('common.date_range_picker.year_to_date'), getValue: () => range(dayjs().startOf("year"), dayjs()) },
{ label: trans('common.date_range_picker.last_year'), getValue: () => range(dayjs().subtract(1, "year").startOf("year"), dayjs().subtract(1, "year").endOf("year")) },
],
])
type Preset = { label: string, getValue: () => { start: any, end: any } }
const applyPreset = (preset: Preset) => {
value.value = preset.getValue()
isOpen.value = false
}
watch(
() => props.modelValue,
(newVal) => {
if (!isUpdating.value) {
value.value = {
start: toCalendarDate(newVal.start),
end: toCalendarDate(newVal.end),
}
}
},
{ deep: true },
)
watch(
value,
(newVal) => {
if (newVal.start && newVal.end) {
isUpdating.value = true
emit("update:modelValue", {
start: toDate(newVal.start),
end: toDate(newVal.end),
})
nextTick(() => {
isUpdating.value = false
})
}
},
{ deep: true },
)
</script>
<template>
<Popover v-model:open="isOpen">
<PopoverTrigger as-child>
<Button
variant="outline"
:class="cn(
'w-full justify-start text-left font-medium sm:w-auto',
!value && 'text-foreground/60',
props.triggerClass,
)"
>
<template v-if="value.start">
<template v-if="value.end">
{{ dayjs(toDate(value.start)).format('LL') }} -
{{ dayjs(toDate(value.end)).format('LL') }}
</template>
<template v-else>
{{ dayjs(toDate(value.start)).format('LL') }}
</template>
</template>
<template v-else>
{{ $t('common.date_range_picker.placeholder') }}
</template>
<IconCalendar class="ml-auto size-4 text-foreground/60" />
</Button>
</PopoverTrigger>
<PopoverContent class="w-auto p-0" align="end">
<div class="flex flex-col sm:flex-row">
<div class="hidden flex-col border-b-2 border-foreground py-2 sm:flex sm:w-[170px] sm:shrink-0 sm:border-b-0 sm:border-r-2">
<template v-for="(group, groupIndex) in presetGroups" :key="groupIndex">
<div v-if="groupIndex > 0" class="my-1 border-t-2 border-dashed border-foreground/20" />
<div class="space-y-0.5 px-2">
<Button
v-for="preset in group"
:key="preset.label"
variant="ghost"
size="sm"
class="h-7 w-full justify-start text-xs font-bold text-foreground hover:bg-violet-100 hover:text-foreground"
@click="applyPreset(preset)"
>
{{ preset.label }}
</Button>
</div>
</template>
</div>
<div class="shrink-0">
<RangeCalendar
v-model="value"
initial-focus
:locale="calendarLocale"
:number-of-months="numberOfMonths"
/>
</div>
</div>
</PopoverContent>
</Popover>
</template>