Skip to content

Commit

Permalink
fix(WelcomeTitle): use non-deprecated IdentityApi methods
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Kuang <pkuang@factset.com>
  • Loading branch information
kuangp committed Dec 17, 2021
1 parent 6ae3e64 commit 6d36220
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-scissors-yell.md
@@ -0,0 +1,5 @@
---
'@backstage/plugin-home': patch
---

Fix undefined identity bug in `WelcomeTitle` caused by using deprecated methods of the IdentityApi
29 changes: 24 additions & 5 deletions plugins/home/src/homePageComponents/WelcomeTitle/WelcomeTitle.tsx
Expand Up @@ -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>
);
};

0 comments on commit 6d36220

Please sign in to comment.