Merge pull request #8521 from reinoudk/fix-settings-page

fix(user-settings): use non-deprecated IdentityApi methods
This commit is contained in:
Fredrik Adelöw
2021-12-17 20:25:11 +01:00
committed by GitHub
3 changed files with 44 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-user-settings': patch
---
Fix undefined identity bug in UserSettingsProfileCard caused by using deprecated methods of the IdentityApi
+1
View File
@@ -119,5 +119,6 @@ export const UserSettingsThemeToggle: () => JSX.Element;
export const useUserProfile: () => {
profile: ProfileInfo;
displayName: string;
loading: boolean;
};
```
@@ -14,13 +14,46 @@
* limitations under the License.
*/
import { useApi, identityApiRef } from '@backstage/core-plugin-api';
import {
alertApiRef,
identityApiRef,
ProfileInfo,
useApi,
} from '@backstage/core-plugin-api';
import { useEffect } from 'react';
import { useAsync } from 'react-use';
export const useUserProfile = () => {
const identityApi = useApi(identityApiRef);
const userId = identityApi.getUserId();
const profile = identityApi.getProfile();
const displayName = profile.displayName ?? userId;
const alertApi = useApi(alertApiRef);
return { profile, displayName };
const { value, loading, error } = useAsync(async () => {
return {
profile: await identityApi.getProfileInfo(),
identity: await identityApi.getBackstageIdentity(),
};
}, []);
useEffect(() => {
if (error) {
alertApi.post({
message: `Failed to load user identity: ${error}`,
severity: 'error',
});
}
}, [error, alertApi]);
if (loading || error) {
return {
profile: {} as ProfileInfo,
displayName: '',
loading,
};
}
return {
profile: value!.profile,
displayName: value!.profile.displayName ?? value!.identity.userEntityRef,
loading,
};
};