Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

add-datepicker-state-to-url (#348)

Max Deichmanne9fed81cffda
diff --git a/src/components/date-picker.tsx b/src/components/date-picker.tsx
index 7153016978..cb415dbfc4 100644
--- a/src/components/date-picker.tsx
+++ b/src/components/date-picker.tsx
@@ -19,7 +19,7 @@ import {
SelectTrigger,
SelectValue,
} from "@/src/components/ui/select";
-import { useState } from "react";
+import { useEffect, useState } from "react";
import {
type DateTimeAggregationOption,
findClosestInterval,
@@ -28,6 +28,12 @@ import {
} from "@/src/features/dashboard/lib/timeseries-aggregation";
import { useMediaQuery } from "react-responsive";
import { type DashboardDateRange } from "@/src/pages/project/[projectId]";
+import { isValidOption } from "@/src/utils/types";
+
+export const DEFAULT_DATE_RANGE_SELECTION = "Select date range" as const;
+export type AvailableDateRangeSelections =
+ | typeof DEFAULT_DATE_RANGE_SELECTION
+ | DateTimeAggregationOption;
export function DatePicker({
date,
@@ -67,30 +73,29 @@ export function DatePicker({
export type DatePickerWithRangeProps = {
dateRange?: DashboardDateRange;
- setDateRange: (date?: DashboardDateRange) => void;
className?: string;
setAgg: (agg: DateTimeAggregationOption) => void;
+ selectedOption: AvailableDateRangeSelections;
+ setDateRangeAndOption: (
+ option: AvailableDateRangeSelections,
+ date?: DashboardDateRange,
+ ) => void;
};
export function DatePickerWithRange({
className,
dateRange,
- setDateRange,
+ selectedOption,
setAgg,
+ setDateRangeAndOption,
}: DatePickerWithRangeProps) {
const [internalDateRange, setInternalDateRange] = useState<
DateRange | undefined
>(dateRange);
- const [selectedOption, setSelectedOption] = useState<
- DateTimeAggregationOption | "Select date"
- >("Select date");
- function isValidOption(value: unknown): value is DateTimeAggregationOption {
- return (
- typeof value === "string" &&
- dateTimeAggregationOptions.includes(value as DateTimeAggregationOption)
- );
- }
+ useEffect(() => {
+ setInternalDateRange(dateRange);
+ }, [dateRange]);
const closestInterval = dateRange
? findClosestInterval(dateRange)
@@ -103,19 +108,23 @@ export function DatePickerWithRange({
const setting = dateTimeAggregationSettings[value];
const fromDate = addMinutes(new Date(), -1 * setting.minutes);
- setDateRange({ from: fromDate, to: new Date() });
+ setDateRangeAndOption(value, {
+ from: fromDate,
+ to: new Date(),
+ });
setInternalDateRange({ from: fromDate, to: new Date() });
- setSelectedOption(value);
} else {
- setSelectedOption("Select date");
+ setDateRangeAndOption(DEFAULT_DATE_RANGE_SELECTION, undefined);
}
};
const onCalendarSelection = (range?: DateRange) => {
setInternalDateRange(range);
if (range && range.from && range.to) {
- setDateRange({ from: range.from, to: range.to });
- setSelectedOption("Select date");
+ setDateRangeAndOption(DEFAULT_DATE_RANGE_SELECTION, {
+ from: range.from,
+ to: range.to,
+ });
}
};
@@ -166,8 +175,11 @@ export function DatePickerWithRange({
<SelectValue placeholder="Select" />
</SelectTrigger>
<SelectContent position="popper" defaultValue={60}>
- <SelectItem key={"Select date"} value={"Select date"}>
- {"Select date"}
+ <SelectItem
+ key={DEFAULT_DATE_RANGE_SELECTION}
+ value={DEFAULT_DATE_RANGE_SELECTION}
+ >
+ {DEFAULT_DATE_RANGE_SELECTION}
</SelectItem>
{dateTimeAggregationOptions.toReversed().map((item) => (
<SelectItem key={item} value={`${item}`}>
diff --git a/src/features/dashboard/server/dashboard-router.ts b/src/features/dashboard/server/dashboard-router.ts
index fa61e0c27a..cdab057223 100644
--- a/src/features/dashboard/server/dashboard-router.ts
+++ b/src/features/dashboard/server/dashboard-router.ts
@@ -15,7 +15,6 @@ export const dashboardRouter = createTRPCRouter({
chart: protectedProjectProcedure
.input(sqlInterface.extend({ projectId: z.string() }))
.query(async ({ input, ctx }) => {
- console.log(`Trying to fetch chart data`);
return await executeQuery(ctx.prisma, input.projectId, input);
}),
diff --git a/src/pages/project/[projectId]/index.tsx b/src/pages/project/[projectId]/index.tsx
index 96500b3f38..869d674518 100644
--- a/src/pages/project/[projectId]/index.tsx
+++ b/src/pages/project/[projectId]/index.tsx
@@ -13,8 +13,19 @@ import { UserChart } from "@/src/features/dashboard/components/UserChart";
import { Button } from "@/src/components/ui/button";
import Link from "next/link";
import { env } from "@/src/env.mjs";
-import { DatePickerWithRange } from "@/src/components/date-picker";
+import {
+ type AvailableDateRangeSelections,
+ DEFAULT_DATE_RANGE_SELECTION,
+ DatePickerWithRange,
+} from "@/src/components/date-picker";
import { addDays } from "date-fns";
+import {
+ NumberParam,
+ StringParam,
+ useQueryParams,
+ withDefault,
+} from "use-query-params";
+import { isValidOption } from "@/src/utils/types";
export type DashboardDateRange = {
from: Date;
@@ -26,28 +37,53 @@ export default function Start() {
const router = useRouter();
const projectId = router.query.projectId as string;
- const [dateRange, setDateRange] = useState<DashboardDateRange | undefined>({
- from: addDays(new Date(), -30),
- to: new Date(),
+ const currDate = new Date();
+ const FromParam = withDefault(NumberParam, addDays(currDate, -7).getTime());
+ const ToParam = withDefault(NumberParam, currDate.getTime());
+ const SelectParam = withDefault(StringParam, "Select a date range");
+
+ const [urlParams, setUrlParams] = useQueryParams({
+ from: FromParam,
+ to: ToParam,
+ select: SelectParam,
});
- const globalFilterState =
- dateRange && dateRange.from && dateRange.to
- ? [
- {
- type: "datetime" as const,
- column: "startTime",
- operator: ">" as const,
- value: dateRange.from,
- },
- {
- type: "datetime" as const,
- column: "startTime",
- operator: "<" as const,
- value: dateRange.to,
- },
- ]
- : [];
+ const dateRange =
+ urlParams.from && urlParams.to
+ ? { from: new Date(urlParams.from), to: new Date(urlParams.to) }
+ : undefined;
+
+ const selectedOption = isValidOption(urlParams.select)
+ ? urlParams.select
+ : DEFAULT_DATE_RANGE_SELECTION;
+
+ const setDateRangeAndOption = (
+ option?: AvailableDateRangeSelections,
+ dateRange?: DashboardDateRange,
+ ) => {
+ setUrlParams({
+ select: option ? option.toString() : urlParams.select,
+ from: dateRange ? dateRange.from?.getTime() : urlParams.from,
+ to: dateRange ? dateRange.to?.getTime() : urlParams.to,
+ });
+ };
+
+ const globalFilterState = dateRange
+ ? [
+ {
+ type: "datetime" as const,
+ column: "startTime",
+ operator: ">" as const,
+ value: dateRange.from,
+ },
+ {
+ type: "datetime" as const,
+ column: "startTime",
+ operator: "<" as const,
+ value: dateRange.to,
+ },
+ ]
+ : [];
return (
<div className="md:container">
@@ -65,8 +101,9 @@ export default function Start() {
/>
<DatePickerWithRange
dateRange={dateRange}
- setDateRange={setDateRange}
setAgg={setAgg}
+ setDateRangeAndOption={setDateRangeAndOption}
+ selectedOption={selectedOption}
className=" max-w-full overflow-x-auto"
/>
<div className="grid w-full grid-cols-1 gap-4 overflow-hidden lg:grid-cols-2 xl:grid-cols-6">
diff --git a/src/utils/types.ts b/src/utils/types.ts
index 5bc646c4b6..5a49c95e58 100644
--- a/src/utils/types.ts
+++ b/src/utils/types.ts
@@ -1,6 +1,10 @@
import { type Observation } from "@prisma/client";
import { type inferRouterInputs, type inferRouterOutputs } from "@trpc/server";
import { type AppRouter } from "@/src/server/api/root";
+import {
+ type DateTimeAggregationOption,
+ dateTimeAggregationOptions,
+} from "@/src/features/dashboard/lib/timeseries-aggregation";
export type NestedObservation = Observation & {
children: NestedObservation[];
@@ -35,3 +39,12 @@ export type RouterOutput = inferRouterOutputs<AppRouter>;
export const isNotUndefinedOrNull = <T>(
val?: T | null,
): val is undefined | null => val !== undefined || val !== null;
+
+export function isValidOption(
+ value: unknown,
+): value is DateTimeAggregationOption {
+ return (
+ typeof value === "string" &&
+ dateTimeAggregationOptions.includes(value as DateTimeAggregationOption)
+ );
+}