Skip to content

Commit

Permalink
remove static perspective extension
Browse files Browse the repository at this point in the history
  • Loading branch information
christianvogt committed Jul 30, 2021
1 parent 5c9b34c commit 49eba84
Show file tree
Hide file tree
Showing 25 changed files with 204 additions and 148 deletions.
@@ -1,5 +1,4 @@
import { isNavItem } from '@console/dynamic-plugin-sdk';
import { isPerspective } from '@console/plugin-sdk';
import { isNavItem, isPerspective } from '@console/dynamic-plugin-sdk';
import { testedExtensions } from '../plugin-test-utils';

describe('NavItem', () => {
Expand Down
@@ -1,9 +1,10 @@
import * as _ from 'lodash';
import { isPerspective } from '@console/plugin-sdk';
import { isPerspective } from '@console/dynamic-plugin-sdk';
import { testedExtensions } from '../plugin-test-utils';

describe('Perspective', () => {
it('duplicate ids are not allowed', () => {
// TODO need to load console-extensions.json for tests to function
xit('duplicate ids are not allowed', () => {
const perspectives = testedExtensions.toArray().filter(isPerspective);
const dedupedPerspectives = _.uniqWith(
perspectives,
Expand All @@ -14,7 +15,7 @@ describe('Perspective', () => {
expect(duplicatePerspectives).toEqual([]);
});

it('exactly one default perspective is allowed', () => {
xit('exactly one default perspective is allowed', () => {
const perspectives = testedExtensions.toArray().filter(isPerspective);
const defaultPerspectives = perspectives.filter((p) => p.properties.default);

Expand Down
@@ -1,5 +1,6 @@
import * as React from 'react';
import { isPerspective, Perspective, useExtensions } from '@console/plugin-sdk';
import { isPerspective, Perspective } from '@console/dynamic-plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import { PerpsectiveContext } from './perspective-context';
import PerspectiveDetector from './PerspectiveDetector';
import { useValuesForPerspectiveContext } from './useValuesForPerspectiveContext';
Expand Down
@@ -1,14 +1,16 @@
import * as React from 'react';
import { useExtensions, Perspective, isPerspective } from '@console/plugin-sdk';
import { useResolvedExtensions, Perspective, isPerspective } from '@console/dynamic-plugin-sdk';

type PerspectiveDetectorProps = {
setActivePerspective: (perspective: string) => void;
};

const PerspectiveDetector: React.FC<PerspectiveDetectorProps> = ({ setActivePerspective }) => {
let detectedPerspective: string;
const perspectiveExtensions = useExtensions<Perspective>(isPerspective);
const defaultPerspective = perspectiveExtensions.find((p) => p.properties.default);
// TODO resolve only the perspective detection
const [perspectiveExtensions, resolved] = useResolvedExtensions<Perspective>(isPerspective);
const defaultPerspective =
perspectiveExtensions.find((p) => p.properties.default) || perspectiveExtensions[0];
const detectors = perspectiveExtensions.filter((p) => p.properties.usePerspectiveDetection);
const detectionResults = detectors.map((p) => p.properties.usePerspectiveDetection());

Expand All @@ -21,17 +23,21 @@ const PerspectiveDetector: React.FC<PerspectiveDetectorProps> = ({ setActivePers
});

React.useEffect(() => {
if (detectedPerspective) {
setActivePerspective(detectedPerspective);
} else if (detectors.length < 1 || detectionComplete) {
setActivePerspective(defaultPerspective.properties.id); // set default perspective if there are no detectors or none of the detections were successfull
if (resolved) {
if (detectedPerspective) {
setActivePerspective(detectedPerspective);
} else if (defaultPerspective && (detectors.length < 1 || detectionComplete)) {
// set default perspective if there are no detectors or none of the detections were successfull
setActivePerspective(defaultPerspective.properties.id);
}
}
}, [
defaultPerspective,
detectedPerspective,
detectionComplete,
detectors.length,
setActivePerspective,
resolved,
]);

return null;
Expand Down
@@ -1,54 +1,54 @@
import * as React from 'react';
import { mount } from 'enzyme';
import { Perspective, useExtensions } from '@console/plugin-sdk';
import { Perspective, ResolvedExtension, useResolvedExtensions } from '@console/dynamic-plugin-sdk';
import PerspectiveDetector from '../PerspectiveDetector';

jest.mock('@console/plugin-sdk', () => ({
useExtensions: jest.fn(),
jest.mock('@console/dynamic-plugin-sdk', () => ({
useResolvedExtensions: jest.fn(),
}));

const mockPerspectives = [
{
type: 'Perspective',
type: 'console.perspective',
properties: {
id: 'admin',
name: 'Admin Perspective',
default: true,
},
},
{
type: 'Perspective',
type: 'console.perspective',
properties: {
id: 'dev',
name: 'Dev Perspective',
usePerspectiveDetection: undefined,
},
},
] as Perspective[];
] as ResolvedExtension<Perspective>[];

const setActivePerspective = jest.fn();

describe('PerspectiveDetector', () => {
it('should set default perspective if there are no perspective detectors available', () => {
(useExtensions as jest.Mock).mockImplementation(() => mockPerspectives);
it('should set default perspective if there are no perspective detectors available', async () => {
(useResolvedExtensions as jest.Mock).mockImplementation(() => [mockPerspectives, true]);

const wrapper = mount(<PerspectiveDetector setActivePerspective={setActivePerspective} />);
expect(wrapper.isEmptyRender()).toBe(true);
expect(setActivePerspective).toHaveBeenCalledWith('admin');
});

it('should set detected perspective if detection is successful', () => {
it('should set detected perspective if detection is successful', async () => {
mockPerspectives[1].properties.usePerspectiveDetection = () => [true, false];
(useExtensions as jest.Mock).mockImplementation(() => mockPerspectives);
(useResolvedExtensions as jest.Mock).mockImplementation(() => [mockPerspectives, true]);

const wrapper = mount(<PerspectiveDetector setActivePerspective={setActivePerspective} />);
expect(wrapper.isEmptyRender()).toBe(true);
expect(setActivePerspective).toHaveBeenCalledWith('dev');
});

it('should set default perspective if detection fails', () => {
it('should set default perspective if detection fails', async () => {
mockPerspectives[1].properties.usePerspectiveDetection = () => [false, false];
(useExtensions as jest.Mock).mockImplementation(() => mockPerspectives);
(useResolvedExtensions as jest.Mock).mockImplementation(() => [mockPerspectives, true]);

const wrapper = mount(<PerspectiveDetector setActivePerspective={setActivePerspective} />);
expect(wrapper.isEmptyRender()).toBe(true);
Expand Down
@@ -1,5 +1,6 @@
import * as React from 'react';
import { isPerspective, Perspective, useExtensions } from '@console/plugin-sdk';
import { isPerspective, Perspective } from '@console/dynamic-plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import { useUserSettingsCompatibility } from '@console/shared';
import { PerspectiveType } from './perspective-context';

Expand Down
1 change: 0 additions & 1 deletion frontend/packages/console-plugin-sdk/src/typings/index.ts
Expand Up @@ -8,7 +8,6 @@ export * from './models';
export * from './nav-section';
export * from './overview';
export * from './pages';
export * from './perspectives';
export * from './global-configs';
export * from './clusterserviceversions';
export * from './horizontal-nav';
Expand Down
36 changes: 0 additions & 36 deletions frontend/packages/console-plugin-sdk/src/typings/perspectives.ts

This file was deleted.

11 changes: 9 additions & 2 deletions frontend/packages/console-shared/src/hooks/usePinnedResources.ts
@@ -1,6 +1,8 @@
import { useMemo, useCallback } from 'react';
import * as _ from 'lodash';
import { isPerspective, Perspective, useExtensions } from '@console/plugin-sdk';
import { isPerspective, Perspective } from '@console/dynamic-plugin-sdk';
import { referenceForExtensionModel } from '@console/internal/module/k8s';
import { useExtensions } from '@console/plugin-sdk/src/api/useExtensions';
import { PINNED_RESOURCES_LOCAL_STORAGE_KEY } from '../constants';
import { useActivePerspective } from './useActivePerspective';
import { useTelemetry } from './useTelemetry';
Expand All @@ -19,7 +21,12 @@ export const usePinnedResources = (): [string[], (pinnedResources: string[]) =>
const defaultPins = useMemo(
() =>
perspectiveExtensions.reduce(
(acc, e) => ({ ...acc, [e.properties.id]: e.properties.defaultPins || [] }),
(acc, e) => ({
...acc,
[e.properties.id]: (e.properties.defaultPins || []).map((gvk) =>
referenceForExtensionModel(gvk),
),
}),
{},
),
[perspectiveExtensions],
Expand Down
24 changes: 24 additions & 0 deletions frontend/packages/dev-console/console-extensions.json
@@ -1,4 +1,28 @@
[
{
"type": "console.perspective",
"properties": {
"id": "dev",
"name": "%devconsole~Developer%",
"icon": { "$codeRef": "perspective.icon" },
"defaultPins": [
{
"group": "ConfigMapModel",
"version": "",
"kind": ""
},
{
"group": "SecretModel",
"version": "",
"kind": ""
}
],
"getLandingPageURL": { "$codeRef": "perspective.getLandingPageURL" },
"getK8sLandingPageURL": { "$codeRef": "perspective.getK8sLandingPageURL" },
"getImportRedirectURL": { "$codeRef": "perspective.getImportRedirectURL" },
"usePerspectiveDetection": { "$codeRef": "perspective.usePerspectiveDetection" }
}
},
{
"type": "dev-console.add/action-group",
"properties": {
Expand Down
Expand Up @@ -2,10 +2,11 @@ import * as React from 'react';
import { Formik, FormikProps } from 'formik';
import * as _ from 'lodash';
import { useTranslation } from 'react-i18next';
import { Perspective, isPerspective } from '@console/dynamic-plugin-sdk';
import { history } from '@console/internal/components/utils';
import { ImageStreamModel } from '@console/internal/models';
import { k8sGet, K8sResourceKind } from '@console/internal/module/k8s';
import { useExtensions, Perspective, isPerspective } from '@console/plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import { useActivePerspective } from '@console/shared';
import { NormalizedBuilderImages, normalizeBuilderImages } from '../../utils/imagestream-utils';
import { createOrUpdateDeployImageResources } from '../import/deployImage-submit-utils';
Expand Down
Expand Up @@ -2,10 +2,11 @@ import * as React from 'react';
import { FormikBag, Formik } from 'formik';
import { safeLoad } from 'js-yaml';
import { useTranslation } from 'react-i18next';
import { Perspective, isPerspective } from '@console/dynamic-plugin-sdk';
import { history } from '@console/internal/components/utils';
import { DeploymentConfigModel, DeploymentModel } from '@console/internal/models';
import { K8sResourceKind, k8sUpdate } from '@console/internal/module/k8s';
import { useExtensions, Perspective, isPerspective } from '@console/plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import { useActivePerspective } from '@console/shared';
import { EditorType } from '@console/shared/src/components/synced-editor/editor-toggle';
import { getResourcesType } from '../edit-application/edit-application-utils';
Expand Down
Expand Up @@ -3,10 +3,11 @@ import { Formik, FormikProps } from 'formik';
import * as _ from 'lodash';
import { useTranslation } from 'react-i18next';
import { connect } from 'react-redux';
import { Perspective, isPerspective } from '@console/dynamic-plugin-sdk';
import { history, AsyncComponent } from '@console/internal/components/utils';
import { getActiveApplication } from '@console/internal/reducers/ui';
import { RootState } from '@console/internal/redux';
import { useExtensions, Perspective, isPerspective } from '@console/plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import {
ALL_APPLICATIONS_KEY,
useActivePerspective,
Expand Down
@@ -1,6 +1,7 @@
import * as GitUrlParse from 'git-url-parse';
import { TFunction } from 'i18next';
import * as _ from 'lodash';
import { Perspective } from '@console/dynamic-plugin-sdk';
import { BuildStrategyType } from '@console/internal/components/build';
import { SecretType } from '@console/internal/components/secrets/create-secret';
import { history } from '@console/internal/components/utils';
Expand Down Expand Up @@ -39,7 +40,6 @@ import {
updateServiceAccount,
getSecretAnnotations,
} from '@console/pipelines-plugin/src/utils/pipeline-utils';
import { Perspective } from '@console/plugin-sdk';
import { getRandomChars, getResourceLimitsData } from '@console/shared/src/utils';
import {
getAppLabels,
Expand Down Expand Up @@ -749,12 +749,12 @@ export const createOrUpdateResources = async (
return responses;
};

export const handleRedirect = (
export const handleRedirect = async (
project: string,
perspective: string,
perspectiveExtensions: Perspective[],
) => {
const perspectiveData = perspectiveExtensions.find((item) => item.properties.id === perspective);
const redirectURL = perspectiveData.properties.getImportRedirectURL(project);
const redirectURL = (await perspectiveData.properties.getImportRedirectURL())(project);
history.push(redirectURL);
};
Expand Up @@ -2,10 +2,11 @@ import * as React from 'react';
import { Formik, FormikHelpers } from 'formik';
import * as _ from 'lodash';
import { useTranslation } from 'react-i18next';
import { Perspective, isPerspective } from '@console/dynamic-plugin-sdk';
import { history } from '@console/internal/components/utils';
import { WatchK8sResultsObject } from '@console/internal/components/utils/k8s-watch-hook';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { useExtensions, Perspective, isPerspective } from '@console/plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import {
ALL_APPLICATIONS_KEY,
useActivePerspective,
Expand Down
Expand Up @@ -3,6 +3,7 @@ import { Formik } from 'formik';
import * as _ from 'lodash';
import { useTranslation } from 'react-i18next';
import { connect } from 'react-redux';
import { Perspective, isPerspective } from '@console/dynamic-plugin-sdk';
import { history } from '@console/internal/components/utils';
import {
K8sResourceKind,
Expand All @@ -13,7 +14,7 @@ import {
} from '@console/internal/module/k8s';
import { getActiveApplication } from '@console/internal/reducers/ui';
import { RootState } from '@console/internal/redux';
import { isPerspective, Perspective, useExtensions } from '@console/plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import { ALL_APPLICATIONS_KEY, useActivePerspective } from '@console/shared';
import { EditorType } from '@console/shared/src/components/synced-editor/editor-toggle';
import { sanitizeApplicationValue } from '@console/topology/src/utils/application-utils';
Expand Down
Expand Up @@ -3,9 +3,10 @@ import { Formik, FormikHelpers } from 'formik';
import { safeLoad } from 'js-yaml';
import { useTranslation } from 'react-i18next';
import { handleRedirect } from '@console/dev-console/src/components/import/import-submit-utils';
import { Perspective, isPerspective } from '@console/dynamic-plugin-sdk';
import { history } from '@console/internal/components/utils';
import { K8sResourceKind, k8sCreate } from '@console/internal/module/k8s';
import { useExtensions, isPerspective, Perspective } from '@console/plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import { useActivePerspective } from '@console/shared';
import { EditorType } from '@console/shared/src/components/synced-editor/editor-toggle';
import { EventingBrokerModel } from '../../../models';
Expand Down
Expand Up @@ -2,11 +2,12 @@ import * as React from 'react';
import { Formik } from 'formik';
import { useTranslation } from 'react-i18next';
import { connect } from 'react-redux';
import { Perspective, isPerspective } from '@console/dynamic-plugin-sdk';
import { history } from '@console/internal/components/utils';
import { K8sResourceKind, k8sCreate, modelFor, referenceFor } from '@console/internal/module/k8s';
import { getActiveApplication } from '@console/internal/reducers/ui';
import { RootState } from '@console/internal/redux';
import { isPerspective, Perspective, useExtensions } from '@console/plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import { ALL_APPLICATIONS_KEY, useActivePerspective } from '@console/shared';
import { sanitizeApplicationValue } from '@console/topology/src/utils/application-utils';
import { getCreateChannelResource } from '../../../utils/create-channel-utils';
Expand Down
Expand Up @@ -9,6 +9,7 @@ import { DeployImageFormData } from '@console/dev-console/src/components/import/
import NamespacedPage, {
NamespacedPageVariants,
} from '@console/dev-console/src/components/NamespacedPage';
import { Perspective, isPerspective } from '@console/dynamic-plugin-sdk';
import { LoadingBox, history, PageHeading } from '@console/internal/components/utils';
import {
useK8sWatchResources,
Expand All @@ -17,7 +18,7 @@ import {
} from '@console/internal/components/utils/k8s-watch-hook';
import { ProjectModel, ServiceModel } from '@console/internal/models';
import { k8sGet, K8sResourceKind } from '@console/internal/module/k8s';
import { useExtensions, Perspective, isPerspective } from '@console/plugin-sdk';
import { useExtensions } from '@console/plugin-sdk';
import { BadgeType, getBadgeFromType, useActivePerspective, useRelatedHPA } from '@console/shared';
import {
getInitialValuesKnatify,
Expand Down

0 comments on commit 49eba84

Please sign in to comment.