this repo has no description madoka.systmes

cache status and lint

+38 -36
+38 -36
static/js/status.js
··· 1 - (function() { 2 - const BADGE_URL = 'https://status.madoka.systems/api/badge/12/status'; 3 - const REFRESH_INTERVAL = 60000; // 1 minute 1 + /* global localStorage */ 2 + (function () { 3 + 'use strict' 4 4 5 - async function fetchStatus() { 6 - try { 7 - const response = await fetch(BADGE_URL); 8 - if (!response.ok) throw new Error('badge request failed'); 5 + const BADGE_URL = 'https://status.madoka.systems/api/badge/12/status' 6 + const REFRESH_INTERVAL = 60000 7 + const CACHE_KEY = 'site-status' 8 + const STATUS_CLASSES = ['up', 'down', 'pending', 'maintenance', 'unknown'] 9 9 10 - const svg = await response.text(); 10 + function parseStatus (svg) { 11 + const lower = svg.toLowerCase() 12 + if (lower.includes('>up<') || lower.includes('up</text>')) return 'up' 13 + if (lower.includes('>down<') || lower.includes('down</text>')) return 'down' 14 + if (lower.includes('>pending<') || lower.includes('pending</text>')) return 'pending' 15 + if (lower.includes('>maintenance<') || lower.includes('maintenance</text>')) return 'maintenance' 16 + return 'unknown' 17 + } 11 18 12 - // parse status from badge svg text content 13 - if (svg.includes('>Up<') || svg.includes('up</text>')) { 14 - updateIndicator('up'); 15 - } else if (svg.includes('>Down<') || svg.includes('down</text>')) { 16 - updateIndicator('down'); 17 - } else if (svg.includes('>Pending<') || svg.includes('pending</text>')) { 18 - updateIndicator('pending'); 19 - } else if (svg.includes('>Maintenance<') || svg.includes('maintenance</text>')) { 20 - updateIndicator('maintenance'); 21 - } else { 22 - updateIndicator('unknown'); 23 - } 19 + function updateIndicator (status) { 20 + const indicator = document.getElementById('status-indicator') 21 + if (!indicator) return 24 22 25 - } catch (error) { 26 - console.error('failed to fetch uptime status:', error); 27 - updateIndicator('unknown'); 28 - } 23 + indicator.classList.remove(...STATUS_CLASSES) 24 + indicator.classList.add(status) 29 25 } 30 26 31 - function updateIndicator(status) { 32 - const indicator = document.getElementById('status-indicator'); 33 - if (!indicator) return; 27 + async function fetchStatus () { 28 + try { 29 + const response = await fetch(BADGE_URL) 30 + if (!response.ok) throw new Error('badge request failed') 31 + 32 + const svg = await response.text() 33 + const status = parseStatus(svg) 34 34 35 - // remove all status classes 36 - indicator.classList.remove('up', 'down', 'pending', 'maintenance', 'unknown'); 37 - // add current status class 38 - indicator.classList.add(status); 35 + updateIndicator(status) 36 + localStorage.setItem(CACHE_KEY, status) 37 + } catch (error) { 38 + console.error('failed to fetch uptime status:', error) 39 + updateIndicator('unknown') 40 + } 39 41 } 40 42 41 - // fetch immediately on load 42 - fetchStatus(); 43 + const cached = localStorage.getItem(CACHE_KEY) 44 + if (cached) updateIndicator(cached) 43 45 44 - // refresh periodically 45 - setInterval(fetchStatus, REFRESH_INTERVAL); 46 - })(); 46 + fetchStatus() 47 + setInterval(fetchStatus, REFRESH_INTERVAL) 48 + })()