diff --git a/frontend/src/app/hooks/useGetMe.js b/frontend/src/app/hooks/useGetMe.js new file mode 100644 index 00000000..cf890d62 --- /dev/null +++ b/frontend/src/app/hooks/useGetMe.js @@ -0,0 +1,8 @@ +import { useSelector } from 'react-redux'; + +import { selectUser } from 'app/store/userSlice'; + +export const useGetMe = () => { + const user = useSelector(selectUser); + return user; +}; diff --git a/frontend/src/app/main/applications/NamespacesSelect.js b/frontend/src/app/main/applications/NamespacesSelect.js index 3ee1e761..c37f521e 100644 --- a/frontend/src/app/main/applications/NamespacesSelect.js +++ b/frontend/src/app/main/applications/NamespacesSelect.js @@ -3,18 +3,28 @@ import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete'; import { useState, useEffect } from 'react'; import { getNamespacesList as getNamespacesListAPI } from '../../api'; +import { useGetMe } from '../../hooks/useGetMe'; +import { formattedNamespace } from '../../uitls/formattedNamespace'; const filter = createFilterOptions(); const NamespacesSelect = ({ clusterContextName, handleGetNamespace }) => { const [namespace, setNamespace] = useState(null); - const [open, toggleOpen] = useState(false); const [list, setList] = useState([]); + const user = useGetMe(); useEffect(() => { handleGetNamespace(namespace?.name); }, [namespace]); + useEffect(() => { + const defaultNamespace = { + name: formattedNamespace(user.email), + states: 'active', + }; + setNamespace(defaultNamespace); + }, [user]); + useEffect(() => { let canceled = false; if (clusterContextName) { @@ -38,13 +48,6 @@ const NamespacesSelect = ({ clusterContextName, handleGetNamespace }) => { }; }, [clusterContextName]); - useEffect(() => { - if (list.length) { - const namespaceItem = list.find((item) => item.name === 'default'); - setNamespace(namespaceItem); - } - }, [list]); - return ( { const [namespace, setNamespace] = useState(null); const [open, toggleOpen] = useState(false); const [list, setList] = useState([]); + const user = useGetMe(); useEffect(() => { handleGetNamespace(namespace?.name); }, [namespace]); + useEffect(() => { + const defaultNamespace = { + name: formattedNamespace(user.email), + states: 'active', + }; + + setNamespace(defaultNamespace); + }, [user]); + useEffect(() => { let canceled = false; if (clusterContextName) { @@ -38,13 +50,6 @@ const NamespacesSelect = ({ clusterContextName, handleGetNamespace }) => { }; }, [clusterContextName]); - useEffect(() => { - if (list.length) { - const namespaceItem = list.find((item) => item.name === 'default'); - setNamespace(namespaceItem); - } - }, [list]); - const handleClose = () => { setDialogValue({ name: '', diff --git a/frontend/src/app/uitls/formattedNamespace.js b/frontend/src/app/uitls/formattedNamespace.js new file mode 100644 index 00000000..931554e5 --- /dev/null +++ b/frontend/src/app/uitls/formattedNamespace.js @@ -0,0 +1,3 @@ +export const formattedNamespace = (namespace) => { + return namespace.replaceAll(/[^@a-z0-9]/gi, '-').replaceAll(/[@]/g, '--'); +};