Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVPROD-7765: Provide specific types for parsing query params #543

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion apps/parsley/cypress/integration/ansiLogs/ansi_filtering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe("Filtering", () => {
describe("Applying filters", () => {
describe("Basic filtering", () => {
beforeEach(() => {
cy.resetDrawerState();
cy.visit(logLink);
cy.dataCy("paginated-virtual-list").should("be.visible");
});
Expand All @@ -20,6 +21,13 @@ describe("Filtering", () => {
.and("match", /log-row-(0|5|6|297)/);
});
});

it("does not corrupt filters that are large numbers", () => {
cy.addFilter("5553072873648668703");
cy.dataCy("log-row-0").should("be.visible").dblclick({ force: true });
cy.location("search").should("contain", "5553072873648668703");
cy.dataCy("filter-5553072873648668703").should("be.visible");
});
});

describe("Advanced filtering", () => {
Expand Down Expand Up @@ -206,7 +214,8 @@ describe("Filtering", () => {
cy.dataCy(`filter-${filter}`).within(() => {
cy.get(`[aria-label="Edit filter"]`).click();
});
cy.dataCy("edit-filter-name").clear().type("running");
cy.dataCy("edit-filter-name").clear();
cy.dataCy("edit-filter-name").type("running");
cy.contains("button", "Apply").click();
cy.location("search").should("contain", "filters=100running");
cy.get("[data-cy^='log-row-']")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe("Filtering", () => {
describe("Applying filters", () => {
describe("Basic filtering", () => {
beforeEach(() => {
cy.resetDrawerState();
cy.visit(logLink);
cy.dataCy("paginated-virtual-list").should("be.visible");
});
Expand All @@ -20,6 +21,13 @@ describe("Filtering", () => {
.and("match", /log-row-(0|5|6|130)/);
});
});

it("does not corrupt filters that are large numbers", () => {
cy.addFilter("5553072873648668703");
cy.dataCy("log-row-0").should("be.visible").dblclick({ force: true });
cy.location("search").should("contain", "5553072873648668703");
cy.dataCy("filter-5553072873648668703").should("be.visible");
});
});

describe("Advanced filtering", () => {
Expand Down Expand Up @@ -206,7 +214,8 @@ describe("Filtering", () => {
cy.dataCy(`filter-${filter}`).within(() => {
cy.get(`[aria-label="Edit filter"]`).click();
});
cy.dataCy("edit-filter-name").clear().type("session");
cy.dataCy("edit-filter-name").clear();
cy.dataCy("edit-filter-name").type("session");
cy.contains("button", "Apply").click();
cy.location("search").should("contain", "filters=100session");
cy.get("[data-cy^='log-row-']")
Expand Down
2 changes: 1 addition & 1 deletion apps/parsley/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"linkifyjs": "4.1.3",
"lossless-json": "4.0.1",
"pluralize": "8.0.0",
"query-string": "9.0.0",
"query-string": "9.1.1",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-dropzone": "14.2.3",
Expand Down
6 changes: 5 additions & 1 deletion apps/parsley/src/components/BookmarksBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ const BookmarksBar: React.FC<BookmarksBarProps> = ({
}, []); // eslint-disable-line react-hooks/exhaustive-deps

const lineNumbers = Array.from(
new Set([...bookmarks, shareLine ?? 0, failingLine ?? 0]),
new Set([
...bookmarks,
...(shareLine ? [shareLine] : []),
...(failingLine ? [failingLine] : []),
]),
).sort((a, b) => a - b);

return (
Expand Down
4 changes: 1 addition & 3 deletions apps/parsley/src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const Search: React.FC = () => {
const containerRef = useRef<HTMLDivElement>(null);
const [filters, setFilters] = useFilterParam();
const [highlights, setHighlights] = useHighlightParam();
const [searchParams, setSearchParams] = useQueryParams({
parseNumbers: false,
});
const [searchParams, setSearchParams] = useQueryParams();
const {
hasLogs,
logMetadata,
Expand Down
20 changes: 19 additions & 1 deletion apps/parsley/src/constants/queryParams.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ParseOptions } from "query-string";

enum QueryParams {
Search = "search",
Highlights = "highlights",
Bookmarks = "bookmarks",
Filters = "filters",
Expand All @@ -12,4 +13,21 @@ enum QueryParams {
SelectedLineRange = "selectedLineRange",
}

const urlParseOptions: ParseOptions = {
arrayFormat: "comma",
parseBooleans: true,
parseNumbers: false,
types: {
bookmarks: "number",
filterLogic: "string",
filters: "string[]",
highlights: "string[]",
lower: "number",
selectedLineRange: "string",
shareLine: "number",
upper: "number",
},
};

export { QueryParams };
export { urlParseOptions };
4 changes: 1 addition & 3 deletions apps/parsley/src/hooks/useFilterParam/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import { parseFilters, stringifyFilters } from "utils/query-string";
* @returns a tuple containing the parsed filters and a function to set the filters
*/
const useFilterParam = () => {
const [searchParams, setSearchParams] = useQueryParams({
parseNumbers: false,
});
const [searchParams, setSearchParams] = useQueryParams();

const parsedFilters = parseFilters(
conditionalToArray(searchParams.filters ?? [], true) as string[],
Expand Down
4 changes: 1 addition & 3 deletions apps/parsley/src/hooks/useHighlightParam/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import { useQueryParams } from "hooks/useQueryParam";
* @returns a tuple containing the parsed highlights and a function to set the highlights
*/
const useHighlightParam = () => {
const [searchParams, setSearchParams] = useQueryParams({
parseNumbers: false,
});
const [searchParams, setSearchParams] = useQueryParams();

const parsedHighlights = useMemo(
() =>
Expand Down
10 changes: 6 additions & 4 deletions apps/parsley/src/hooks/useQueryParam/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
parseQueryString,
stringifyQuery,
} from "@evg-ui/lib/utils/query-string";
import { QueryParams } from "constants/queryParams";
import { QueryParams, urlParseOptions } from "constants/queryParams";

/**
* `useQueryParams` returns all of the query params that exist in the url.
* @param parseOptions - options which define how to parse params from the url (optional)
* @returns a tuple containing the parsed query params and a function to set the query params
*/
const useQueryParams = (parseOptions?: ParseOptions) => {
const useQueryParams = (parseOptions: ParseOptions = urlParseOptions) => {
const [searchParams] = useSearchParams();
const navigate = useNavigate();
const setQueryString = useCallback(
Expand All @@ -39,13 +39,15 @@ const useQueryParams = (parseOptions?: ParseOptions) => {
* `useQueryParam` will default to the second argument if the query param is not present in the url.
* @param param - the name of the query param
* @param defaultParam - the default value of the query param
* @param parseOptions - options which define how to parse params from the url (optional)
* @returns a tuple containing the parsed query param and a function to set the query param
*/
const useQueryParam = <T>(
param: QueryParams,
defaultParam: T,
parseOptions: ParseOptions = urlParseOptions,
): readonly [T, (set: T) => void] => {
const [searchParams, setSearchParams] = useQueryParams();
const [searchParams, setSearchParams] = useQueryParams(parseOptions);

const setQueryParam = useCallback(
(value: T) => {
Expand All @@ -58,7 +60,7 @@ const useQueryParam = <T>(
}
if (Array.isArray(paramValue)) {
newParams[paramKey] = paramValue.map((v) =>
v != null ? encodeURIComponent(v) : null,
v !== null ? encodeURIComponent(v) : null,
);
}
});
Expand Down
15 changes: 13 additions & 2 deletions apps/parsley/src/hooks/useQueryParam/useQueryParam.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ParseOptions } from "query-string";
import { MemoryRouter, useLocation } from "react-router-dom";
import { act, renderHook } from "@evg-ui/lib/test_utils";
import { QueryParams } from "constants/queryParams";
Expand Down Expand Up @@ -29,8 +30,18 @@ describe("useQueryParams", () => {
});

const useQueryJointHook = (param: string, def: any) => {
const [queryParam, setQueryParam] = useQueryParam(param as QueryParams, def);
const [allQueryParams] = useQueryParams();
const defaultParseOptions: ParseOptions = {
arrayFormat: "comma",
parseBooleans: true,
parseNumbers: true,
};

const [queryParam, setQueryParam] = useQueryParam(
param as QueryParams,
def,
defaultParseOptions,
);
const [allQueryParams] = useQueryParams(defaultParseOptions);
return { allQueryParams, queryParam, setQueryParam };
};

Expand Down
2 changes: 1 addition & 1 deletion apps/spruce/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"lodash.set": "4.3.2",
"lodash.throttle": "4.1.1",
"pluralize": "8.0.0",
"query-string": "9.0.0",
"query-string": "9.1.1",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-router-dom": "6.16.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@opentelemetry/auto-instrumentations-web": "^0.40.0",
"@opentelemetry/instrumentation-user-interaction": "^0.41.0",
"graphql": "16.8.1",
"query-string": "9.0.0"
"query-string": "9.1.1"
},
"devDependencies": {
"@evg-ui/eslint-config": "*",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9934,10 +9934,10 @@ qs@~6.10.3:
dependencies:
side-channel "^1.0.4"

query-string@9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/query-string/-/query-string-9.0.0.tgz#1fe177cd95545600f0deab93f5fb02fd4e3e7273"
integrity sha512-4EWwcRGsO2H+yzq6ddHcVqkCQ2EFUSfDMEjF8ryp8ReymyZhIuaFRGLomeOQLkrzacMHoyky2HW0Qe30UbzkKw==
query-string@9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/query-string/-/query-string-9.1.1.tgz#dbfebb4196aeb2919915f2b2b81b91b965cf03a0"
integrity sha512-MWkCOVIcJP9QSKU52Ngow6bsAWAPlPK2MludXvcrS2bGZSl+T1qX9MZvRIkqUIkGLJquMJHWfsT6eRqUpp4aWg==
dependencies:
decode-uri-component "^0.4.1"
filter-obj "^5.1.0"
Expand Down