Skip to content

BED-5504: Move common query utils to shared-ui #1347

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

Merged
merged 1 commit into from
Apr 10, 2025
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
1 change: 1 addition & 0 deletions packages/javascript/bh-shared-ui/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export * from './mocks';
export * from './parseItemId';
export * from './passwd';
export * from './permissions';
export * from './queries';
export * from './searchParams';
export * from './theme';
export * from './user';
17 changes: 17 additions & 0 deletions packages/javascript/bh-shared-ui/src/utils/queries/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2025 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

export * from './queries';
45 changes: 45 additions & 0 deletions packages/javascript/bh-shared-ui/src/utils/queries/queries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2025 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 { queriesAreLoadingOrErrored } from './queries';

describe('queriesAreLoadingOrErrored', () => {
it.each(['isLoading', 'isError'] as const)('%s returns as expected based on all queries combined', (key) => {
// all keys to true
const allTrueActual = queriesAreLoadingOrErrored(
{ [key]: true } as any,
...([{ [key]: true }, { [key]: true }] as any),
...([{ [key]: true }] as any)
);
expect(allTrueActual[key]).toBe(true);

// Partial keys are true
const partialTrueActual = queriesAreLoadingOrErrored(
{ [key]: true } as any,
...([{ [key]: false }, { [key]: true }] as any),
...([{ [key]: false }] as any)
);
expect(partialTrueActual[key]).toBe(true);

// set all keys to false
const allFalseActual = queriesAreLoadingOrErrored(
{ [key]: false } as any,
...([{ [key]: false }, { [key]: false }] as any),
...([{ [key]: false }] as any)
);
expect(allFalseActual[key]).toBe(false);
});
});
41 changes: 41 additions & 0 deletions packages/javascript/bh-shared-ui/src/utils/queries/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2025 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 { UseQueryOptions, UseQueryResult } from 'react-query';

export type GenericQueryOptions<T> = Omit<UseQueryOptions<T, unknown, T, string[]>, 'queryFn'>;

export const getQueryKey = (baseKey: string[], customKeys?: string[]) => {
return customKeys?.length ? [...baseKey, ...customKeys] : baseKey;
};

/**
* Use this when you want to treat the loading or error state of multiple queries as one.
* For example, display a component when 2 separate APIs are done loading.
* @param queries Variadic function that will check if any query isLoading or isError
* @returns isLoading = true if any query is loading, and isError = true if any query has an error
*/
export const queriesAreLoadingOrErrored = (...queries: UseQueryResult<unknown, unknown>[]) => {
let isLoading = false;
let isError = false;

queries.forEach((query) => {
if (query.isLoading) isLoading = true;
if (query.isError) isError = true;
});

return { isLoading, isError };
};
Loading