Skip to content

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

Feat: Pagination in query params (#147)

Marc Klingenc6410bdf903c
diff --git a/package-lock.json b/package-lock.json
index 56fc8b9b2f..ab07af0845 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -57,6 +57,7 @@
"lucide-react": "^0.274.0",
"next": "^13.4.19",
"next-auth": "^4.23.1",
+ "next-query-params": "^4.2.3",
"next-swagger-doc": "^0.4.0",
"posthog-js": "^1.78.1",
"posthog-node": "^3.1.2",
@@ -70,6 +71,7 @@
"tailwindcss-animate": "^1.0.7",
"tiktoken": "^1.0.10",
"ts-pattern": "^5.0.5",
+ "use-query-params": "^2.2.1",
"uuid": "^9.0.0",
"zod": "^3.22.2"
},
@@ -9634,6 +9636,19 @@
"uuid": "dist/bin/uuid"
}
},
+ "node_modules/next-query-params": {
+ "version": "4.2.3",
+ "resolved": "https://registry.npmjs.org/next-query-params/-/next-query-params-4.2.3.tgz",
+ "integrity": "sha512-hGNCYRH8YyA5ItiBGSKrtMl21b2MAqfPkdI1mvwloNVqSU142IaGzqHN+OTovyeLIpQfonY01y7BAHb/UH4POg==",
+ "dependencies": {
+ "tslib": "^2.0.3"
+ },
+ "peerDependencies": {
+ "next": "^10.0.0 || ^11.0.0 || ^12.0.0 || ^13.0.0",
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
+ "use-query-params": "^2.0.0"
+ }
+ },
"node_modules/next-swagger-doc": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/next-swagger-doc/-/next-swagger-doc-0.4.0.tgz",
@@ -11380,6 +11395,11 @@
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
},
+ "node_modules/serialize-query-params": {
+ "version": "2.0.2",
+ "resolved": "https://registry.npmjs.org/serialize-query-params/-/serialize-query-params-2.0.2.tgz",
+ "integrity": "sha512-1chMo1dST4pFA9RDXAtF0Rbjaut4is7bzFbI1Z26IuMub68pNCILku85aYmeFhvnY//BXUPUhoRMjYcsT93J/Q=="
+ },
"node_modules/set-blocking": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
@@ -12543,6 +12563,28 @@
}
}
},
+ "node_modules/use-query-params": {
+ "version": "2.2.1",
+ "resolved": "https://registry.npmjs.org/use-query-params/-/use-query-params-2.2.1.tgz",
+ "integrity": "sha512-i6alcyLB8w9i3ZK3caNftdb+UnbfBRNPDnc89CNQWkGRmDrm/gfydHvMBfVsQJRq3NoHOM2dt/ceBWG2397v1Q==",
+ "dependencies": {
+ "serialize-query-params": "^2.0.2"
+ },
+ "peerDependencies": {
+ "@reach/router": "^1.2.1",
+ "react": ">=16.8.0",
+ "react-dom": ">=16.8.0",
+ "react-router-dom": ">=5"
+ },
+ "peerDependenciesMeta": {
+ "@reach/router": {
+ "optional": true
+ },
+ "react-router-dom": {
+ "optional": true
+ }
+ }
+ },
"node_modules/use-sidecar": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/use-sidecar/-/use-sidecar-1.1.2.tgz",
diff --git a/package.json b/package.json
index eb4212c5b4..f32bc2e28b 100644
--- a/package.json
+++ b/package.json
@@ -69,6 +69,7 @@
"lucide-react": "^0.274.0",
"next": "^13.4.19",
"next-auth": "^4.23.1",
+ "next-query-params": "^4.2.3",
"next-swagger-doc": "^0.4.0",
"posthog-js": "^1.78.1",
"posthog-node": "^3.1.2",
@@ -82,6 +83,7 @@
"tailwindcss-animate": "^1.0.7",
"tiktoken": "^1.0.10",
"ts-pattern": "^5.0.5",
+ "use-query-params": "^2.2.1",
"uuid": "^9.0.0",
"zod": "^3.22.2"
},
diff --git a/src/components/table/use-cases/traces.tsx b/src/components/table/use-cases/traces.tsx
index 86bf25e01e..51b5b819d8 100644
--- a/src/components/table/use-cases/traces.tsx
+++ b/src/components/table/use-cases/traces.tsx
@@ -13,9 +13,10 @@ import {
} from "@/src/utils/tanstack";
import { type RouterInput, type RouterOutput } from "@/src/utils/types";
import { type Score } from "@prisma/client";
-import { type PaginationState, type ColumnDef } from "@tanstack/react-table";
+import { type ColumnDef } from "@tanstack/react-table";
import router from "next/router";
import { useState } from "react";
+import { NumberParam, useQueryParams, withDefault } from "use-query-params";
export type TraceTableRow = {
id: string;
@@ -73,9 +74,9 @@ export default function TracesTable({
const [selectedMetadata, setSelectedMetadata] = useState<KeyValue[]>([]);
- const [paginationState, setPaginationState] = useState<PaginationState>({
- pageIndex: 0,
- pageSize: 50,
+ const [paginationState, setPaginationState] = useQueryParams({
+ pageIndex: withDefault(NumberParam, 0),
+ pageSize: withDefault(NumberParam, 50),
});
const traces = api.traces.all.useQuery({
@@ -94,21 +95,15 @@ export default function TracesTable({
const setFilterInParams = (filter?: TraceFilterInput) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { ...query } = router.query;
- void router.replace(
- {
- pathname: router.pathname,
- query: {
- ...query,
- ...(filter
- ? { filter: encodeURIComponent(JSON.stringify(filter)) }
- : {}),
- },
- },
- undefined,
- {
- scroll: false,
+ void router.push({
+ pathname: router.pathname,
+ query: {
+ ...query,
+ ...(filter
+ ? { filter: encodeURIComponent(JSON.stringify(filter)) }
+ : {}),
},
- );
+ });
};
const convertToTableRow = (
diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx
index a33c80fef1..ac3d07cf99 100644
--- a/src/pages/_app.tsx
+++ b/src/pages/_app.tsx
@@ -6,6 +6,9 @@ import { useSession } from "next-auth/react";
import { api } from "@/src/utils/api";
+import NextAdapterPages from "next-query-params/pages";
+import { QueryParamProvider } from "use-query-params";
+
import "@/src/styles/globals.css";
import Layout from "@/src/components/layouts/layout";
import { useEffect } from "react";
@@ -70,15 +73,17 @@ const MyApp: AppType<{ session: Session | null }> = ({
}, []);
return (
- <PostHogProvider client={posthog}>
- <SessionProvider session={session} refetchOnWindowFocus={true}>
- <Layout>
- <Component {...pageProps} />
- <UserTracking />
- </Layout>
- <CrispWidget />
- </SessionProvider>
- </PostHogProvider>
+ <QueryParamProvider adapter={NextAdapterPages}>
+ <PostHogProvider client={posthog}>
+ <SessionProvider session={session} refetchOnWindowFocus={true}>
+ <Layout>
+ <Component {...pageProps} />
+ <UserTracking />
+ </Layout>
+ <CrispWidget />
+ </SessionProvider>
+ </PostHogProvider>
+ </QueryParamProvider>
);
};
diff --git a/src/pages/project/[projectId]/analytics.tsx b/src/pages/project/[projectId]/analytics.tsx
index d4ce13b4d2..5f5676ac31 100644
--- a/src/pages/project/[projectId]/analytics.tsx
+++ b/src/pages/project/[projectId]/analytics.tsx
@@ -15,8 +15,8 @@ import {
CardDescription,
CardContent,
} from "@/src/components/ui/card";
-import { useEffect, useState } from "react";
import { openChat } from "@/src/features/support-chat/chat";
+import { StringParam, useQueryParam, withDefault } from "use-query-params";
export default function AnalyticsPage() {
const router = useRouter();
@@ -78,24 +78,10 @@ const dashboards = [
] as const;
const DashboardEmbed = (props: { projectId: string }) => {
- const router = useRouter();
- const initialTab = router.query.dashboard as string | undefined;
- const [activeTab, setActiveTab] = useState(initialTab || dashboards[0].title);
-
- const handleTabChange = (value: string) => {
- //update the state
- setActiveTab(value);
- // update the URL query parameter
- void router.push({
- query: { dashboard: value },
- pathname: window.location.pathname,
- });
- };
-
- // if the query parameter changes, update the state
- useEffect(() => {
- setActiveTab(router.query.dashboard as string);
- }, [router.query.dashboard]);
+ const [dashboard, setDashboard] = useQueryParam(
+ "dashboard",
+ withDefault(StringParam, dashboards[0].title),
+ );
return (
<>
@@ -128,8 +114,8 @@ const DashboardEmbed = (props: { projectId: string }) => {
</Alert>
<Tabs
defaultValue={dashboards[0].title}
- value={activeTab}
- onValueChange={handleTabChange}
+ value={dashboard}
+ onValueChange={setDashboard}
className="pt-10"
>
<TabsList>