gabriel / musehub public
new-repo.ts typescript
111 lines 4.5 KB
Raw
sha256:73f24973678ceefd56628ee17c6d0535d86e33533828af6c63348f50941515ad Merge branch 'fix/smoke-test-tag-label' into dev Human 15 days 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 12 commits
sha256:cfefc25a166c3c3eed8ea3529aee19ea350bc05f2954d007420e924133b7d8ce chore: pivot to nightly channel — bump version to 0.2.0.dev… Sonnet 5 patch 13 days ago
sha256:d035733f21ccff27735fddebfbbe0ed24565a32a22db8de5885402262671ecd2 chore: bump version to 0.2.0rc15 for musehub#113 fix release Sonnet 4.6 patch 15 days ago
sha256:0032d6cfa33bc3c8367436ad768e7dd0e339b4332153160247da8266cb5fa352 Merge branch 'task/version-tags-phase3-server' into dev Human 17 days ago
sha256:4669620efda9ff41c55bdefd1f7bfe1c239d468428744c84ead9957e5a003a53 merge: rescue snapshot-recovery hardening (c00aa21d) into d… Opus 4.8 minor 30 days ago
sha256:a59da49c4611b970fc4b6ae48678ce4943261c213a07ddbd73ce9201df869b4a fix: remove false-positive proposal_comments index drop fro… Sonnet 4.6 patch 34 days ago
sha256:0a240d6dbff234f07d98a28a4a9a68db702f3f9ff9260196f24219bdb1c0b6f3 feat: render markdown mists as HTML with heading anchor links Sonnet 4.6 patch 35 days ago
sha256:24a7d47486ebc4ebd1832830580e177ec6f877b48dced8c000e198cdec4ce9d6 Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump … Human 36 days ago
sha256:b9ff931d147e0114a1f17060f415b89ed551c170a91ff226c70437aa5c85f9ee Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump … Human 36 days ago
sha256:d1122d21e73471879b460037b22c0b50fded7c423444a176f248428f75dac39c Merge 'task/fix-issue-pagination-cursor' into 'dev' — propo… Human 36 days ago
sha256:01e18975e73d2b3cd5b6db7929c895bef9aa6e0d4391dc5b2adfc548b41318dd Merge 'feat/adding-debug-logs-to-staging' into 'dev' — prop… Human 36 days ago
sha256:6b1949fc2797ca4c1936a637a4cbfec828ef56cf52398a2e74ca3c4f494e728f fix: use wire_bytes not mpack_bytes_raw in compute_object_b… Sonnet 4.6 patch 48 days ago
sha256:b99f2455dc346966d040133f5203297e6e3ef5803a93728a2c30568d0a0f7583 rename: delta_add → delta_upsert across wire format, models… Sonnet 4.6 patch 50 days ago