Fixed resizing of stats card when all metrics except rank are hidden (#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 <rick.staa@outlook.com>
This commit is contained in:
iwbc-mzk 2023-06-22 19:07:52 +09:00 committed by GitHub
parent f282ce4d64
commit a48d653fa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 11 deletions

View File

@ -3,6 +3,7 @@ import { Card } from "../common/Card.js";
import { I18n } from "../common/I18n.js"; import { I18n } from "../common/I18n.js";
import { icons, rankIcon } from "../common/icons.js"; import { icons, rankIcon } from "../common/icons.js";
import { import {
CustomError,
clampValue, clampValue,
flexLayout, flexLayout,
getCardColors, getCardColors,
@ -16,6 +17,8 @@ const CARD_MIN_WIDTH = 287;
const CARD_DEFAULT_WIDTH = 287; const CARD_DEFAULT_WIDTH = 287;
const RANK_CARD_MIN_WIDTH = 420; const RANK_CARD_MIN_WIDTH = 420;
const RANK_CARD_DEFAULT_WIDTH = 450; const RANK_CARD_DEFAULT_WIDTH = 450;
const RANK_ONLY_CARD_MIN_WIDTH = 290;
const RANK_ONLY_CARD_DEFAULT_WIDTH = 290;
/** /**
* Create a stats card text item. * Create a stats card text item.
@ -234,11 +237,18 @@ const renderStatsCard = (stats = {}, options = {}) => {
}), }),
); );
if (statItems.length === 0 && hide_rank) {
throw new CustomError(
"Could not render stats card.",
"Either stats or rank are required.",
);
}
// Calculate the card height depending on how many items there are // Calculate the card height depending on how many items there are
// but if rank circle is visible clamp the minimum height to `150` // but if rank circle is visible clamp the minimum height to `150`
let height = Math.max( let height = Math.max(
45 + (statItems.length + 1) * lheight, 45 + (statItems.length + 1) * lheight,
hide_rank ? 0 : 150, hide_rank ? 0 : statItems.length ? 150 : 180,
); );
// the lower the user's percentile the better // the lower the user's percentile the better
@ -253,7 +263,13 @@ const renderStatsCard = (stats = {}, options = {}) => {
}); });
const calculateTextWidth = () => { const calculateTextWidth = () => {
return measureText(custom_title ? custom_title : i18n.t("statcard.title")); return measureText(
custom_title
? custom_title
: statItems.length
? i18n.t("statcard.title")
: i18n.t("statcard.ranktitle"),
);
}; };
/* /*
@ -261,7 +277,7 @@ const renderStatsCard = (stats = {}, options = {}) => {
When hide_rank=false, the minimum card_width is 340 px + the icon width (if show_icons=true). When hide_rank=false, the minimum card_width is 340 px + the icon width (if show_icons=true).
Numbers are picked by looking at existing dimensions on production. Numbers are picked by looking at existing dimensions on production.
*/ */
const iconWidth = show_icons ? 16 + /* padding */ 1 : 0; const iconWidth = show_icons && statItems.length ? 16 + /* padding */ 1 : 0;
const minCardWidth = const minCardWidth =
(hide_rank (hide_rank
? clampValue( ? clampValue(
@ -269,9 +285,15 @@ const renderStatsCard = (stats = {}, options = {}) => {
CARD_MIN_WIDTH, CARD_MIN_WIDTH,
Infinity, Infinity,
) )
: RANK_CARD_MIN_WIDTH) + iconWidth; : statItems.length
? RANK_CARD_MIN_WIDTH
: RANK_ONLY_CARD_MIN_WIDTH) + iconWidth;
const defaultCardWidth = const defaultCardWidth =
(hide_rank ? CARD_DEFAULT_WIDTH : RANK_CARD_DEFAULT_WIDTH) + iconWidth; (hide_rank
? CARD_DEFAULT_WIDTH
: statItems.length
? RANK_CARD_DEFAULT_WIDTH
: RANK_ONLY_CARD_DEFAULT_WIDTH) + iconWidth;
let width = isNaN(card_width) ? defaultCardWidth : card_width; let width = isNaN(card_width) ? defaultCardWidth : card_width;
if (width < minCardWidth) { if (width < minCardWidth) {
width = minCardWidth; width = minCardWidth;
@ -279,7 +301,9 @@ const renderStatsCard = (stats = {}, options = {}) => {
const card = new Card({ const card = new Card({
customTitle: custom_title, customTitle: custom_title,
defaultTitle: i18n.t("statcard.title"), defaultTitle: statItems.length
? i18n.t("statcard.title")
: i18n.t("statcard.ranktitle"),
width, width,
height, height,
border_radius, border_radius,
@ -309,12 +333,16 @@ const renderStatsCard = (stats = {}, options = {}) => {
* @returns {number} - Rank circle translation value. * @returns {number} - Rank circle translation value.
*/ */
const calculateRankXTranslation = () => { const calculateRankXTranslation = () => {
const minXTranslation = RANK_CARD_MIN_WIDTH + iconWidth - 70; if (statItems.length) {
if (width > RANK_CARD_DEFAULT_WIDTH) { const minXTranslation = RANK_CARD_MIN_WIDTH + iconWidth - 70;
const xMaxExpansion = minXTranslation + (450 - minCardWidth) / 2; if (width > RANK_CARD_DEFAULT_WIDTH) {
return xMaxExpansion + width - RANK_CARD_DEFAULT_WIDTH; const xMaxExpansion = minXTranslation + (450 - minCardWidth) / 2;
return xMaxExpansion + width - RANK_CARD_DEFAULT_WIDTH;
} else {
return minXTranslation + (width - minCardWidth) / 2;
}
} else { } else {
return minXTranslation + (width - minCardWidth) / 2; return width / 2 + 20 - 10;
} }
}; };

View File

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