gabriel / musehub public
settings.ts typescript
147 lines 5.7 KB
Raw
sha256:73f24973678ceefd56628ee17c6d0535d86e33533828af6c63348f50941515ad Merge branch 'fix/smoke-test-tag-label' into dev Human 15 days ago
1 /**
2 * settings.ts — Repository settings page.
3 *
4 * Handles topic tag input, collaborator invite, and delete confirmation guard.
5 * Config is read from the #page-data JSON element.
6 * Registered as: window.MusePages['settings']
7 */
8
9 // ── Types ─────────────────────────────────────────────────────────────────────
10
11 interface SettingsCfg {
12 repoId: string;
13 owner: string;
14 repoSlug: string;
15 base: string;
16 fullName: string;
17 }
18
19 declare global {
20 interface Window {
21 htmx?: { trigger(el: Element | string, event: string): void };
22 }
23 }
24
25 // ── Topic tag input ───────────────────────────────────────────────────────────
26
27 function addTopic(val: string, input: HTMLInputElement): void {
28 const cleaned = val.trim().toLowerCase().replace(/[^a-z0-9-]/g, '-');
29 const container = document.getElementById('topics-container');
30 if (!cleaned || !container) return;
31
32 const pill = document.createElement('span');
33 pill.className = 'tag-pill';
34 const removeBtn = document.createElement('button');
35 removeBtn.type = 'button';
36 removeBtn.className = 'tag-pill-remove';
37 removeBtn.dataset.action = 'remove-pill';
38 removeBtn.textContent = '×';
39 pill.textContent = cleaned;
40 pill.appendChild(removeBtn);
41 container.insertBefore(pill, input);
42 input.value = '';
43 }
44
45 function setupTopicInput(): void {
46 const input = document.getElementById('topic-input') as HTMLInputElement | null;
47 if (!input) return;
48
49 input.addEventListener('keydown', (e) => {
50 if (e.key === 'Enter' || e.key === ',') {
51 e.preventDefault();
52 addTopic(input.value, input);
53 } else if (e.key === 'Backspace' && input.value === '') {
54 const container = document.getElementById('topics-container');
55 const pills = container ? container.querySelectorAll('.tag-pill') : [];
56 if (pills.length > 0) pills[pills.length - 1].remove();
57 }
58 });
59 }
60
61 // ── Collaborator invite ───────────────────────────────────────────────────────
62
63 async function inviteCollaborator(repoId: string): Promise<void> {
64 const usernameEl = document.getElementById('invite-username') as HTMLInputElement | null;
65 const roleEl = document.getElementById('invite-role') as HTMLSelectElement | null;
66 const msgEl = document.getElementById('invite-msg') as HTMLElement | null;
67 if (!usernameEl || !roleEl || !msgEl) return;
68
69 const username = usernameEl.value.trim();
70 const role = roleEl.value;
71 if (!username) return;
72
73 try {
74 const resp = await fetch('/api/repos/' + repoId + '/collaborators', {
75 method: 'POST',
76 headers: { 'Content-Type': 'application/json' },
77 body: JSON.stringify({ username, role }),
78 });
79
80 if (!resp.ok) {
81 const err = await resp.json().catch(() => ({ detail: resp.statusText })) as { detail?: string };
82 msgEl.textContent = '❌ ' + (err.detail || 'Invite failed.');
83 msgEl.style.color = '#f85149';
84 } else {
85 usernameEl.value = '';
86 msgEl.textContent = '✅ Invited ' + username;
87 msgEl.style.color = '#3fb950';
88 const listEl = document.getElementById('collaborators-list');
89 if (listEl && window.htmx) window.htmx.trigger(listEl, 'load');
90 }
91 msgEl.style.display = 'block';
92 setTimeout(() => { msgEl.style.display = 'none'; }, 5000);
93 } catch(e) {
94 msgEl.textContent = '❌ ' + (e as Error).message;
95 msgEl.style.color = '#f85149';
96 msgEl.style.display = 'block';
97 }
98 }
99
100 // ── Delete confirmation guard ─────────────────────────────────────────────────
101
102 function setupDeleteGuard(fullName: string): void {
103 const form = document.getElementById('delete-repo-form');
104 if (!form) return;
105
106 form.addEventListener('htmx:before-request', (e) => {
107 const val = (document.getElementById('confirm-delete-name') as HTMLInputElement | null)?.value.trim();
108 const errorEl = document.getElementById('delete-name-error') as HTMLElement | null;
109 if (val !== fullName) {
110 if (errorEl) errorEl.style.display = 'block';
111 (e as CustomEvent).preventDefault();
112 }
113 });
114 }
115
116 // ── Event delegation ──────────────────────────────────────────────────────────
117
118 function setupEventDelegation(cfg: SettingsCfg): void {
119 document.addEventListener('click', (e) => {
120 const target = (e.target as Element).closest<HTMLElement>('[data-action]');
121 if (!target) return;
122 switch (target.dataset.action) {
123 case 'remove-pill':
124 target.parentElement?.remove();
125 break;
126 case 'invite-collaborator':
127 void inviteCollaborator(cfg.repoId);
128 break;
129 }
130 });
131 }
132
133 // ── Entry point ───────────────────────────────────────────────────────────────
134
135 export function initSettings(data: Record<string, unknown> = {}): void {
136 const cfg: SettingsCfg = {
137 repoId: String(data['repoId'] ?? ''),
138 owner: String(data['owner'] ?? ''),
139 repoSlug: String(data['repoSlug'] ?? ''),
140 base: String(data['base'] ?? ''),
141 fullName: String(data['fullName'] ?? ''),
142 };
143 if (!cfg.repoId) return;
144 setupTopicInput();
145 setupEventDelegation(cfg);
146 setupDeleteGuard(cfg.fullName);
147 }
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