@roadiehq/backstage-plugin-github-pull-requests
Version:
114 lines (111 loc) • 4.03 kB
JavaScript
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState } from 'react';
import { MissingAnnotationEmptyState, InfoCard, StructuredMetadataTable } from '@backstage/core-components';
import { isGithubSlugSet, GITHUB_PULL_REQUESTS_ANNOTATION } from '../../utils/isGithubSlugSet.esm.js';
import { usePullRequestsStatistics } from '../usePullRequestsStatistics.esm.js';
import { makeStyles, Tooltip, CircularProgress, Box, FormControl, Select, MenuItem, FormHelperText } from '@material-ui/core';
import { useEntity } from '@backstage/plugin-catalog-react';
import { TooltipContent } from './components/TooltipContent.esm.js';
import { getHostname } from '../../utils/githubUtils.esm.js';
import { GitHubAuthorizationWrapper } from '@roadiehq/github-auth-utils-react';
const useStyles = makeStyles((theme) => ({
infoCard: {
marginBottom: theme.spacing(3),
"& + .MuiAlert-root": {
marginTop: theme.spacing(3)
},
"& .MuiCardContent-root": {
padding: theme.spacing(2, 1, 2, 2)
},
"& td": {
whiteSpace: "normal"
}
}
}));
const StatsCard = (props) => {
const { entity } = useEntity();
const classes = useStyles();
const [pageSize, setPageSize] = useState(20);
const projectName = isGithubSlugSet(entity);
const [owner, repo] = (projectName ?? "/").split("/");
const [{ statsData, loading: loadingStatistics }] = usePullRequestsStatistics(
{
owner,
repo,
pageSize,
state: "closed"
}
);
const metadata = {
"average time of PR until merge": statsData?.avgTimeUntilMerge,
"merged to closed ratio": statsData?.mergedToClosedRatio,
"average size of PR": /* @__PURE__ */ jsx(
Tooltip,
{
title: /* @__PURE__ */ jsx(
TooltipContent,
{
additions: statsData?.avgAdditions,
deletions: statsData?.avgDeletions
}
),
children: /* @__PURE__ */ jsxs("div", { children: [
statsData?.avgChangedLinesCount,
" lines"
] })
}
),
"average changed files of PR": `${statsData?.avgChangedFilesCount}`,
"average coding time of PR": `${statsData?.avgCodingTime}`
};
return /* @__PURE__ */ jsx(
InfoCard,
{
title: "GitHub Pull Requests Statistics",
className: classes.infoCard,
variant: props.variant,
children: loadingStatistics ? /* @__PURE__ */ jsx(CircularProgress, {}) : /* @__PURE__ */ jsxs(Box, { position: "relative", children: [
/* @__PURE__ */ jsx(StructuredMetadataTable, { metadata }),
/* @__PURE__ */ jsx(Box, { display: "flex", justifyContent: "flex-end", children: /* @__PURE__ */ jsxs(FormControl, { children: [
/* @__PURE__ */ jsxs(
Select,
{
value: pageSize,
onChange: (event) => setPageSize(Number(event.target.value)),
children: [
/* @__PURE__ */ jsx(MenuItem, { value: 10, children: "10" }),
/* @__PURE__ */ jsx(MenuItem, { value: 20, children: "20" }),
/* @__PURE__ */ jsx(MenuItem, { value: 50, children: "50" }),
/* @__PURE__ */ jsx(MenuItem, { value: 100, children: "100" })
]
}
),
/* @__PURE__ */ jsx(FormHelperText, { children: "Number of PRs" })
] }) })
] })
}
);
};
const PullRequestsStatsCard = (props) => {
const { entity } = useEntity();
const hostname = getHostname(entity);
const projectName = isGithubSlugSet(entity);
if (!projectName || projectName === "") {
return /* @__PURE__ */ jsx(
MissingAnnotationEmptyState,
{
annotation: GITHUB_PULL_REQUESTS_ANNOTATION
}
);
}
return /* @__PURE__ */ jsx(
GitHubAuthorizationWrapper,
{
title: "GitHub Pull Requests Statistics",
hostname,
children: /* @__PURE__ */ jsx(StatsCard, { ...props })
}
);
};
export { PullRequestsStatsCard as default };
//# sourceMappingURL=PullRequestsStatsCard.esm.js.map