Merge pull request #8521 from reinoudk/fix-settings-page
fix(user-settings): use non-deprecated IdentityApi methods
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-user-settings': patch
|
||||
---
|
||||
|
||||
Fix undefined identity bug in UserSettingsProfileCard caused by using deprecated methods of the IdentityApi
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user