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

Add new hook to determine the loading interval for entities pages #4068

Merged
merged 1 commit into from
Jun 17, 2024
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
98 changes: 98 additions & 0 deletions src/web/entities/__tests__/useEntitiesReloadInterval.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

/* eslint-disable react/prop-types */

import {describe, test, expect} from '@gsa/testing';

import {screen, rendererWith} from 'web/utils/testing';

import useEntitiesReloadInterval from '../useEntitiesReloadInterval';

const TestComponent = ({entities, useActive, isVisible = true}) => {
const timeoutFunc = useEntitiesReloadInterval(entities, {useActive});
return <span data-testid="timeout">{timeoutFunc({isVisible})}</span>;
};

describe('useEntitiesReloadInterval', () => {
test('should return the reload interval', () => {
const entities = [{isActive: () => true}];
const gmp = {settings: {reloadInterval: 60000}};
const {render} = rendererWith({gmp});

render(<TestComponent entities={entities} />);

expect(screen.getByTestId('timeout')).toHaveTextContent('60000');
});

test('should return the active reload interval', () => {
const entities = [{isActive: () => true}];
const gmp = {
settings: {reloadInterval: 60000, reloadIntervalActive: 30000},
};
const {render} = rendererWith({gmp});

render(<TestComponent entities={entities} useActive={true} />);

expect(screen.getByTestId('timeout')).toHaveTextContent('30000');
});

test('should return the reload interval when all entities are inactive', () => {
const entities = [{isActive: () => false}];
const gmp = {
settings: {reloadInterval: 60000, reloadIntervalActive: 30000},
};
const {render} = rendererWith({gmp});

render(<TestComponent entities={entities} useActive={true} />);

expect(screen.getByTestId('timeout')).toHaveTextContent('60000');
});

test('should return the active reload interval if at least one entity is active', () => {
const entities = [{isActive: () => false}, {isActive: () => true}];
const gmp = {
settings: {reloadInterval: 60000, reloadIntervalActive: 30000},
};
const {render} = rendererWith({gmp});

render(<TestComponent entities={entities} useActive={true} />);

expect(screen.getByTestId('timeout')).toHaveTextContent('30000');
});

test('should return the reload interval if not entity is available', () => {
const entities = [];
const gmp = {
settings: {
reloadInterval: 60000,
reloadIntervalActive: 30000,
},
};
const {render} = rendererWith({gmp});

render(<TestComponent entities={entities} useActive={true} />);

expect(screen.getByTestId('timeout')).toHaveTextContent('60000');
});

test('should return the inactive reload interval if not visible', () => {
const entities = [{isActive: () => false}, {isActive: () => true}];
const gmp = {
settings: {
reloadInterval: 60000,
reloadIntervalActive: 30000,
reloadIntervalInactive: 40000,
},
};
const {render} = rendererWith({gmp});

render(
<TestComponent entities={entities} useActive={true} isVisible={false} />,
);

expect(screen.getByTestId('timeout')).toHaveTextContent('40000');
});
});
52 changes: 52 additions & 0 deletions src/web/entities/useEntitiesReloadInterval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {useCallback} from 'react';

import {isDefined} from 'gmp/utils/identity';

import useGmp from 'web/utils/useGmp';

/**
* Hook to get the reload interval for entities
*
* Can be best used in conjunction with useReload
*
* @example
* const entities = [entity1, entity2];
* const timeoutFunc = useEntitiesReloadInterval(entities);
* const [startTimer, clearTimer, isRunning] = useReload(reloadFunc, timeoutFunc);
*
* @param {Array} entities Optional array of entities to consider
* @param {Object} options Set useActive to true to consider the active state
* of the entities for the reload interval. If at least one entity is active
* the reload interval will be the active interval (shorter). If no entity is
* active the normal interval will be used. Default is false.
* @returns Function A timeout function that calculates the reload interval
*/
const useEntitiesReloadInterval = (entities, {useActive = false} = {}) => {
const gmp = useGmp();
const gmpSettings = gmp.settings;
const timeoutFunc = useCallback(
({isVisible}) => {
if (!isVisible) {
return gmpSettings.reloadIntervalInactive;
}
if (
useActive &&
isDefined(entities) &&
entities.some(entity => entity.isActive())
) {
return gmpSettings.reloadIntervalActive;
}
return gmpSettings.reloadInterval;
},
[entities, gmpSettings, useActive],
);

return timeoutFunc;
};

export default useEntitiesReloadInterval;
Loading