From 06d868dcfa4c2697f9d5782fd23e857dc375deb5 Mon Sep 17 00:00:00 2001 From: vikingowl Date: Mon, 7 Apr 2025 18:58:29 +0200 Subject: [PATCH] Refactor BuildStats for dynamic rendering and reuse Replaced hardcoded items with dynamically generated lists for stats and LTO sections. Introduced reusable styles and refactored the `getStats` function to use async/await, ensuring better readability and maintainability. Enhanced code structure for easier updates and scalability. --- frontend/src/components/BuildStats.vue | 95 +++++++++++++++++++------- 1 file changed, 72 insertions(+), 23 deletions(-) diff --git a/frontend/src/components/BuildStats.vue b/frontend/src/components/BuildStats.vue index a738f08..17fc0d0 100644 --- a/frontend/src/components/BuildStats.vue +++ b/frontend/src/components/BuildStats.vue @@ -1,17 +1,26 @@ @@ -20,6 +29,20 @@ import { onMounted, ref } from 'vue' import { type Stats } from '@/types/Stats' +// Define reusable styles and colors +const sheetStyles = { gap: '50px' } +const listStyles = { borderRadius: '5px' } +const colors = { + latest: '#069b35', + queued: '#b97808', + skipped: '#878787', + failed: '#b30303', + enabled: '#069b35', + disabled: '#b30303', + unknown: '#878787' +} + +// Reactive stats object const stats = ref({ latest: 0, queued: 0, @@ -32,21 +55,47 @@ const stats = ref({ } }) -const getStats = () => { - fetch('https://api.alhp.dev/stats') - .then((response) => { - if (!response.ok) throw new Error(response.statusText) - return response.json() - }) - .then((statistics) => { - stats.value = statistics - }) - .catch((error) => { - console.error(error) - }) - .finally(() => {}) +// Map stats and LTO data for simpler rendering in the template +const statsList = ref([ + { label: 'latest', value: stats.value.latest, color: colors.latest }, + { label: 'queued', value: stats.value.queued, color: colors.queued }, + { label: 'skipped', value: stats.value.skipped, color: colors.skipped }, + { label: 'failed', value: stats.value.failed, color: colors.failed } +]) + +const ltoList = ref([ + { label: 'enabled', value: stats.value.lto.enabled, color: colors.enabled }, + { label: 'disabled', value: stats.value.lto.disabled, color: colors.disabled }, + { label: 'unknown', value: stats.value.lto.unknown, color: colors.unknown } +]) + +// Function to fetch stats data (refactored with async/await) +const getStats = async (): Promise => { + try { + const response = await fetch('https://api.alhp.dev/stats') + if (!response.ok) throw new Error(response.statusText) + + const statistics = await response.json() + stats.value = statistics + + // Update lists with fresh data + statsList.value = [ + { label: 'latest', value: stats.value.latest, color: colors.latest }, + { label: 'queued', value: stats.value.queued, color: colors.queued }, + { label: 'skipped', value: stats.value.skipped, color: colors.skipped }, + { label: 'failed', value: stats.value.failed, color: colors.failed } + ] + ltoList.value = [ + { label: 'enabled', value: stats.value.lto.enabled, color: colors.enabled }, + { label: 'disabled', value: stats.value.lto.disabled, color: colors.disabled }, + { label: 'unknown', value: stats.value.lto.unknown, color: colors.unknown } + ] + } catch (error) { + console.error('Failed to fetch stats:', error) + } } +// Fetch stats on mount onMounted(() => { getStats() })