|
| 1 | +import { formatDuration } from './useUrchinLayout.js'; |
| 2 | + |
| 3 | +function toHexChannel(value) { |
| 4 | + const channel = Number.parseInt(value, 16); |
| 5 | + if (Number.isNaN(channel)) { |
| 6 | + return 0; |
| 7 | + } |
| 8 | + return channel; |
| 9 | +} |
| 10 | + |
| 11 | +function computeTextColor(background) { |
| 12 | + if (typeof background !== 'string') { |
| 13 | + return '#0f172a'; |
| 14 | + } |
| 15 | + let hex = background.trim(); |
| 16 | + if (hex.startsWith('#')) { |
| 17 | + hex = hex.slice(1); |
| 18 | + } |
| 19 | + if (hex.length === 3) { |
| 20 | + hex = hex |
| 21 | + .split('') |
| 22 | + .map((char) => char + char) |
| 23 | + .join(''); |
| 24 | + } |
| 25 | + if (hex.length !== 6) { |
| 26 | + return '#0f172a'; |
| 27 | + } |
| 28 | + const r = toHexChannel(hex.slice(0, 2)); |
| 29 | + const g = toHexChannel(hex.slice(2, 4)); |
| 30 | + const b = toHexChannel(hex.slice(4, 6)); |
| 31 | + const luminance = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255; |
| 32 | + return luminance > 0.6 ? '#0f172a' : '#f8fafc'; |
| 33 | +} |
| 34 | + |
| 35 | +export function prepareActivityShareSegments(activities) { |
| 36 | + const filtered = Array.isArray(activities) |
| 37 | + ? activities.filter((activity) => Number.isFinite(activity?.minutes) && activity.minutes > 0) |
| 38 | + : []; |
| 39 | + const totalMinutes = filtered.reduce((sum, activity) => sum + activity.minutes, 0); |
| 40 | + if (totalMinutes <= 0) { |
| 41 | + return { totalMinutes: 0, segments: [] }; |
| 42 | + } |
| 43 | + const segments = filtered.map((activity) => ({ |
| 44 | + id: activity.id ?? activity.label, |
| 45 | + label: activity.label, |
| 46 | + minutes: activity.minutes, |
| 47 | + color: activity.color, |
| 48 | + percentage: activity.minutes / totalMinutes, |
| 49 | + })); |
| 50 | + return { totalMinutes, segments }; |
| 51 | +} |
| 52 | + |
| 53 | +export class ActivityShareBar { |
| 54 | + constructor(container) { |
| 55 | + this.root = container; |
| 56 | + this.root.classList.add('radial-urchin__share'); |
| 57 | + this.root.setAttribute('role', 'group'); |
| 58 | + this.root.setAttribute('aria-label', 'Activity balance overview'); |
| 59 | + |
| 60 | + this.header = document.createElement('div'); |
| 61 | + this.header.className = 'activity-share__header'; |
| 62 | + this.title = document.createElement('span'); |
| 63 | + this.title.className = 'activity-share__title'; |
| 64 | + this.title.textContent = 'Activity balance'; |
| 65 | + this.total = document.createElement('span'); |
| 66 | + this.total.className = 'activity-share__total'; |
| 67 | + this.total.textContent = '—'; |
| 68 | + this.header.append(this.title, this.total); |
| 69 | + |
| 70 | + this.track = document.createElement('div'); |
| 71 | + this.track.className = 'activity-share__track'; |
| 72 | + this.track.setAttribute('role', 'list'); |
| 73 | + this.track.hidden = true; |
| 74 | + this.track.addEventListener('mouseleave', () => { |
| 75 | + this.hideTooltip(); |
| 76 | + }); |
| 77 | + |
| 78 | + this.empty = document.createElement('p'); |
| 79 | + this.empty.className = 'activity-share__empty'; |
| 80 | + this.empty.textContent = 'Select activities to see their balance.'; |
| 81 | + this.empty.hidden = false; |
| 82 | + |
| 83 | + this.tooltip = document.createElement('div'); |
| 84 | + this.tooltip.className = 'activity-share__tooltip'; |
| 85 | + this.tooltip.hidden = true; |
| 86 | + this.tooltip.setAttribute('role', 'dialog'); |
| 87 | + this.tooltip.setAttribute('aria-live', 'polite'); |
| 88 | + this.tooltip.setAttribute('aria-hidden', 'true'); |
| 89 | + |
| 90 | + this.root.append(this.header, this.track, this.empty, this.tooltip); |
| 91 | + |
| 92 | + this.currentSegments = []; |
| 93 | + this.boundHideTooltip = this.hideTooltip.bind(this); |
| 94 | + } |
| 95 | + |
| 96 | + update(segments, totalMinutes = 0) { |
| 97 | + this.hideTooltip(); |
| 98 | + const hasSegments = Array.isArray(segments) && segments.length > 0 && totalMinutes > 0; |
| 99 | + this.track.innerHTML = ''; |
| 100 | + this.currentSegments = hasSegments ? segments : []; |
| 101 | + this.total.textContent = hasSegments ? formatDuration(Math.round(totalMinutes)) : '—'; |
| 102 | + this.track.hidden = !hasSegments; |
| 103 | + this.empty.hidden = hasSegments; |
| 104 | + if (!hasSegments) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + segments.forEach((segment, index) => { |
| 109 | + const element = document.createElement('div'); |
| 110 | + element.className = 'activity-share__segment'; |
| 111 | + element.style.setProperty('--segment-color', segment.color || '#6366f1'); |
| 112 | + element.style.setProperty('--segment-text-color', computeTextColor(segment.color)); |
| 113 | + element.style.flexGrow = String(segment.minutes); |
| 114 | + element.setAttribute('role', 'listitem'); |
| 115 | + const percentValue = Math.round(segment.percentage * 100); |
| 116 | + const labelText = this.getSegmentLabel(segment, percentValue); |
| 117 | + if (labelText) { |
| 118 | + const label = document.createElement('span'); |
| 119 | + label.className = 'activity-share__segment-label'; |
| 120 | + label.textContent = labelText; |
| 121 | + element.append(label); |
| 122 | + } |
| 123 | + element.setAttribute( |
| 124 | + 'aria-label', |
| 125 | + `${segment.label}: ${formatDuration(Math.round(segment.minutes))} (${percentValue}%)`, |
| 126 | + ); |
| 127 | + element.dataset.index = String(index); |
| 128 | + element.addEventListener('mouseenter', (event) => { |
| 129 | + this.showTooltip(event, segment); |
| 130 | + }); |
| 131 | + element.addEventListener('mousemove', (event) => { |
| 132 | + this.positionTooltip(event); |
| 133 | + }); |
| 134 | + element.addEventListener('mouseleave', this.boundHideTooltip); |
| 135 | + this.track.append(element); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + getSegmentLabel(segment, percentValue) { |
| 140 | + if (segment.percentage >= 0.18) { |
| 141 | + return `${segment.label} · ${percentValue}%`; |
| 142 | + } |
| 143 | + if (segment.percentage >= 0.1) { |
| 144 | + return `${percentValue}%`; |
| 145 | + } |
| 146 | + return ''; |
| 147 | + } |
| 148 | + |
| 149 | + showTooltip(event, segment) { |
| 150 | + const percentValue = Math.round(segment.percentage * 100); |
| 151 | + const durationLabel = formatDuration(Math.round(segment.minutes)); |
| 152 | + this.tooltip.innerHTML = `<strong>${segment.label}</strong><span>${durationLabel}</span><span>${percentValue}%</span>`; |
| 153 | + this.tooltip.hidden = false; |
| 154 | + this.tooltip.setAttribute('aria-hidden', 'false'); |
| 155 | + this.positionTooltip(event); |
| 156 | + } |
| 157 | + |
| 158 | + positionTooltip(event) { |
| 159 | + if (this.tooltip.hidden) { |
| 160 | + return; |
| 161 | + } |
| 162 | + const rootRect = this.root.getBoundingClientRect(); |
| 163 | + const x = event.clientX - rootRect.left; |
| 164 | + const y = event.clientY - rootRect.top; |
| 165 | + this.tooltip.style.left = `${x}px`; |
| 166 | + this.tooltip.style.top = `${y}px`; |
| 167 | + } |
| 168 | + |
| 169 | + hideTooltip() { |
| 170 | + this.tooltip.hidden = true; |
| 171 | + this.tooltip.setAttribute('aria-hidden', 'true'); |
| 172 | + } |
| 173 | +} |
0 commit comments