-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for perspective detection using extensions
- Loading branch information
1 parent
9bf0541
commit a995d4d
Showing
8 changed files
with
133 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
frontend/packages/console-app/src/components/detect-perspective/DetectPerspective.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import { connect, Dispatch } from 'react-redux'; | ||
import { useExtensions, isPerspective } from '@console/plugin-sdk'; | ||
import { getActivePerspective } from '@console/internal/reducers/ui'; | ||
import { RootState } from '@console/internal/redux'; | ||
import * as UIActions from '@console/internal/actions/ui'; | ||
|
||
type OwnProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
type StateProps = { | ||
activePerspective: string; | ||
}; | ||
|
||
type DispatchProps = { | ||
setActivePerspective: (string) => void; | ||
}; | ||
|
||
type DetectPerspectiveProps = OwnProps & StateProps & DispatchProps; | ||
|
||
const DetectPerspective: React.FC<DetectPerspectiveProps> = ({ | ||
activePerspective, | ||
children, | ||
setActivePerspective, | ||
}) => { | ||
let detectedPerspective: string; | ||
let detectionComplete: boolean; | ||
const perspectiveExtensions = useExtensions(isPerspective); | ||
const defaultPerspective = perspectiveExtensions.find((p) => p.properties.default); | ||
const perspectiveDetectors = perspectiveExtensions.filter( | ||
(p) => p.properties.usePerspectiveDetection, | ||
); | ||
|
||
perspectiveDetectors.some((p) => { | ||
const isDetectedPerspective = p.properties.usePerspectiveDetection(); | ||
detectionComplete = _.isBoolean(isDetectedPerspective); | ||
if (isDetectedPerspective) { | ||
detectedPerspective = p.properties.id; | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
React.useEffect(() => { | ||
if (!activePerspective) { | ||
if (detectedPerspective) { | ||
setActivePerspective(detectedPerspective); | ||
} else if (perspectiveDetectors.length < 1 || detectionComplete) { | ||
setActivePerspective(defaultPerspective.properties.id); // set default perspective if there are no detectors or none of the detections were successfull | ||
} | ||
} | ||
}, [ | ||
activePerspective, | ||
defaultPerspective, | ||
detectedPerspective, | ||
detectionComplete, | ||
perspectiveDetectors.length, | ||
setActivePerspective, | ||
]); | ||
|
||
return activePerspective ? <>{children}</> : null; | ||
}; | ||
|
||
const mapStateToProps = (state: RootState) => ({ | ||
activePerspective: getActivePerspective(state), | ||
}); | ||
|
||
const mapDispatchToProps = (dispatch: Dispatch) => ({ | ||
setActivePerspective: (perspective) => dispatch(UIActions.setActivePerspective(perspective)), | ||
}); | ||
|
||
export default connect<StateProps, DispatchProps, OwnProps>( | ||
mapStateToProps, | ||
mapDispatchToProps, | ||
)(DetectPerspective); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
frontend/packages/dev-console/src/utils/__tests__/usePerspectiveDetection.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore: FIXME missing exports due to out-of-sync @types/react-redux version | ||
import { useSelector } from 'react-redux'; | ||
import { testHook } from '@console/shared/src/test-utils/hooks-utils'; | ||
import { usePerspectiveDetection } from '../usePerspectiveDetection'; | ||
|
||
jest.mock('react-redux', () => ({ | ||
useSelector: jest.fn(), | ||
})); | ||
|
||
describe('usePerspectiveDetection', () => { | ||
it('should return null if CAN_GET_NS flag is pending', () => { | ||
(useSelector as jest.Mock).mockImplementation(() => ({ | ||
CAN_GET_NS: undefined, | ||
})); | ||
|
||
testHook(() => { | ||
const isDetectedPerspective = usePerspectiveDetection(); | ||
|
||
expect(isDetectedPerspective).toBe(null); | ||
}); | ||
}); | ||
|
||
it('should return true if CAN_GET_NS flag is false', () => { | ||
(useSelector as jest.Mock).mockImplementation(() => ({ | ||
CAN_GET_NS: false, | ||
})); | ||
|
||
testHook(() => { | ||
const isDetectedPerspective = usePerspectiveDetection(); | ||
|
||
expect(isDetectedPerspective).toBe(true); | ||
}); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
frontend/packages/dev-console/src/utils/usePerspectiveDetection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore: FIXME missing exports due to out-of-sync @types/react-redux version | ||
import { useSelector } from 'react-redux'; | ||
import { RootState } from '@console/internal/redux'; | ||
import { getFlagsObject, flagPending } from '@console/internal/reducers/features'; | ||
|
||
export const usePerspectiveDetection = () => { | ||
const flags = useSelector((state: RootState) => getFlagsObject(state)); | ||
const canGetNS = flags.CAN_GET_NS; | ||
const isDeveloper = !canGetNS; | ||
|
||
return flagPending(canGetNS) ? null : isDeveloper; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters