|
| 1 | +/** |
| 2 | + * Huntarr - Circular Progress Indicators |
| 3 | + * Creates animated circular progress indicators for API usage counters |
| 4 | + */ |
| 5 | + |
| 6 | +document.addEventListener('DOMContentLoaded', function() { |
| 7 | + // Create and inject SVG progress indicators for API counts |
| 8 | + const apps = ['sonarr', 'radarr', 'lidarr', 'readarr', 'whisparr', 'eros']; |
| 9 | + |
| 10 | + // App-specific colors matching your existing design |
| 11 | + const appColors = { |
| 12 | + 'sonarr': '#3498db', // Blue |
| 13 | + 'radarr': '#f39c12', // Yellow/orange |
| 14 | + 'lidarr': '#2ecc71', // Green |
| 15 | + 'readarr': '#e74c3c', // Red |
| 16 | + 'whisparr': '#9b59b6', // Purple |
| 17 | + 'eros': '#1abc9c' // Teal |
| 18 | + }; |
| 19 | + |
| 20 | + // Add circular progress indicators to each API count indicator |
| 21 | + apps.forEach(app => { |
| 22 | + const capContainer = document.querySelector(`#${app}-hourly-cap`); |
| 23 | + if (!capContainer) return; |
| 24 | + |
| 25 | + // Get current API count and limit |
| 26 | + const countElement = document.querySelector(`#${app}-api-count`); |
| 27 | + const limitElement = document.querySelector(`#${app}-api-limit`); |
| 28 | + |
| 29 | + if (!countElement || !limitElement) return; |
| 30 | + |
| 31 | + const count = parseInt(countElement.textContent); |
| 32 | + const limit = parseInt(limitElement.textContent); |
| 33 | + |
| 34 | + // Create SVG container for progress circle |
| 35 | + const svgSize = 28; |
| 36 | + const circleRadius = 10; |
| 37 | + const circleStrokeWidth = 2.5; |
| 38 | + const circumference = 2 * Math.PI * circleRadius; |
| 39 | + |
| 40 | + // Calculate progress percentage |
| 41 | + const percentage = Math.min(count / limit, 1); |
| 42 | + const dashOffset = circumference * (1 - percentage); |
| 43 | + |
| 44 | + // Create SVG element |
| 45 | + const svgNamespace = "http://www.w3.org/2000/svg"; |
| 46 | + const svg = document.createElementNS(svgNamespace, "svg"); |
| 47 | + svg.setAttribute("width", svgSize); |
| 48 | + svg.setAttribute("height", svgSize); |
| 49 | + svg.setAttribute("viewBox", `0 0 ${svgSize} ${svgSize}`); |
| 50 | + svg.classList.add("api-progress-circle"); |
| 51 | + |
| 52 | + // Background circle |
| 53 | + const bgCircle = document.createElementNS(svgNamespace, "circle"); |
| 54 | + bgCircle.setAttribute("cx", svgSize / 2); |
| 55 | + bgCircle.setAttribute("cy", svgSize / 2); |
| 56 | + bgCircle.setAttribute("r", circleRadius); |
| 57 | + bgCircle.setAttribute("fill", "none"); |
| 58 | + bgCircle.setAttribute("stroke", "rgba(255, 255, 255, 0.1)"); |
| 59 | + bgCircle.setAttribute("stroke-width", circleStrokeWidth); |
| 60 | + |
| 61 | + // Progress circle |
| 62 | + const progressCircle = document.createElementNS(svgNamespace, "circle"); |
| 63 | + progressCircle.setAttribute("cx", svgSize / 2); |
| 64 | + progressCircle.setAttribute("cy", svgSize / 2); |
| 65 | + progressCircle.setAttribute("r", circleRadius); |
| 66 | + progressCircle.setAttribute("fill", "none"); |
| 67 | + progressCircle.setAttribute("stroke", appColors[app]); |
| 68 | + progressCircle.setAttribute("stroke-width", circleStrokeWidth); |
| 69 | + progressCircle.setAttribute("stroke-dasharray", circumference); |
| 70 | + progressCircle.setAttribute("stroke-dashoffset", dashOffset); |
| 71 | + progressCircle.setAttribute("transform", `rotate(-90 ${svgSize/2} ${svgSize/2})`); |
| 72 | + |
| 73 | + // Add circles to SVG |
| 74 | + svg.appendChild(bgCircle); |
| 75 | + svg.appendChild(progressCircle); |
| 76 | + |
| 77 | + // Add SVG before text content |
| 78 | + capContainer.insertBefore(svg, capContainer.firstChild); |
| 79 | + |
| 80 | + // Style for the indicator |
| 81 | + const style = document.createElement('style'); |
| 82 | + style.textContent = ` |
| 83 | + .api-progress-circle { |
| 84 | + margin-right: 5px; |
| 85 | + filter: drop-shadow(0 0 3px ${appColors[app]}40); |
| 86 | + } |
| 87 | + |
| 88 | + .hourly-cap-status { |
| 89 | + display: flex; |
| 90 | + align-items: center; |
| 91 | + } |
| 92 | + |
| 93 | + @keyframes pulse-${app} { |
| 94 | + 0% { filter: drop-shadow(0 0 3px ${appColors[app]}40); } |
| 95 | + 50% { filter: drop-shadow(0 0 6px ${appColors[app]}80); } |
| 96 | + 100% { filter: drop-shadow(0 0 3px ${appColors[app]}40); } |
| 97 | + } |
| 98 | + |
| 99 | + .api-progress-circle circle:nth-child(2) { |
| 100 | + animation: pulse-${app} 2s infinite; |
| 101 | + transition: stroke-dashoffset 0.5s ease; |
| 102 | + } |
| 103 | + `; |
| 104 | + document.head.appendChild(style); |
| 105 | + |
| 106 | + // Update progress when API counts change |
| 107 | + const updateProgressCircle = () => { |
| 108 | + const newCount = parseInt(countElement.textContent); |
| 109 | + const newLimit = parseInt(limitElement.textContent); |
| 110 | + const newPercentage = Math.min(newCount / newLimit, 1); |
| 111 | + const newDashOffset = circumference * (1 - newPercentage); |
| 112 | + |
| 113 | + progressCircle.setAttribute("stroke-dashoffset", newDashOffset); |
| 114 | + |
| 115 | + // Change color based on usage percentage |
| 116 | + if (newPercentage > 0.9) { |
| 117 | + progressCircle.setAttribute("stroke", "#e74c3c"); // Red when near limit |
| 118 | + } else if (newPercentage > 0.75) { |
| 119 | + progressCircle.setAttribute("stroke", "#f39c12"); // Orange/yellow for moderate usage |
| 120 | + } else { |
| 121 | + progressCircle.setAttribute("stroke", appColors[app]); // Default color |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + // Set up a mutation observer to watch for changes in the count value |
| 126 | + const observer = new MutationObserver((mutations) => { |
| 127 | + mutations.forEach((mutation) => { |
| 128 | + if (mutation.type === 'characterData' || mutation.type === 'childList') { |
| 129 | + updateProgressCircle(); |
| 130 | + } |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + // Observe both count and limit elements |
| 135 | + observer.observe(countElement, { characterData: true, childList: true, subtree: true }); |
| 136 | + observer.observe(limitElement, { characterData: true, childList: true, subtree: true }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments