🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(date-picker): first calendar click starts a new range (LFE-8156) (#14536)
Nikita Kabardin4c18421abb66
diff --git a/web/src/components/date-picker.clienttest.tsx b/web/src/components/date-picker.clienttest.tsxnew file mode 100644index 0000000000..bbe45f7cfc--- /dev/null+++ b/web/src/components/date-picker.clienttest.tsx@@ -0,0 +1,137 @@+import { fireEvent, render, screen } from "@testing-library/react";+import { useState } from "react";+import { type DateRange } from "react-day-picker";+import { Calendar } from "@/src/components/ui/calendar";+import { nextRangeForDayClick } from "@/src/components/date-picker";++/**+ * Regression coverage for LFE-8156. The range calendar used to feel "sticky":+ * with a range already selected, a single click extended that range (the click+ * became the end of the OLD range) instead of starting a new one. The pickers+ * now drive selection from the clicked day via `nextRangeForDayClick` so the+ * first click always starts a fresh range and the second sets the end —+ * avoiding react-day-picker's edge behaviours (extend-complete-range,+ * clear-on-same-day-click) entirely.+ */+describe("nextRangeForDayClick (LFE-8156)", () => {+ const jun = (d: number) => new Date(2026, 5, d);++ it("starts a fresh range when there is no current selection", () => {+ expect(nextRangeForDayClick(undefined, jun(10))).toEqual({+ from: jun(10),+ to: undefined,+ });+ });++ it("starts a fresh range (first click) when a complete range exists", () => {+ expect(+ nextRangeForDayClick({ from: jun(18), to: jun(25) }, jun(10)),+ ).toEqual({ from: jun(10), to: undefined });+ });++ it("starts a fresh range when re-clicking the single day of a same-day range (does not clear)", () => {+ // A short preset (e.g. Past 1 hour) leaves from/to on the same calendar day.+ // react-day-picker's resetOnSelect would clear the selection here; we must+ // start a new range at the clicked day instead.+ expect(+ nextRangeForDayClick({ from: jun(25), to: jun(25) }, jun(25)),+ ).toEqual({ from: jun(25), to: undefined });+ });++ it("sets the end on the second click", () => {+ expect(+ nextRangeForDayClick({ from: jun(10), to: undefined }, jun(20)),+ ).toEqual({ from: jun(10), to: jun(20) });+ });++ it("swaps when the second click lands before the start", () => {+ expect(+ nextRangeForDayClick({ from: jun(20), to: undefined }, jun(10)),+ ).toEqual({ from: jun(10), to: jun(20) });+ });+});++/**+ * Integration: drive the real calendar exactly as the pickers do — compute the+ * next range from the clicked day and "commit" (write URL / form) only once+ * both ends are set.+ */+function RangeCalendarHarness({+ initial,+ onCommit,+}: {+ initial: DateRange;+ onCommit: (range: { from: Date; to: Date }) => void;+}) {+ const [range, setRange] = useState<DateRange | undefined>(initial);+ return (+ <Calendar+ mode="range"+ defaultMonth={new Date(2026, 5, 1)}+ selected={range}+ onSelect={(_, triggerDay) => {+ if (!triggerDay) return;+ const next = nextRangeForDayClick(range, triggerDay);+ setRange(next);+ if (next.from && next.to) {+ onCommit({ from: next.from, to: next.to });+ }+ }}+ />+ );+}++const clickDay = (name: RegExp) =>+ fireEvent.click(screen.getByRole("button", { name }));++const ariaOf = (name: RegExp) =>+ screen.getByRole("button", { name }).getAttribute("aria-label") ?? "";++describe("range calendar first-click selection (LFE-8156)", () => {+ it("first click starts fresh (no commit); second click commits the user's two days", () => {+ const onCommit = vi.fn();+ render(+ <RangeCalendarHarness+ initial={{ from: new Date(2026, 5, 18), to: new Date(2026, 5, 25) }}+ onCommit={onCommit}+ />,+ );++ // First click = Start: previous range cleared, nothing committed yet.+ clickDay(/June 10th, 2026/);+ expect(onCommit).not.toHaveBeenCalled();+ expect(ariaOf(/June 10th, 2026/)).toContain("selected");+ // The stale Jun 25 end is no longer part of the selection.+ expect(ariaOf(/June 25th, 2026/)).not.toContain("selected");++ // Second click = End: commits Jun 10 – Jun 20, not the stale Jun 25.+ clickDay(/June 20th, 2026/);+ expect(onCommit).toHaveBeenCalledTimes(1);+ const committed = onCommit.mock.calls[0][0];+ expect(committed.from.getDate()).toBe(10);+ expect(committed.to.getDate()).toBe(20);+ });++ it("re-clicking the only day of a same-day range starts a new range instead of clearing it", () => {+ const onCommit = vi.fn();+ render(+ <RangeCalendarHarness+ initial={{ from: new Date(2026, 5, 25), to: new Date(2026, 5, 25) }}+ onCommit={onCommit}+ />,+ );++ // Clicking the single selected day must keep it selected as the new start,+ // not wipe the highlight (the react-day-picker resetOnSelect trap).+ clickDay(/June 25th, 2026/);+ expect(onCommit).not.toHaveBeenCalled();+ expect(ariaOf(/June 25th, 2026/)).toContain("selected");++ // Second click before the start swaps into a valid range.+ clickDay(/June 20th, 2026/);+ expect(onCommit).toHaveBeenCalledTimes(1);+ const committed = onCommit.mock.calls[0][0];+ expect(committed.from.getDate()).toBe(20);+ expect(committed.to.getDate()).toBe(25);+ });+});diff --git a/web/src/components/date-picker.tsx b/web/src/components/date-picker.tsxindex c609fca3a2..a3bfb41dc9 100644--- a/web/src/components/date-picker.tsx+++ b/web/src/components/date-picker.tsx@@ -13,7 +13,7 @@ import {import { cn } from "@/src/utils/tailwind";import { type DateRange as RDPDateRange } from "react-day-picker";import { format } from "date-fns";-import { useEffect, useState, useCallback } from "react";+import { useEffect, useMemo, useState, useCallback } from "react";import { setBeginningOfDay, setEndOfDay } from "@/src/utils/dates";import { TimePicker } from "@/src/components/ui/time-picker";import { DashboardDateRangeDropdown } from "@/src/components/date-range-dropdowns";@@ -100,6 +100,30 @@ export type DatePickerWithRangeProps = {) => void;};+/**+ * Computes the next range for a calendar day click so the FIRST click always+ * starts a new range and the SECOND click sets the end (LFE-8156). The decision+ * is driven by the clicked day, not by react-day-picker's range state machine,+ * which otherwise extends a complete range (the original "sticky" bug), clears+ * the selection when the single day of a same-day range is re-clicked, or+ * extends a left-over start that survived a closed popover.+ */+export function nextRangeForDayClick(+ current: RDPDateRange | undefined,+ clickedDay: Date,+): { from: Date; to: Date | undefined } {+ // Mid-selection — a start without an end → this click sets the end (swapping+ // if the click lands before the start).+ if (current?.from && !current.to) {+ return clickedDay < current.from+ ? { from: clickedDay, to: current.from }+ : { from: current.from, to: clickedDay };+ }+ // Empty or a complete range → start a fresh range; the end stays orphaned+ // until the next click.+ return { from: clickedDay, to: undefined };+}+export function DatePickerWithRange({className,dateRange,@@ -147,14 +171,13 @@ export function DatePickerWithRange({}};- const onCalendarSelection = (range?: RDPDateRange) => {- const newRange = range- ? {- from: range.from ? setBeginningOfDay(range.from) : undefined,- to: range.to ? setEndOfDay(range.to) : undefined,- }- : undefined;-+ const onCalendarSelection = (triggerDay?: Date) => {+ if (!triggerDay) return;+ const next = nextRangeForDayClick(internalDateRange, triggerDay);+ const newRange: RDPDateRange = {+ from: setBeginningOfDay(next.from),+ to: next.to ? setEndOfDay(next.to) : undefined,+ };setInternalDateRange(newRange);updateDashboardDateRange(newRange, setDateRangeAndOption);};@@ -185,7 +208,15 @@ export function DatePickerWithRange({<divclassName={cn("my-3 flex flex-col-reverse gap-2 md:flex-row", className)}>- <Popover>+ <Popover+ onOpenChange={(open) => {+ // Discard an abandoned first click on close so the next open starts+ // fresh instead of extending a left-over start (LFE-8156).+ if (!open && internalDateRange?.from && !internalDateRange.to) {+ setInternalDateRange(dateRange);+ }+ }}+ ><PopoverTrigger asChild><Buttonid="date"@@ -214,35 +245,47 @@ export function DatePickerWithRange({<CalendarautoFocus={true}mode="range"+ // First click starts a new range, second sets the end — see+ // onCalendarSelection / nextRangeForDayClick (LFE-8156).defaultMonth={internalDateRange?.from}selected={internalDateRange}- onSelect={onCalendarSelection}+ onSelect={(_, triggerDay) => onCalendarSelection(triggerDay)}numberOfMonths={2}- className="[&>div]:hidden sm:[&>div]:block [&>div:first-child]:block"+ // react-day-picker v9 lays the two months out in a flex row; on+ // narrow screens drop the second month rather than overriding that+ // flex (the old `[&>div]` rule stacked them vertically). LFE-8156.+ className="max-sm:[&>div>div:last-child]:hidden"disabled={disabled}/>- <div className="flex flex-col gap-2 border-t-2 py-1.5 sm:flex-row sm:gap-0">- <div className="px-3">- <p className="px-1 text-sm font-medium">- Start<span className="hidden sm:inline"> time</span>- </p>- <TimePicker- date={internalDateRange?.from}- setDate={onStartTimeSelection}- className="border-0 px-0 pt-1"- />- </div>- <div className="px-3">- <p className="px-1 text-sm font-medium">- End<span className="hidden sm:inline"> time</span>- </p>- <TimePicker- date={internalDateRange?.to}- setDate={onEndTimeSelection}- className="border-0 px-0 pt-1"- />+ {/* Time pickers tune the boundaries of a *complete* range. During+ the partial range (after the first click, before the second) they+ are hidden: the End-time input would silently ignore edits, and a+ typed Start time would be clobbered by setBeginningOfDay on the+ next calendar click. See LFE-8156. */}+ {internalDateRange?.from && internalDateRange.to && (+ <div className="flex flex-col gap-2 border-t-2 py-1.5 sm:flex-row sm:gap-0">+ <div className="px-3">+ <p className="px-1 text-sm font-medium">+ Start<span className="hidden sm:inline"> time</span>+ </p>+ <TimePicker+ date={internalDateRange?.from}+ setDate={onStartTimeSelection}+ className="border-0 px-0 pt-1"+ />+ </div>+ <div className="px-3">+ <p className="px-1 text-sm font-medium">+ End<span className="hidden sm:inline"> time</span>+ </p>+ <TimePicker+ date={internalDateRange?.to}+ setDate={onEndTimeSelection}+ className="border-0 px-0 pt-1"+ />+ </div></div>- </div>+ )}</PopoverContent></Popover><DashboardDateRangeDropdown@@ -296,31 +339,32 @@ export function TimeRangePicker({// Convert TimeRange to DateRange for internal useconst dateRange = timeRange && "from" in timeRange ? timeRange : undefined;- const [internalDateRange, setInternalDateRange] = useState<- RDPDateRange | undefined- >(dateRange);-- // Update internal date range when timeRange changes- useEffect(() => {+ // The committed range expressed as a react-day-picker DateRange: a custom+ // range as-is, a named preset as its current absolute window (presets+ // re-evaluate to "now"), otherwise none. Reused to reset the editable range+ // when the popover closes mid-selection.+ const committedDateRange = useMemo<RDPDateRange | undefined>(() => {if (rangeType === "custom") {- // Custom range - use as is- setInternalDateRange(dateRange);- } else if (rangeType === "named" && timeRange && "range" in timeRange) {- // Preset range - look up in generic time ranges+ return dateRange;+ }+ if (rangeType === "named" && timeRange && "range" in timeRange) {const setting = TIME_RANGES[timeRange.range as keyof typeof TIME_RANGES];if (setting && setting.minutes) {const now = new Date();- setInternalDateRange({- from: addMinutes(now, -setting.minutes),- to: now,- });- } else {- setInternalDateRange(undefined);+ return { from: addMinutes(now, -setting.minutes), to: now };}- } else {- setInternalDateRange(undefined);}- }, [timeRange, dateRange, rangeType]);+ return undefined;+ }, [rangeType, timeRange, dateRange]);++ const [internalDateRange, setInternalDateRange] = useState<+ RDPDateRange | undefined+ >(committedDateRange);++ // Re-sync the editable range whenever the committed selection changes.+ useEffect(() => {+ setInternalDateRange(committedDateRange);+ }, [committedDateRange]);const setNewDateRange = (internalDateRange: RDPDateRange | undefined,@@ -344,14 +388,13 @@ export function TimeRangePicker({}};- const onCalendarSelection = (range?: RDPDateRange) => {- const newRange = range- ? {- from: range.from ? setBeginningOfDay(range.from) : undefined,- to: range.to ? setEndOfDay(range.to) : undefined,- }- : undefined;-+ const onCalendarSelection = (triggerDay?: Date) => {+ if (!triggerDay) return;+ const next = nextRangeForDayClick(internalDateRange, triggerDay);+ const newRange: RDPDateRange = {+ from: setBeginningOfDay(next.from),+ to: next.to ? setEndOfDay(next.to) : undefined,+ };setInternalDateRange(newRange);updateDateRange(newRange);};@@ -387,12 +430,19 @@ export function TimeRangePicker({const [isOpen, setIsOpen] = useState(false);const [tab, setTab] = useState<"presets" | "calendar">("presets");- const handleOpenChange = useCallback((open: boolean) => {- setIsOpen(open);- if (open) {- setTab("presets");- }- }, []);+ const handleOpenChange = useCallback(+ (open: boolean) => {+ setIsOpen(open);+ if (open) {+ setTab("presets");+ } else if (internalDateRange?.from && !internalDateRange.to) {+ // Discard an abandoned first click so the next open starts fresh+ // instead of extending a left-over start (LFE-8156).+ setInternalDateRange(committedDateRange);+ }+ },+ [internalDateRange, committedDateRange],+ );const getDisplayContent = () => {if (rangeType === "custom") {@@ -451,30 +501,39 @@ export function TimeRangePicker({<><Calendarmode="range"+ // First click starts a new range, second sets the end — see+ // onCalendarSelection / nextRangeForDayClick (LFE-8156).defaultMonth={internalDateRange?.from || new Date()}selected={internalDateRange}- onSelect={onCalendarSelection}+ onSelect={(_, triggerDay) => onCalendarSelection(triggerDay)}numberOfMonths={1}disabled={calendarDisabled}/>- <div className="flex flex-col gap-3 border-t p-3">- <div className="flex flex-col gap-1">- <p className="px-1 text-sm font-medium">Start time</p>- <TimePicker- date={internalDateRange?.from}- setDate={onStartTimeSelection}- className="border-0 px-0 py-0"- />- </div>- <div className="flex flex-col gap-1">- <p className="px-1 text-sm font-medium">End time</p>- <TimePicker- date={internalDateRange?.to}- setDate={onEndTimeSelection}- className="border-0 px-0 py-0"- />+ {/* Time pickers tune the boundaries of a *complete* range.+ During the partial range (after the first click, before the+ second) they are hidden: the End-time input would silently+ ignore edits, and a typed Start time would be clobbered by+ setBeginningOfDay on the next calendar click. See LFE-8156. */}+ {internalDateRange?.from && internalDateRange.to && (+ <div className="flex flex-col gap-3 border-t p-3">+ <div className="flex flex-col gap-1">+ <p className="px-1 text-sm font-medium">Start time</p>+ <TimePicker+ date={internalDateRange?.from}+ setDate={onStartTimeSelection}+ className="border-0 px-0 py-0"+ />+ </div>+ <div className="flex flex-col gap-1">+ <p className="px-1 text-sm font-medium">End time</p>+ <TimePicker+ date={internalDateRange?.to}+ setDate={onEndTimeSelection}+ className="border-0 px-0 py-0"+ />+ </div></div>- </div>+ )}</>) : (/* Always show preset options dropdown */