@backstage/plugin-techdocs
Version:
The Backstage plugin that renders technical documentation for your components
64 lines (61 loc) • 2.12 kB
JavaScript
import { useEffect, useCallback } from 'react';
import { useLocation } from 'react-router-dom';
import { useTechDocsReaderPage, useShadowDomStylesLoading, useShadowRootElements } from '@backstage/plugin-techdocs-react';
import { useApp } from '@backstage/core-plugin-api';
import { useTechDocsReaderDom } from '../reader/components/TechDocsReaderPageContent/dom.esm.js';
import { useTechDocsReader } from '../reader/components/TechDocsReaderProvider.esm.js';
function useTechDocsReaderContentData(options) {
const { defaultPath, onReady } = options;
const {
entityMetadata: { value: entityMetadata, loading: entityMetadataLoading },
entityRef,
setShadowRoot
} = useTechDocsReaderPage();
const { state } = useTechDocsReader();
const dom = useTechDocsReaderDom(entityRef, defaultPath);
const location = useLocation();
const path = location.pathname;
const hash = location.hash;
const isStyleLoading = useShadowDomStylesLoading(dom);
const [hashElement] = useShadowRootElements([`[id="${hash.slice(1)}"]`]);
const app = useApp();
const { NotFoundErrorPage } = app.getComponents();
useEffect(() => {
if (isStyleLoading) return;
if (hash) {
if (hashElement) {
hashElement.scrollIntoView();
const link = hashElement.querySelector("a.headerlink");
if (link) {
link.focus();
}
}
} else {
document?.querySelector("header")?.scrollIntoView();
}
}, [path, hash, hashElement, isStyleLoading]);
const handleAppend = useCallback(
(newShadowRoot) => {
setShadowRoot(newShadowRoot);
if (onReady instanceof Function) {
onReady();
}
},
[setShadowRoot, onReady]
);
const isNotFound = entityMetadataLoading === false && !entityMetadata;
const isDomReady = !!dom;
const showProgress = state === "CHECKING" || isStyleLoading;
return {
entityRef,
entityMetadata,
dom,
handleAppend,
isNotFound,
isDomReady,
showProgress,
NotFoundErrorPage
};
}
export { useTechDocsReaderContentData };
//# sourceMappingURL=useTechDocsReaderContentData.esm.js.map