@redocly/theme
Version:
Shared UI components lib
42 lines • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCatalogEntityLink = useCatalogEntityLink;
const react_1 = require("react");
function isEntityExcluded(excludes, entityKey) {
var _a;
return (_a = excludes === null || excludes === void 0 ? void 0 : excludes.some((ex) => ex.key === entityKey)) !== null && _a !== void 0 ? _a : false;
}
function doesCatalogIncludeEntityType(config, entityType) {
var _a, _b;
const hasNoIncludes = !config.includes || config.includes.length === 0;
if (hasNoIncludes)
return true;
return (_b = (_a = config.includes) === null || _a === void 0 ? void 0 : _a.some((inc) => inc.type === entityType)) !== null && _b !== void 0 ? _b : false;
}
function useCatalogEntityLink(entitiesCatalogConfig) {
const catalogEntries = (0, react_1.useMemo)(() => {
const catalogs = entitiesCatalogConfig === null || entitiesCatalogConfig === void 0 ? void 0 : entitiesCatalogConfig.catalogs;
if (!catalogs)
return [];
return Object.entries(catalogs)
.filter((entry) => entry[1] != null)
.sort(([keyA], [keyB]) => (keyA === 'all' ? 1 : 0) - (keyB === 'all' ? 1 : 0));
}, [entitiesCatalogConfig]);
const getEntityLink = (entity) => {
var _a, _b;
if (!(entity === null || entity === void 0 ? void 0 : entity.key))
return undefined;
const entityType = (_a = entity.type) !== null && _a !== void 0 ? _a : '';
for (const [catalogKey, config] of catalogEntries) {
if (isEntityExcluded(config.excludes, entity.key))
continue;
if (!doesCatalogIncludeEntityType(config, entityType))
continue;
const slug = (_b = config.slug) !== null && _b !== void 0 ? _b : catalogKey;
return `catalogs/${slug}/entities/${entity.key}`;
}
return undefined;
};
return { getEntityLink };
}
//# sourceMappingURL=use-catalog-entity-link.js.map