Skip to content

Commit 1518f4e

Browse files
Fix typescript errors in Vue files, fix regression in "Recent Commits" chart (#32649)
- Fix all typescript errors in `.vue` files - Fix regression from #32329 where "Recent Commits" chart would not render. --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 96d3a03 commit 1518f4e

File tree

6 files changed

+59
-37
lines changed

6 files changed

+59
-37
lines changed

Diff for: web_src/js/components/DashboardRepoList.vue

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ import {fomanticQuery} from '../modules/fomantic/base.ts';
66
77
const {appSubUrl, assetUrlPrefix, pageData} = window.config;
88
9+
type CommitStatus = 'pending' | 'success' | 'error' | 'failure' | 'warning';
10+
11+
type CommitStatusMap = {
12+
[status in CommitStatus]: {
13+
name: string,
14+
color: string,
15+
};
16+
};
17+
918
// make sure this matches templates/repo/commit_status.tmpl
10-
const commitStatus = {
19+
const commitStatus: CommitStatusMap = {
1120
pending: {name: 'octicon-dot-fill', color: 'yellow'},
1221
success: {name: 'octicon-check', color: 'green'},
1322
error: {name: 'gitea-exclamation', color: 'red'},
@@ -281,18 +290,18 @@ const sfc = {
281290
return 'octicon-repo';
282291
},
283292
284-
statusIcon(status) {
293+
statusIcon(status: CommitStatus) {
285294
return commitStatus[status].name;
286295
},
287296
288-
statusColor(status) {
297+
statusColor(status: CommitStatus) {
289298
return commitStatus[status].color;
290299
},
291300
292301
reposFilterKeyControl(e) {
293302
switch (e.key) {
294303
case 'Enter':
295-
document.querySelector('.repo-owner-name-list li.active a')?.click();
304+
document.querySelector<HTMLAnchorElement>('.repo-owner-name-list li.active a')?.click();
296305
break;
297306
case 'ArrowUp':
298307
if (this.activeIndex > 0) {

Diff for: web_src/js/components/DiffCommitSelector.vue

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default {
1414
issueLink: el.getAttribute('data-issuelink'),
1515
locale: {
1616
filter_changes_by_commit: el.getAttribute('data-filter_changes_by_commit'),
17-
},
17+
} as Record<string, string>,
1818
commits: [],
1919
hoverActivated: false,
2020
lastReviewCommitSha: null,
@@ -41,16 +41,16 @@ export default {
4141
this.$el.removeEventListener('keyup', this.onKeyUp);
4242
},
4343
methods: {
44-
onBodyClick(event) {
44+
onBodyClick(event: MouseEvent) {
4545
// close this menu on click outside of this element when the dropdown is currently visible opened
4646
if (this.$el.contains(event.target)) return;
4747
if (this.menuVisible) {
4848
this.toggleMenu();
4949
}
5050
},
51-
onKeyDown(event) {
51+
onKeyDown(event: KeyboardEvent) {
5252
if (!this.menuVisible) return;
53-
const item = document.activeElement;
53+
const item = document.activeElement as HTMLElement;
5454
if (!this.$el.contains(item)) return;
5555
switch (event.key) {
5656
case 'ArrowDown': // select next element
@@ -73,7 +73,7 @@ export default {
7373
if (commitIdx) this.highlight(this.commits[commitIdx]);
7474
}
7575
},
76-
onKeyUp(event) {
76+
onKeyUp(event: KeyboardEvent) {
7777
if (!this.menuVisible) return;
7878
const item = document.activeElement;
7979
if (!this.$el.contains(item)) return;
@@ -95,7 +95,7 @@ export default {
9595
}
9696
},
9797
/** Focus given element */
98-
focusElem(elem, prevElem) {
98+
focusElem(elem: HTMLElement, prevElem: HTMLElement) {
9999
if (elem) {
100100
elem.tabIndex = 0;
101101
if (prevElem) prevElem.tabIndex = -1;
@@ -149,7 +149,7 @@ export default {
149149
window.location.assign(`${this.issueLink}/files/${this.lastReviewCommitSha}..${this.commits.at(-1).id}${this.queryParams}`);
150150
},
151151
/** Clicking on a single commit opens this specific commit */
152-
commitClicked(commitId, newWindow = false) {
152+
commitClicked(commitId: string, newWindow = false) {
153153
const url = `${this.issueLink}/commits/${commitId}${this.queryParams}`;
154154
if (newWindow) {
155155
window.open(url);

Diff for: web_src/js/components/RepoCodeFrequency.vue

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
PointElement,
99
LineElement,
1010
Filler,
11+
type ChartOptions,
12+
type ChartData,
1113
} from 'chart.js';
1214
import {GET} from '../modules/fetch.ts';
1315
import {Line as ChartLine} from 'vue-chartjs';
@@ -16,6 +18,7 @@ import {
1618
firstStartDateAfterDate,
1719
fillEmptyStartDaysWithZeroes,
1820
type DayData,
21+
type DayDataObject,
1922
} from '../utils/time.ts';
2023
import {chartJsColors} from '../utils/color.ts';
2124
import {sleep} from '../utils.ts';
@@ -64,12 +67,12 @@ async function fetchGraphData() {
6467
}
6568
} while (response.status === 202);
6669
if (response.ok) {
67-
data.value = await response.json();
68-
const weekValues = Object.values(data.value);
70+
const dayDataObject: DayDataObject = await response.json();
71+
const weekValues = Object.values(dayDataObject);
6972
const start = weekValues[0].week;
7073
const end = firstStartDateAfterDate(new Date());
7174
const startDays = startDaysBetween(start, end);
72-
data.value = fillEmptyStartDaysWithZeroes(startDays, data.value);
75+
data.value = fillEmptyStartDaysWithZeroes(startDays, dayDataObject);
7376
errorText.value = '';
7477
} else {
7578
errorText.value = response.statusText;
@@ -81,7 +84,7 @@ async function fetchGraphData() {
8184
}
8285
}
8386
84-
function toGraphData(data) {
87+
function toGraphData(data: Array<Record<string, any>>): ChartData<'line'> {
8588
return {
8689
datasets: [
8790
{
@@ -108,10 +111,9 @@ function toGraphData(data) {
108111
};
109112
}
110113
111-
const options = {
114+
const options: ChartOptions<'line'> = {
112115
responsive: true,
113116
maintainAspectRatio: false,
114-
animation: true,
115117
plugins: {
116118
legend: {
117119
display: true,

Diff for: web_src/js/components/RepoContributors.vue

+18-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
PointElement,
1010
LineElement,
1111
Filler,
12+
type ChartOptions,
13+
type ChartData,
14+
type Plugin,
1215
} from 'chart.js';
1316
import {GET} from '../modules/fetch.ts';
1417
import zoomPlugin from 'chartjs-plugin-zoom';
@@ -22,8 +25,9 @@ import {chartJsColors} from '../utils/color.ts';
2225
import {sleep} from '../utils.ts';
2326
import 'chartjs-adapter-dayjs-4/dist/chartjs-adapter-dayjs-4.esm';
2427
import {fomanticQuery} from '../modules/fomantic/base.ts';
28+
import type {Entries} from 'type-fest';
2529
26-
const customEventListener = {
30+
const customEventListener: Plugin = {
2731
id: 'customEventListener',
2832
afterEvent: (chart, args, opts) => {
2933
// event will be replayed from chart.update when reset zoom,
@@ -65,10 +69,10 @@ export default {
6569
data: () => ({
6670
isLoading: false,
6771
errorText: '',
68-
totalStats: {},
69-
sortedContributors: {},
72+
totalStats: {} as Record<string, any>,
73+
sortedContributors: {} as Record<string, any>,
7074
type: 'commits',
71-
contributorsStats: [],
75+
contributorsStats: {} as Record<string, any>,
7276
xAxisStart: null,
7377
xAxisEnd: null,
7478
xAxisMin: null,
@@ -99,7 +103,7 @@ export default {
99103
async fetchGraphData() {
100104
this.isLoading = true;
101105
try {
102-
let response;
106+
let response: Response;
103107
do {
104108
response = await GET(`${this.repoLink}/activity/contributors/data`);
105109
if (response.status === 202) {
@@ -112,15 +116,15 @@ export default {
112116
// below line might be deleted if we are sure go produces map always sorted by keys
113117
total.weeks = Object.fromEntries(Object.entries(total.weeks).sort());
114118
115-
const weekValues = Object.values(total.weeks);
119+
const weekValues = Object.values(total.weeks) as any;
116120
this.xAxisStart = weekValues[0].week;
117121
this.xAxisEnd = firstStartDateAfterDate(new Date());
118122
const startDays = startDaysBetween(this.xAxisStart, this.xAxisEnd);
119123
total.weeks = fillEmptyStartDaysWithZeroes(startDays, total.weeks);
120124
this.xAxisMin = this.xAxisStart;
121125
this.xAxisMax = this.xAxisEnd;
122126
this.contributorsStats = {};
123-
for (const [email, user] of Object.entries(rest)) {
127+
for (const [email, user] of Object.entries(rest) as Entries<Record<string, Record<string, any>>>) {
124128
user.weeks = fillEmptyStartDaysWithZeroes(startDays, user.weeks);
125129
this.contributorsStats[email] = user;
126130
}
@@ -146,7 +150,7 @@ export default {
146150
user.total_additions = 0;
147151
user.total_deletions = 0;
148152
user.max_contribution_type = 0;
149-
const filteredWeeks = user.weeks.filter((week) => {
153+
const filteredWeeks = user.weeks.filter((week: Record<string, number>) => {
150154
const oneWeek = 7 * 24 * 60 * 60 * 1000;
151155
if (week.week >= this.xAxisMin - oneWeek && week.week <= this.xAxisMax + oneWeek) {
152156
user.total_commits += week.commits;
@@ -195,7 +199,7 @@ export default {
195199
return (1 - (coefficient % 1)) * 10 ** exp + maxValue;
196200
},
197201
198-
toGraphData(data) {
202+
toGraphData(data: Array<Record<string, any>>): ChartData<'line'> {
199203
return {
200204
datasets: [
201205
{
@@ -211,9 +215,9 @@ export default {
211215
};
212216
},
213217
214-
updateOtherCharts(event, reset) {
215-
const minVal = event.chart.options.scales.x.min;
216-
const maxVal = event.chart.options.scales.x.max;
218+
updateOtherCharts({chart}: {chart: Chart}, reset?: boolean = false) {
219+
const minVal = chart.options.scales.x.min;
220+
const maxVal = chart.options.scales.x.max;
217221
if (reset) {
218222
this.xAxisMin = this.xAxisStart;
219223
this.xAxisMax = this.xAxisEnd;
@@ -225,7 +229,7 @@ export default {
225229
}
226230
},
227231
228-
getOptions(type) {
232+
getOptions(type: string): ChartOptions<'line'> {
229233
return {
230234
responsive: true,
231235
maintainAspectRatio: false,
@@ -238,6 +242,7 @@ export default {
238242
position: 'top',
239243
align: 'center',
240244
},
245+
// @ts-expect-error: bug in chart.js types
241246
customEventListener: {
242247
chartType: type,
243248
instance: this,

Diff for: web_src/js/components/RepoRecentCommits.vue

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
LinearScale,
88
TimeScale,
99
type ChartOptions,
10+
type ChartData,
1011
} from 'chart.js';
1112
import {GET} from '../modules/fetch.ts';
1213
import {Bar} from 'vue-chartjs';
@@ -15,6 +16,7 @@ import {
1516
firstStartDateAfterDate,
1617
fillEmptyStartDaysWithZeroes,
1718
type DayData,
19+
type DayDataObject,
1820
} from '../utils/time.ts';
1921
import {chartJsColors} from '../utils/color.ts';
2022
import {sleep} from '../utils.ts';
@@ -61,11 +63,11 @@ async function fetchGraphData() {
6163
}
6264
} while (response.status === 202);
6365
if (response.ok) {
64-
const data = await response.json();
65-
const start = Object.values(data)[0].week;
66+
const dayDataObj: DayDataObject = await response.json();
67+
const start = Object.values(dayDataObj)[0].week;
6668
const end = firstStartDateAfterDate(new Date());
6769
const startDays = startDaysBetween(start, end);
68-
data.value = fillEmptyStartDaysWithZeroes(startDays, data).slice(-52);
70+
data.value = fillEmptyStartDaysWithZeroes(startDays, dayDataObj).slice(-52);
6971
errorText.value = '';
7072
} else {
7173
errorText.value = response.statusText;
@@ -77,10 +79,11 @@ async function fetchGraphData() {
7779
}
7880
}
7981
80-
function toGraphData(data) {
82+
function toGraphData(data: DayData[]): ChartData<'bar'> {
8183
return {
8284
datasets: [
8385
{
86+
// @ts-expect-error -- bar chart expects one-dimensional data, but apparently x/y still works
8487
data: data.map((i) => ({x: i.week, y: i.commits})),
8588
label: 'Commits',
8689
backgroundColor: chartJsColors['commits'],
@@ -91,10 +94,9 @@ function toGraphData(data) {
9194
};
9295
}
9396
94-
const options = {
97+
const options: ChartOptions<'bar'> = {
9598
responsive: true,
9699
maintainAspectRatio: false,
97-
animation: true,
98100
scales: {
99101
x: {
100102
type: 'time',

Diff for: web_src/js/utils/time.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export type DayData = {
4949
commits: number,
5050
}
5151

52-
export function fillEmptyStartDaysWithZeroes(startDays: number[], data: DayData[]): DayData[] {
52+
export type DayDataObject = {
53+
[timestamp: string]: DayData,
54+
}
55+
56+
export function fillEmptyStartDaysWithZeroes(startDays: number[], data: DayDataObject): DayData[] {
5357
const result = {};
5458

5559
for (const startDay of startDays) {

0 commit comments

Comments
 (0)