task-track 0.7.0

A JJ workspace-based task and TODO management CLI tool
Documentation
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! HTTP route handlers for the WebUI.

use crate::models::TodoStatus;
use crate::services::{LinkService, RepoService, ScrapService, TaskService, TodoService};
use crate::use_cases::{ApplyTodoActionUseCase, GetTaskInfoUseCase};
use crate::utils::TrackError;
use crate::webui::error::WebError;
use crate::webui::state::{AppState, SseEvent};
use crate::webui::templates::SharedTemplates;
use crate::webui::view::{self, format_scraps, format_todos, StatusResponse};
use axum::{
    extract::{Path, State},
    response::Html,
    Form, Json,
};
use serde::Deserialize;

/// Extended application state with templates
#[derive(Clone)]
pub struct WebState {
    pub app: AppState,
    pub templates: SharedTemplates,
}

/// Error response wrapper
pub type AppError = WebError;

/// Form data for adding a todo
#[derive(Deserialize)]
pub struct AddTodoForm {
    pub content: String,
    #[serde(default)]
    pub create_worktree: bool,
    #[serde(default)]
    pub no_workspace: bool,
}

/// Form data for adding a scrap
#[derive(Deserialize)]
pub struct AddScrapForm {
    pub content: String,
}

/// Form data for updating description
#[derive(Deserialize)]
pub struct UpdateDescriptionForm {
    pub description: String,
}

/// Form data for updating ticket
#[derive(Deserialize)]
pub struct UpdateTicketForm {
    pub ticket_id: String,
    pub ticket_url: Option<String>,
}

/// Form data for adding a link
#[derive(Deserialize)]
pub struct AddLinkForm {
    pub url: String,
    pub title: Option<String>,
}

fn render_todo_list_html(
    templates: &crate::webui::templates::Templates,
    db: &crate::db::Database,
    task_id: i64,
) -> Result<String, AppError> {
    let snapshot = GetTaskInfoUseCase::new(db).load(task_id)?;
    let todos = format_todos(&snapshot.todos, &snapshot.worktrees, &snapshot.scraps)?;
    Ok(templates.render(
        "partials/todo_list.html",
        serde_json::json!({ "todos": todos }),
    )?)
}

/// Main dashboard page
pub async fn index(State(state): State<WebState>) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = match db.get_current_task_id()? {
        Some(id) => id,
        None => {
            let html = state.templates.render(
                "index.html",
                serde_json::json!({
                    "task": null,
                    "todos": [],
                    "scraps": [],
                    "links": [],
                    "repos": [],
                    "worktrees": [],
                }),
            )?;
            return Ok(Html(html));
        }
    };

    let context = view::build_template_context(&db, current_task_id)?;
    let html = state.templates.render("index.html", context)?;
    Ok(Html(html))
}

/// JSON API endpoint for status data
pub async fn api_status(State(state): State<WebState>) -> Result<Json<StatusResponse>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = match db.get_current_task_id()? {
        Some(id) => id,
        None => return Ok(Json(StatusResponse::empty())),
    };

    let info = GetTaskInfoUseCase::new(&db);
    let snapshot = info.load(current_task_id)?;
    let response = view::build_api_status(&db, &snapshot)?;

    Ok(Json(response))
}

/// Get description card HTML
pub async fn get_description(State(state): State<WebState>) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;
    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let task_service = TaskService::new(&db);
    let task = task_service.get_task(current_task_id)?;

    let html = state.templates.render(
        "partials/description.html",
        serde_json::json!({
            "task": task,
        }),
    )?;

    Ok(Html(html))
}

/// Get ticket card HTML
pub async fn get_ticket(State(state): State<WebState>) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;
    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let task_service = TaskService::new(&db);
    let task = task_service.get_task(current_task_id)?;

    let html = state.templates.render(
        "partials/ticket.html",
        serde_json::json!({
            "task": task,
        }),
    )?;

    Ok(Html(html))
}

/// Get links card HTML
pub async fn get_links(State(state): State<WebState>) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;
    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let link_service = LinkService::new(&db);
    let links = link_service.list_links(current_task_id)?;

    let html = state.templates.render(
        "partials/links.html",
        serde_json::json!({
            "links": links,
        }),
    )?;

    Ok(Html(html))
}

/// Get repos card HTML
pub async fn get_repos(State(state): State<WebState>) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;
    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let repo_service = RepoService::new(&db);
    let repos = repo_service.list_repos(current_task_id)?;

    let html = state.templates.render(
        "partials/repos.html",
        serde_json::json!({
            "repos": repos,
        }),
    )?;

    Ok(Html(html))
}

/// Get todos card HTML
pub async fn get_todos(State(state): State<WebState>) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;
    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;
    let html = render_todo_list_html(&state.templates, &db, current_task_id)?;
    Ok(Html(html))
}

/// Get scraps card HTML
pub async fn get_scraps(State(state): State<WebState>) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;
    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let scrap_service = ScrapService::new(&db);
    let scraps = scrap_service.list_scraps(current_task_id)?;

    let html = state.templates.render(
        "partials/scrap_list.html",
        serde_json::json!({
            "scraps": format_scraps(&scraps),
        }),
    )?;

    Ok(Html(html))
}

/// Add a new todo
pub async fn add_todo(
    State(state): State<WebState>,
    Form(form): Form<AddTodoForm>,
) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    if form.create_worktree {
        return Err(TrackError::WorktreeFlagRemoved.into());
    }

    let todo_service = TodoService::new(&db);
    let _todo = todo_service.add_todo(
        current_task_id,
        &form.content,
        crate::models::TodoAddOptions::from_flags(false, form.no_workspace),
    )?;

    // Broadcast SSE event
    state.app.broadcast(SseEvent::Todos);

    let html = render_todo_list_html(&state.templates, &db, current_task_id)?;
    Ok(Html(html))
}

