Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import userEvent from '@testing-library/user-event';
import { Route, Routes } from 'react-router-dom';
import { GloballySupportedSearchParams } from '../..';
import { GloballySupportedSearchParams } from '../../hooks/useGlobalParamsExtension';
import { render } from '../../test-utils';
import { AppLink } from './AppLink';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
// SPDX-License-Identifier: Apache-2.0

import { Link, LinkProps, Path } from 'react-router-dom';
import {
AppNavigateProps,
GloballySupportedSearchParams,
applyPreservedParams,
persistSearchParams,
} from '../../utils/searchParams/searchParams';
import { GloballySupportedSearchParams } from '../../hooks/useGlobalParamsExtension';
import { AppNavigateProps, applyPreservedParams, persistSearchParams } from '../../utils/searchParams/searchParams';

export const AppLink = ({ children, to, discardQueryParams, ...props }: LinkProps & AppNavigateProps) => {
const path = typeof to === 'string' ? to : (to as Partial<Path>).pathname || '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// SPDX-License-Identifier: Apache-2.0

import { Route, Routes } from 'react-router-dom';
import { GloballySupportedSearchParams } from '../..';

import { GloballySupportedSearchParams } from '../../hooks/useGlobalParamsExtension';
import { render } from '../../test-utils';
import { AppNavigate } from './AppNavigate';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import { Navigate, NavigateProps } from 'react-router-dom';
import {
AppNavigateProps,
GloballySupportedSearchParams,
applyPreservedParams,
persistSearchParams,
} from '../../utils/searchParams/searchParams';
import { GloballySupportedSearchParams } from '../../hooks';
import { AppNavigateProps, applyPreservedParams, persistSearchParams } from '../../utils/searchParams/searchParams';

export const AppNavigate: React.FC<NavigateProps & AppNavigateProps> = (props) => {
const { discardQueryParams, to, ...rest } = props;
Expand Down
1 change: 1 addition & 0 deletions packages/javascript/bh-shared-ui/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export * from './useFileIngest';
export * from './useFileUploadDialogContext';
export * from './useFileUploadQuery';
export * from './useFinishedJobs';
export * from './useGlobalParamsExtension';
export * from './useGraphHasData';
export * from './useGraphItem';
export * from './useInitialEnvironment';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2026 Specter Ops, Inc.
//
// Licensed under the Apache License, Version 2.0
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

import { expectTypeOf } from 'vitest';
import { type EnvironmentQueryParams } from './useEnvironmentParams';
import { useGlobalParamsExtension } from './useGlobalParamsExtension';

type PageQueryParams = {
findingName: string | null;
};

describe('useGlobalParamsExtension', () => {
it('allows page query params to be appended to the return type', () => {
type ExtendedGlobalParams = ReturnType<typeof useGlobalParamsExtension<PageQueryParams>>;

expectTypeOf<ExtendedGlobalParams>().toEqualTypeOf<EnvironmentQueryParams & PageQueryParams>();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EnvironmentQueryParams, useEnvironmentParams } from './useEnvironmentParams';
import { PZQueryParams, usePZQueryParams } from './usePZParams';

type GlobalParams = EnvironmentQueryParams & PZQueryParams;

type GloballySupportedParamKeys = keyof GlobalParams;

export type GlobalParamsExtension<T> = GlobalParams & T;

export const GloballySupportedSearchParams = [
'environmentId',
'environmentAggregation',
'assetGroupTagId',
] satisfies GloballySupportedParamKeys[];

// Params pulled into this hook will be plumbed into each page level param hook.
export const useGlobalParamsExtension = (): GlobalParams => {
const { setEnvironmentParams, ...environmentRest } = useEnvironmentParams();
const { assetGroupTagId } = usePZQueryParams();
return { ...environmentRest, assetGroupTagId };
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
// SPDX-License-Identifier: Apache-2.0

import { createSearchParams, NavigateOptions, Path, To, useSearchParams } from 'react-router-dom';
import { EnvironmentQueryParams } from '../../hooks/useEnvironmentParams';
import { ExploreQueryParams } from '../../hooks/useExploreParams';

// FUTURE DEV: SetURLSearchParams is in both v6 and v7, but v6 uses this type internally and v7 exports it.
// When we upgrade to v7, we can import SetURLSearchParams from react-router-dom
type SetURLSearchParams = ReturnType<typeof useSearchParams>[1];

export type SearchParamKeys = keyof EnvironmentQueryParams | keyof ExploreQueryParams;
export const GloballySupportedSearchParams = ['environmentId', 'environmentAggregation'] satisfies SearchParamKeys[];

type EmptyParam = undefined | null | '';

export type AppNavigateProps = { discardQueryParams?: boolean };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
// SPDX-License-Identifier: Apache-2.0

import { NavigateOptions, To, useNavigate } from 'react-router-dom';
import {
AppNavigateProps,
GloballySupportedSearchParams,
applyPreservedParams,
persistSearchParams,
} from './searchParams';
import { GloballySupportedSearchParams } from '../../hooks/useGlobalParamsExtension';
import { AppNavigateProps, applyPreservedParams, persistSearchParams } from './searchParams';

export type AppNavigateOptions = NavigateOptions & AppNavigateProps;

Expand Down
Loading