Skip to content

Commit

Permalink
update hook being used to update privileges, one performs better than…
Browse files Browse the repository at this point in the history
… the other
  • Loading branch information
yctercero committed Mar 8, 2022
1 parent 4e3b91d commit 360cf66
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import React from 'react';
import { Route, Switch } from 'react-router-dom';

import * as i18n from './translations';
import { ALERTS_PATH, SecurityPageName } from '../../../../common/constants';
import { NotFoundPage } from '../../../app/404';
import * as i18n from './translations';
import { TrackApplicationView } from '../../../../../../../src/plugins/usage_collection/public';
import { DetectionEnginePage } from '../../pages/detection_engine/detection_engine';
import { SpyRoute } from '../../../common/utils/route/spy_routes';
Expand Down
17 changes: 6 additions & 11 deletions x-pack/plugins/security_solution/public/use_readonly_header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,33 @@
* 2.0.
*/

import { useCallback, useEffect } from 'react';
import { useEffect } from 'react';

import * as i18n from './translations';
import { useKibana } from './common/lib/kibana';
import { useUserInfo } from './detections/components/user_info';
import { useAlertsPrivileges } from './detections/containers/detection_engine/alerts/use_alerts_privileges';

/**
* This component places a read-only icon badge in the header
* if user only has read *Kibana* privileges, not individual data index
* privileges
*/
export function useReadonlyHeader(tooltip: string) {
const { canUserREAD, canUserCRUD } = useUserInfo();
const { hasKibanaREAD, hasKibanaCRUD } = useAlertsPrivileges();
const chrome = useKibana().services.chrome;

// if the user is read only then display the glasses badge in the global navigation header
const setBadge = useCallback(() => {
if (canUserREAD && !canUserCRUD) {
useEffect(() => {
if (hasKibanaREAD && !hasKibanaCRUD) {
chrome.setBadge({
text: i18n.READ_ONLY_BADGE_TEXT,
tooltip,
iconType: 'glasses',
});
}
}, [chrome, canUserREAD, canUserCRUD, tooltip]);

useEffect(() => {
setBadge();

// remove the icon after the component unmounts
return () => {
chrome.setBadge();
};
}, [setBadge, chrome]);
}, [chrome, hasKibanaREAD, hasKibanaCRUD, tooltip]);
}

0 comments on commit 360cf66

Please sign in to comment.