Skip to content

Commit

Permalink
[Security Solution] Deprecate useVariation (#188525)
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc authored Jul 18, 2024
1 parent 179b78b commit e4852dc
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import { useLocation } from 'react-router-dom';
import { useVariationMock } from '../../../common/components/utils.mocks';
import { GlobalHeader } from '.';
import {
ADD_DATA_PATH,
Expand Down Expand Up @@ -60,10 +59,6 @@ describe('global header', () => {
};
const store = createMockStore(state);

beforeEach(() => {
useVariationMock.mockReset();
});

it('has add data link', () => {
(useLocation as jest.Mock).mockReturnValue([
{ pageName: SecurityPageName.overview, detailName: undefined },
Expand Down Expand Up @@ -100,26 +95,6 @@ describe('global header', () => {
expect(link?.getAttribute('href')).toBe(ADD_THREAT_INTELLIGENCE_DATA_PATH);
});

it('points to the resolved Add data URL by useVariation', () => {
(useLocation as jest.Mock).mockReturnValue([
{ pageName: SecurityPageName.overview, detailName: undefined },
]);

const customResolvedUrl = '/test/url';
useVariationMock.mockImplementationOnce(
(cloudExperiments, featureFlagName, defaultValue, setter) => {
setter(customResolvedUrl);
}
);
const { queryByTestId } = render(
<TestProviders store={store}>
<GlobalHeader />
</TestProviders>
);
const link = queryByTestId('add-data');
expect(link?.getAttribute('href')).toBe(customResolvedUrl);
});

it.each(sourcererPaths)('shows sourcerer on %s page', (pathname) => {
(useLocation as jest.Mock).mockReturnValue({ pathname });

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
*/

import { throttle } from 'lodash/fp';
import { useEffect, useMemo, useState } from 'react';
import { useMemo, useState } from 'react';
import useResizeObserver from 'use-resize-observer/polyfilled';
import { niceTimeFormatByDay, timeFormatter } from '@elastic/charts';
import moment from 'moment-timezone';
import type {
CloudExperimentsFeatureFlagNames,
CloudExperimentsPluginStart,
} from '@kbn/cloud-experiments-plugin/common';

export const getDaysDiff = (minDate: moment.Moment, maxDate: moment.Moment) => {
const diff = maxDate.diff(minDate, 'days');
Expand All @@ -39,26 +35,3 @@ export const useThrottledResizeObserver = (wait = 100) => {

return { ref, ...size };
};

/**
* Retrieves the variation of the feature flag if the cloudExperiments plugin is enabled.
* @param cloudExperiments {@link CloudExperimentsPluginStart}
* @param featureFlagName The name of the feature flag {@link CloudExperimentsFeatureFlagNames}
* @param defaultValue The default value in case it cannot retrieve the feature flag
* @param setter The setter from {@link useState} to update the value.
*/
export const useVariation = <Data>(
cloudExperiments: CloudExperimentsPluginStart | undefined,
featureFlagName: CloudExperimentsFeatureFlagNames,
defaultValue: Data,
setter: (value: Data) => void
) => {
useEffect(() => {
(async function loadVariation() {
const variationUrl = await cloudExperiments?.getVariation(featureFlagName, defaultValue);
if (variationUrl) {
setter(variationUrl);
}
})();
}, [cloudExperiments, featureFlagName, defaultValue, setter]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useMemo } from 'react';
import { useLocation } from 'react-router-dom';
import { ADD_DATA_PATH, ADD_THREAT_INTELLIGENCE_DATA_PATH } from '../../../common/constants';
import { isThreatIntelligencePath } from '../../helpers';
import { useVariation } from '../components/utils';

import { useKibana, useNavigateTo } from '../lib/kibana';

Expand All @@ -17,23 +16,15 @@ export const useAddIntegrationsUrl = () => {
http: {
basePath: { prepend },
},
cloudExperiments,
} = useKibana().services;
const { pathname } = useLocation();
const { navigateTo } = useNavigateTo();

const isThreatIntelligence = isThreatIntelligencePath(pathname);

const integrationsUrl = isThreatIntelligence ? ADD_THREAT_INTELLIGENCE_DATA_PATH : ADD_DATA_PATH;
const [addIntegrationsUrl, setAddIntegrationsUrl] = useState(integrationsUrl);
useVariation(
cloudExperiments,
'security-solutions.add-integrations-url',
integrationsUrl,
setAddIntegrationsUrl
);

const href = useMemo(() => prepend(addIntegrationsUrl), [prepend, addIntegrationsUrl]);
const href = useMemo(() => prepend(integrationsUrl), [prepend, integrationsUrl]);

const onClick = useCallback(
(e) => {
Expand Down

0 comments on commit e4852dc

Please sign in to comment.