(function() { var cfg = document.getElementById('tab-project-code-cfg'); var projectSlug = cfg.dataset.projectSlug; document.querySelectorAll('.collab-add-btn').forEach(function(btn) { btn.addEventListener('click', function() { var repoId = btn.dataset.repoId; var input = document.querySelector('.collab-username-input[data-repo-id="' + repoId + '"]'); var username = input.value.trim(); if (!username) return; btn.disabled = true; var body = new URLSearchParams(); body.append('username', username); fetch('/api/repos/' + repoId + '/collaborators', { method: 'POST', headers: {...csrfHeaders(), 'Content-Type': 'application/x-www-form-urlencoded'}, body: body.toString() }).then(function(r) { if (r.ok) { htmx.ajax('GET', '/dashboard/project/' + projectSlug + '/tabs/code', {target: '#tab-content', swap: 'innerHTML'}); } else { return r.json().then(function(data) { alert(data.error || 'Failed to add collaborator'); }); } }).catch(function() { alert('Network error'); }).finally(function() { btn.disabled = false; }); }); }); })();