Skip to content

Commit 9c05e23

Browse files
feat: add repo Activity & OpenRank Trends (hypertrons#965)
1 parent 0656a4c commit 9c05e23

File tree

4 files changed

+88
-15
lines changed

4 files changed

+88
-15
lines changed

src/helpers/get-gitee-repo-info.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export function getRepoName() {
1212
export function getRepoNameByUrl() {
1313
return pageDetect.utils.getRepositoryInfo(window.location)!.nameWithOwner;
1414
}
15-
15+
export async function isRepoRoot() {
16+
return pageDetect.isRepoRoot();
17+
}
1618
export function hasRepoContainerHeader() {
1719
const headerElement = $('#git-project-header-details');
1820
return headerElement && !headerElement.attr('hidden');
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import $ from 'jquery';
3+
import { createRoot } from 'react-dom/client';
4+
import features from '../../../../feature-manager';
5+
import { getRepoName, isPublicRepoWithMeta, isRepoRoot } from '../../../../helpers/get-gitee-repo-info';
6+
import { getActivity, getOpenrank } from '../../../../api/repo';
7+
import { RepoMeta, metaStore } from '../../../../api/common';
8+
import View from './view';
9+
import { getPlatform } from '../../../../helpers/get-platform';
10+
import isGitee from '../../../../helpers/is-gitee';
11+
const featureId = features.getFeatureID(import.meta.url);
12+
let repoName: string;
13+
let activity: any;
14+
let openrank: any;
15+
let meta: RepoMeta;
16+
let platform: string;
17+
const getData = async () => {
18+
activity = await getActivity(platform, repoName);
19+
openrank = await getOpenrank(platform, repoName);
20+
meta = (await metaStore.get(platform, repoName)) as RepoMeta;
21+
};
22+
23+
const renderTo = (container: any) => {
24+
createRoot(container).render(<View repoName={repoName} activity={activity} openrank={openrank} meta={meta} />);
25+
};
26+
27+
const init = async (): Promise<void> => {
28+
platform = getPlatform();
29+
repoName = getRepoName();
30+
await getData();
31+
32+
// create container
33+
const newBorderGridRow = document.createElement('div');
34+
newBorderGridRow.id = featureId;
35+
newBorderGridRow.className = 'side-item trend';
36+
newBorderGridRow.style.marginBottom = '0';
37+
newBorderGridRow.style.fontWeight = '600';
38+
newBorderGridRow.style.fontSize = '16px';
39+
renderTo(newBorderGridRow);
40+
const borderGridRows = $('div.side-item.contrib');
41+
borderGridRows.after(newBorderGridRow);
42+
};
43+
44+
const restore = async () => {
45+
// Clicking another repo link in one repo will trigger a turbo:visit,
46+
// so in a restoration visit we should be careful of the current repo.
47+
if (repoName !== getRepoName()) {
48+
repoName = getRepoName();
49+
await getData();
50+
}
51+
// rerender the chart or it will be empty
52+
renderTo($(`#${featureId}`).children('.BorderGrid-cell')[0]);
53+
};
54+
55+
features.add(featureId, {
56+
asLongAs: [isGitee, isPublicRepoWithMeta, isRepoRoot],
57+
awaitDomReady: true,
58+
init,
59+
restore,
60+
});

src/pages/ContentScripts/features/repo-activity-openrank-trends/view.tsx

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import Bars from '../../../../components/Bars';
66
import { RepoMeta } from '../../../../api/common';
77
import { useTranslation } from 'react-i18next';
88
import '../../../../helpers/i18n';
9-
const githubTheme = getGithubTheme();
9+
import isGithub from '../../../../helpers/is-github';
10+
const theme = isGithub() ? getGithubTheme() : 'light';
1011

1112
const generateBarsData = (activity: any, openrank: any, updatedAt: number) => {
1213
return {
@@ -47,21 +48,30 @@ const View = ({ repoName, activity, openrank, meta }: Props): JSX.Element | null
4748
window.open(`/${repoName}/issues?q=updated:${year}-${month} sort:updated-asc`);
4849
}
4950
};
50-
51-
return (
51+
const BarsComponent = (
52+
<Bars
53+
theme={theme as 'light' | 'dark'}
54+
height={350}
55+
legend1={t('component_repoActORTrend_legend1')}
56+
legend2={t('component_repoActORTrend_legend2')}
57+
yName1={t('component_repoActORTrend_yName1')}
58+
yName2={t('component_repoActORTrend_yName2')}
59+
data1={barsData.data1}
60+
data2={barsData.data2}
61+
onClick={onClick}
62+
/>
63+
);
64+
return isGithub() ? (
5265
<div>
5366
<h2 className="h4 mb-3">{t('component_repoActORTrend_title')}</h2>
54-
<Bars
55-
theme={githubTheme as 'light' | 'dark'}
56-
height={350}
57-
legend1={t('component_repoActORTrend_legend1')}
58-
legend2={t('component_repoActORTrend_legend2')}
59-
yName1={t('component_repoActORTrend_yName1')}
60-
yName2={t('component_repoActORTrend_yName2')}
61-
data1={barsData.data1}
62-
data2={barsData.data2}
63-
onClick={onClick}
64-
/>
67+
{BarsComponent}
68+
</div>
69+
) : (
70+
<div>
71+
<div className="header">{t('component_repoActORTrend_title')}</div>
72+
<div className="content" id="repo-activity-racing-bar">
73+
{BarsComponent}
74+
</div>
6575
</div>
6676
);
6777
};

src/pages/ContentScripts/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import './index.scss';
22

33
import './features/repo-activity-openrank-trends';
4+
import './features/repo-activity-openrank-trends/gitee-index.tsx';
45
import './features/developer-activity-openrank-trends';
56
import './features/developer-activity-openrank-trends/gitee-index';
67
import './features/repo-header-labels';

0 commit comments

Comments
 (0)