Skip to content

Commit 9b7cc83

Browse files
authored
add new date search filter (#3065)
* add new complicated filters * clarity updates * update date range filter
1 parent e69303e commit 9b7cc83

File tree

3 files changed

+114
-13
lines changed

3 files changed

+114
-13
lines changed

web/src/components/search/filtering/Filters.tsx

+89-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import { FilterDropdown } from "./FilterDropdown";
2424
import { listSourceMetadata } from "@/lib/sources";
2525
import { SourceIcon } from "@/components/SourceIcon";
2626
import { TagFilter } from "./TagFilter";
27+
import { Calendar } from "@/components/ui/calendar";
28+
import { Popover, PopoverTrigger } from "@/components/ui/popover";
29+
import { PopoverContent } from "@radix-ui/react-popover";
30+
import { CalendarIcon } from "lucide-react";
31+
import { buildDateString, getTimeAgoString } from "@/lib/dateUtils";
2732

2833
const SectionTitle = ({ children }: { children: string }) => (
2934
<div className="font-bold text-xs mt-2 flex">{children}</div>
@@ -106,10 +111,39 @@ export function SourceSelector({
106111
<FiFilter className="my-auto ml-2" size="16" />
107112
</div>
108113

109-
<SectionTitle>Time Range</SectionTitle>
110-
<div className="mt-2">
111-
<DateRangeSelector value={timeRange} onValueChange={setTimeRange} />
112-
</div>
114+
<Popover>
115+
<PopoverTrigger asChild>
116+
<div className="cursor-pointer">
117+
<SectionTitle>Time Range</SectionTitle>
118+
<p className="text-sm text-default mt-2">
119+
{getTimeAgoString(timeRange?.from!) || "Select a time range"}
120+
</p>
121+
</div>
122+
</PopoverTrigger>
123+
<PopoverContent
124+
className="bg-background border-border border rounded-md z-[200] p-0"
125+
align="start"
126+
>
127+
<Calendar
128+
mode="range"
129+
selected={
130+
timeRange
131+
? { from: new Date(timeRange.from), to: new Date(timeRange.to) }
132+
: undefined
133+
}
134+
onSelect={(daterange) => {
135+
const initialDate = daterange?.from || new Date();
136+
const endDate = daterange?.to || new Date();
137+
setTimeRange({
138+
from: initialDate,
139+
to: endDate,
140+
selectValue: timeRange?.selectValue || "",
141+
});
142+
}}
143+
className="rounded-md "
144+
/>
145+
</PopoverContent>
146+
</Popover>
113147

114148
{availableTags.length > 0 && (
115149
<>
@@ -424,14 +458,57 @@ export function HorizontalSourceSelector({
424458

425459
return (
426460
<div className="flex flex-nowrap space-x-2">
427-
<div className="max-w-24">
428-
<DateRangeSelector
429-
isHorizontal
430-
value={timeRange}
431-
onValueChange={setTimeRange}
432-
className="bg-background-search-filter"
433-
/>
434-
</div>
461+
<Popover>
462+
<PopoverTrigger asChild>
463+
<div
464+
className={`
465+
border
466+
max-w-36
467+
border-border
468+
rounded-lg
469+
bg-background
470+
max-h-96
471+
overflow-y-scroll
472+
overscroll-contain
473+
px-3
474+
text-sm
475+
py-1.5
476+
select-none
477+
cursor-pointer
478+
w-fit
479+
gap-x-1
480+
hover:bg-hover
481+
bg-hover-light
482+
flex
483+
items-center
484+
bg-background-search-filter
485+
`}
486+
>
487+
<CalendarIcon className="h-4 w-4" />
488+
489+
{timeRange?.from ? getTimeAgoString(timeRange.from) : "Since"}
490+
</div>
491+
</PopoverTrigger>
492+
<PopoverContent
493+
className="bg-background border-border border rounded-md z-[200] p-0"
494+
align="start"
495+
>
496+
<Calendar
497+
mode="single"
498+
selected={timeRange ? new Date(timeRange.from) : undefined}
499+
onSelect={(date) => {
500+
const selectedDate = date || new Date();
501+
const today = new Date();
502+
setTimeRange({
503+
from: selectedDate > today ? today : selectedDate,
504+
to: today,
505+
selectValue: timeRange?.selectValue || "",
506+
});
507+
}}
508+
className="rounded-md "
509+
/>
510+
</PopoverContent>
511+
</Popover>
435512

436513
{existingSources.length > 0 && (
437514
<FilterDropdown

web/src/components/ui/calendar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function Calendar({
4141
buttonVariants({ variant: "ghost" }),
4242
"h-9 w-9 p-0 font-normal aria-selected:opacity-100"
4343
),
44-
day_range_end: "day-range-end",
44+
day_range_end: "day-range-end !text-neutral-200",
4545
day_selected:
4646
"bg-neutral-900 text-neutral-50 hover:bg-neutral-900 hover:text-neutral-50 focus:bg-neutral-900 focus:text-neutral-50 dark:bg-neutral-50 dark:text-neutral-900 dark:hover:bg-neutral-50 dark:hover:text-neutral-900 dark:focus:bg-neutral-50 dark:focus:text-neutral-900",
4747
day_today:

web/src/lib/dateUtils.ts

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { DateRangePickerValue } from "@/app/ee/admin/performance/DateRangeSelector";
2+
13
export function getXDaysAgo(daysAgo: number) {
24
const today = new Date();
35
const daysAgoDate = new Date(today);
@@ -46,3 +48,25 @@ export const timestampToReadableDate = (timestamp: string) => {
4648
date.toLocaleTimeString(undefined, timeOptions)
4749
);
4850
};
51+
52+
export const buildDateString = (date: Date | null) => {
53+
return date
54+
? `${Math.round(
55+
(new Date().getTime() - date.getTime()) / (1000 * 60 * 60 * 24)
56+
)} days ago`
57+
: "Select a time range";
58+
};
59+
export const getTimeAgoString = (date: Date | null) => {
60+
if (!date) return null;
61+
62+
const diffMs = new Date().getTime() - date.getTime();
63+
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
64+
const diffWeeks = Math.floor(diffDays / 7);
65+
const diffMonths = Math.floor(diffDays / 30);
66+
67+
if (buildDateString(date).includes("Today")) return "Today";
68+
if (diffDays === 1) return "Yesterday";
69+
if (diffDays < 7) return `${diffDays}d ago`;
70+
if (diffDays < 30) return `${diffWeeks}w ago`;
71+
return `${diffMonths}mo ago`;
72+
};

0 commit comments

Comments
 (0)