|
| 1 | +// File: src/content/components/BillableBadge.js |
| 2 | + |
| 3 | +const BADGE_CLASS = 'esti-badge-nonbillable'; |
| 4 | +const TOGGLE_CLASS = 'esti-archive-toggle'; |
| 5 | +const HIDDEN_CLASS = 'esti-hidden'; |
| 6 | +const MASKED_CLASS = 'esti-totals-masked'; |
| 7 | + |
| 8 | +export default class BillableBadge { |
| 9 | + constructor() { |
| 10 | + this.observer = null; |
| 11 | + this.projectId = null; |
| 12 | + this.billableMap = null; |
| 13 | + this.archivedSet = null; |
| 14 | + this.currentPeriod = null; |
| 15 | + this.hideArchived = false; |
| 16 | + this._updating = false; |
| 17 | + } |
| 18 | + |
| 19 | + init() { |
| 20 | + const match = window.location.pathname.match(/^\/projects\/(\d+)$/); |
| 21 | + if (!match) return; |
| 22 | + this.projectId = match[1]; |
| 23 | + |
| 24 | + this.observer = new MutationObserver(() => this.onMutation()); |
| 25 | + this.observer.observe(document.body, { childList: true, subtree: true }); |
| 26 | + |
| 27 | + // Apply immediately if rows are already present |
| 28 | + this.onMutation(); |
| 29 | + } |
| 30 | + |
| 31 | + async onMutation() { |
| 32 | + if (this._updating) return; |
| 33 | + this._updating = true; |
| 34 | + try { |
| 35 | + const rows = document.querySelectorAll('tr[data-task-id]'); |
| 36 | + if (rows.length === 0) return; |
| 37 | + |
| 38 | + // Detect current period from the dropdown |
| 39 | + const period = this.detectPeriod(); |
| 40 | + |
| 41 | + // Re-fetch if period changed or first run |
| 42 | + if (!this.billableMap || period !== this.currentPeriod) { |
| 43 | + this.currentPeriod = period; |
| 44 | + const result = await this.fetchTaskData(period); |
| 45 | + this.billableMap = result.billableMap; |
| 46 | + this.archivedSet = result.archivedSet; |
| 47 | + } |
| 48 | + |
| 49 | + // Pause observer while we modify the DOM to avoid infinite loops |
| 50 | + this.observer.disconnect(); |
| 51 | + this.applyBadges(rows); |
| 52 | + this.injectArchiveToggle(); |
| 53 | + this.applyArchiveFilter(); |
| 54 | + this.observer.observe(document.body, { childList: true, subtree: true }); |
| 55 | + } finally { |
| 56 | + this._updating = false; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + detectPeriod() { |
| 61 | + // Harvest's period dropdown stores value on a select or in the URL |
| 62 | + const select = document.querySelector('select[name="period"]'); |
| 63 | + if (select) return select.value; |
| 64 | + |
| 65 | + // Fallback: check URL params |
| 66 | + const params = new URLSearchParams(window.location.search); |
| 67 | + return params.get('period') || 'all_time'; |
| 68 | + } |
| 69 | + |
| 70 | + async fetchTaskData(period) { |
| 71 | + const billableMap = new Map(); |
| 72 | + const archivedSet = new Set(); |
| 73 | + try { |
| 74 | + const resp = await fetch(`/projects/${this.projectId}/analysis?period=${period}`, { |
| 75 | + credentials: 'same-origin', |
| 76 | + headers: { 'Accept': 'application/json' }, |
| 77 | + }); |
| 78 | + if (!resp.ok) return { billableMap, archivedSet }; |
| 79 | + |
| 80 | + const data = await resp.json(); |
| 81 | + if (Array.isArray(data.tasks)) { |
| 82 | + for (const task of data.tasks) { |
| 83 | + const id = String(task.task_id); |
| 84 | + billableMap.set(id, task.billable); |
| 85 | + if (task.archived) archivedSet.add(id); |
| 86 | + } |
| 87 | + } |
| 88 | + } catch (e) { |
| 89 | + console.error('[Esti\'mate] Failed to fetch task data:', e); |
| 90 | + } |
| 91 | + return { billableMap, archivedSet }; |
| 92 | + } |
| 93 | + |
| 94 | + applyBadges(rows) { |
| 95 | + for (const row of rows) { |
| 96 | + const taskId = row.getAttribute('data-task-id'); |
| 97 | + if (!taskId || !this.billableMap.has(taskId)) continue; |
| 98 | + |
| 99 | + // Skip if badge already injected |
| 100 | + if (row.querySelector(`.${BADGE_CLASS}`)) continue; |
| 101 | + |
| 102 | + const isBillable = this.billableMap.get(taskId); |
| 103 | + if (isBillable) continue; |
| 104 | + |
| 105 | + const nameSpan = row.querySelector('td.col-name > span'); |
| 106 | + if (!nameSpan) continue; |
| 107 | + |
| 108 | + const badge = document.createElement('span'); |
| 109 | + badge.className = `pds-badge ${BADGE_CLASS}`; |
| 110 | + badge.textContent = 'Non-billable'; |
| 111 | + nameSpan.after(badge); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + injectArchiveToggle() { |
| 116 | + if (document.querySelector(`.${TOGGLE_CLASS}`)) return; |
| 117 | + |
| 118 | + const rightGroup = document.querySelector( |
| 119 | + '#project-tabs-timeframe .pds-flex-list > .pds-flex-list:last-child' |
| 120 | + ); |
| 121 | + if (!rightGroup) return; |
| 122 | + |
| 123 | + const wrapper = document.createElement('div'); |
| 124 | + wrapper.className = `pds-checkbox ${TOGGLE_CLASS}`; |
| 125 | + |
| 126 | + const input = document.createElement('input'); |
| 127 | + input.type = 'checkbox'; |
| 128 | + input.id = 'esti-hide-archived'; |
| 129 | + input.checked = this.hideArchived; |
| 130 | + |
| 131 | + const label = document.createElement('label'); |
| 132 | + label.htmlFor = 'esti-hide-archived'; |
| 133 | + label.textContent = 'Hide archived'; |
| 134 | + |
| 135 | + wrapper.append(input, label); |
| 136 | + rightGroup.prepend(wrapper); |
| 137 | + |
| 138 | + input.addEventListener('change', () => { |
| 139 | + this.hideArchived = input.checked; |
| 140 | + this.applyArchiveFilter(); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + applyArchiveFilter() { |
| 145 | + if (!this.archivedSet) return; |
| 146 | + |
| 147 | + const hiding = this.hideArchived; |
| 148 | + const rows = document.querySelectorAll('tr[data-task-id]'); |
| 149 | + |
| 150 | + for (const row of rows) { |
| 151 | + const taskId = row.getAttribute('data-task-id'); |
| 152 | + if (hiding && this.archivedSet.has(taskId)) { |
| 153 | + row.classList.add(HIDDEN_CLASS); |
| 154 | + } else { |
| 155 | + row.classList.remove(HIDDEN_CLASS); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + this.updateTotalsRow(hiding); |
| 160 | + } |
| 161 | + |
| 162 | + updateTotalsRow(hiding) { |
| 163 | + // Harvest actively re-renders totals cells — never touch innerHTML. |
| 164 | + // Toggle a CSS class that visually masks numeric cells with "—". |
| 165 | + const totalsRows = document.querySelectorAll('tr.tbody-foot.js-totals'); |
| 166 | + for (const row of totalsRows) { |
| 167 | + row.classList.toggle(MASKED_CLASS, hiding); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + destroy() { |
| 172 | + if (this.observer) { |
| 173 | + this.observer.disconnect(); |
| 174 | + this.observer = null; |
| 175 | + } |
| 176 | + document.querySelectorAll(`.${BADGE_CLASS}`).forEach((el) => el.remove()); |
| 177 | + document.querySelectorAll(`.${TOGGLE_CLASS}`).forEach((el) => el.remove()); |
| 178 | + document.querySelectorAll(`.${HIDDEN_CLASS}`).forEach((el) => el.classList.remove(HIDDEN_CLASS)); |
| 179 | + document.querySelectorAll(`.${MASKED_CLASS}`).forEach((el) => el.classList.remove(MASKED_CLASS)); |
| 180 | + this.billableMap = null; |
| 181 | + this.archivedSet = null; |
| 182 | + this.currentPeriod = null; |
| 183 | + this.hideArchived = false; |
| 184 | + } |
| 185 | +} |
0 commit comments