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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use crate::app::{App, BoardFocus, DialogMode};
use crate::dialog::{handle_dialog_input, DialogAction};
use crossterm::event::KeyCode;
use kanban_domain::FieldUpdate;
/// Context for handling different types of prefix dialogs
enum PrefixDialogContext {
/// Board-level sprint prefix
BoardSprint,
/// Sprint-level sprint prefix override
Sprint,
/// Sprint-level card prefix override
SprintCard,
}
impl App {
pub fn handle_create_board_dialog(&mut self, key_code: KeyCode) {
match handle_dialog_input(&mut self.input, key_code, false) {
DialogAction::Confirm => {
self.create_board();
self.pop_mode();
self.input.clear();
}
DialogAction::Cancel => {
self.pop_mode();
self.input.clear();
}
DialogAction::None => {}
}
}
pub fn handle_create_card_dialog(&mut self, key_code: KeyCode) {
// Enter always submits and Tab always toggles focus, regardless of
// which sub-field currently has focus.
match key_code {
KeyCode::Enter => {
self.create_card();
self.pop_mode();
self.input.clear();
self.dialog_input.create_card_sprint_picker.clear();
self.dialog_input.reset_create_card_focus();
return;
}
KeyCode::Tab => {
self.dialog_input.toggle_create_card_focus();
return;
}
_ => {}
}
if self.dialog_input.create_card_focus_is_title() {
// Title focus: Down/Esc drop focus into the sprint picker so the
// visual cursor moves out of the text input; all other keys edit
// the title.
match key_code {
KeyCode::Down | KeyCode::Esc => {
self.dialog_input.toggle_create_card_focus();
}
_ => {
handle_dialog_input(&mut self.input, key_code, false);
}
}
return;
}
// Sprint focus: Esc closes the dialog, Up/Down navigate the picker,
// everything else is ignored so a stray keystroke does not modify
// the title behind the user's back.
if matches!(key_code, KeyCode::Esc) {
self.pop_mode();
self.input.clear();
self.dialog_input.create_card_sprint_picker.clear();
self.dialog_input.reset_create_card_focus();
return;
}
if let Some(idx) = self.selection.active_board_index {
if let Some(board) = self.model.boards().get(idx) {
let now = chrono::Utc::now();
self.dialog_input.create_card_sprint_picker.handle_key(
key_code,
self.model.sprints(),
board,
now,
);
}
}
}
pub fn handle_create_sprint_dialog(&mut self, key_code: KeyCode) {
match handle_dialog_input(&mut self.input, key_code, true) {
DialogAction::Confirm => {
self.create_sprint();
self.pop_mode();
self.focus.board_focus = BoardFocus::Sprints;
self.input.clear();
}
DialogAction::Cancel => {
self.pop_mode();
self.focus.board_focus = BoardFocus::Sprints;
self.input.clear();
}
DialogAction::None => {}
}
}
pub fn handle_rename_board_dialog(&mut self, key_code: KeyCode) {
match handle_dialog_input(&mut self.input, key_code, false) {
DialogAction::Confirm => {
self.rename_board();
self.pop_mode();
self.input.clear();
}
DialogAction::Cancel => {
self.pop_mode();
self.input.clear();
}
DialogAction::None => {}
}
}
pub fn handle_export_board_dialog(&mut self, key_code: KeyCode) {
match handle_dialog_input(&mut self.input, key_code, false) {
DialogAction::Confirm => {
if let Err(e) = self.export_board_with_filename() {
tracing::error!("Failed to export board: {}", e);
self.set_error(format!("Failed to export board: {}", e));
}
self.pop_mode();
self.input.clear();
}
DialogAction::Cancel => {
self.pop_mode();
self.input.clear();
}
DialogAction::None => {}
}
}
pub fn handle_export_all_dialog(&mut self, key_code: KeyCode) {
match handle_dialog_input(&mut self.input, key_code, false) {
DialogAction::Confirm => {
if let Err(e) = self.export_all_boards_with_filename() {
tracing::error!("Failed to export all boards: {}", e);
self.set_error(format!("Failed to export all boards: {}", e));
}
self.pop_mode();
self.input.clear();
}
DialogAction::Cancel => {
self.pop_mode();
self.input.clear();
}
DialogAction::None => {}
}
}
pub fn handle_set_card_points_dialog(&mut self, key_code: KeyCode) -> bool {
match handle_dialog_input(&mut self.input, key_code, true) {
DialogAction::Confirm => {
let input_str = self.input.as_str().trim();
let points = if input_str.is_empty() {
None
} else if let Ok(p) = input_str.parse::<u8>() {
if (1..=5).contains(&p) {
Some(p)
} else {
tracing::error!("Points must be between 1-5");
self.pop_mode();
self.input.clear();
return false;
}
} else {
tracing::error!("Invalid points value");
self.pop_mode();
self.input.clear();
return false;
};
let card_id = self
.selection
.active_card_id
.and_then(|id| self.model.card(id))
.map(|c| c.id)
.or_else(|| self.get_selected_card_in_context().map(|c| c.id));
if let Some(card_id) = card_id {
let cmd = kanban_domain::commands::Command::Card(
kanban_domain::commands::CardCommand::Update(
kanban_domain::commands::UpdateCard {
card_id,
updates: kanban_domain::CardUpdate {
points: points.into(),
..Default::default()
},
},
),
);
if let Err(e) = self.execute_command(cmd) {
tracing::error!("Failed to set card points: {}", e);
self.set_error(format!("Failed to set card points: {}", e));
} else {
tracing::info!("Set points to: {:?}", points);
}
}
self.pop_mode();
self.input.clear();
false
}
DialogAction::Cancel => {
self.pop_mode();
self.input.clear();
false
}
DialogAction::None => false,
}
}
/// Generic handler for prefix dialogs that handles common validation and state management
fn handle_prefix_dialog_impl(&mut self, key_code: KeyCode, context: PrefixDialogContext) {
match handle_dialog_input(&mut self.input, key_code, true) {
DialogAction::Confirm => {
let prefix_str = self.input.as_str().trim().to_string();
if prefix_str.is_empty() {
match context {
PrefixDialogContext::BoardSprint => {
if let Some(board_idx) = self.selection.board.get() {
if let Some(board_id) =
self.model.boards().get(board_idx).map(|b| b.id)
{
let cmd = kanban_domain::commands::Command::Board(
kanban_domain::commands::BoardCommand::Update(
kanban_domain::commands::UpdateBoard {
board_id,
updates: kanban_domain::BoardUpdate {
sprint_prefix: FieldUpdate::Clear,
..Default::default()
},
},
),
);
if let Err(e) = self.execute_command(cmd) {
tracing::error!("Failed to clear sprint prefix: {}", e);
self.set_error(format!(
"Failed to clear sprint prefix: {}",
e
));
} else {
tracing::info!("Cleared sprint prefix");
}
}
}
}
PrefixDialogContext::Sprint => {
if let Some(sprint_idx) = self.selection.active_sprint_index {
if let Some(sprint_id) =
self.model.sprints().get(sprint_idx).map(|s| s.id)
{
let cmd = kanban_domain::commands::Command::Sprint(
kanban_domain::commands::SprintCommand::Update(
kanban_domain::commands::UpdateSprint {
sprint_id,
updates: kanban_domain::SprintUpdate {
prefix: FieldUpdate::Clear,
..Default::default()
},
},
),
);
if let Err(e) = self.execute_command(cmd) {
tracing::error!("Failed to clear sprint prefix: {}", e);
self.set_error(format!(
"Failed to clear sprint prefix: {}",
e
));
} else {
tracing::info!("Cleared sprint prefix");
}
}
}
}
PrefixDialogContext::SprintCard => {
if let Some(sprint_idx) = self.selection.active_sprint_index {
if let Some(sprint_id) =
self.model.sprints().get(sprint_idx).map(|s| s.id)
{
let cmd = kanban_domain::commands::Command::Sprint(
kanban_domain::commands::SprintCommand::Update(
kanban_domain::commands::UpdateSprint {
sprint_id,
updates: kanban_domain::SprintUpdate {
card_prefix: FieldUpdate::Clear,
..Default::default()
},
},
),
);
if let Err(e) = self.execute_command(cmd) {
tracing::error!(
"Failed to clear sprint card prefix override: {}",
e
);
self.set_error(format!(
"Failed to clear sprint card prefix override: {}",
e
));
} else {
tracing::info!("Cleared sprint card prefix override");
}
}
}
}
}
} else if kanban_core::validate_branch_prefix(&prefix_str) {
match context {
PrefixDialogContext::BoardSprint => {
if let Some(board_idx) = self.selection.board.get() {
if let Some(board_id) =
self.model.boards().get(board_idx).map(|b| b.id)
{
let cmd = kanban_domain::commands::Command::Board(
kanban_domain::commands::BoardCommand::Update(
kanban_domain::commands::UpdateBoard {
board_id,
updates: kanban_domain::BoardUpdate {
sprint_prefix: FieldUpdate::Set(
prefix_str.clone(),
),
..Default::default()
},
},
),
);
if let Err(e) = self.execute_command(cmd) {
tracing::error!("Failed to set sprint prefix: {}", e);
self.set_error(format!(
"Failed to set sprint prefix: {}",
e
));
} else {
tracing::info!("Set sprint prefix to: {}", prefix_str);
}
}
}
}
PrefixDialogContext::Sprint => {
if let Some(sprint_idx) = self.selection.active_sprint_index {
if let Some(sprint_id) =
self.model.sprints().get(sprint_idx).map(|s| s.id)
{
let cmd = kanban_domain::commands::Command::Sprint(
kanban_domain::commands::SprintCommand::Update(
kanban_domain::commands::UpdateSprint {
sprint_id,
updates: kanban_domain::SprintUpdate {
prefix: FieldUpdate::Set(prefix_str.clone()),
..Default::default()
},
},
),
);
if let Err(e) = self.execute_command(cmd) {
tracing::error!("Failed to set sprint prefix: {}", e);
self.set_error(format!(
"Failed to set sprint prefix: {}",
e
));
} else {
tracing::info!("Set sprint prefix to: {}", prefix_str);
}
}
}
}
PrefixDialogContext::SprintCard => {
if let Some(sprint_idx) = self.selection.active_sprint_index {
if let Some(sprint_id) =
self.model.sprints().get(sprint_idx).map(|s| s.id)
{
let cmd = kanban_domain::commands::Command::Sprint(
kanban_domain::commands::SprintCommand::Update(
kanban_domain::commands::UpdateSprint {
sprint_id,
updates: kanban_domain::SprintUpdate {
card_prefix: FieldUpdate::Set(
prefix_str.clone(),
),
..Default::default()
},
},
),
);
if let Err(e) = self.execute_command(cmd) {
tracing::error!(
"Failed to set sprint card prefix override: {}",
e
);
self.set_error(format!(
"Failed to set sprint card prefix override: {}",
e
));
} else {
tracing::info!(
"Set sprint card prefix override to: {}",
prefix_str
);
}
}
}
}
}
} else {
tracing::error!("Invalid prefix: use alphanumeric, hyphens, underscores only");
}
self.pop_mode();
self.input.clear();
}
DialogAction::Cancel => {
self.pop_mode();
self.input.clear();
}
DialogAction::None => {}
}
}
pub fn handle_set_branch_prefix_dialog(&mut self, key_code: KeyCode) {
self.handle_prefix_dialog_impl(key_code, PrefixDialogContext::BoardSprint);
}
pub fn handle_set_sprint_prefix_dialog(&mut self, key_code: KeyCode) {
self.handle_prefix_dialog_impl(key_code, PrefixDialogContext::Sprint);
}
pub fn handle_set_sprint_card_prefix_dialog(&mut self, key_code: KeyCode) {
self.handle_prefix_dialog_impl(key_code, PrefixDialogContext::SprintCard);
}
pub fn handle_confirm_sprint_prefix_collision_popup(&mut self, key_code: KeyCode) {
use crossterm::event::KeyCode;
match key_code {
KeyCode::Esc => {
self.pop_mode();
}
KeyCode::Enter | KeyCode::Char('y') => {
// User confirmed they want to continue with the colliding prefix
// The actual prefix application should happen before this mode is entered
self.pop_mode();
}
KeyCode::Char('n') | KeyCode::Char('N') => {
// User declined, go back to prefix dialog
self.pop_mode();
self.open_dialog(DialogMode::SetSprintPrefix);
}
_ => {}
}
}
pub fn handle_conflict_resolution_popup(&mut self, key_code: KeyCode) {
use crossterm::event::KeyCode;
match key_code {
KeyCode::Char('o') | KeyCode::Char('O') => {
// Keep our changes - force overwrite
// Store the action to be processed in the event loop
self.pending_key = Some('o');
self.pop_mode();
}
KeyCode::Char('t') | KeyCode::Char('T') => {
// Take their changes - reload from disk
self.pending_key = Some('t');
self.pop_mode();
}
KeyCode::Esc => {
// Retry later - just go back to previous mode
self.ctx.clear_conflict();
self.pop_mode();
}
_ => {}
}
}
pub fn handle_external_change_detected_popup(&mut self, key_code: KeyCode) {
use crossterm::event::KeyCode;
match key_code {
KeyCode::Char('r') | KeyCode::Char('R') => {
// Reload from external file - discard local changes
self.pending_key = Some('r');
self.pop_mode();
}
KeyCode::Char('k') | KeyCode::Char('K') => {
// Keep local changes - continue editing
self.pop_mode();
}
KeyCode::Esc => {
// Dismiss dialog - continue with current state
self.pop_mode();
}
_ => {}
}
}
}
#[cfg(test)]
mod tests {
use crate::test_helpers::setup_reload_resort_fixture;
use crate::App;
use crossterm::event::KeyCode;
#[test]
fn test_handle_set_card_points_dialog_after_reload_resort_updates_originally_selected_card_points(
) {
let mut app = App::test_default();
let fx = setup_reload_resort_fixture(&mut app);
app.input.set("3".to_string());
app.handle_set_card_points_dialog(KeyCode::Enter);
let cards = app.ctx.data_store().list_all_cards().unwrap();
let a_card = cards.iter().find(|c| c.id == fx.a_id).expect("A exists");
let p_card = cards.iter().find(|c| c.id == fx.p_id).expect("P exists");
assert_eq!(
a_card.points,
Some(3),
"points dialog must set points on A (the active card by id), not on the wrong card at A's stale index"
);
assert_eq!(
p_card.points, None,
"points dialog must leave P's points unchanged when A is active"
);
}
}