1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
import { api, type Task, type Project } from '../lib/api.js';
import { wsClient } from '../lib/ws.js';
import { renderBoard } from '../components/board.js';
let currentProject: string | null = null;
let cleanupWs: (() => void) | null = null;
export async function renderProjectPage(container: HTMLElement, projectName: string): Promise<void> {
// Cleanup previous subscriptions
if (currentProject && currentProject !== projectName) {
wsClient.unsubscribe(currentProject);
}
if (cleanupWs) cleanupWs();
currentProject = projectName;
container.innerHTML = `
<div class="board-header">
<div style="display:flex;align-items:center;gap:12px">
<a href="/" data-route="/" class="btn btn-secondary btn-sm">← Back</a>
<h1 id="project-title">Loading...</h1>
</div>
<button class="btn btn-primary" id="new-task-btn">+ New Task</button>
</div>
<div id="board-root"></div>
`;
const boardRoot = container.querySelector('#board-root') as HTMLElement;
const titleEl = container.querySelector('#project-title')!;
const newTaskBtn = container.querySelector('#new-task-btn')!;
try {
const [project, tasks] = await Promise.all([
api.getProject(projectName),
api.getTasks(projectName),
]);
titleEl.textContent = project.display_name;
renderBoard(boardRoot, projectName, tasks);
// WebSocket subscription
wsClient.subscribe(projectName);
const unsub1 = wsClient.on('task:created', (data: unknown) => {
const event = data as { project: string; task: Task };
if (event.project === projectName) {
refreshBoard(boardRoot, projectName);
}
});
const unsub2 = wsClient.on('task:updated', (data: unknown) => {
const event = data as { project: string; task: Task };
if (event.project === projectName) {
refreshBoard(boardRoot, projectName);
}
});
const unsub3 = wsClient.on('task:deleted', (data: unknown) => {
const event = data as { project: string };
if (event.project === projectName) {
refreshBoard(boardRoot, projectName);
}
});
cleanupWs = () => { unsub1(); unsub2(); unsub3(); };
} catch (err) {
boardRoot.innerHTML = `<div class="empty-state"><h3>Failed to load project</h3><p>${(err as Error).message}</p></div>`;
}
newTaskBtn.addEventListener('click', () => showCreateTaskDialog(projectName, boardRoot));
}
async function refreshBoard(boardRoot: HTMLElement, projectName: string): Promise<void> {
try {
const tasks = await api.getTasks(projectName);
renderBoard(boardRoot, projectName, tasks);
} catch {
// silently fail on refresh
}
}
function showCreateTaskDialog(projectName: string, boardRoot: HTMLElement): void {
document.querySelector('.modal-overlay')?.remove();
const overlay = document.createElement('div');
overlay.className = 'modal-overlay';
overlay.innerHTML = `
<div class="modal">
<div class="modal-header">
<h2>New Task</h2>
<button class="btn-icon close-btn">×</button>
</div>
<form id="create-task-form">
<div class="form-group">
<label for="task-title">Title</label>
<input type="text" id="task-title" placeholder="What needs to be done?" required />
</div>
<div class="form-group">
<label for="task-desc">Description</label>
<textarea id="task-desc" rows="4" placeholder="Detailed requirements..."></textarea>
</div>
<div style="display:flex;gap:12px">
<div class="form-group" style="flex:1">
<label for="task-priority">Priority</label>
<select id="task-priority">
<option value="low">Low</option>
<option value="medium" selected>Medium</option>
<option value="high">High</option>
</select>
</div>
<div class="form-group" style="flex:1">
<label for="task-level">Level</label>
<select id="task-level">
<option value="1">L1 — Quick</option>
<option value="2">L2 — Standard</option>
<option value="3" selected>L3 — Full</option>
</select>
</div>
</div>
<div class="form-group">
<label for="task-tags">Tags (comma-separated)</label>
<input type="text" id="task-tags" placeholder="frontend, bug-fix" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary close-btn">Cancel</button>
<button type="submit" class="btn btn-primary">Create Task</button>
</div>
</form>
</div>
`;
document.body.appendChild(overlay);
overlay.querySelectorAll('.close-btn').forEach(btn => {
btn.addEventListener('click', () => overlay.remove());
});
overlay.addEventListener('click', (e) => {
if (e.target === overlay) overlay.remove();
});
const form = overlay.querySelector('#create-task-form') as HTMLFormElement;
form.addEventListener('submit', async (e) => {
e.preventDefault();
const submitBtn = form.querySelector('button[type="submit"]') as HTMLButtonElement;
submitBtn.disabled = true;
submitBtn.textContent = 'Creating...';
const tagsStr = (overlay.querySelector('#task-tags') as HTMLInputElement).value;
const tags = tagsStr ? tagsStr.split(',').map(t => t.trim()).filter(Boolean) : [];
try {
await api.createTask(projectName, {
title: (overlay.querySelector('#task-title') as HTMLInputElement).value,
description: (overlay.querySelector('#task-desc') as HTMLTextAreaElement).value || undefined,
priority: (overlay.querySelector('#task-priority') as HTMLSelectElement).value,
level: parseInt((overlay.querySelector('#task-level') as HTMLSelectElement).value),
tags,
});
overlay.remove();
await refreshBoard(boardRoot, projectName);
} catch (err) {
submitBtn.disabled = false;
submitBtn.textContent = 'Create Task';
alert((err as Error).message);
}
});
(overlay.querySelector('#task-title') as HTMLInputElement).focus();
}
|