1use ratatui::{
2 layout::{Constraint, Direction, Layout, Rect},
3 style::{Color, Modifier, Style},
4 text::{Line, Span},
5 widgets::{Block, BorderType, Borders, Clear, List, ListItem, Paragraph, Wrap},
6 Frame,
7};
8
9use crate::app::{App, DeleteTarget, Modal};
10use crate::format::format_bytes;
11
12pub fn render(f: &mut Frame, app: &App) {
13 let modal = match &app.modal {
14 Some(m) => m,
15 None => return,
16 };
17
18 match modal {
19 Modal::HostSwitcher {
20 cursor,
21 custom_input,
22 entering_custom,
23 } => render_host_switcher(f, app, *cursor, custom_input, *entering_custom),
24 Modal::Search { input } => render_search(f, input),
25 Modal::QuickAppendPrompt {
26 content_input,
27 section_input,
28 as_task,
29 focus_field,
30 } => render_quick_append(f, content_input, section_input, *as_task, *focus_field),
31 Modal::ProjectForm {
32 id,
33 name_input,
34 desc_input,
35 focus_desc,
36 } => render_project_form(f, id.is_some(), name_input, desc_input, *focus_desc),
37 Modal::PromptForm {
38 id,
39 title_input,
40 project_choice_idx,
41 pinned,
42 focus_field,
43 } => render_prompt_form(
44 f,
45 app,
46 id.is_some(),
47 title_input,
48 *project_choice_idx,
49 *pinned,
50 *focus_field,
51 ),
52 Modal::ConfirmDelete { target } => render_confirm_delete(f, target),
53 Modal::Attachments {
54 cursor,
55 upload_input,
56 dest_input,
57 } => render_attachments(
58 f,
59 app,
60 *cursor,
61 upload_input.as_deref(),
62 dest_input.as_deref(),
63 ),
64 Modal::Help => render_help(f, app.config.allow_host_switcher),
65 }
66}
67
68fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
69 let popup_layout = Layout::default()
70 .direction(Direction::Vertical)
71 .constraints([
72 Constraint::Percentage((100 - percent_y) / 2),
73 Constraint::Percentage(percent_y),
74 Constraint::Percentage((100 - percent_y) / 2),
75 ])
76 .split(r);
77
78 Layout::default()
79 .direction(Direction::Horizontal)
80 .constraints([
81 Constraint::Percentage((100 - percent_x) / 2),
82 Constraint::Percentage(percent_x),
83 Constraint::Percentage((100 - percent_x) / 2),
84 ])
85 .split(popup_layout[1])[1]
86}
87
88fn render_host_switcher(
89 f: &mut Frame,
90 app: &App,
91 cursor: usize,
92 custom_input: &str,
93 entering_custom: bool,
94) {
95 let area = centered_rect(60, 45, f.area());
96 f.render_widget(Clear, area);
97
98 let block = Block::default()
99 .title(" Switch Devbook Host URL ")
100 .borders(Borders::ALL)
101 .border_type(BorderType::Rounded)
102 .border_style(Style::default().fg(Color::Cyan));
103
104 let inner = block.inner(area);
105 f.render_widget(block, area);
106
107 let mut lines = Vec::new();
108 lines.push(Line::from(Span::styled(
109 format!("Current Active Host: {}", app.config.base_url),
110 Style::default()
111 .fg(Color::LightCyan)
112 .add_modifier(Modifier::BOLD),
113 )));
114 lines.push(Line::from(""));
115 lines.push(Line::from(Span::styled(
116 "Preset Hosts:",
117 Style::default().fg(Color::Gray),
118 )));
119
120 for (i, preset) in app.preset_hosts.iter().enumerate() {
121 let is_sel = !entering_custom && cursor == i;
122 let prefix = if is_sel { " ▶ " } else { " " };
123 let style = if is_sel {
124 Style::default()
125 .fg(Color::Cyan)
126 .add_modifier(Modifier::BOLD)
127 } else if *preset == app.config.base_url {
128 Style::default().fg(Color::Green)
129 } else {
130 Style::default().fg(Color::White)
131 };
132 let active_mark = if *preset == app.config.base_url {
133 " (active)"
134 } else {
135 ""
136 };
137 lines.push(Line::from(vec![
138 Span::styled(prefix, style),
139 Span::styled(preset.as_str(), style),
140 Span::styled(active_mark, Style::default().fg(Color::Green)),
141 ]));
142 }
143
144 lines.push(Line::from(""));
145 let custom_sel = entering_custom || cursor == app.preset_hosts.len();
146 let custom_prefix = if custom_sel {
147 " ▶ Custom URL: "
148 } else {
149 " Custom URL: "
150 };
151 let custom_style = if custom_sel {
152 Style::default()
153 .fg(Color::Cyan)
154 .add_modifier(Modifier::BOLD)
155 } else {
156 Style::default().fg(Color::Gray)
157 };
158
159 lines.push(Line::from(vec![
160 Span::styled(custom_prefix, custom_style),
161 Span::styled(
162 if custom_input.is_empty() && !entering_custom {
163 "(type or press c to enter)"
164 } else {
165 custom_input
166 },
167 if entering_custom {
168 Style::default()
169 .fg(Color::White)
170 .bg(Color::DarkGray)
171 } else {
172 Style::default().fg(Color::Gray)
173 },
174 ),
175 ]));
176
177 lines.push(Line::from(""));
178 lines.push(Line::from(Span::styled(
179 "Keys: j/k: select preset | Enter: switch | c: custom input | Esc: cancel",
180 Style::default().fg(Color::DarkGray),
181 )));
182
183 f.render_widget(Paragraph::new(lines).wrap(Wrap { trim: false }), inner);
184}
185
186fn render_search(f: &mut Frame, input: &str) {
187 let area = centered_rect(60, 20, f.area());
188 f.render_widget(Clear, area);
189
190 let block = Block::default()
191 .title(" Search Prompts ")
192 .borders(Borders::ALL)
193 .border_type(BorderType::Rounded)
194 .border_style(Style::default().fg(Color::Cyan));
195
196 let inner = block.inner(area);
197 f.render_widget(block, area);
198
199 let lines = vec![
200 Line::from(Span::styled(
201 "Type search query (matches title or prompt body):",
202 Style::default().fg(Color::Gray),
203 )),
204 Line::from(""),
205 Line::from(vec![
206 Span::styled(" Query: ", Style::default().fg(Color::LightCyan)),
207 Span::styled(
208 format!("{}_", input),
209 Style::default()
210 .fg(Color::White)
211 .bg(Color::DarkGray)
212 .add_modifier(Modifier::BOLD),
213 ),
214 ]),
215 Line::from(""),
216 Line::from(Span::styled(
217 "Press Enter to apply filter, Esc to cancel / clear",
218 Style::default().fg(Color::DarkGray),
219 )),
220 ];
221
222 f.render_widget(Paragraph::new(lines), inner);
223}
224
225fn render_quick_append(
226 f: &mut Frame,
227 content_input: &str,
228 section_input: &str,
229 as_task: bool,
230 focus_field: usize,
231) {
232 let area = centered_rect(65, 45, f.area());
233 f.render_widget(Clear, area);
234
235 let block = Block::default()
236 .title(" Quick Append to Prompt ")
237 .borders(Borders::ALL)
238 .border_type(BorderType::Rounded)
239 .border_style(Style::default().fg(Color::Cyan));
240
241 let inner = block.inner(area);
242 f.render_widget(block, area);
243
244 let sec_style = if focus_field == 0 {
245 Style::default()
246 .fg(Color::White)
247 .bg(Color::DarkGray)
248 .add_modifier(Modifier::BOLD)
249 } else {
250 Style::default().fg(Color::Gray)
251 };
252
253 let content_style = if focus_field == 1 {
254 Style::default()
255 .fg(Color::White)
256 .bg(Color::DarkGray)
257 .add_modifier(Modifier::BOLD)
258 } else {
259 Style::default().fg(Color::Gray)
260 };
261
262 let task_mode_str = if as_task {
263 "[x] Append as checklist task (- [ ] ...)"
264 } else {
265 "[ ] Append as normal Markdown prose"
266 };
267
268 let task_style = if as_task {
269 Style::default()
270 .fg(Color::Green)
271 .add_modifier(Modifier::BOLD)
272 } else {
273 Style::default().fg(Color::Gray)
274 };
275
276 let lines = vec![
277 Line::from(Span::styled(
278 "Optional Section Heading (e.g. 'Instructions' or 'Examples'):",
279 Style::default().fg(Color::LightCyan),
280 )),
281 Line::from(vec![
282 Span::styled(" Section: ## ", Style::default().fg(Color::Cyan)),
283 Span::styled(format!("{}_", section_input), sec_style),
284 ]),
285 Line::from(""),
286 Line::from(Span::styled(
287 "Content to append:",
288 Style::default().fg(Color::LightCyan),
289 )),
290 Line::from(vec![
291 Span::styled(" Text: ", Style::default().fg(Color::Cyan)),
292 Span::styled(format!("{}_", content_input), content_style),
293 ]),
294 Line::from(""),
295 Line::from(Span::styled(task_mode_str, task_style)),
296 Line::from(""),
297 Line::from(Span::styled(
298 "Keys: Tab: switch field | Ctrl+T: toggle task mode | Enter: append | Esc: cancel",
299 Style::default().fg(Color::DarkGray),
300 )),
301 ];
302
303 f.render_widget(Paragraph::new(lines).wrap(Wrap { trim: false }), inner);
304}
305
306fn render_project_form(
307 f: &mut Frame,
308 is_edit: bool,
309 name_input: &str,
310 desc_input: &str,
311 focus_desc: bool,
312) {
313 let area = centered_rect(55, 35, f.area());
314 f.render_widget(Clear, area);
315
316 let title = if is_edit {
317 " Edit Project "
318 } else {
319 " Create New Project "
320 };
321
322 let block = Block::default()
323 .title(title)
324 .borders(Borders::ALL)
325 .border_type(BorderType::Rounded)
326 .border_style(Style::default().fg(Color::Cyan));
327
328 let inner = block.inner(area);
329 f.render_widget(block, area);
330
331 let name_style = if !focus_desc {
332 Style::default()
333 .fg(Color::White)
334 .bg(Color::DarkGray)
335 .add_modifier(Modifier::BOLD)
336 } else {
337 Style::default().fg(Color::Gray)
338 };
339
340 let desc_style = if focus_desc {
341 Style::default()
342 .fg(Color::White)
343 .bg(Color::DarkGray)
344 .add_modifier(Modifier::BOLD)
345 } else {
346 Style::default().fg(Color::Gray)
347 };
348
349 let lines = vec![
350 Line::from(Span::styled(
351 "Project Name (required):",
352 Style::default().fg(Color::LightCyan),
353 )),
354 Line::from(vec![
355 Span::styled(" Name: ", Style::default().fg(Color::Cyan)),
356 Span::styled(format!("{}_", name_input), name_style),
357 ]),
358 Line::from(""),
359 Line::from(Span::styled(
360 "Description (optional):",
361 Style::default().fg(Color::LightCyan),
362 )),
363 Line::from(vec![
364 Span::styled(" Desc: ", Style::default().fg(Color::Cyan)),
365 Span::styled(format!("{}_", desc_input), desc_style),
366 ]),
367 Line::from(""),
368 Line::from(Span::styled(
369 "Keys: Tab: switch field | Enter: save | Esc: cancel",
370 Style::default().fg(Color::DarkGray),
371 )),
372 ];
373
374 f.render_widget(Paragraph::new(lines).wrap(Wrap { trim: false }), inner);
375}
376
377fn render_prompt_form(
378 f: &mut Frame,
379 app: &App,
380 is_edit: bool,
381 title_input: &str,
382 project_choice_idx: usize,
383 pinned: bool,
384 focus_field: usize,
385) {
386 let area = centered_rect(65, 45, f.area());
387 f.render_widget(Clear, area);
388
389 let modal_title = if is_edit {
390 " Edit Prompt Metadata "
391 } else {
392 " Create New Prompt "
393 };
394
395 let block = Block::default()
396 .title(modal_title)
397 .borders(Borders::ALL)
398 .border_type(BorderType::Rounded)
399 .border_style(Style::default().fg(Color::Cyan));
400
401 let inner = block.inner(area);
402 f.render_widget(block, area);
403
404 let title_style = if focus_field == 0 {
405 Style::default()
406 .fg(Color::White)
407 .bg(Color::DarkGray)
408 .add_modifier(Modifier::BOLD)
409 } else {
410 Style::default().fg(Color::Gray)
411 };
412
413 let proj_label = if project_choice_idx == 0 {
414 "(None — Standalone scratchpad)".to_string()
415 } else if let Some(p) = app.projects.get(project_choice_idx - 1) {
416 format!("{} (ID: {})", p.name, p.id)
417 } else {
418 "(None)".to_string()
419 };
420
421 let proj_style = if focus_field == 1 {
422 Style::default()
423 .fg(Color::Cyan)
424 .add_modifier(Modifier::BOLD)
425 } else {
426 Style::default().fg(Color::Gray)
427 };
428
429 let pin_str = if pinned {
430 "[x] Pinned (★)"
431 } else {
432 "[ ] Not Pinned"
433 };
434
435 let pin_style = if focus_field == 2 {
436 Style::default()
437 .fg(Color::LightCyan)
438 .add_modifier(Modifier::BOLD)
439 } else {
440 Style::default().fg(Color::Gray)
441 };
442
443 let lines = vec![
444 Line::from(Span::styled(
445 "Prompt Title (required):",
446 Style::default().fg(Color::LightCyan),
447 )),
448 Line::from(vec![
449 Span::styled(" Title: ", Style::default().fg(Color::Cyan)),
450 Span::styled(format!("{}_", title_input), title_style),
451 ]),
452 Line::from(""),
453 Line::from(Span::styled(
454 "Assigned Project (Left/Right to choose):",
455 Style::default().fg(Color::LightCyan),
456 )),
457 Line::from(vec![
458 Span::styled(" Project: ", Style::default().fg(Color::Cyan)),
459 Span::styled(format!("◀ {} ▶", proj_label), proj_style),
460 ]),
461 Line::from(""),
462 Line::from(vec![
463 Span::styled(" Pinned: ", Style::default().fg(Color::Cyan)),
464 Span::styled(pin_str, pin_style),
465 ]),
466 Line::from(""),
467 Line::from(Span::styled(
468 "Keys: Tab/Shift-Tab: navigate | h/l: select project | Space: toggle pin | Enter: save | Esc: cancel",
469 Style::default().fg(Color::DarkGray),
470 )),
471 ];
472
473 f.render_widget(Paragraph::new(lines).wrap(Wrap { trim: false }), inner);
474}
475
476fn render_confirm_delete(f: &mut Frame, target: &DeleteTarget) {
477 let area = centered_rect(50, 25, f.area());
478 f.render_widget(Clear, area);
479
480 let (type_name, obj_name) = match target {
481 DeleteTarget::Project(p) => ("Project", p.name.as_str()),
482 DeleteTarget::Prompt(p) => ("Prompt", p.title.as_str()),
483 DeleteTarget::Attachment { filename, .. } => ("Attachment", filename.as_str()),
484 };
485
486 let block = Block::default()
487 .title(format!(" Confirm Delete {} ", type_name))
488 .borders(Borders::ALL)
489 .border_type(BorderType::Rounded)
490 .border_style(Style::default().fg(Color::Red));
491
492 let inner = block.inner(area);
493 f.render_widget(block, area);
494
495 let lines = vec![
496 Line::from(Span::styled(
497 format!("Are you sure you want to delete this {}?", type_name),
498 Style::default().fg(Color::White),
499 )),
500 Line::from(""),
501 Line::from(Span::styled(
502 format!(" ▶ \"{}\"", obj_name),
503 Style::default()
504 .fg(Color::Red)
505 .add_modifier(Modifier::BOLD),
506 )),
507 Line::from(""),
508 Line::from(Span::styled(
509 "Press 'y' to confirm delete, 'n' or Esc to cancel",
510 Style::default().fg(Color::DarkGray),
511 )),
512 ];
513
514 f.render_widget(Paragraph::new(lines).wrap(Wrap { trim: false }), inner);
515}
516
517fn render_attachments(
518 f: &mut Frame,
519 app: &App,
520 cursor: usize,
521 upload_input: Option<&str>,
522 dest_input: Option<&str>,
523) {
524 let area = centered_rect(75, 65, f.area());
525 f.render_widget(Clear, area);
526
527 let prompt_title = app
528 .current_prompt
529 .as_ref()
530 .map(|p| p.title.as_str())
531 .unwrap_or("Prompt");
532
533 let block = Block::default()
534 .title(format!(" Attachments Manager — \"{}\" ", prompt_title))
535 .borders(Borders::ALL)
536 .border_type(BorderType::Rounded)
537 .border_style(Style::default().fg(Color::Cyan));
538
539 let inner = block.inner(area);
540 f.render_widget(block, area);
541
542 if let Some(input) = upload_input {
544 let input_lines = vec![
545 Line::from(Span::styled(
546 "Upload File to Prompt Attachments",
547 Style::default()
548 .fg(Color::Cyan)
549 .add_modifier(Modifier::BOLD),
550 )),
551 Line::from(""),
552 Line::from(Span::styled(
553 "Enter absolute or relative path to file:",
554 Style::default().fg(Color::Gray),
555 )),
556 Line::from(vec![
557 Span::styled(" Path: ", Style::default().fg(Color::LightCyan)),
558 Span::styled(
559 format!("{}_", input),
560 Style::default()
561 .fg(Color::White)
562 .bg(Color::DarkGray)
563 .add_modifier(Modifier::BOLD),
564 ),
565 ]),
566 Line::from(""),
567 Line::from(Span::styled(
568 "Press Enter to upload, Esc to cancel",
569 Style::default().fg(Color::DarkGray),
570 )),
571 ];
572 f.render_widget(Paragraph::new(input_lines), inner);
573 return;
574 }
575
576 if let Some(input) = dest_input {
577 let input_lines = vec![
578 Line::from(Span::styled(
579 "Save Attachment to Local Disk",
580 Style::default()
581 .fg(Color::Cyan)
582 .add_modifier(Modifier::BOLD),
583 )),
584 Line::from(""),
585 Line::from(Span::styled(
586 "Enter destination file path:",
587 Style::default().fg(Color::Gray),
588 )),
589 Line::from(vec![
590 Span::styled(" Dest: ", Style::default().fg(Color::LightCyan)),
591 Span::styled(
592 format!("{}_", input),
593 Style::default()
594 .fg(Color::White)
595 .bg(Color::DarkGray)
596 .add_modifier(Modifier::BOLD),
597 ),
598 ]),
599 Line::from(""),
600 Line::from(Span::styled(
601 "Press Enter to download, Esc to cancel",
602 Style::default().fg(Color::DarkGray),
603 )),
604 ];
605 f.render_widget(Paragraph::new(input_lines), inner);
606 return;
607 }
608
609 let mut items = Vec::new();
610 let attachments = app
611 .current_prompt
612 .as_ref()
613 .map(|p| &p.attachments)
614 .cloned()
615 .unwrap_or_default();
616
617 if attachments.is_empty() {
618 items.push(ListItem::new(Line::from(Span::styled(
619 " (no attachments on this prompt — press 'u' to upload a file)",
620 Style::default().fg(Color::DarkGray),
621 ))));
622 } else {
623 for (i, att) in attachments.iter().enumerate() {
624 let is_sel = cursor == i;
625 let prefix = if is_sel { "▶ " } else { " " };
626 let style = if is_sel {
627 Style::default()
628 .fg(Color::Cyan)
629 .add_modifier(Modifier::BOLD)
630 } else {
631 Style::default().fg(Color::White)
632 };
633
634 let size_str = format_bytes(att.byte_size.unwrap_or(0));
635 let line = Line::from(vec![
636 Span::styled(prefix, style),
637 Span::styled(format!("{:<30} ", att.filename), style),
638 Span::styled(
639 format!("{:>10} ", size_str),
640 Style::default().fg(Color::LightCyan),
641 ),
642 Span::styled(
643 att.content_type.as_deref().unwrap_or("unknown"),
644 Style::default().fg(Color::DarkGray),
645 ),
646 ]);
647 items.push(ListItem::new(line));
648 }
649 }
650
651 let layout = Layout::default()
652 .direction(Direction::Vertical)
653 .constraints([Constraint::Min(0), Constraint::Length(2)])
654 .split(inner);
655
656 f.render_widget(List::new(items), layout[0]);
657
658 let footer = Paragraph::new(Line::from(Span::styled(
659 "Keys: j/k: select | u: upload file | s: save to disk | y: copy URL | d: delete | Esc: close",
660 Style::default().fg(Color::DarkGray),
661 )));
662 f.render_widget(footer, layout[1]);
663}
664
665fn render_help(f: &mut Frame, allow_switch: bool) {
666 let area = centered_rect(76, 78, f.area());
667 f.render_widget(Clear, area);
668
669 let block = Block::default()
670 .title(" Devbook-TUI Keyboard Shortcuts & Help ")
671 .borders(Borders::ALL)
672 .border_type(BorderType::Rounded)
673 .border_style(Style::default().fg(Color::Cyan));
674
675 let inner = block.inner(area);
676 f.render_widget(block, area);
677
678 let host_line = if allow_switch {
679 Line::from(" H / u Switch Host URL presets or enter custom URL")
680 } else {
681 Line::from(
682 " H / u [Locked by DEVBOOK_REMOTE_URL] (pass --allow-switch to enable)",
683 )
684 };
685
686 let lines = vec![
687 Line::from(Span::styled(
688 "Navigation & Focus",
689 Style::default()
690 .fg(Color::Cyan)
691 .add_modifier(Modifier::BOLD),
692 )),
693 Line::from(" 1, 2, 3 Direct focus to pane 1 (Sidebar), 2 (Prompts), 3 (Detail)"),
694 Line::from(" Tab / BackTab Cycle focus forward / backward between panes"),
695 Line::from(" h / l Move focus left / right"),
696 Line::from(" j / k Move selection down / up in focused pane"),
697 Line::from(" Enter Enter pane 2 from sidebar, or pane 3 from prompts list"),
698 Line::from(""),
699 Line::from(Span::styled(
700 "Prompt Detail, Markdown & Checklists (Pane 3)",
701 Style::default()
702 .fg(Color::Cyan)
703 .add_modifier(Modifier::BOLD),
704 )),
705 Line::from(" v / m Toggle between Styled Preview and Raw Markdown (line numbers)"),
706 Line::from(" j / k Scroll prompt (or navigate checklist items when present)"),
707 Line::from(" Space / Enter Toggle active checklist item [ ] ⇄ [x] (if prompt has tasks)"),
708 Line::from(" a Quick-append Markdown section, log entry, or task to prompt"),
709 Line::from(" E Open prompt Markdown body in external $EDITOR (nvim/vim/nano)"),
710 Line::from(" J / K Scroll detail document view down / up by 3 lines"),
711 Line::from(" g / G Jump to top / bottom of prompt document"),
712 Line::from(" c / Y Copy entire Markdown body to system clipboard"),
713 Line::from(" y Yank active task text (or prompt title) to system clipboard"),
714 Line::from(""),
715 Line::from(Span::styled(
716 "Workspace Management & Modals",
717 Style::default()
718 .fg(Color::Cyan)
719 .add_modifier(Modifier::BOLD),
720 )),
721 host_line,
722 Line::from(" / Search & filter prompts by title or body in real-time"),
723 Line::from(" n Create New Project (in pane 1) or Prompt (in pane 2/3)"),
724 Line::from(" e Edit metadata of selected Project or Prompt"),
725 Line::from(" d Delete selected Project, Prompt, or Attachment"),
726 Line::from(" p Toggle pinned status of selected Prompt"),
727 Line::from(" K / J Reorder pinned prompts up / down (in Pinned scope)"),
728 Line::from(" A Open attachments manager modal (upload, download, delete)"),
729 Line::from(" r Refresh all data from Devbook server"),
730 Line::from(" ? Open this help dialog"),
731 Line::from(" q Quit application (or Esc to close dialogs)"),
732 ];
733
734 f.render_widget(Paragraph::new(lines).wrap(Wrap { trim: false }), inner);
735}