fix(WelcomeTitle): use non-deprecated IdentityApi methods

Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
Phil Kuang
2021-12-17 14:34:43 -05:00
parent 6ae3e64b14
commit 6d36220ef2
2 changed files with 29 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-home': patch
---
Fix undefined identity bug in `WelcomeTitle` caused by using deprecated methods of the IdentityApi
@@ -13,20 +13,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { identityApiRef, useApi } from '@backstage/core-plugin-api';
import {
alertApiRef,
identityApiRef,
useApi,
} from '@backstage/core-plugin-api';
import { Tooltip } from '@material-ui/core';
import React, { useMemo } from 'react';
import React, { useEffect, useMemo } from 'react';
import { useAsync } from 'react-use';
import { getTimeBasedGreeting } from './timeUtil';
export const WelcomeTitle = () => {
const identityApi = useApi(identityApiRef);
const profile = identityApi.getProfile();
const userId = identityApi.getUserId();
const alertApi = useApi(alertApiRef);
const greeting = useMemo(() => getTimeBasedGreeting(), []);
const { value: profile, error } = useAsync(() =>
identityApi.getProfileInfo(),
);
useEffect(() => {
if (error) {
alertApi.post({
message: `Failed to load user identity: ${error}`,
severity: 'error',
});
}
}, [error, alertApi]);
return (
<Tooltip title={greeting.language}>
<span>{`${greeting.greeting}, ${profile.displayName || userId}!`}</span>
<span>{`${greeting.greeting}${
profile?.displayName ? `, ${profile?.displayName}` : ''
}!`}</span>
</Tooltip>
);
};