Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Luxon

Edward Cui5eeb2abfbe84
diff --git a/client/src/components/SiteSettings/ImportManager.tsx b/client/src/components/SiteSettings/ImportManager.tsx
index a8dcf4430..1dda5e8a7 100644
--- a/client/src/components/SiteSettings/ImportManager.tsx
+++ b/client/src/components/SiteSettings/ImportManager.tsx
@@ -37,7 +37,6 @@ import {
import { DateTime } from "luxon";
import { useGetSiteImports, useImportSiteData } from "@/api/admin/import";
import { SplitDateRangePicker, DateRange } from "@/components/SplitDateRangePicker";
-import { format } from "date-fns";
interface ImportManagerProps {
siteId: number;
@@ -126,8 +125,8 @@ export function ImportManager({ siteId, disabled }: ImportManagerProps) {
const handleImport = () => {
if (file && source) {
- const startDate = dateRange.startDate ? format(dateRange.startDate, "yyyy-MM-dd") : undefined;
- const endDate = dateRange.endDate ? format(dateRange.endDate, "yyyy-MM-dd") : undefined;
+ const startDate = dateRange.startDate ? dateRange.startDate.toFormat("yyyy-MM-dd") : undefined;
+ const endDate = dateRange.endDate ? dateRange.endDate.toFormat("yyyy-MM-dd") : undefined;
mutation.mutate({
file,
diff --git a/client/src/components/SplitDateRangePicker.tsx b/client/src/components/SplitDateRangePicker.tsx
index e2ae4dfe6..67451d472 100644
--- a/client/src/components/SplitDateRangePicker.tsx
+++ b/client/src/components/SplitDateRangePicker.tsx
@@ -1,7 +1,7 @@
"use client";
import { useState } from "react";
-import { format } from "date-fns";
+import { DateTime, DateTimeFormatOptions } from "luxon";
import { CalendarIcon, X } from "lucide-react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
@@ -14,8 +14,8 @@ import {
} from "@/components/ui/popover";
export interface DateRange {
- startDate?: Date;
- endDate?: Date;
+ startDate?: DateTime;
+ endDate?: DateTime;
}
interface DateRangePickerProps {
@@ -28,9 +28,9 @@ interface DateRangePickerProps {
showClear?: boolean;
clearButtonText?: string;
showDescription?: boolean;
- dateFormat?: string;
- maxDate?: Date;
- minDate?: Date;
+ dateFormat?: DateTimeFormatOptions;
+ maxDate?: DateTime;
+ minDate?: DateTime;
className?: string;
layout?: "horizontal" | "vertical";
}
@@ -45,30 +45,36 @@ export function SplitDateRangePicker({
showClear = true,
clearButtonText = "Clear dates",
showDescription = true,
- dateFormat = "PPP",
- maxDate = new Date(),
+ dateFormat = DateTime.DATE_FULL,
+ maxDate = DateTime.utc(),
minDate,
className,
layout = "horizontal"
}: DateRangePickerProps) {
const [internalValue, setInternalValue] = useState<DateRange>({});
- const currentValue = value !== undefined ? value : internalValue;
+ const currentValue = value ?? internalValue;
const { startDate, endDate } = currentValue;
const handleDateChange = (newDateRange: DateRange) => {
- if (value === undefined) {
+ if (!value) {
setInternalValue(newDateRange);
}
onChange?.(newDateRange);
};
- const handleStartDateChange = (date: Date | undefined) => {
- handleDateChange({ ...currentValue, startDate: date });
+ const handleStartDateChange = (date?: Date) => {
+ handleDateChange({
+ ...currentValue,
+ startDate: date ? DateTime.fromJSDate(date) : undefined,
+ });
};
- const handleEndDateChange = (date: Date | undefined) => {
- handleDateChange({ ...currentValue, endDate: date });
+ const handleEndDateChange = (date?: Date) => {
+ handleDateChange({
+ ...currentValue,
+ endDate: date ? DateTime.fromJSDate(date) : undefined,
+ });
};
const handleClear = () => {
@@ -104,18 +110,19 @@ export function SplitDateRangePicker({
disabled={disabled}
>
<CalendarIcon className="mr-2 h-4 w-4" />
- {startDate ? format(startDate, dateFormat) : startDatePlaceholder}
+ {startDate ? startDate.toLocaleString(dateFormat) : startDatePlaceholder}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
- selected={startDate}
+ selected={startDate?.toJSDate()}
onSelect={handleStartDateChange}
disabled={(date) => {
- if (maxDate && date > maxDate) return true;
- if (minDate && date < minDate) return true;
- if (endDate && date > endDate) return true;
+ const d = DateTime.fromJSDate(date);
+ if (maxDate && d > maxDate) return true;
+ if (minDate && d < minDate) return true;
+ if (endDate && d > endDate) return true;
return false;
}}
captionLayout="dropdown"
@@ -137,18 +144,19 @@ export function SplitDateRangePicker({
disabled={disabled}
>
<CalendarIcon className="mr-2 h-4 w-4" />
- {endDate ? format(endDate, dateFormat) : endDatePlaceholder}
+ {endDate ? endDate.toLocaleString(dateFormat) : endDatePlaceholder}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
- selected={endDate}
+ selected={endDate?.toJSDate()}
onSelect={handleEndDateChange}
disabled={(date) => {
- if (maxDate && date > maxDate) return true;
- if (minDate && date < minDate) return true;
- if (startDate && date < startDate) return true;
+ const d = DateTime.fromJSDate(date);
+ if (maxDate && d > maxDate) return true;
+ if (minDate && d < minDate) return true;
+ if (startDate && d < startDate) return true;
return false;
}}
captionLayout="dropdown"
@@ -176,13 +184,12 @@ export function SplitDateRangePicker({
{showDescription && hasDateSelected && (
<p className="text-xs text-muted-foreground">
{startDate && endDate
- ? `Selected range: ${format(startDate, dateFormat)} to ${format(endDate, dateFormat)}`
+ ? `Selected range: ${startDate.toLocaleString(dateFormat)} to ${endDate.toLocaleString(dateFormat)}`
: startDate
- ? `From ${format(startDate, dateFormat)} onwards`
+ ? `From ${startDate.toLocaleString(dateFormat)} onwards`
: endDate
- ? `Up to ${format(endDate, dateFormat)}`
- : ""
- }
+ ? `Up to ${endDate.toLocaleString(dateFormat)}`
+ : ""}
</p>
)}
</div>