message: Integrate favorited namespace into 'actual' namespace context/hook
diff: @@ -6,46 +6,63 @@ 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 } from '@console/shared/src/constants';
  +import {
  + ALL_NAMESPACES_KEY,
  + USERSETTINGS_PREFIX,
  + NAMESPACE_USERSETTINGS_PREFIX,
  + NAMESPACE_LOCAL_STORAGE_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`;
  -const LAST_NAMESPACE_NAME_USER_SETTINGS_KEY = 'console.lastNamespace';
  export const NamespaceContext = React.createContext<NamespaceContextType>({});
  export const useValuesForNamespaceContext = () => {
  const { pathname } = useLocation();
  const urlNamespace = getNamespace(pathname);
  - /**
  - * [TODO]: use favorite namespace here if there is any
  - * const [favorite] = useUserSettings()
  - */
  - const [namespace, setNs, loaded] = useUserSettingsCompatibility(
  - LAST_NAMESPACE_NAME_USER_SETTINGS_KEY,
  - LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY,
  - urlNamespace || ALL_NAMESPACES_KEY,
  +
  + const [favoritedNamespace, , favoriteLoaded] = useUserSettingsCompatibility<string>(
  + FAVORITE_NAMESPACE_NAME_USERSETTINGS_KEY,
  + FAVORITE_NAMESPACE_NAME_LOCAL_STORAGE_KEY,
  );
  - const dispatchreduxAction = useDispatch();
  + const [lastNamespace, setLastnameNamespace, lastNamespaceLoaded] = useUserSettingsCompatibility<
  + string
  + >(LAST_NAMESPACE_NAME_USER_SETTINGS_KEY, LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY);
  +
  + const dispatch = useDispatch();
  const setNamespace = React.useCallback(
  - (ns: string) => {
  - dispatchreduxAction(setActiveNamespace(ns));
  - setNs(ns);
  + (namespace: string) => {
  + dispatch(setActiveNamespace(namespace));
  + setLastnameNamespace(namespace);
  },
  - [dispatchreduxAction, setNs],
  + [dispatch, setLastnameNamespace],
  );
  + // Keep namespace in sync with redux
  React.useEffect(() => {
  - if (loaded) {
  - dispatchreduxAction(setActiveNamespace(urlNamespace || namespace || ALL_NAMESPACES_KEY));
  + // Url namespace overrides everything. We don't need to wait for loaded
  + if (urlNamespace) {
  + dispatch(setActiveNamespace(urlNamespace));
  + } else if (favoriteLoaded && lastNamespaceLoaded) {
  + // Calculate namespace after loading: favorite > last used namespace > all
  + dispatch(setActiveNamespace(favoritedNamespace || lastNamespace || ALL_NAMESPACES_KEY));
  }
  - // only run this hook when namespace is loaded from user settings
  + // only run this hook when favorite and last namespace is loaded from user settings
  // eslint-disable-next-line react-hooks/exhaustive-deps
  - }, [loaded]);
  + }, [urlNamespace, favoriteLoaded, lastNamespaceLoaded]);
  - return { namespace: urlNamespace || namespace || ALL_NAMESPACES_KEY, setNamespace, loaded };
  + return {
  + namespace: urlNamespace || lastNamespace || favoritedNamespace || ALL_NAMESPACES_KEY,
  + setNamespace,
  + loaded: favoriteLoaded && lastNamespaceLoaded,
  + };
  };
  