Fix unsafe type casts via shared entityPresentationSnapshot utility

Extract entityPresentationSnapshot helper to eliminate unsafe `as Entity`
casts when passing CompoundEntityRef to EntityPresentationApi.forEntity(),
which only accepts Entity | string. The helper safely discriminates input
types and stringifies CompoundEntityRef before calling the API.

- Add entityPresentationSnapshot as a public export from catalog-react
- Remove duplicated getEntityTitle/getTitle helpers across 5 files
- Fix useAsync dependency array in TemplateFormPreviewer
- Update TSDoc across all presentation API surfaces to reference the
  new helper
- Update entity-presentation.md docs with usage guidance and migration
  table

Made-with: Cursor
Signed-off-by: Marat Dyatko <maratd@spotify.com>
This commit is contained in:
Marat Dyatko
2026-04-07 12:41:29 +02:00
parent f55c195f03
commit b32ab39670
16 changed files with 371 additions and 111 deletions
@@ -14,13 +14,9 @@
* limitations under the License.
*/
import { RELATION_OWNED_BY, Entity } from '@backstage/catalog-model';
import {
RELATION_OWNED_BY,
Entity,
stringifyEntityRef,
} from '@backstage/catalog-model';
import {
defaultEntityPresentation,
entityPresentationSnapshot,
getEntityRelations,
type EntityPresentationApi,
} from '@backstage/plugin-catalog-react';
@@ -54,15 +50,14 @@ export function entitiesToDocsMapper(
}),
ownedByRelations,
ownedByRelationsTitle: ownedByRelations
.map(r => {
if (entityPresentationApi) {
return entityPresentationApi.forEntity(stringifyEntityRef(r), {
defaultKind: 'group',
}).snapshot.primaryTitle;
}
return defaultEntityPresentation(r, { defaultKind: 'group' })
.primaryTitle;
})
.map(
r =>
entityPresentationSnapshot(
r,
{ defaultKind: 'group' },
entityPresentationApi,
).primaryTitle,
)
.join(', '),
},
};