Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 5 additions & 35 deletions site/src/components/Filter/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ import { SearchField } from "#/components/SearchField/SearchField";
import { Skeleton, type SkeletonProps } from "#/components/Skeleton/Skeleton";
import { useDebouncedFunction } from "#/hooks/debounce";
import { cn } from "#/utils/cn";
import {
type FilterValues,
parseFilterQuery,
stringifyFilter,
} from "./filterQuery";

type PresetFilter = {
name: string;
query: string;
};

type FilterValues = Record<string, string | undefined>;

type UseFilterConfig = {
/**
* The fallback value to use in the event that no filter params can be
Expand Down Expand Up @@ -102,39 +105,6 @@ export const useFilter = ({
};
};

const parseFilterQuery = (filterQuery: string): FilterValues => {
if (filterQuery === "") {
return {};
}

const result: FilterValues = {};
const keyValuePair = /(\w+):"([^"]+)"|(\w+):(\S+)/g;

for (const match of filterQuery.matchAll(keyValuePair)) {
const key = match[1] ?? match[3];
const value = match[2] ?? match[4];
if (key && value) {
result[key] = value;
}
}

return result;
};

const stringifyFilter = (filterValue: FilterValues): string => {
let result = "";

for (const key in filterValue) {
const value = filterValue[key];
if (value) {
const needsQuotes = value.includes(" ");
result += needsQuotes ? `${key}:"${value}" ` : `${key}:${value} `;
}
}

return result.trim();
};

Comment on lines -105 to -137

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self-review: moved to filterQuery.ts so it can be tested properly

const BaseSkeleton: FC<SkeletonProps> = ({ children, ...skeletonProps }) => {
return (
<Skeleton
Expand Down
48 changes: 48 additions & 0 deletions site/src/components/Filter/filterQuery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";
import { parseFilterQuery, stringifyFilter } from "./filterQuery";

describe("stringifyFilter", () => {
it("leaves simple values unquoted", () => {
expect(stringifyFilter({ initiator: "me" })).toBe("initiator:me");
});

it("quotes values containing spaces", () => {
expect(stringifyFilter({ session_id: "abc def" })).toBe(
'session_id:"abc def"',
);
});

it("quotes values containing colons so the backend parser accepts them", () => {
expect(stringifyFilter({ started_after: "2026-08-16T20:42:00Z" })).toBe(
'started_after:"2026-08-16T20:42:00Z"',
);
});

it("drops empty values", () => {
expect(stringifyFilter({ initiator: undefined, model: "gpt-4" })).toBe(
"model:gpt-4",
);
});
});

describe("parseFilterQuery", () => {
it("round-trips quoted timestamp values", () => {
const values = {
initiator: "me",
started_after: "2026-08-16T20:42:00Z",
started_before: "2026-08-17T20:42:00Z",
};
expect(parseFilterQuery(stringifyFilter(values))).toEqual(values);
});

it("parses unquoted and quoted pairs", () => {
expect(parseFilterQuery('initiator:me session_id:"abc def"')).toEqual({
initiator: "me",
session_id: "abc def",
});
});

it("returns an empty object for an empty query", () => {
expect(parseFilterQuery("")).toEqual({});
});
});
38 changes: 38 additions & 0 deletions site/src/components/Filter/filterQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export type FilterValues = Record<string, string | undefined>;

export const parseFilterQuery = (filterQuery: string): FilterValues => {
if (filterQuery === "") {
return {};
}

const result: FilterValues = {};
const keyValuePair = /(\w+):"([^"]+)"|(\w+):(\S+)/g;

for (const match of filterQuery.matchAll(keyValuePair)) {
const key = match[1] ?? match[3];
const value = match[2] ?? match[4];
if (key && value) {
result[key] = value;
}
}

return result;
};

// Values containing spaces or colons must be quoted: the backend query
// parser splits unquoted elements on ':' and rejects more than one colon.
const needsQuotes = (value: string): boolean =>
value.includes(" ") || value.includes(":");
Comment on lines +22 to +25

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self-review: This is the meat of the change.


export const stringifyFilter = (filterValue: FilterValues): string => {
let result = "";

for (const key in filterValue) {
const value = filterValue[key];
if (value) {
result += needsQuotes(value) ? `${key}:"${value}" ` : `${key}:${value} `;
}
}

return result.trim();
};
Loading