Skip to content

Commit a48d653

Browse files
iwbc-mzkrickstaa
andauthored
Fixed resizing of stats card when all metrics except rank are hidden (anuraghazra#2868)
* fixed card resizing in case of rank only * fixed to display error when both stats and rank are hidden * fix: fix visual alignment --------- Co-authored-by: rickstaa <[email protected]>
1 parent f282ce4 commit a48d653

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

Diff for: src/cards/stats-card.js

+39-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Card } from "../common/Card.js";
33
import { I18n } from "../common/I18n.js";
44
import { icons, rankIcon } from "../common/icons.js";
55
import {
6+
CustomError,
67
clampValue,
78
flexLayout,
89
getCardColors,
@@ -16,6 +17,8 @@ const CARD_MIN_WIDTH = 287;
1617
const CARD_DEFAULT_WIDTH = 287;
1718
const RANK_CARD_MIN_WIDTH = 420;
1819
const RANK_CARD_DEFAULT_WIDTH = 450;
20+
const RANK_ONLY_CARD_MIN_WIDTH = 290;
21+
const RANK_ONLY_CARD_DEFAULT_WIDTH = 290;
1922

2023
/**
2124
* Create a stats card text item.
@@ -234,11 +237,18 @@ const renderStatsCard = (stats = {}, options = {}) => {
234237
}),
235238
);
236239

240+
if (statItems.length === 0 && hide_rank) {
241+
throw new CustomError(
242+
"Could not render stats card.",
243+
"Either stats or rank are required.",
244+
);
245+
}
246+
237247
// Calculate the card height depending on how many items there are
238248
// but if rank circle is visible clamp the minimum height to `150`
239249
let height = Math.max(
240250
45 + (statItems.length + 1) * lheight,
241-
hide_rank ? 0 : 150,
251+
hide_rank ? 0 : statItems.length ? 150 : 180,
242252
);
243253

244254
// the lower the user's percentile the better
@@ -253,33 +263,47 @@ const renderStatsCard = (stats = {}, options = {}) => {
253263
});
254264

255265
const calculateTextWidth = () => {
256-
return measureText(custom_title ? custom_title : i18n.t("statcard.title"));
266+
return measureText(
267+
custom_title
268+
? custom_title
269+
: statItems.length
270+
? i18n.t("statcard.title")
271+
: i18n.t("statcard.ranktitle"),
272+
);
257273
};
258274

259275
/*
260276
When hide_rank=true, the minimum card width is 270 px + the title length and padding.
261277
When hide_rank=false, the minimum card_width is 340 px + the icon width (if show_icons=true).
262278
Numbers are picked by looking at existing dimensions on production.
263279
*/
264-
const iconWidth = show_icons ? 16 + /* padding */ 1 : 0;
280+
const iconWidth = show_icons && statItems.length ? 16 + /* padding */ 1 : 0;
265281
const minCardWidth =
266282
(hide_rank
267283
? clampValue(
268284
50 /* padding */ + calculateTextWidth() * 2,
269285
CARD_MIN_WIDTH,
270286
Infinity,
271287
)
272-
: RANK_CARD_MIN_WIDTH) + iconWidth;
288+
: statItems.length
289+
? RANK_CARD_MIN_WIDTH
290+
: RANK_ONLY_CARD_MIN_WIDTH) + iconWidth;
273291
const defaultCardWidth =
274-
(hide_rank ? CARD_DEFAULT_WIDTH : RANK_CARD_DEFAULT_WIDTH) + iconWidth;
292+
(hide_rank
293+
? CARD_DEFAULT_WIDTH
294+
: statItems.length
295+
? RANK_CARD_DEFAULT_WIDTH
296+
: RANK_ONLY_CARD_DEFAULT_WIDTH) + iconWidth;
275297
let width = isNaN(card_width) ? defaultCardWidth : card_width;
276298
if (width < minCardWidth) {
277299
width = minCardWidth;
278300
}
279301

