Skip to content

Commit

Permalink
Merge pull request #11135 from jerolimov/bz2009345-4.7
Browse files Browse the repository at this point in the history
[release-4.7] Bug 2060451: Fix a crash when the preferred namespace contains just numbers
  • Loading branch information
openshift-merge-robot committed Apr 7, 2022
2 parents 59d7e2b + 276f15c commit b023854
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,25 @@ import * as React from 'react';
// @ts-ignore: FIXME missing exports due to out-of-sync @types/react-redux version
import { useDispatch } from 'react-redux';
import { useLocation } from 'react-router-dom';
import { getNamespace } from '@console/internal/components/utils/link';
import { useUserSettingsCompatibility } from '@console/shared/src/hooks/useUserSettingsCompatibility';
import { setActiveNamespace } from '@console/internal/actions/ui';
import {
ALL_NAMESPACES_KEY,
USERSETTINGS_PREFIX,
NAMESPACE_USERSETTINGS_PREFIX,
NAMESPACE_LOCAL_STORAGE_KEY,
} from '@console/shared/src/constants';
import { getNamespace } from '@console/internal/components/utils/link';
import { usePreferredNamespace } from '../user-preferences/namespace/usePreferredNamespace';
import { useLastNamespace } from './useLastNamespace';
import { ALL_NAMESPACES_KEY } from '@console/shared/src/constants';

type NamespaceContextType = {
namespace?: string;
setNamespace?: (ns: string) => void;
};

const FAVORITE_NAMESPACE_NAME_USERSETTINGS_KEY = `${NAMESPACE_USERSETTINGS_PREFIX}.favorite`;
const FAVORITE_NAMESPACE_NAME_LOCAL_STORAGE_KEY = NAMESPACE_LOCAL_STORAGE_KEY;

const LAST_NAMESPACE_NAME_USER_SETTINGS_KEY = `${USERSETTINGS_PREFIX}.lastNamespace`;
const LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY = `bridge/last-namespace-name`;

export const NamespaceContext = React.createContext<NamespaceContextType>({});

export const useValuesForNamespaceContext = () => {
const { pathname } = useLocation();
const urlNamespace = getNamespace(pathname);

const [favoritedNamespace, , favoriteLoaded] = useUserSettingsCompatibility<string>(
FAVORITE_NAMESPACE_NAME_USERSETTINGS_KEY,
FAVORITE_NAMESPACE_NAME_LOCAL_STORAGE_KEY,
);
const [lastNamespace, setLastNamespace, lastNamespaceLoaded] = useUserSettingsCompatibility<
string
>(LAST_NAMESPACE_NAME_USER_SETTINGS_KEY, LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY);
const [favoritedNamespace, , favoriteLoaded] = usePreferredNamespace();
const [lastNamespace, setLastNamespace, lastNamespaceLoaded] = useLastNamespace();

const dispatch = useDispatch();
const setNamespace = React.useCallback(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
LAST_NAMESPACE_NAME_USER_SETTINGS_KEY,
LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY,
} from '@console/shared/src/constants';
import { useUserSettingsCompatibility } from '@console/shared/src/hooks/useUserSettingsCompatibility';

export const useLastNamespace = (): [
string,
React.Dispatch<React.SetStateAction<string>>,
boolean,
] => {
const [lastNamespace, setLastNamespace, lastNamespaceLoaded] = useUserSettingsCompatibility<
string
>(LAST_NAMESPACE_NAME_USER_SETTINGS_KEY, LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY);

// This toString is workaround because the useUserSettings hook returns a number or boolean
// when the saved value represents a number (1234) or boolean (true/false).
// This is a workaround for https://bugzilla.redhat.com/show_bug.cgi?id=2009345.
// We will implement a more generic fix with https://issues.redhat.com/browse/ODC-6514
return [lastNamespace?.toString(), setLastNamespace, lastNamespaceLoaded];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Dispatch, SetStateAction } from 'react';
import { useUserSettingsCompatibility } from '@console/shared';

const PREFERRED_NAMESPACE_USER_SETTING_KEY: string = 'console.namespace.favorite';
const PREFERRED_NAMESPACE_NAME_LOCAL_STORAGE_KEY = 'dropdown-storage-namespaces';

export const usePreferredNamespace = (): [string, Dispatch<SetStateAction<string>>, boolean] => {
const [
preferredNamespace,
setPreferredNamespace,
preferredNamespaceLoaded,
] = useUserSettingsCompatibility<string>(
PREFERRED_NAMESPACE_USER_SETTING_KEY,
PREFERRED_NAMESPACE_NAME_LOCAL_STORAGE_KEY,
);

// This toString is workaround because the useUserSettings hook returns a number or boolean
// when the saved value represents a number (1234) or boolean (true/false).
// This is a workaround for https://bugzilla.redhat.com/show_bug.cgi?id=2009345.
// We will implement a more generic fix with https://issues.redhat.com/browse/ODC-6514
return [preferredNamespace?.toString(), setPreferredNamespace, preferredNamespaceLoaded];
};
1 change: 1 addition & 0 deletions frontend/packages/console-shared/src/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const NAMESPACE_USERSETTINGS_PREFIX = `${USERSETTINGS_PREFIX}.namespace`;
export const NAMESPACE_LOCAL_STORAGE_KEY = 'dropdown-storage-namespaces';
export const APPLICATION_USERSETTINGS_PREFIX = `${USERSETTINGS_PREFIX}.applications`;
export const APPLICATION_LOCAL_STORAGE_KEY = 'dropdown-storage-applications';
export const LAST_NAMESPACE_NAME_USER_SETTINGS_KEY = `${USERSETTINGS_PREFIX}.lastNamespace`;
export const LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY = `${STORAGE_PREFIX}/last-namespace-name`;
export const API_DISCOVERY_RESOURCES_LOCAL_STORAGE_KEY = `${STORAGE_PREFIX}/api-discovery-resources`;
export const COMMUNITY_PROVIDERS_WARNING_LOCAL_STORAGE_KEY = `${STORAGE_PREFIX}/community-providers-warning`;
Expand Down

0 comments on commit b023854

Please sign in to comment.