@roadiehq/backstage-plugin-github-insights
Version:
74 lines (71 loc) • 2.53 kB
JavaScript
import React, { 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__ */ React.createElement(Progress, null);
} else if (error) {
return /* @__PURE__ */ React.createElement(Alert, { severity: "error" }, error.message);
}
if (!value) {
return /* @__PURE__ */ React.createElement(Progress, null);
}
let content = value.content;
if (!preserveHtmlComments) {
content = content.replace(/<!--(.|\n)*?-->/g, "");
}
return /* @__PURE__ */ React.createElement(
MarkdownContent$1,
{
transformImageUri,
transformLinkUri,
content
}
);
};
const MarkdownContent = (props) => {
const { hostname } = props;
return /* @__PURE__ */ React.createElement(GitHubAuthorizationWrapper, { title: "Markdown Card", hostname }, /* @__PURE__ */ React.createElement(GithubFileContent, { ...props }));
};
export { MarkdownContent as default };
//# sourceMappingURL=MarkdownContent.esm.js.map