Skip to content

Commit

Permalink
feat: add contributor in repo-header-labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenth-crew committed Apr 24, 2024
1 parent 4a09f0d commit 88f8db7
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/api/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const metricNameMap = new Map([
['activity', 'activity'],
['openrank', 'openrank'],
['participant', 'participants'],
['contributor', 'new_contributors'],
['forks', 'technical_fork'],
['stars', 'stars'],
['issues_opened', 'issues_new'],
Expand Down Expand Up @@ -33,6 +34,10 @@ export const getParticipant = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'participant');
};

export const getContributor = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'contributor');
};

export const getForks = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'forks');
};
Expand Down
3 changes: 3 additions & 0 deletions src/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@
"header_label_participant": {
"message": "Participants"
},
"header_label_contributor": {
"message": "Contributors"
},
"fork_popup_title": {
"message": "Fork Events"
},
Expand Down
3 changes: 3 additions & 0 deletions src/locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@
"header_label_participant": {
"message": "参与人数"
},
"header_label_contributor": {
"message": "贡献人数"
},
"fork_popup_title": {
"message": "Fork 事件"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import React, { useEffect, useRef } from 'react';
import * as echarts from 'echarts';

import { formatNum, numberWithCommas } from '../../../../helpers/formatter';

const LIGHT_THEME = {
FG_COLOR: '#24292F',
BG_COLOR: '#ffffff',
SPLIT_LINE_COLOR: '#D0D7DE',
BAR_COLOR: '#3E90F1',
LINE_COLOR: '#267FE8',
};

const DARK_THEME = {
FG_COLOR: '#c9d1d9',
BG_COLOR: '#0d1118',
SPLIT_LINE_COLOR: '#30363D',
BAR_COLOR: '#3E90F1',
LINE_COLOR: '#82BBFF',
};

interface ContributorChartProps {
theme: 'light' | 'dark';
width: number;
height: number;
data: [string, number][];
}

const ContributorChart = (props: ContributorChartProps): JSX.Element => {
const { theme, width, height, data } = props;

const divEL = useRef(null);

const TH = theme == 'light' ? LIGHT_THEME : DARK_THEME;

const option: echarts.EChartsOption = {
tooltip: {
trigger: 'axis',
textStyle: {
color: TH.FG_COLOR,
},
backgroundColor: TH.BG_COLOR,
formatter: tooltipFormatter,
},
grid: {
top: '10%',
bottom: '5%',
left: '8%',
right: '5%',
containLabel: true,
},
xAxis: {
type: 'time',
// 30 * 3600 * 24 * 1000 milliseconds
minInterval: 2592000000,
splitLine: {
show: false,
},
axisLabel: {
color: TH.FG_COLOR,
formatter: {
year: '{yearStyle|{yy}}',
month: '{MMM}',
},
rich: {
yearStyle: {
fontWeight: 'bold',
},
},
},
},
yAxis: [
{
type: 'value',
position: 'left',
axisLabel: {
color: TH.FG_COLOR,
formatter: formatNum,
},
splitLine: {
lineStyle: {
color: TH.SPLIT_LINE_COLOR,
},
},
},
],
dataZoom: [
{
type: 'inside',
start: 0,
end: 100,
minValueSpan: 3600 * 24 * 1000 * 180,
},
],
series: [
{
type: 'bar',
data: data,
itemStyle: {
color: '#ff8061',
},
emphasis: {
focus: 'series',
},
yAxisIndex: 0,
},
{
type: 'line',
symbol: 'none',
lineStyle: {
color: '#ff8061',
},
data: data,
emphasis: {
focus: 'series',
},
yAxisIndex: 0,
},
],
animationEasing: 'elasticOut',
animationDelayUpdate: function (idx: any) {
return idx * 5;
},
};

useEffect(() => {
let chartDOM = divEL.current;
const instance = echarts.init(chartDOM as any);

return () => {
instance.dispose();
};
}, []);

useEffect(() => {
let chartDOM = divEL.current;
const instance = echarts.getInstanceByDom(chartDOM as any);
if (instance) {
instance.setOption(option);
}
}, []);

return <div ref={divEL} style={{ width, height }}></div>;
};

const tooltipFormatter = (params: any) => {
const res = `
${params[0].data[0]}<br/>
${params[0].marker}
<span style="font-weight:bold;">
${numberWithCommas(params[0].data[1])}
</span>
`;
return res;
};

export default ContributorChart;
10 changes: 9 additions & 1 deletion src/pages/ContentScripts/features/repo-header-labels/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
hasRepoContainerHeader,
isPublicRepoWithMeta,
} from '../../../../helpers/get-repo-info';
import { getActivity, getOpenrank, getParticipant } from '../../../../api/repo';
import {
getActivity,
getOpenrank,
getParticipant,
getContributor,
} from '../../../../api/repo';
import { RepoMeta, metaStore } from '../../../../api/common';
import View from './view';

