Update frontend to React 19 (#22275)
* remove unused RecoilRoot and fix implicit ref callback
Remove the vestigial recoil dependency (zero consumers) and convert
the implicit-return ref callback in SearchView to block form to
prevent React 19 interpreting it as a cleanup function.
* replace react-transition-group with framer-motion in Chip
Replace CSSTransition with framer-motion AnimatePresence + motion.div
for React 19 compatibility (react-transition-group uses findDOMNode).
framer-motion is already a project dependency.
* migrate react-grid-layout v1 to v2
- Replace WidthProvider(Responsive) HOC with useContainerWidth hook
- Update types: Layout (single item) → LayoutItem, Layout[] → Layout
- Replace isDraggable/isResizable/resizeHandles with dragConfig/resizeConfig
- Update EventCallback signature for v2 API
- Remove @types/react-grid-layout (v2 includes its own types)
* upgrade vaul, next-themes, framer-motion, react-zoom-pan-pinch
- vaul: ^0.9.1 → ^1.1.2
- next-themes: ^0.3.0 → ^0.4.6
- framer-motion: ^11.5.4 → ^12.35.0 (React 19 native support)
- react-zoom-pan-pinch: 3.4.4 → latest
* upgrade to React 19, react-konva v19, eslint-plugin-react-hooks v5
Core React 19 upgrade with all necessary type fixes:
- Update RefObject types to accept T | null (React 19 refs always nullable)
- Add JSX namespace imports (no longer global in React 19)
- Add initial values to useRef calls (required in React 19)
- Fix ReactElement.props unknown type in config-form components
- Fix IconWrapper interface to use HTMLAttributes instead of index signature
- Add monaco-editor as dev dependency for type declarations
- Upgrade react-konva to v19, eslint-plugin-react-hooks to v5
* upgrade typescript to 5.9.3
* modernize Context.Provider to React 19 shorthand
Replace <Context.Provider value={...}> with <Context value={...}>
across all project-owned context providers. External library contexts
(react-icons IconContext, radix TooltipPrimitive) left unchanged.
* add runtime patches for React 19 compatibility
- Patch @radix-ui/react-compose-refs@1.1.2: stabilize useComposedRefs
to prevent infinite render loops from unstable ref callbacks
https://github.com/radix-ui/primitives/issues/3799
- Patch @radix-ui/react-slot@1.2.4: use useComposedRefs hook in
SlotClone instead of inline composeRefs to prevent re-render cycles
https://github.com/radix-ui/primitives/pull/3804
- Patch react-use-websocket@4.8.1: remove flushSync wrappers that
cause "Maximum update depth exceeded" with React 19 auto-batching
https://github.com/facebook/react/issues/27613
- Add npm overrides to ensure single hoisted copies of compose-refs
and react-slot across all Radix packages
- Add postinstall script for patch-package
- Remove leftover react-transition-group dependency
* formatting
* use availableWidth instead of useContainerWidth for grid layout
The useContainerWidth hook from react-grid-layout v2 returns raw
container width without accounting for scrollbar width, causing the
grid to not fill the full available space. Use the existing
availableWidth value from useResizeObserver which already compensates
for scrollbar width, matching the working implementation.
* remove unused carousel component and fix React 19 peer deps
Remove embla-carousel-react and its unused Carousel UI component.
Upgrade sonner v1 → v2 for native React 19 support. Remove
@types/react-icons stub (react-icons bundles its own types).
These changes eliminate all peer dependency conflicts, so
npm install works without --legacy-peer-deps.
* fix React 19 infinite re-render loop on live dashboard
The "Maximum update depth exceeded" error was caused by two issues:
1. useDeferredStreamMetadata returned a new `{}` default on every render
when SWR data was undefined, creating an unstable reference that
triggered the useEffect in useCameraLiveMode on every render cycle.
Fixed by using a stable module-level EMPTY_METADATA constant.
2. useResizeObserver's rest parameter `...refs` created a new array on
every render, causing its useEffect to re-run and re-observe elements
continuously. Fixed by stabilizing refs with useRef and only
reconnecting the observer when actual DOM elements change.
2026-03-05 17:42:38 +03:00
|
|
|
import type { JSX } from "react";
|
2024-09-06 22:26:32 +03:00
|
|
|
import { useState, useEffect, useRef } from "react";
|
2024-06-23 23:58:00 +03:00
|
|
|
import { Button } from "./button";
|
|
|
|
|
import { Calendar } from "./calendar";
|
|
|
|
|
import { Label } from "./label";
|
|
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from "./select";
|
|
|
|
|
import { Switch } from "./switch";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
import { LuCheck } from "react-icons/lu";
|
2025-05-16 01:13:32 +03:00
|
|
|
import { TZDate } from "react-day-picker";
|
2025-03-16 20:13:34 +03:00
|
|
|
import { t } from "i18next";
|
2024-06-23 23:58:00 +03:00
|
|
|
|
|
|
|
|
export interface DateRangePickerProps {
|
|
|
|
|
/** Click handler for applying the updates from DateRangePicker. */
|
|
|
|
|
onUpdate?: (values: { range: DateRange; rangeCompare?: DateRange }) => void;
|
2024-09-06 22:26:32 +03:00
|
|
|
onReset?: () => void;
|
2024-06-23 23:58:00 +03:00
|
|
|
/** Initial value for start date */
|
|
|
|
|
initialDateFrom?: Date | string;
|
|
|
|
|
/** Initial value for end date */
|
|
|
|
|
initialDateTo?: Date | string;
|
|
|
|
|
/** Initial value for start date for compare */
|
|
|
|
|
initialCompareFrom?: Date | string;
|
|
|
|
|
/** Initial value for end date for compare */
|
|
|
|
|
initialCompareTo?: Date | string;
|
|
|
|
|
/** Alignment of popover */
|
|
|
|
|
align?: "start" | "center" | "end";
|
|
|
|
|
/** Option for locale */
|
|
|
|
|
locale?: string;
|
|
|
|
|
/** Option for showing compare feature */
|
|
|
|
|
showCompare?: boolean;
|
2025-05-16 01:13:32 +03:00
|
|
|
/** timezone */
|
|
|
|
|
timezone?: string;
|
2025-12-24 17:03:09 +03:00
|
|
|
/** First day of the week: 0 = Sunday, 1 = Monday */
|
|
|
|
|
weekStartsOn?: number;
|
2024-06-23 23:58:00 +03:00
|
|
|
}
|
|
|
|
|
|
2025-05-16 01:13:32 +03:00
|
|
|
const getDateAdjustedForTimezone = (
|
|
|
|
|
dateInput: Date | string,
|
|
|
|
|
timezone?: string,
|
|
|
|
|
): Date => {
|
2024-06-23 23:58:00 +03:00
|
|
|
if (typeof dateInput === "string") {
|
|
|
|
|
// Split the date string to get year, month, and day parts
|
|
|
|
|
const parts = dateInput.split("-").map((part) => parseInt(part, 10));
|
|
|
|
|
// Create a new Date object using the local timezone
|
|
|
|
|
// Note: Month is 0-indexed, so subtract 1 from the month part
|
2025-05-16 01:13:32 +03:00
|
|
|
const date = new TZDate(parts[0], parts[1] - 1, parts[2], timezone);
|
2024-06-23 23:58:00 +03:00
|
|
|
return date;
|
|
|
|
|
} else {
|
|
|
|
|
// If dateInput is already a Date object, return it directly
|
2025-05-16 01:13:32 +03:00
|
|
|
return new TZDate(dateInput, timezone);
|
2024-06-23 23:58:00 +03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface DateRange {
|
|
|
|
|
from: Date;
|
|
|
|
|
to: Date | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface Preset {
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Define presets
|
|
|
|
|
const PRESETS: Preset[] = [
|
2025-03-17 15:26:01 +03:00
|
|
|
{ name: "today", label: t("time.today", { ns: "common" }) },
|
|
|
|
|
{ name: "yesterday", label: t("time.yesterday", { ns: "common" }) },
|
|
|
|
|
{ name: "last7", label: t("time.last7", { ns: "common" }) },
|
|
|
|
|
{ name: "last14", label: t("time.last14", { ns: "common" }) },
|
|
|
|
|
{ name: "last30", label: t("time.last30", { ns: "common" }) },
|
|
|
|
|
{ name: "thisWeek", label: t("time.thisWeek", { ns: "common" }) },
|
|
|
|
|
{ name: "lastWeek", label: t("time.lastWeek", { ns: "common" }) },
|
|
|
|
|
{ name: "thisMonth", label: t("time.thisMonth", { ns: "common" }) },
|
|
|
|
|
{ name: "lastMonth", label: t("time.lastMonth", { ns: "common" }) },
|
2024-06-23 23:58:00 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/** The DateRangePicker component allows a user to select a range of dates */
|
2024-09-06 22:26:32 +03:00
|
|
|
export function DateRangePicker({
|
2025-05-16 01:13:32 +03:00
|
|
|
timezone,
|
|
|
|
|
initialDateFrom = (() => {
|
|
|
|
|
const date = new TZDate(new Date(), timezone);
|
|
|
|
|
date.setHours(0, 0, 0, 0);
|
|
|
|
|
return date;
|
|
|
|
|
})(),
|
2024-06-23 23:58:00 +03:00
|
|
|
initialDateTo,
|
|
|
|
|
initialCompareFrom,
|
|
|
|
|
initialCompareTo,
|
|
|
|
|
onUpdate,
|
2024-09-06 22:26:32 +03:00
|
|
|
onReset,
|
2024-06-23 23:58:00 +03:00
|
|
|
showCompare = true,
|
2025-12-24 17:03:09 +03:00
|
|
|
weekStartsOn = 0,
|
2024-09-06 22:26:32 +03:00
|
|
|
}: DateRangePickerProps) {
|
2024-06-23 23:58:00 +03:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
|
|
|
|
|
|
const [range, setRange] = useState<DateRange>({
|
2025-05-16 01:13:32 +03:00
|
|
|
from: getDateAdjustedForTimezone(initialDateFrom, timezone),
|
2024-06-23 23:58:00 +03:00
|
|
|
to: initialDateTo
|
2025-05-16 01:13:32 +03:00
|
|
|
? getDateAdjustedForTimezone(initialDateTo, timezone)
|
|
|
|
|
: getDateAdjustedForTimezone(initialDateFrom, timezone),
|
2024-06-23 23:58:00 +03:00
|
|
|
});
|
|
|
|
|
const [rangeCompare, setRangeCompare] = useState<DateRange | undefined>(
|
|
|
|
|
initialCompareFrom
|
|
|
|
|
? {
|
2025-05-16 01:13:32 +03:00
|
|
|
from: new TZDate(
|
|
|
|
|
new Date(initialCompareFrom).setHours(0, 0, 0, 0),
|
|
|
|
|
timezone,
|
|
|
|
|
),
|
2024-06-23 23:58:00 +03:00
|
|
|
to: initialCompareTo
|
2025-05-16 01:13:32 +03:00
|
|
|
? new TZDate(
|
|
|
|
|
new Date(initialCompareTo).setHours(0, 0, 0, 0),
|
|
|
|
|
timezone,
|
|
|
|
|
)
|
|
|
|
|
: new TZDate(
|
|
|
|
|
new Date(initialCompareFrom).setHours(0, 0, 0, 0),
|
|
|
|
|
timezone,
|
|
|
|
|
),
|
2024-06-23 23:58:00 +03:00
|
|
|
}
|
|
|
|
|
: undefined,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Refs to store the values of range and rangeCompare when the date picker is opened
|
Update frontend to React 19 (#22275)
* remove unused RecoilRoot and fix implicit ref callback
Remove the vestigial recoil dependency (zero consumers) and convert
the implicit-return ref callback in SearchView to block form to
prevent React 19 interpreting it as a cleanup function.
* replace react-transition-group with framer-motion in Chip
Replace CSSTransition with framer-motion AnimatePresence + motion.div
for React 19 compatibility (react-transition-group uses findDOMNode).
framer-motion is already a project dependency.
* migrate react-grid-layout v1 to v2
- Replace WidthProvider(Responsive) HOC with useContainerWidth hook
- Update types: Layout (single item) → LayoutItem, Layout[] → Layout
- Replace isDraggable/isResizable/resizeHandles with dragConfig/resizeConfig
- Update EventCallback signature for v2 API
- Remove @types/react-grid-layout (v2 includes its own types)
* upgrade vaul, next-themes, framer-motion, react-zoom-pan-pinch
- vaul: ^0.9.1 → ^1.1.2
- next-themes: ^0.3.0 → ^0.4.6
- framer-motion: ^11.5.4 → ^12.35.0 (React 19 native support)
- react-zoom-pan-pinch: 3.4.4 → latest
* upgrade to React 19, react-konva v19, eslint-plugin-react-hooks v5
Core React 19 upgrade with all necessary type fixes:
- Update RefObject types to accept T | null (React 19 refs always nullable)
- Add JSX namespace imports (no longer global in React 19)
- Add initial values to useRef calls (required in React 19)
- Fix ReactElement.props unknown type in config-form components
- Fix IconWrapper interface to use HTMLAttributes instead of index signature
- Add monaco-editor as dev dependency for type declarations
- Upgrade react-konva to v19, eslint-plugin-react-hooks to v5
* upgrade typescript to 5.9.3
* modernize Context.Provider to React 19 shorthand
Replace <Context.Provider value={...}> with <Context value={...}>
across all project-owned context providers. External library contexts
(react-icons IconContext, radix TooltipPrimitive) left unchanged.
* add runtime patches for React 19 compatibility
- Patch @radix-ui/react-compose-refs@1.1.2: stabilize useComposedRefs
to prevent infinite render loops from unstable ref callbacks
https://github.com/radix-ui/primitives/issues/3799
- Patch @radix-ui/react-slot@1.2.4: use useComposedRefs hook in
SlotClone instead of inline composeRefs to prevent re-render cycles
https://github.com/radix-ui/primitives/pull/3804
- Patch react-use-websocket@4.8.1: remove flushSync wrappers that
cause "Maximum update depth exceeded" with React 19 auto-batching
https://github.com/facebook/react/issues/27613
- Add npm overrides to ensure single hoisted copies of compose-refs
and react-slot across all Radix packages
- Add postinstall script for patch-package
- Remove leftover react-transition-group dependency
* formatting
* use availableWidth instead of useContainerWidth for grid layout
The useContainerWidth hook from react-grid-layout v2 returns raw
container width without accounting for scrollbar width, causing the
grid to not fill the full available space. Use the existing
availableWidth value from useResizeObserver which already compensates
for scrollbar width, matching the working implementation.
* remove unused carousel component and fix React 19 peer deps
Remove embla-carousel-react and its unused Carousel UI component.
Upgrade sonner v1 → v2 for native React 19 support. Remove
@types/react-icons stub (react-icons bundles its own types).
These changes eliminate all peer dependency conflicts, so
npm install works without --legacy-peer-deps.
* fix React 19 infinite re-render loop on live dashboard
The "Maximum update depth exceeded" error was caused by two issues:
1. useDeferredStreamMetadata returned a new `{}` default on every render
when SWR data was undefined, creating an unstable reference that
triggered the useEffect in useCameraLiveMode on every render cycle.
Fixed by using a stable module-level EMPTY_METADATA constant.
2. useResizeObserver's rest parameter `...refs` created a new array on
every render, causing its useEffect to re-run and re-observe elements
continuously. Fixed by stabilizing refs with useRef and only
reconnecting the observer when actual DOM elements change.
2026-03-05 17:42:38 +03:00
|
|
|
const openedRangeRef = useRef<DateRange | undefined>(undefined);
|
|
|
|
|
const openedRangeCompareRef = useRef<DateRange | undefined>(undefined);
|
2024-06-23 23:58:00 +03:00
|
|
|
|
|
|
|
|
const [selectedPreset, setSelectedPreset] = useState<string | undefined>(
|
|
|
|
|
undefined,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const [isSmallScreen, setIsSmallScreen] = useState(
|
|
|
|
|
typeof window !== "undefined" ? window.innerWidth < 960 : false,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleResize = (): void => {
|
|
|
|
|
setIsSmallScreen(window.innerWidth < 960);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener("resize", handleResize);
|
|
|
|
|
|
|
|
|
|
// Clean up event listener on unmount
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener("resize", handleResize);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const getPresetRange = (presetName: string): DateRange => {
|
|
|
|
|
const preset = PRESETS.find(({ name }) => name === presetName);
|
|
|
|
|
if (!preset) throw new Error(`Unknown date range preset: ${presetName}`);
|
2025-05-16 01:13:32 +03:00
|
|
|
const from = new TZDate(new Date(), timezone);
|
|
|
|
|
const to = new TZDate(new Date(), timezone);
|
2025-12-24 17:03:09 +03:00
|
|
|
const dayOfWeek = from.getDay();
|
|
|
|
|
const daysFromWeekStart = (dayOfWeek - weekStartsOn + 7) % 7;
|
|
|
|
|
const first = from.getDate() - daysFromWeekStart;
|
2024-06-23 23:58:00 +03:00
|
|
|
|
|
|
|
|
switch (preset.name) {
|
|
|
|
|
case "today":
|
|
|
|
|
from.setHours(0, 0, 0, 0);
|
|
|
|
|
to.setHours(23, 59, 59, 999);
|
|
|
|
|
break;
|
|
|
|
|
case "yesterday":
|
|
|
|
|
from.setDate(from.getDate() - 1);
|
|
|
|
|
from.setHours(0, 0, 0, 0);
|
|
|
|
|
to.setDate(to.getDate() - 1);
|
|
|
|
|
to.setHours(23, 59, 59, 999);
|
|
|
|
|
break;
|
|
|
|
|
case "last7":
|
|
|
|
|
from.setDate(from.getDate() - 6);
|
|
|
|
|
from.setHours(0, 0, 0, 0);
|
|
|
|
|
to.setHours(23, 59, 59, 999);
|
|
|
|
|
break;
|
|
|
|
|
case "last14":
|
|
|
|
|
from.setDate(from.getDate() - 13);
|
|
|
|
|
from.setHours(0, 0, 0, 0);
|
|
|
|
|
to.setHours(23, 59, 59, 999);
|
|
|
|
|
break;
|
|
|
|
|
case "last30":
|
|
|
|
|
from.setDate(from.getDate() - 29);
|
|
|
|
|
from.setHours(0, 0, 0, 0);
|
|
|
|
|
to.setHours(23, 59, 59, 999);
|
|
|
|
|
break;
|
|
|
|
|
case "thisWeek":
|
|
|
|
|
from.setDate(first);
|
|
|
|
|
from.setHours(0, 0, 0, 0);
|
|
|
|
|
to.setHours(23, 59, 59, 999);
|
|
|
|
|
break;
|
|
|
|
|
case "lastWeek":
|
2025-12-24 17:03:09 +03:00
|
|
|
from.setDate(first - 7);
|
|
|
|
|
to.setDate(first - 1);
|
2024-06-23 23:58:00 +03:00
|
|
|
from.setHours(0, 0, 0, 0);
|
|
|
|
|
to.setHours(23, 59, 59, 999);
|
|
|
|
|
break;
|
|
|
|
|
case "thisMonth":
|
|
|
|
|
from.setDate(1);
|
|
|
|
|
from.setHours(0, 0, 0, 0);
|
|
|
|
|
to.setHours(23, 59, 59, 999);
|
|
|
|
|
break;
|
|
|
|
|
case "lastMonth":
|
|
|
|
|
from.setMonth(from.getMonth() - 1);
|
|
|
|
|
from.setDate(1);
|
|
|
|
|
from.setHours(0, 0, 0, 0);
|
|
|
|
|
to.setDate(0);
|
|
|
|
|
to.setHours(23, 59, 59, 999);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { from, to };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setPreset = (preset: string): void => {
|
|
|
|
|
const range = getPresetRange(preset);
|
|
|
|
|
setRange(range);
|
|
|
|
|
if (rangeCompare) {
|
|
|
|
|
const rangeCompare = {
|
2025-05-16 01:13:32 +03:00
|
|
|
from: new TZDate(
|
2024-06-23 23:58:00 +03:00
|
|
|
range.from.getFullYear() - 1,
|
|
|
|
|
range.from.getMonth(),
|
|
|
|
|
range.from.getDate(),
|
2025-05-16 01:13:32 +03:00
|
|
|
timezone,
|
2024-06-23 23:58:00 +03:00
|
|
|
),
|
|
|
|
|
to: range.to
|
2025-05-16 01:13:32 +03:00
|
|
|
? new TZDate(
|
2024-06-23 23:58:00 +03:00
|
|
|
range.to.getFullYear() - 1,
|
|
|
|
|
range.to.getMonth(),
|
|
|
|
|
range.to.getDate(),
|
2025-05-16 01:13:32 +03:00
|
|
|
timezone,
|
2024-06-23 23:58:00 +03:00
|
|
|
)
|
|
|
|
|
: undefined,
|
|
|
|
|
};
|
|
|
|
|
setRangeCompare(rangeCompare);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const checkPreset = (): void => {
|
|
|
|
|
for (const preset of PRESETS) {
|
|
|
|
|
const presetRange = getPresetRange(preset.name);
|
|
|
|
|
|
2025-05-16 01:13:32 +03:00
|
|
|
const normalizedRangeFrom = new TZDate(range.from, timezone);
|
2024-06-23 23:58:00 +03:00
|
|
|
normalizedRangeFrom.setHours(0, 0, 0, 0);
|
2025-05-16 01:13:32 +03:00
|
|
|
const normalizedPresetFrom = new TZDate(
|
2024-06-23 23:58:00 +03:00
|
|
|
presetRange.from.setHours(0, 0, 0, 0),
|
2025-05-16 01:13:32 +03:00
|
|
|
timezone,
|
2024-06-23 23:58:00 +03:00
|
|
|
);
|
|
|
|
|
|
2025-05-16 01:13:32 +03:00
|
|
|
const normalizedRangeTo = new TZDate(range.to ?? new Date(0), timezone);
|
2024-06-23 23:58:00 +03:00
|
|
|
normalizedRangeTo.setHours(0, 0, 0, 0);
|
2025-05-16 01:13:32 +03:00
|
|
|
const normalizedPresetTo = new TZDate(
|
2024-06-23 23:58:00 +03:00
|
|
|
presetRange.to?.setHours(0, 0, 0, 0) ?? 0,
|
2025-05-16 01:13:32 +03:00
|
|
|
timezone,
|
2024-06-23 23:58:00 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
normalizedRangeFrom.getTime() === normalizedPresetFrom.getTime() &&
|
|
|
|
|
normalizedRangeTo.getTime() === normalizedPresetTo.getTime()
|
|
|
|
|
) {
|
|
|
|
|
setSelectedPreset(preset.name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSelectedPreset(undefined);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resetValues = (): void => {
|
|
|
|
|
setRange({
|
|
|
|
|
from:
|
|
|
|
|
typeof initialDateFrom === "string"
|
|
|
|
|
? getDateAdjustedForTimezone(initialDateFrom)
|
|
|
|
|
: initialDateFrom,
|
|
|
|
|
to: initialDateTo
|
|
|
|
|
? typeof initialDateTo === "string"
|
|
|
|
|
? getDateAdjustedForTimezone(initialDateTo)
|
|
|
|
|
: initialDateTo
|
|
|
|
|
: typeof initialDateFrom === "string"
|
|
|
|
|
? getDateAdjustedForTimezone(initialDateFrom)
|
|
|
|
|
: initialDateFrom,
|
|
|
|
|
});
|
|
|
|
|
setRangeCompare(
|
|
|
|
|
initialCompareFrom
|
|
|
|
|
? {
|
|
|
|
|
from:
|
|
|
|
|
typeof initialCompareFrom === "string"
|
|
|
|
|
? getDateAdjustedForTimezone(initialCompareFrom)
|
|
|
|
|
: initialCompareFrom,
|
|
|
|
|
to: initialCompareTo
|
|
|
|
|
? typeof initialCompareTo === "string"
|
|
|
|
|
? getDateAdjustedForTimezone(initialCompareTo)
|
|
|
|
|
: initialCompareTo
|
|
|
|
|
: typeof initialCompareFrom === "string"
|
|
|
|
|
? getDateAdjustedForTimezone(initialCompareFrom)
|
|
|
|
|
: initialCompareFrom,
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
checkPreset();
|
|
|
|
|
}, [range]);
|
|
|
|
|
|
|
|
|
|
const PresetButton = ({
|
|
|
|
|
preset,
|
|
|
|
|
label,
|
|
|
|
|
isSelected,
|
|
|
|
|
}: {
|
|
|
|
|
preset: string;
|
|
|
|
|
label: string;
|
|
|
|
|
isSelected: boolean;
|
|
|
|
|
}): JSX.Element => (
|
|
|
|
|
<Button
|
2024-09-06 22:26:32 +03:00
|
|
|
className={cn(isSelected && "pointer-events-none text-primary")}
|
2024-10-23 01:07:42 +03:00
|
|
|
aria-label={label}
|
2024-06-23 23:58:00 +03:00
|
|
|
variant="ghost"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setPreset(preset);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<>
|
|
|
|
|
<span className={cn("pr-2 opacity-0", isSelected && "opacity-70")}>
|
|
|
|
|
<LuCheck width={18} height={18} />
|
|
|
|
|
</span>
|
|
|
|
|
{label}
|
|
|
|
|
</>
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Helper function to check if two date ranges are equal
|
|
|
|
|
const areRangesEqual = (a?: DateRange, b?: DateRange): boolean => {
|
|
|
|
|
if (!a || !b) return a === b; // If either is undefined, return true if both are undefined
|
|
|
|
|
return (
|
|
|
|
|
a.from.getTime() === b.from.getTime() &&
|
|
|
|
|
(!a.to || !b.to || a.to.getTime() === b.to.getTime())
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isOpen) {
|
|
|
|
|
openedRangeRef.current = range;
|
|
|
|
|
openedRangeCompareRef.current = rangeCompare;
|
|
|
|
|
}
|
|
|
|
|
}, [isOpen]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="w-full">
|
2024-09-14 16:42:56 +03:00
|
|
|
<div className="flex flex-row items-start justify-center py-2">
|
2024-06-23 23:58:00 +03:00
|
|
|
<div className="flex">
|
|
|
|
|
<div className="flex flex-col">
|
|
|
|
|
<div className="flex flex-col items-center justify-end gap-2 px-3 pb-4 lg:flex-row lg:items-start lg:pb-0">
|
|
|
|
|
{showCompare && (
|
|
|
|
|
<div className="flex items-center space-x-2 py-1 pr-4">
|
|
|
|
|
<Switch
|
|
|
|
|
defaultChecked={Boolean(rangeCompare)}
|
|
|
|
|
onCheckedChange={(checked: boolean) => {
|
|
|
|
|
if (checked) {
|
|
|
|
|
if (!range.to) {
|
|
|
|
|
setRange({
|
|
|
|
|
from: range.from,
|
|
|
|
|
to: range.from,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setRangeCompare({
|
|
|
|
|
from: new Date(
|
|
|
|
|
range.from.getFullYear(),
|
|
|
|
|
range.from.getMonth(),
|
|
|
|
|
range.from.getDate() - 365,
|
|
|
|
|
),
|
|
|
|
|
to: range.to
|
|
|
|
|
? new Date(
|
|
|
|
|
range.to.getFullYear() - 1,
|
|
|
|
|
range.to.getMonth(),
|
|
|
|
|
range.to.getDate(),
|
|
|
|
|
)
|
|
|
|
|
: new Date(
|
|
|
|
|
range.from.getFullYear() - 1,
|
|
|
|
|
range.from.getMonth(),
|
|
|
|
|
range.from.getDate(),
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
setRangeCompare(undefined);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
id="compare-mode"
|
|
|
|
|
/>
|
|
|
|
|
<Label htmlFor="compare-mode">Compare</Label>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
{isSmallScreen && (
|
|
|
|
|
<Select
|
|
|
|
|
defaultValue={selectedPreset}
|
|
|
|
|
onValueChange={(value) => {
|
|
|
|
|
setPreset(value);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<SelectTrigger className="mx-auto mb-2 w-[180px]">
|
2025-05-15 01:44:06 +03:00
|
|
|
<SelectValue
|
|
|
|
|
placeholder={t("dates.selectPreset", {
|
|
|
|
|
ns: "components/filter",
|
|
|
|
|
})}
|
|
|
|
|
/>
|
2024-06-23 23:58:00 +03:00
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{PRESETS.map((preset) => (
|
|
|
|
|
<SelectItem key={preset.name} value={preset.name}>
|
|
|
|
|
{preset.label}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
)}
|
|
|
|
|
<div>
|
|
|
|
|
<Calendar
|
|
|
|
|
mode="range"
|
|
|
|
|
onSelect={(value: { from?: Date; to?: Date } | undefined) => {
|
|
|
|
|
if (value?.from != null) {
|
|
|
|
|
setRange({ from: value.from, to: value?.to });
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
selected={range}
|
|
|
|
|
numberOfMonths={isSmallScreen ? 1 : 2}
|
|
|
|
|
defaultMonth={
|
|
|
|
|
new Date(
|
|
|
|
|
new Date().setMonth(
|
|
|
|
|
new Date().getMonth() - (isSmallScreen ? 0 : 1),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-05-16 01:13:32 +03:00
|
|
|
timeZone={timezone}
|
2024-06-23 23:58:00 +03:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{!isSmallScreen && (
|
|
|
|
|
<div className="flex flex-col items-end gap-1 pb-6 pl-6 pr-2">
|
|
|
|
|
<div className="flex w-full flex-col items-end gap-1 pb-6 pl-6 pr-2">
|
|
|
|
|
{PRESETS.map((preset) => (
|
|
|
|
|
<PresetButton
|
|
|
|
|
key={preset.name}
|
|
|
|
|
preset={preset.name}
|
|
|
|
|
label={preset.label}
|
|
|
|
|
isSelected={selectedPreset === preset.name}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2024-09-25 21:45:42 +03:00
|
|
|
<div className="mx-auto flex w-64 items-center justify-evenly gap-2 py-2">
|
2024-06-23 23:58:00 +03:00
|
|
|
<Button
|
2024-09-06 22:26:32 +03:00
|
|
|
variant="select"
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("button.apply", { ns: "common" })}
|
2024-06-23 23:58:00 +03:00
|
|
|
onClick={() => {
|
|
|
|
|
setIsOpen(false);
|
|
|
|
|
if (
|
|
|
|
|
!areRangesEqual(range, openedRangeRef.current) ||
|
|
|
|
|
!areRangesEqual(rangeCompare, openedRangeCompareRef.current)
|
|
|
|
|
) {
|
|
|
|
|
onUpdate?.({ range, rangeCompare });
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-03-16 20:13:34 +03:00
|
|
|
{t("button.apply", { ns: "common" })}
|
2024-06-23 23:58:00 +03:00
|
|
|
</Button>
|
2024-09-25 21:45:42 +03:00
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setIsOpen(false);
|
|
|
|
|
resetValues();
|
|
|
|
|
onReset?.();
|
|
|
|
|
}}
|
|
|
|
|
variant="ghost"
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("button.reset", { ns: "common" })}
|
2024-09-25 21:45:42 +03:00
|
|
|
>
|
2025-03-16 20:13:34 +03:00
|
|
|
{t("button.reset", { ns: "common" })}
|
2024-09-25 21:45:42 +03:00
|
|
|
</Button>
|
2024-06-23 23:58:00 +03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-09-06 22:26:32 +03:00
|
|
|
}
|