/// Update todo status
pub async fn update_todo_status(
    State(state): State<WebState>,
    Path((todo_index, new_status)): Path<(i64, String)>,
) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let todo_service = TodoService::new(&db);
    let todo = todo_service.get_todo_by_index(current_task_id, todo_index)?;

    if new_status.as_str() == TodoStatus::PENDING {
        todo_service.update_status(todo.id, &new_status)?;
    } else {
        let action = crate::models::TodoAction::from_web_route(&new_status)?;
        ApplyTodoActionUseCase::new(&db).execute(current_task_id, todo_index, action)?;
    }

    // Broadcast SSE event
    state.app.broadcast(SseEvent::Todos);

    let html = render_todo_list_html(&state.templates, &db, current_task_id)?;
    Ok(Html(html))
}

/// Delete a todo by task-scoped index
pub async fn delete_todo(
    State(state): State<WebState>,
    Path(todo_index): Path<i64>,
) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let todo_service = TodoService::new(&db);
    let todo = todo_service.get_todo_by_index(current_task_id, todo_index)?;
    todo_service.delete_todo(todo.id)?;

    // Broadcast SSE event
    state.app.broadcast(SseEvent::Todos);

    let html = render_todo_list_html(&state.templates, &db, current_task_id)?;
    Ok(Html(html))
}

/// Move a todo to the front (make it the next todo to work on)
pub async fn move_todo_to_next(
    State(state): State<WebState>,
    Path(todo_index): Path<i64>,
) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let todo_service = TodoService::new(&db);
    todo_service.move_to_next(current_task_id, todo_index)?;

    // Broadcast SSE event
    state.app.broadcast(SseEvent::Todos);

    let html = render_todo_list_html(&state.templates, &db, current_task_id)?;
    Ok(Html(html))
}

/// Add a new scrap
pub async fn add_scrap(
    State(state): State<WebState>,
    Form(form): Form<AddScrapForm>,
) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let scrap_service = ScrapService::new(&db);
    let _scrap = scrap_service.add_scrap(current_task_id, &form.content)?;

    // Broadcast SSE event
    state.app.broadcast(SseEvent::Scraps);

    // Return updated scrap list partial
    let scraps = scrap_service.list_scraps(current_task_id)?;
    let html = state.templates.render(
        "partials/scrap_list.html",
        serde_json::json!({
            "scraps": format_scraps(&scraps),
        }),
    )?;

    Ok(Html(html))
}

/// Update task description
pub async fn update_description(
    State(state): State<WebState>,
    Form(form): Form<UpdateDescriptionForm>,
) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let task_service = TaskService::new(&db);
    task_service.set_description(current_task_id, &form.description)?;

    // Get updated task
    let task = task_service.get_task(current_task_id)?;

    // Broadcast SSE event
    state.app.broadcast(SseEvent::Description);

    // Return updated description section
    let html = state.templates.render(
        "partials/description.html",
        serde_json::json!({
            "task": task,
        }),
    )?;

    Ok(Html(html))
}

/// Update task ticket
pub async fn update_ticket(
    State(state): State<WebState>,
    Form(form): Form<UpdateTicketForm>,
) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let task_service = TaskService::new(&db);

    // Clean up ticket_url if empty
    let ticket_url = form.ticket_url.filter(|url| !url.trim().is_empty());
    let ticket_url_str = ticket_url.as_deref().unwrap_or("");

    task_service.link_ticket(current_task_id, &form.ticket_id, ticket_url_str)?;

    // Get updated task
    let task = task_service.get_task(current_task_id)?;

    // Broadcast SSE event
    state.app.broadcast(SseEvent::Ticket);

    // Return updated ticket section
    let html = state.templates.render(
        "partials/ticket.html",
        serde_json::json!({
            "task": task,
        }),
    )?;

    Ok(Html(html))
}

/// Add a new link
pub async fn add_link(
    State(state): State<WebState>,
    Form(form): Form<AddLinkForm>,
) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let link_service = LinkService::new(&db);

    // Clean up title if empty
    let title = form.title.filter(|t| !t.trim().is_empty());

    link_service.add_link(current_task_id, &form.url, title.as_deref())?;

    // Broadcast SSE event
    state.app.broadcast(SseEvent::Links);

    // Return updated links list partial
    let links = link_service.list_links(current_task_id)?;
    let html = state.templates.render(
        "partials/links.html",
        serde_json::json!({
            "links": links,
        }),
    )?;

    Ok(Html(html))
}

/// Delete a link by task-scoped index
pub async fn delete_link(
    State(state): State<WebState>,
    Path(link_index): Path<i64>,
) -> Result<Html<String>, AppError> {
    let db = state.app.db.lock().await;

    let current_task_id = db.get_current_task_id()?.ok_or(TrackError::NoActiveTask)?;

    let link_service = LinkService::new(&db);
    let links = link_service.list_links(current_task_id)?;

    // Find link by task_index
    let link = links
        .iter()
        .find(|l| l.task_index == link_index)
        .ok_or(TrackError::LinkNotFound(link_index))?;

    // Delete link via service
    link_service.delete_link(link.id)?;

    // Broadcast SSE event
    state.app.broadcast(SseEvent::Links);

    // Return updated links list partial
    let links = link_service.list_links(current_task_id)?;
    let html = state.templates.render(
        "partials/links.html",
        serde_json::json!({
            "links": links,
        }),
    )?;

    Ok(Html(html))
}