mirror of
https://github.com/Aviortheking/codestats-readme.git
synced 2025-04-22 10:42:08 +00:00
199 lines
4.5 KiB
JavaScript
199 lines
4.5 KiB
JavaScript
const {
|
|
kFormatter,
|
|
getCardColors,
|
|
FlexLayout,
|
|
encodeHTML,
|
|
} = require("../src/utils");
|
|
const { getStyles } = require("./getStyles");
|
|
const icons = require("./icons");
|
|
const Card = require("./Card");
|
|
|
|
const createTextNode = ({
|
|
icon,
|
|
label,
|
|
value,
|
|
id,
|
|
index,
|
|
showIcons,
|
|
shiftValuePos,
|
|
}) => {
|
|
const kValue = kFormatter(value);
|
|
const staggerDelay = (index + 3) * 150;
|
|
|
|
const labelOffset = showIcons ? `x="25"` : "";
|
|
const iconSvg = showIcons
|
|
? `
|
|
<svg data-testid="icon" class="icon" viewBox="0 0 16 16" version="1.1" width="16" height="16">
|
|
${icon}
|
|
</svg>
|
|
`
|
|
: "";
|
|
return `
|
|
<g class="stagger" style="animation-delay: ${staggerDelay}ms" transform="translate(25, 0)">
|
|
${iconSvg}
|
|
<text class="stat bold" ${labelOffset} y="12.5">${label}:</text>
|
|
<text
|
|
class="stat"
|
|
x="${shiftValuePos ? (showIcons ? 200 : 170) : 150}"
|
|
y="12.5"
|
|
data-testid="${id}"
|
|
>${kValue}</text>
|
|
</g>
|
|
`;
|
|
};
|
|
|
|
const renderStatsCard = (stats = {}, options = { hide: [] }) => {
|
|
const {
|
|
name,
|
|
totalStars,
|
|
totalCommits,
|
|
totalIssues,
|
|
totalPRs,
|
|
contributedTo,
|
|
rank,
|
|
} = stats;
|
|
const {
|
|
hide = [],
|
|
show_icons = false,
|
|
hide_title = false,
|
|
hide_border = false,
|
|
hide_rank = false,
|
|
include_all_commits = false,
|
|
line_height = 25,
|
|
title_color,
|
|
icon_color,
|
|
text_color,
|
|
bg_color,
|
|
theme = "default",
|
|
} = options;
|
|
|
|
const lheight = parseInt(line_height, 10);
|
|
|
|
// returns theme based colors with proper overrides and defaults
|
|
const { titleColor, textColor, iconColor, bgColor } = getCardColors({
|
|
title_color,
|
|
icon_color,
|
|
text_color,
|
|
bg_color,
|
|
theme,
|
|
});
|
|
|
|
// Meta data for creating text nodes with createTextNode function
|
|
const STATS = {
|
|
stars: {
|
|
icon: icons.star,
|
|
label: "Total Stars",
|
|
value: totalStars,
|
|
id: "stars",
|
|
},
|
|
commits: {
|
|
icon: icons.commits,
|
|
label: `Total Commits${
|
|
include_all_commits ? "" : ` (${new Date().getFullYear()})`
|
|
}`,
|
|
value: totalCommits,
|
|
id: "commits",
|
|
},
|
|
prs: {
|
|
icon: icons.prs,
|
|
label: "Total PRs",
|
|
value: totalPRs,
|
|
id: "prs",
|
|
},
|
|
issues: {
|
|
icon: icons.issues,
|
|
label: "Total Issues",
|
|
value: totalIssues,
|
|
id: "issues",
|
|
},
|
|
contribs: {
|
|
icon: icons.contribs,
|
|
label: "Contributed to",
|
|
value: contributedTo,
|
|
id: "contribs",
|
|
},
|
|
};
|
|
|
|
// filter out hidden stats defined by user & create the text nodes
|
|
const statItems = Object.keys(STATS)
|
|
.filter((key) => !hide.includes(key))
|
|
.map((key, index) =>
|
|
// create the text nodes, and pass index so that we can calculate the line spacing
|
|
createTextNode({
|
|
...STATS[key],
|
|
index,
|
|
showIcons: show_icons,
|
|
shiftValuePos: !include_all_commits,
|
|
})
|
|
);
|
|
|
|
// Calculate the card height depending on how many items there are
|
|
// but if rank circle is visible clamp the minimum height to `150`
|
|
let height = Math.max(
|
|
45 + (statItems.length + 1) * lheight,
|
|
hide_rank ? 0 : 150
|
|
);
|
|
|
|
// Conditionally rendered elements
|
|
const rankCircle = hide_rank
|
|
? ""
|
|
: `<g data-testid="rank-circle"
|
|
transform="translate(400, ${height / 2 - 50})">
|
|
<circle class="rank-circle-rim" cx="-10" cy="8" r="40" />
|
|
<circle class="rank-circle" cx="-10" cy="8" r="40" />
|
|
<g class="rank-text">
|
|
<text
|
|
x="${rank.level.length === 1 ? "-4" : "0"}"
|
|
y="0"
|
|
alignment-baseline="central"
|
|
dominant-baseline="central"
|
|
text-anchor="middle"
|
|
>
|
|
${rank.level}
|
|
</text>
|
|
</g>
|
|
</g>`;
|
|
|
|
// the better user's score the the rank will be closer to zero so
|
|
// subtracting 100 to get the progress in 100%
|
|
const progress = 100 - rank.score;
|
|
const cssStyles = getStyles({
|
|
titleColor,
|
|
textColor,
|
|
iconColor,
|
|
show_icons,
|
|
progress,
|
|
});
|
|
|
|
const apostrophe = ["x", "s"].includes(name.slice(-1)) ? "" : "s";
|
|
const card = new Card({
|
|
title: `${encodeHTML(name)}'${apostrophe} GitHub Stats`,
|
|
width: 495,
|
|
height,
|
|
colors: {
|
|
titleColor,
|
|
textColor,
|
|
iconColor,
|
|
bgColor,
|
|
},
|
|
});
|
|
|
|
card.setHideBorder(hide_border);
|
|
card.setHideTitle(hide_title);
|
|
card.setCSS(cssStyles);
|
|
|
|
return card.render(`
|
|
${rankCircle}
|
|
|
|
<svg x="0" y="0">
|
|
${FlexLayout({
|
|
items: statItems,
|
|
gap: lheight,
|
|
direction: "column",
|
|
}).join("")}
|
|
</svg>
|
|
`);
|
|
};
|
|
|
|
module.exports = renderStatsCard;
|