280302
const card = new Card({
281303
customTitle: custom_title,
282-
defaultTitle: i18n.t("statcard.title"),
304+
defaultTitle: statItems.length
305+
? i18n.t("statcard.title")
306+
: i18n.t("statcard.ranktitle"),
283307
width,
284308
height,
285309
border_radius,
@@ -309,12 +333,16 @@ const renderStatsCard = (stats = {}, options = {}) => {
309333
* @returns {number} - Rank circle translation value.
310334
*/
311335
const calculateRankXTranslation = () => {
312-
const minXTranslation = RANK_CARD_MIN_WIDTH + iconWidth - 70;
313-
if (width > RANK_CARD_DEFAULT_WIDTH) {
314-
const xMaxExpansion = minXTranslation + (450 - minCardWidth) / 2;
315-
return xMaxExpansion + width - RANK_CARD_DEFAULT_WIDTH;
336+
if (statItems.length) {
337+
const minXTranslation = RANK_CARD_MIN_WIDTH + iconWidth - 70;
338+
if (width > RANK_CARD_DEFAULT_WIDTH) {
339+
const xMaxExpansion = minXTranslation + (450 - minCardWidth) / 2;
340+
return xMaxExpansion + width - RANK_CARD_DEFAULT_WIDTH;
341+
} else {
342+
return minXTranslation + (width - minCardWidth) / 2;
343+
}
316344
} else {
317-
return minXTranslation + (width - minCardWidth) / 2;
345+
return width / 2 + 20 - 10;
318346
}
319347
};
320348

Diff for: src/translations.js

+31
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,37 @@ const statCardLocales = ({ name, apostrophe }) => {
4343
vi: `Thống Kê GitHub ${encodedName}`,
4444
se: `GitHubstatistik för ${encodedName}`,
4545
},
46+
"statcard.ranktitle": {
47+
ar: `${encodedName} إحصائيات غيت هاب`,
48+
cn: `${encodedName} 的 GitHub 统计数据`,
49+
"zh-tw": `${encodedName} 的 GitHub 統計數據`,
50+
cs: `GitHub statistiky uživatele ${encodedName}`,
51+
de: `${encodedName + apostrophe} GitHub-Statistiken`,
52+
en: `${encodedName}'${apostrophe} GitHub Rank`,
53+
bn: `${encodedName} এর GitHub পরিসংখ্যান`,
54+
es: `Estadísticas de GitHub de ${encodedName}`,
55+
fr: `Statistiques GitHub de ${encodedName}`,
56+
hu: `${encodedName} GitHub statisztika`,
57+
it: `Statistiche GitHub di ${encodedName}`,
58+
ja: `${encodedName} の GitHub ランク`,
59+
kr: `${encodedName}의 GitHub 통계`,
60+
nl: `${encodedName}'${apostrophe} GitHub-statistieken`,
61+
"pt-pt": `Estatísticas do GitHub de ${encodedName}`,
62+
"pt-br": `Estatísticas do GitHub de ${encodedName}`,
63+
np: `${encodedName}'${apostrophe} गिटहब तथ्याङ्क`,
64+
el: `Στατιστικά GitHub του ${encodedName}`,
65+
ru: `Статистика GitHub пользователя ${encodedName}`,
66+
"uk-ua": `Статистика GitHub користувача ${encodedName}`,
67+
id: `Statistik GitHub ${encodedName}`,
68+
ml: `${encodedName}'${apostrophe} ഗിറ്റ്ഹബ് സ്ഥിതിവിവരക്കണക്കുകൾ`,
69+
my: `Statistik GitHub ${encodedName}`,
70+
sk: `GitHub štatistiky používateľa ${encodedName}`,
71+
tr: `${encodedName} Hesabının GitHub Yıldızları`,
72+
pl: `Statystyki GitHub użytkownika ${encodedName}`,
73+
uz: `${encodedName}ning GitHub'dagi statistikasi`,
74+
vi: `Thống Kê GitHub ${encodedName}`,
75+
se: `GitHubstatistik för ${encodedName}`,
76+
},
4677
"statcard.totalstars": {
4778
ar: "مجموع النجوم",
4879
cn: "获标星数(star)",

0 commit comments

Comments
 (0)