Skip to content

Commit

Permalink
fix(date-picker): ensure date range automatically applies on selectio…
Browse files Browse the repository at this point in the history
…n for dashboard and request pages
  • Loading branch information
use-tusk[bot] authored Oct 8, 2024
1 parent f7b37ab commit 84bd865
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
23 changes: 12 additions & 11 deletions web/components/shared/themed/themedTimeFilterShadCN.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export function ThemedTimeFilterShadCN({

const handleDateChange = (newDate: DateRange | undefined) => {
if (newDate?.from && newDate?.to) {
if (newDate.from > newDate.to) {
// Swap dates if from is after to
[newDate.from, newDate.to] = [newDate.to, newDate.from];
}
const daysDifference = differenceInDays(newDate.to, newDate.from);
if (daysDifference > 31 && !hasAccess) {
setIsDialogOpen(true);
Expand Down Expand Up @@ -188,14 +192,18 @@ export function ThemedTimeFilterShadCN({
type="number"
min="1"
value={customNumber}
onChange={(e) => setCustomNumber(parseInt(e.target.value) || 1)}
onChange={(e) => {
setCustomNumber(parseInt(e.target.value) || 1);
handleCustomRangeChange();
}}
className="w-16 px-2 py-1 border rounded text-xs"
/>
<Select
value={customUnit}
onValueChange={(value: "hour" | "day" | "week") =>
setCustomUnit(value)
}
onValueChange={(value: "hour" | "day" | "week") => {
setCustomUnit(value);
handleCustomRangeChange();
}}
>
<SelectTrigger className="w-[100px] text-xs">
<SelectValue />
Expand All @@ -212,13 +220,6 @@ export function ThemedTimeFilterShadCN({
</SelectItem>
</SelectContent>
</Select>
<Button
onClick={handleCustomRangeChange}
size="sm_sleek"
variant={"ghost"}
>
Apply
</Button>
</div>
</div>

Expand Down
19 changes: 19 additions & 0 deletions web/components/templates/dashboard/dashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ const DashboardPage = (props: DashboardPageProps) => {
},
[encodeFilters, refetch]
);

const onTimeSelectHandler = (newDate: DateRange | undefined) => {
if (newDate?.from && newDate?.to) {
const start = newDate.from;
const end = newDate.to;
searchParams.set(
"t",
`custom_${start.toISOString()}_${end.toISOString()}`
);
setInterval("custom");
setTimeFilter({
start,
end,
});
debouncedRefetch(); // Use debounced refetch
}
};

const metricsData: MetricsPanelProps["metric"][] = [
{
id: "cost-req",
Expand Down Expand Up @@ -579,6 +597,7 @@ const DashboardPage = (props: DashboardPageProps) => {
});
}
},
onDateChange: onTimeSelectHandler,
}}
advancedFilter={{
filterMap,
Expand Down
24 changes: 9 additions & 15 deletions web/components/templates/requestsV2/requestsPageV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -553,37 +553,31 @@ const RequestsPageV2 = (props: RequestsPageV2Props) => {
}
}, [router.query.page, page]);

const onTimeSelectHandler = (key: TimeInterval, value: string) => {
const tableName = getTableName(isCached);
const createdAtColumn = getCreatedAtColumn(isCached);
if (key === "custom") {
const [start, end] = value.split("_");
const onTimeSelectHandler = (newDate: DateRange | undefined) => {
if (newDate?.from && newDate?.to) {
const start = newDate.from;
const end = newDate.to;
const tableName = getTableName(isCached);
const createdAtColumn = getCreatedAtColumn(isCached);
const filter: FilterNode = {
left: {
[tableName]: {
[createdAtColumn]: {
gte: new Date(start).toISOString(),
gte: start.toISOString(),
},
},
},
operator: "and",
right: {
[tableName]: {
[createdAtColumn]: {
lte: new Date(end).toISOString(),
lte: end.toISOString(),
},
},
},
};
setTimeFilter(filter);
} else {
setTimeFilter({
[tableName]: {
[createdAtColumn]: {
gte: new Date(getTimeIntervalAgo(key)).toISOString(),
},
},
});
debouncedRefetch(); // Use debounced refetch
}
};

Expand Down

0 comments on commit 84bd865

Please sign in to comment.