Expand All @@ -18,12 +23,14 @@ let repoName: string;
let activity: any;
let openrank: any;
let participant: any;
let contributor: any;
let meta: RepoMeta;

const getData = async () => {
activity = await getActivity(repoName);
openrank = await getOpenrank(repoName);
participant = await getParticipant(repoName);
contributor = await getContributor(repoName);
meta = (await metaStore.get(repoName)) as RepoMeta;
};

Expand All @@ -33,6 +40,7 @@ const renderTo = (container: Container) => {
activity={activity}
openrank={openrank}
participant={participant}
contributor={contributor}
meta={meta}
/>,
container
Expand Down
22 changes: 21 additions & 1 deletion src/pages/ContentScripts/features/repo-header-labels/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import generateDataByMonth from '../../../../helpers/generate-data-by-month';
import ActivityChart from './ActivityChart';
import OpenRankChart from './OpenRankChart';
import ParticipantChart from './ParticipantChart';
import ContributorChart from './ContributorChart';
import { RepoMeta } from '../../../../api/common';

const githubTheme = getGithubTheme();
Expand All @@ -22,13 +23,15 @@ interface Props {
activity: any;
openrank: any;
participant: any;
contributor: any;
meta: RepoMeta;
}

const View = ({
activity,
openrank,
participant,
contributor,
meta,
}: Props): JSX.Element | null => {
const [options, setOptions] = useState<HypercrxOptions>(defaults);
Expand All @@ -43,11 +46,18 @@ const View = ({
})();
}, []);

if (isNull(activity) || isNull(openrank) || isNull(participant)) return null;
if (
isNull(activity) ||
isNull(openrank) ||
isNull(participant) ||
isNull(contributor)
)
return null;

const activityData = generateDataByMonth(activity, meta.updatedAt);
const openrankData = generateDataByMonth(openrank, meta.updatedAt);
const participantData = generateDataByMonth(participant, meta.updatedAt);
const contributorData = generateDataByMonth(contributor, meta.updatedAt);

return (
<div className="d-flex">
Expand Down Expand Up @@ -134,6 +144,7 @@ const View = ({
d="M448 170.666667a192 192 0 0 1 98.56 356.821333A320.085333 320.085333 0 0 1 768 832a42.666667 42.666667 0 0 1-85.333333 0 234.666667 234.666667 0 0 0-469.333334 0 42.666667 42.666667 0 0 1-85.333333 0 320.128 320.128 0 0 1 221.44-304.554667A192 192 0 0 1 448 170.666667z m256 42.666666a149.333333 149.333333 0 0 1 107.434667 253.056A212.992 212.992 0 0 1 917.333333 650.666667a42.666667 42.666667 0 0 1-85.333333 0 128 128 0 0 0-128-128 42.666667 42.666667 0 0 1-42.325333-48.042667 42.666667 42.666667 0 0 1 37.376-47.701333L704 426.666667a64 64 0 0 0 6.144-127.701334L704 298.666667a42.666667 42.666667 0 0 1 0-85.333334z m-256 42.666667a106.666667 106.666667 0 1 0 0 213.333333 106.666667 106.666667 0 0 0 0-213.333333z"
/>
</svg>
{numberWithCommas(contributorData[contributorData.length - 1][1])}/
{numberWithCommas(participantData[participantData.length - 1][1])}
</span>
<ReactTooltip
Expand Down Expand Up @@ -163,6 +174,15 @@ const View = ({
/>
</ReactTooltip>
<ReactTooltip id="participant-tooltip" clickable={true}>
<div className="chart-title">
{getMessageByLocale('header_label_contributor', options.locale)}
</div>
<ContributorChart
theme={githubTheme as 'light' | 'dark'}
width={270}
height={130}
data={contributorData}
/>
<div className="chart-title">
{getMessageByLocale('header_label_participant', options.locale)}
</div>
Expand Down

0 comments on commit 88f8db7

Please sign in to comment.