UNPKG

@roadiehq/backstage-plugin-github-insights

Version:
75 lines (72 loc) 2.48 kB
import { jsx } from 'react/jsx-runtime'; import { useCallback } from 'react'; import { Alert } from '@material-ui/lab'; import { Progress, MarkdownContent as MarkdownContent$1 } from '@backstage/core-components'; import { useApiHolder, configApiRef } from '@backstage/core-plugin-api'; import useAsync from 'react-use/lib/useAsync'; import { GithubClient } from '../../../apis/GithubClient.esm.js'; import { githubApiRef } from '../../../apis/githubApiRef.esm.js'; import { scmAuthApiRef } from '@backstage/integration-react'; import { GitHubAuthorizationWrapper } from '@roadiehq/github-auth-utils-react'; const getGithubClient = (apiHolder) => { let githubClient = apiHolder.get(githubApiRef); if (!githubClient) { const configApi = apiHolder.get(configApiRef); const scmAuthApi = apiHolder.get(scmAuthApiRef); if (scmAuthApi && configApi) { githubClient = new GithubClient({ configApi, scmAuthApi }); } } if (!githubClient) { throw new Error( "The MarkdownCard component Failed to get the SCM auth client or SCM configuration" ); } return githubClient; }; const GithubFileContent = (props) => { const { preserveHtmlComments } = props; const apiHolder = useApiHolder(); const { value, loading, error } = useAsync(async () => { const githubClient = getGithubClient(apiHolder); return githubClient.getContent({ ...props }); }, [apiHolder]); const transformImageUri = useCallback( (href) => { return value?.media[href] || href; }, [value?.media] ); const transformLinkUri = useCallback( (href) => { return value?.links[href] || href; }, [value?.links] ); if (loading) { return /* @__PURE__ */ jsx(Progress, {}); } else if (error) { return /* @__PURE__ */ jsx(Alert, { severity: "error", children: error.message }); } if (!value) { return /* @__PURE__ */ jsx(Progress, {}); } let content = value.content; if (!preserveHtmlComments) { content = content.replace(/<!--(.|\n)*?-->/g, ""); } return /* @__PURE__ */ jsx( MarkdownContent$1, { transformImageUri, transformLinkUri, content } ); }; const MarkdownContent = (props) => { const { hostname } = props; return /* @__PURE__ */ jsx(GitHubAuthorizationWrapper, { title: "Markdown Card", hostname, children: /* @__PURE__ */ jsx(GithubFileContent, { ...props }) }); }; export { MarkdownContent as default }; //# sourceMappingURL=MarkdownContent.esm.js.map