new-repo.ts
typescript
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2
feat: add repair-commit wire endpoint (API parity with repa…
Opus 4.8
minor
⚠ breaking
1 day ago
| 1 | /** |
| 2 | * new-repo.ts — New repository wizard page module. |
| 3 | * |
| 4 | * Handles client-side validation and tag pill input for the new-repo form. |
| 5 | * The form is mostly server-rendered with HTMX; this module provides: |
| 6 | * - Repo name availability check (debounced) |
| 7 | * - Tag pill multi-input widget |
| 8 | * |
| 9 | * Data expected in #page-data: |
| 10 | * { "page": "new-repo", "owner": "..." } |
| 11 | * |
| 12 | * Registered as: window.MusePages['new-repo'] |
| 13 | */ |
| 14 | |
| 15 | export interface NewRepoData { |
| 16 | page?: string; |
| 17 | owner?: string; |
| 18 | [key: string]: unknown; |
| 19 | } |
| 20 | |
| 21 | function initTagInput(containerId: string, hiddenInputId: string): void { |
| 22 | const container = document.getElementById(containerId); |
| 23 | const hidden = document.getElementById(hiddenInputId) as HTMLInputElement | null; |
| 24 | if (!container || !hidden) return; |
| 25 | |
| 26 | const textInput = container.querySelector('.tag-text-input') as HTMLInputElement | null; |
| 27 | if (!textInput) return; |
| 28 | |
| 29 | let tags: string[] = hidden.value ? hidden.value.split(',').filter(Boolean) : []; |
| 30 | |
| 31 | function render(): void { |
| 32 | container!.querySelectorAll('.tag-pill').forEach((p) => p.remove()); |
| 33 | tags.forEach((tag) => { |
| 34 | const pill = document.createElement('span'); |
| 35 | pill.className = 'tag-pill'; |
| 36 | pill.textContent = tag + ' '; |
| 37 | const btn = document.createElement('button'); |
| 38 | btn.type = 'button'; |
| 39 | btn.className = 'tag-pill-remove'; |
| 40 | btn.textContent = '×'; |
| 41 | btn.addEventListener('click', () => { tags = tags.filter((t) => t !== tag); render(); }); |
| 42 | pill.appendChild(btn); |
| 43 | container!.insertBefore(pill, textInput); |
| 44 | }); |
| 45 | hidden!.value = tags.join(','); |
| 46 | } |
| 47 | |
| 48 | textInput.addEventListener('keydown', (e) => { |
| 49 | if (e.key === 'Enter' || e.key === ',') { |
| 50 | e.preventDefault(); |
| 51 | const val = textInput.value.trim().replace(/,/g, ''); |
| 52 | if (val && !tags.includes(val)) { tags.push(val); render(); } |
| 53 | textInput.value = ''; |
| 54 | } else if (e.key === 'Backspace' && textInput.value === '' && tags.length > 0) { |
| 55 | tags.pop(); |
| 56 | render(); |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | container.addEventListener('click', () => textInput.focus()); |
| 61 | render(); |
| 62 | } |
| 63 | |
| 64 | async function submitWizard(e: Event): Promise<void> { |
| 65 | e.preventDefault(); |
| 66 | const btn = document.getElementById('submit-btn') as HTMLButtonElement | null; |
| 67 | const errorEl = document.getElementById('submit-error') as HTMLElement | null; |
| 68 | if (errorEl) errorEl.style.display = 'none'; |
| 69 | |
| 70 | const owner = (document.getElementById('f-owner') as HTMLInputElement).value.trim(); |
| 71 | const name = (document.getElementById('f-name') as HTMLInputElement).value.trim(); |
| 72 | const desc = (document.getElementById('f-description') as HTMLTextAreaElement).value.trim(); |
| 73 | const license = (document.getElementById('f-license') as HTMLSelectElement).value || null; |
| 74 | const branchEl = document.getElementById('f-branch') as HTMLInputElement | null; |
| 75 | const branch = branchEl ? (branchEl.value.trim() || 'main') : 'main'; |
| 76 | const init = (document.getElementById('f-initialize') as HTMLInputElement).checked; |
| 77 | |
| 78 | // Collect topics from the hidden input synced by initTagInput |
| 79 | const tagsHidden = document.getElementById('tags-hidden') as HTMLInputElement | null; |
| 80 | const topics: string[] = tagsHidden?.value ? tagsHidden.value.split(',').filter(Boolean) : []; |
| 81 | |
| 82 | if (btn) { btn.disabled = true; btn.textContent = 'Creating…'; } |
| 83 | try { |
| 84 | const res = await fetch('/new', { |
| 85 | method: 'POST', |
| 86 | headers: { 'Content-Type': 'application/json' }, |
| 87 | body: JSON.stringify({ owner, name, description: desc, visibility: 'public', license, |
| 88 | topics, tags: [], initialize: init, defaultBranch: branch }), |
| 89 | }); |
| 90 | if (res.status === 401 || res.status === 403) { |
| 91 | if (errorEl) { errorEl.textContent = 'Not authorized — sign in via the muse CLI to create a repository.'; errorEl.style.display = ''; } |
| 92 | return; |
| 93 | } |
| 94 | const data = await res.json() as { redirect?: string; detail?: string }; |
| 95 | if (res.status === 201) { |
| 96 | window.location.href = data.redirect!; |
| 97 | return; |
| 98 | } |
| 99 | if (errorEl) { errorEl.textContent = (data.detail || 'Failed to create repository.'); errorEl.style.display = ''; } |
| 100 | } catch (ex) { |
| 101 | if (errorEl) { errorEl.textContent = (ex instanceof Error ? ex.message : String(ex)); errorEl.style.display = ''; } |
| 102 | } finally { |
| 103 | if (btn) { btn.disabled = false; btn.textContent = 'Create repository'; } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | export function initNewRepo(_data: NewRepoData): void { |
| 108 | initTagInput('tag-input-container', 'tags-hidden'); |
| 109 | const form = document.getElementById('wizard-form') as HTMLFormElement | null; |
| 110 | form?.addEventListener('submit', (e) => void submitWizard(e)); |
| 111 | } |
File History
1 commit
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2
feat: add repair-commit wire endpoint (API parity with repa…
Opus 4.8
minor
⚠
1 day ago