Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-4.7] Bug 2060451: Fix a crash when the preferred namespace contains just numbers #11135

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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