datahub/datahub-web/@datahub/data-models/addon/utils/entity-route-name-resolver.ts
Charlie Tran 843a6c5bbb
feat(frontend): update datahub-web client UI code (#1806)
* Releases updated version of datahub-web client UI code

* Fix typo in yarn lock

* Change yarn lock to match yarn registry directories

* Previous commit missed some paths

* Even more changes to yarnlock missing in previous commit

* Include codegen file for typings

* Add files to get parity for datahub-web and current OS datahub-midtier

* Add in typo fix from previous commit - change to proper license

* Implement proper OS fix for person entity picture url

* Workarounds for open source DH issues

* Fixes institutional memory api and removes unopensourced tabs for datasets

* Fixes search dataset deprecation and user search issue as a result of changes

* Remove internal only options in the avatar menu
2020-08-26 15:44:50 -07:00

39 lines
2.0 KiB
TypeScript

import { resolveDynamicRouteName } from '@datahub/utils/routes/routing';
import { MaybeRouteInfoWithAttributes } from '@datahub/utils/types/vendor/routerjs';
import Transition from '@ember/routing/-private/transition';
import { DataModelEntity, DataModelName } from '@datahub/data-models/constants/entity';
/**
* Indexes the route names we care about to functions that resolve the placeholder value
* defaults to the route.name, if a resolved value cannot be determined
* @type Record<string, ((r: RouteInfoWithOrWithoutAttributes) => string) | undefined>
*/
export const mapOfRouteNamesToResolver: Record<string, ((r: MaybeRouteInfoWithAttributes) => string) | void> = {
'browse.entity': (route: MaybeRouteInfoWithAttributes): string =>
route.attributes ? `browse.${route.attributes.entity}` : route.name,
'browse.entity.index': (route: MaybeRouteInfoWithAttributes): string =>
route.attributes ? `browse.${route.attributes.entity}` : route.name,
'entity-type.urn.tab': (route: MaybeRouteInfoWithAttributes): string =>
route.attributes ? `${route.attributes.entityClass.displayName}.${route.attributes.tabSelected}` : route.name
};
/**
* Guard checks that a route name is an entity route by testing if the routeName begins with the entity name
* @param {string} routeName the name of the route to check against
* @returns {boolean}
*/
const routeNameIsEntityRoute = (routeName: string, entitiesAvailable: Array<DataModelName>): boolean =>
entitiesAvailable.some((entityName: DataModelEntity['displayName']): boolean => routeName.startsWith(entityName));
/**
* Check if the route info instance has a name that is considered an entity route
* @returns {boolean}
*/
export const isRouteEntityPageRoute = (
routeBeingTransitionedTo: Transition['to' | 'from'],
entitiesAvailable: Array<DataModelName>
): boolean => {
const routeName = resolveDynamicRouteName(mapOfRouteNamesToResolver, routeBeingTransitionedTo);
return Boolean(routeName && routeNameIsEntityRoute(routeName, entitiesAvailable));
};