diff --git a/site/src/pages/AgentsPage/AgentAnalyticsPage.tsx b/site/src/pages/AgentsPage/AgentAnalyticsPage.tsx index a7c1930c0a..e5742acafb 100644 --- a/site/src/pages/AgentsPage/AgentAnalyticsPage.tsx +++ b/site/src/pages/AgentsPage/AgentAnalyticsPage.tsx @@ -1,7 +1,7 @@ import { chatCostSummary } from "api/queries/chats"; import { useAuthContext } from "contexts/auth/AuthProvider"; import dayjs, { type Dayjs } from "dayjs"; -import type { FC } from "react"; +import { type FC, useState } from "react"; import { useQuery } from "react-query"; import { AgentAnalyticsPageView } from "./AgentAnalyticsPageView"; import { AgentPageHeader } from "./components/AgentPageHeader"; @@ -23,7 +23,8 @@ interface AgentAnalyticsPageProps { const AgentAnalyticsPage: FC = ({ now }) => { const { user } = useAuthContext(); - const dateRange = createDateRange(now); + const [anchor] = useState(() => dayjs()); + const dateRange = createDateRange(now ?? anchor); const summaryQuery = useQuery({ ...chatCostSummary(user?.id ?? "me", { diff --git a/site/src/pages/AgentsPage/components/InsightsContent.tsx b/site/src/pages/AgentsPage/components/InsightsContent.tsx index 20ec53f5fa..618b891942 100644 --- a/site/src/pages/AgentsPage/components/InsightsContent.tsx +++ b/site/src/pages/AgentsPage/components/InsightsContent.tsx @@ -1,28 +1,42 @@ import { prInsights } from "api/queries/chats"; import { Spinner } from "components/Spinner/Spinner"; -import dayjs from "dayjs"; +import dayjs, { type Dayjs } from "dayjs"; import { type FC, useState } from "react"; import { useQuery } from "react-query"; import { type PRInsightsTimeRange, PRInsightsView } from "./PRInsightsView"; -function timeRangeToDates(range: PRInsightsTimeRange) { - const end = dayjs(); +type TimeRangeSelection = { + timeRange: PRInsightsTimeRange; + anchor: Dayjs; +}; + +function timeRangeToDates(range: PRInsightsTimeRange, anchor: Dayjs) { const days = Number.parseInt(range, 10); - const start = end.subtract(days, "day"); + const start = anchor.subtract(days, "day"); return { start_date: start.toISOString(), - end_date: end.toISOString(), + end_date: anchor.toISOString(), }; } export const InsightsContent: FC = () => { - const [timeRange, setTimeRange] = useState("30d"); - const dates = timeRangeToDates(timeRange); + const [selection, setSelection] = useState(() => ({ + timeRange: "30d", + anchor: dayjs(), + })); + const dates = timeRangeToDates(selection.timeRange, selection.anchor); const { data, isLoading, error } = useQuery(prInsights(dates)); - const handleTimeRangeChange = (range: PRInsightsTimeRange) => - setTimeRange(range); + const handleTimeRangeChange = (timeRange: PRInsightsTimeRange) => + setSelection((current) => + current.timeRange === timeRange + ? current + : { + timeRange, + anchor: dayjs(), + }, + ); if (isLoading) { return ( @@ -49,7 +63,7 @@ export const InsightsContent: FC = () => { return ( );