toss-api 0.1.5

A Vim-inspired TUI and CLI API client for exploring and testing endpoints
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
use crate::tui::app::*;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

pub fn handle_normal_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Char('C') => app.focused_panel = FocusedPanel::Collections,
        KeyCode::Char('R') => {
            app.focus_request_bar();
            app.cursor_position = app.url.len();
        }
        KeyCode::Char('P') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Params;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('H') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Headers;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('U') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Auth;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('B') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Body;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('S') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Scripts;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('E') => app.focused_panel = FocusedPanel::Response,
        KeyCode::Char('T') => app.focused_panel = FocusedPanel::Stats,
        KeyCode::Char('V') => {
            app.left_bottom_tab = crate::tui::app::LeftBottomTab::Environments;
            app.focused_panel = FocusedPanel::Environments;
        }
        KeyCode::Char('A') => {
            app.left_bottom_tab = crate::tui::app::LeftBottomTab::Apis;
            app.focused_panel = FocusedPanel::Apis;
        }
        KeyCode::Char('m') => {
            if app.focused_panel == FocusedPanel::Environments {
                app.toggle_env_mask();
            }
        }

        // gg and G shortcuts
        KeyCode::Char('g') => {
            if app.g_pressed {
                match app.focused_panel {
                    FocusedPanel::Collections => app.selected_collection_index = 0,
                    FocusedPanel::Apis => app.selected_api_index = 0,
                    FocusedPanel::Environments => app.selected_env_index = 0,
                    FocusedPanel::Details => app.property_editor_row = 0,
                    FocusedPanel::Response => app.response_scroll = 0,
                    FocusedPanel::Stats => app.stats_scroll = 0,
                    _ => {}
                }
                app.g_pressed = false;
            } else {
                app.g_pressed = true;
            }
            return;
        }
        KeyCode::Char('G') => match app.focused_panel {
            FocusedPanel::Collections => {
                app.selected_collection_index =
                    app.get_visible_collections().len().saturating_sub(1)
            }
            FocusedPanel::Apis => {
                app.selected_api_index = app.get_visible_items().len().saturating_sub(1)
            }
            FocusedPanel::Environments => {
                app.selected_env_index =
                    app.get_active_collection_env_vars().len().saturating_sub(1)
            }
            FocusedPanel::Details => {
                if let Some(req) = app.get_current_request() {
                    let max_rows = match app.selected_property_tab {
                        PropertyTab::Params => req.params.len(),
                        PropertyTab::Headers => req.headers.len(),
                        PropertyTab::Auth => match req.auth.selected {
                            crate::core::collection::AuthType::None => 0,
                            crate::core::collection::AuthType::Bearer => 1,
                            crate::core::collection::AuthType::Basic => 2,
                            crate::core::collection::AuthType::ApiKey => 3,
                        },
                        PropertyTab::Body => match req.body.selected {
                            crate::core::collection::BodyType::FormData => {
                                req.body.form_data.items.len()
                            }
                            crate::core::collection::BodyType::XWwwFormUrlEncoded => {
                                req.body.x_www_form_urlencoded.items.len()
                            }
                            _ => 0,
                        },
                        _ => 0,
                    };
                    app.property_editor_row = max_rows.saturating_sub(1);
                }
            }
            FocusedPanel::Response => {
                app.response_scroll = 1000;
            }
            FocusedPanel::Stats => {
                app.stats_scroll = 100;
            }
            _ => {}
        },

        // Page Up/Down (Ctrl-u / Ctrl-d)
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            match app.focused_panel {
                FocusedPanel::Collections => {
                    app.selected_collection_index =
                        app.selected_collection_index.saturating_sub(10);
                }
                FocusedPanel::Apis => {
                    app.selected_api_index = app.selected_api_index.saturating_sub(10);
                }
                FocusedPanel::Details => {
                    app.property_editor_row = app.property_editor_row.saturating_sub(10);
                    app.details_scroll = app.details_scroll.saturating_sub(10);
                }
                FocusedPanel::Response => {
                    app.response_scroll = app.response_scroll.saturating_sub(10);
                }
                FocusedPanel::Stats => {
                    app.stats_scroll = app.stats_scroll.saturating_sub(10);
                }
                _ => {}
            }
        }
        KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            match app.focused_panel {
                FocusedPanel::Collections => {
                    let max = app.get_visible_collections().len().saturating_sub(1);
                    app.selected_collection_index = (app.selected_collection_index + 10).min(max);
                }
                FocusedPanel::Apis => {
                    let max = app.get_visible_items().len().saturating_sub(1);
                    app.selected_api_index = (app.selected_api_index + 10).min(max);
                }
                FocusedPanel::Details => {
                    app.property_editor_row += 10; // Capped in render/logic later
                    app.details_scroll = app.details_scroll.saturating_add(10);
                }
                FocusedPanel::Response => {
                    app.response_scroll = app.response_scroll.saturating_add(10);
                }
                FocusedPanel::Stats => {
                    app.stats_scroll = app.stats_scroll.saturating_add(10);
                }
                _ => {}
            }
        }

        KeyCode::Char('q') => app.input_mode = InputMode::ConfirmQuit,
        KeyCode::Char('?') => app.input_mode = InputMode::Help,
        KeyCode::Tab => app.next_panel(),
        KeyCode::BackTab => app.prev_panel(),

        // Navigation Down
        KeyCode::Char('j') | KeyCode::Down => match app.focused_panel {
            FocusedPanel::Collections => {
                let max_idx = app.get_visible_collections().len().saturating_sub(1);
                if app.selected_collection_index < max_idx {
                    app.selected_collection_index += 1;
                    app.update_active_scope_from_tree();
                }
            }
            FocusedPanel::Apis => {
                let visible_items = app.get_visible_items();
                if app.selected_api_index < visible_items.len().saturating_sub(1) {
                    app.selected_api_index += 1;
                }
            }
            FocusedPanel::Environments => {
                let env_vars = app.get_active_collection_env_vars();
                if app.selected_env_index < env_vars.len().saturating_sub(1) {
                    app.selected_env_index += 1;
                }
            }
            FocusedPanel::Details => {
                if let Some(req) = app.get_current_request() {
                    let is_kv_tab = matches!(
                        app.selected_property_tab,
                        PropertyTab::Params | PropertyTab::Headers | PropertyTab::Auth
                    ) || (app.selected_property_tab == PropertyTab::Body
                        && matches!(
                            req.body.selected,
                            crate::core::collection::BodyType::FormData
                                | crate::core::collection::BodyType::XWwwFormUrlEncoded
                        ));

                    if is_kv_tab {
                        let max_rows = match app.selected_property_tab {
                            PropertyTab::Params => req.params.len(),
                            PropertyTab::Headers => req.headers.len(),
                            PropertyTab::Auth => match req.auth.selected {
                                crate::core::collection::AuthType::None => 0,
                                crate::core::collection::AuthType::Bearer => 1,
                                crate::core::collection::AuthType::Basic => 2,
                                crate::core::collection::AuthType::ApiKey => 3,
                            },
                            PropertyTab::Body => match req.body.selected {
                                crate::core::collection::BodyType::FormData => {
                                    req.body.form_data.items.len()
                                }
                                crate::core::collection::BodyType::XWwwFormUrlEncoded => {
                                    req.body.x_www_form_urlencoded.items.len()
                                }
                                _ => 0,
                            },
                            _ => 0,
                        };
                        if app.property_editor_row < max_rows.saturating_sub(1) {
                            app.property_editor_row += 1;
                        }
                    } else {
                        app.details_scroll = app.details_scroll.saturating_add(1);
                    }
                }
            }
            FocusedPanel::Response => {
                app.response_cursor_row = app.response_cursor_row.saturating_add(1);
            }
            FocusedPanel::Stats => {
                app.stats_scroll = app.stats_scroll.saturating_add(1);
            }
            _ => {}
        },

        // Navigation Up
        KeyCode::Char('k') | KeyCode::Up => match app.focused_panel {
            FocusedPanel::Collections => {
                if app.selected_collection_index > 0 {
                    app.selected_collection_index -= 1;
                    app.update_active_scope_from_tree();
                }
            }
            FocusedPanel::Apis => {
                if app.selected_api_index > 0 {
                    app.selected_api_index -= 1;
                }
            }
            FocusedPanel::Environments => {
                if app.selected_env_index > 0 {
                    app.selected_env_index -= 1;
                }
            }
            FocusedPanel::Details => {
                if let Some(req) = app.get_current_request() {
                    let is_kv_tab = matches!(
                        app.selected_property_tab,
                        PropertyTab::Params | PropertyTab::Headers | PropertyTab::Auth
                    ) || (app.selected_property_tab == PropertyTab::Body
                        && matches!(
                            req.body.selected,
                            crate::core::collection::BodyType::FormData
                                | crate::core::collection::BodyType::XWwwFormUrlEncoded
                        ));

                    if is_kv_tab {
                        if app.property_editor_row > 0 {
                            app.property_editor_row -= 1;
                        }
                    } else {
                        app.details_scroll = app.details_scroll.saturating_sub(1);
                    }
                }
            }
            FocusedPanel::Response => {
                app.response_cursor_row = app.response_cursor_row.saturating_sub(1);
            }
            FocusedPanel::Stats => {
                app.stats_scroll = app.stats_scroll.saturating_sub(1);
            }
            _ => {}
        },

        // Drill down / Enter
        KeyCode::Enter => match app.focused_panel {
            FocusedPanel::Collections => {
                let visible_collections = app.get_visible_collections();
                if let Some(item) = visible_collections.get(app.selected_collection_index) {
                    match &item.item_type {
                        crate::tui::app::VisibleItemType::Collection { .. }
                        | crate::tui::app::VisibleItemType::Folder { .. } => {
                            app.toggle_folder();
                        }
                        crate::tui::app::VisibleItemType::Request { method, id, .. } => {
                            app.save_current_request();
                            app.current_request_id = Some(id.clone());
                            app.method = *method;
                            let id_clone = id.clone();
                            for col in &mut app.collections {
                                if let Some(req) = col.find_request_mut(&id_clone) {
                                    app.url = req.url.clone();
                                    req.auth.auto_select();
                                    req.body.auto_select();
                                    break;
                                }
                            }
                            app.focus_request_bar();
                            app.cursor_position = app.url.len();
                            app.reset_scroll();
                        }
                    }
                }
            }
            FocusedPanel::Apis => {
                let visible_items = app.get_visible_items();
                if let Some(item) = visible_items.get(app.selected_api_index) {
                    match &item.item_type {
                        crate::tui::app::VisibleItemType::Folder { .. } => {
                            app.toggle_folder();
                        }
                        crate::tui::app::VisibleItemType::Request { method, id, .. } => {
                            app.save_current_request();
                            app.current_request_id = Some(id.clone());
                            app.method = *method;
                            let id_clone = id.clone();
                            if let Some(col) = app.collections.get_mut(app.active_collection_index)
                            {
                                if let Some(req) = col.find_request_mut(&id_clone) {
                                    app.url = req.url.clone();
                                    req.auth.auto_select();
                                    req.body.auto_select();
                                }
                            }
                            app.focus_request_bar();
                            app.cursor_position = app.url.len();
                            app.reset_scroll();
                        }
                        _ => {}
                    }
                }
            }
            FocusedPanel::Environments => {
                app.input_mode = InputMode::Editing;
                app.property_editor_field = PropertyEditorField::Value;
                let current_val = app.get_env_editor_value();
                app.cursor_position = current_val.len();
            }
            FocusedPanel::Properties => {
                app.focused_panel = FocusedPanel::Details;
                // For Auth, always start on the field name (Key)
                if app.selected_property_tab == PropertyTab::Auth {
                    app.property_editor_field = PropertyEditorField::Key;
                }
            }
            FocusedPanel::Details => {
                if let Some(req) = app.get_current_request() {
                    if app.selected_property_tab == PropertyTab::Auth {
                        if req.auth.selected == crate::core::collection::AuthType::None {
                            return;
                        }
                        if req.auth.selected == crate::core::collection::AuthType::ApiKey {
                            if app.property_editor_row == 2 {
                                app.toggle_auth_bool();
                                return;
                            }
                        }
                        // Always switch to Value field when starting to edit Auth
                        app.property_editor_field = PropertyEditorField::Value;
                    } else if matches!(
                        app.selected_property_tab,
                        PropertyTab::Params | PropertyTab::Headers
                    ) {
                        let items = match app.selected_property_tab {
                            PropertyTab::Params => &req.params,
                            PropertyTab::Headers => &req.headers,
                            _ => unreachable!(),
                        };
                        if items.is_empty() {
                            return;
                        }
                    } else if app.selected_property_tab == PropertyTab::Body {
                        match req.body.selected {
                            crate::core::collection::BodyType::FormData => {
                                if req.body.form_data.items.is_empty() {
                                    return;
                                }
                            }
                            crate::core::collection::BodyType::XWwwFormUrlEncoded => {
                                if req.body.x_www_form_urlencoded.items.is_empty() {
                                    return;
                                }
                            }
                            crate::core::collection::BodyType::Raw
                            | crate::core::collection::BodyType::None => {
                                return;
                            }
                        }
                    } else {
                        // Scripts or other tabs
                        return;
                    }

                    app.input_mode = InputMode::Editing;
                    let current_val = app.get_kv_editor_value();
                    app.cursor_position = current_val.len();
                }
            }
            FocusedPanel::RequestBar => match app.active_request_part {
                RequestBarPart::Method => {
                    app.show_method_search = true;
                    app.method_search_query.clear();
                    app.cursor_position = 0;
                }
                RequestBarPart::Url => {
                    app.input_mode = InputMode::Editing;
                    app.cursor_position = app.url.len();
                }
                RequestBarPart::Send => {
                    app.pending_actions.push(TuiAction::SendRequest);
                    app.focused_panel = FocusedPanel::Response;
                }
            },
            _ => {}
        },

        // Move Right / Next Tab
        KeyCode::Char('l') | KeyCode::Right => match app.focused_panel {
            FocusedPanel::Properties => {
                app.next_property_tab();
            }
            FocusedPanel::Details => match app.selected_property_tab {
                PropertyTab::Params | PropertyTab::Headers => {
                    app.property_editor_field = match app.property_editor_field {
                        PropertyEditorField::Key => PropertyEditorField::Value,
                        PropertyEditorField::Value => PropertyEditorField::Description,
                        PropertyEditorField::Description => PropertyEditorField::Description,
                    };
                }
                _ => {}
            },
            FocusedPanel::Environments => {
                app.property_editor_field = match app.property_editor_field {
                    PropertyEditorField::Key => PropertyEditorField::Value,
                    PropertyEditorField::Value => PropertyEditorField::Value,
                    _ => PropertyEditorField::Value,
                };
            }
            FocusedPanel::RequestBar => {
                app.active_request_part = match app.active_request_part {
                    RequestBarPart::Method => RequestBarPart::Url,
                    RequestBarPart::Url => RequestBarPart::Send,
                    RequestBarPart::Send => RequestBarPart::Method,
                };
            }
            FocusedPanel::Response => {
                app.response_cursor_col = app.response_cursor_col.saturating_add(1);
            }
            FocusedPanel::Stats => {
                app.cycle_stats_tab();
            }
            _ => {}
        },

        // Move Left / Prev Tab
        KeyCode::Char('h') | KeyCode::Left => match app.focused_panel {
            FocusedPanel::Properties => {
                app.prev_property_tab();
            }
            FocusedPanel::Details => match app.selected_property_tab {
                PropertyTab::Params | PropertyTab::Headers => {
                    app.property_editor_field = match app.property_editor_field {
                        PropertyEditorField::Key => PropertyEditorField::Key,
                        PropertyEditorField::Value => PropertyEditorField::Key,
                        PropertyEditorField::Description => PropertyEditorField::Value,
                    };
                }
                _ => {}
            },
            FocusedPanel::Environments => {
                app.property_editor_field = match app.property_editor_field {
                    PropertyEditorField::Key => PropertyEditorField::Key,
                    PropertyEditorField::Value => PropertyEditorField::Key,
                    _ => PropertyEditorField::Key,
                };
            }
            FocusedPanel::RequestBar => {
                app.active_request_part = match app.active_request_part {
                    RequestBarPart::Method => RequestBarPart::Send,
                    RequestBarPart::Url => RequestBarPart::Method,
                    RequestBarPart::Send => RequestBarPart::Url,
                };
            }
            FocusedPanel::Response => {
                app.response_cursor_col = app.response_cursor_col.saturating_sub(1);
            }
            FocusedPanel::Stats => {
                app.prev_stats_tab();
            }
            _ => {
                app.pop_up();
            }
        },

        // Pop up explicitly
        KeyCode::Esc => {
            app.pop_up();
        }

        KeyCode::Char(' ') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.toggle_folder();
            } else if app.focused_panel == FocusedPanel::Details {
                app.toggle_kv_param();
            }
        }

        KeyCode::Char('/') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.input_mode = InputMode::Search;
                app.show_search = true;
                app.search_query.clear();
                app.cursor_position = 0;
            }
        }

        KeyCode::Char('e') => {
            app.focus_request_bar();
            app.cursor_position = app.url.len();
        }

        KeyCode::Char('a') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.input_mode = InputMode::CreateItem;
                app.pending_item_type = Some(PendingItemType::Request);
                app.rename_input.clear();
                app.cursor_position = 0;
                return;
            } else if app.focused_panel == FocusedPanel::Details {
                if let Some(req) = app.get_current_request() {
                    // Check if we can add based on the current tab and its state
                    let can_add = match app.selected_property_tab {
                        PropertyTab::Auth => {
                            // Don't allow adding in Auth tab if it's set to None
                            req.auth.selected != crate::core::collection::AuthType::None
                        }
                        PropertyTab::Params | PropertyTab::Headers => true,
                        PropertyTab::Body => {
                            // Only allow for FormData and XWwwFormUrlEncoded, not Raw or None
                            match req.body.selected {
                                crate::core::collection::BodyType::FormData
                                | crate::core::collection::BodyType::XWwwFormUrlEncoded => true,
                                _ => false,
                            }
                        }
                        _ => false,
                    };

                    if can_add {
                        app.add_kv_param(String::new());
                        app.input_mode = InputMode::Editing;
                        app.property_editor_field = PropertyEditorField::Key;
                        app.cursor_position = 0;
                    }
                    // Always return - don't process 'a' further if in Details panel
                    return;
                }
            } else if app.focused_panel == FocusedPanel::Environments {
                app.add_env_var(String::new());
                app.input_mode = InputMode::Editing;
                app.property_editor_field = PropertyEditorField::Key;
                app.cursor_position = 0;
                return;
            }
        }

        KeyCode::Char('f') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.input_mode = InputMode::CreateItem;
                app.pending_item_type = Some(PendingItemType::Folder);
                app.rename_input.clear();
                app.cursor_position = 0;
            }
        }

        KeyCode::Char('c') => {
            if app.focused_panel == FocusedPanel::Details
                && app.selected_property_tab == PropertyTab::Body
            {
                app.cycle_raw_content_type();
            } else if app.focused_panel == FocusedPanel::Response {
                app.pending_actions.push(TuiAction::CopyResponseValue);
            }
        }

        KeyCode::Char('t') => {
            if app.focused_panel == FocusedPanel::Details {
                match app.selected_property_tab {
                    PropertyTab::Auth => app.cycle_auth_type(),
                    PropertyTab::Body => app.cycle_body_type(),
                    _ => {}
                }
            } else if app.focused_panel == FocusedPanel::Stats {
                app.cycle_stats_tab();
            }
        }

        KeyCode::Char('n') => {
            if app.focused_panel == FocusedPanel::Collections {
                app.input_mode = InputMode::CreateItem;
                app.pending_item_type = Some(PendingItemType::Collection);
                app.rename_input.clear();
                app.cursor_position = 0;
            }
        }

        KeyCode::Char('d') => {
            if app.focused_panel == FocusedPanel::Details {
                app.delete_kv_param();
            } else if app.focused_panel == FocusedPanel::Environments {
                app.delete_env_var();
            } else {
                app.input_mode = InputMode::ConfirmDelete;
            }
        }

        KeyCode::Char('r') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.input_mode = InputMode::Rename;
                app.rename_input.clear();
                if app.focused_panel == FocusedPanel::Collections {
                    let visible_collections = app.get_visible_collections();
                    if let Some(item) = visible_collections.get(app.selected_collection_index) {
                        app.rename_input = item.name.clone();
                    }
                } else if app.focused_panel == FocusedPanel::Apis {
                    let visible_items = app.get_visible_items();
                    if let Some(item) = visible_items.get(app.selected_api_index) {
                        app.rename_input = item.name.clone();
                    }
                }
                app.cursor_position = app.rename_input.len();
            } else if app.focused_panel == FocusedPanel::Environments {
                app.input_mode = InputMode::Editing;
                app.property_editor_field = PropertyEditorField::Key;
                let current_val = app.get_env_editor_value();
                app.cursor_position = current_val.len();
            }
        }

        KeyCode::Char('v') => {
            if app.focused_panel == FocusedPanel::Details
                && app.selected_property_tab == PropertyTab::Body
            {
                app.pending_actions.push(TuiAction::EditBody);
            }
        }

        KeyCode::Char('y') => {
            if app.focused_panel == FocusedPanel::Details
                && app.selected_property_tab == PropertyTab::Body
            {
                app.pending_actions.push(TuiAction::CopyBody);
            } else if app.focused_panel == FocusedPanel::Response {
                app.pending_actions.push(TuiAction::CopyResponseValue);
            } else if app.focused_panel == FocusedPanel::Details {
                app.pending_actions.push(TuiAction::Copy);
            } else if app.focused_panel == FocusedPanel::Environments {
                app.pending_actions.push(TuiAction::Copy);
            }
        }

        KeyCode::Char('p') => {
            if app.focused_panel == FocusedPanel::Details
                && app.selected_property_tab == PropertyTab::Body
            {
                app.pending_actions.push(TuiAction::PasteBody);
            } else if app.focused_panel == FocusedPanel::Details
                || app.focused_panel == FocusedPanel::Environments
            {
                app.pending_actions.push(TuiAction::Paste);
            }
        }

        KeyCode::Char('Y') => {
            if app.focused_panel == FocusedPanel::Response {
                app.pending_actions.push(TuiAction::CopyResponseAll);
            }
        }

        KeyCode::Char(':') => {
            app.input_mode = InputMode::Command;
            app.command_input.clear();
            app.cursor_position = 0;
        }

        KeyCode::Char('i') => {
            if app.focused_panel == FocusedPanel::Details {
                match app.selected_property_tab {
                    PropertyTab::Params | PropertyTab::Headers | PropertyTab::Auth => {
                        if let Some(req) = app.get_current_request() {
                            if app.selected_property_tab == PropertyTab::Auth {
                                if req.auth.selected == crate::core::collection::AuthType::None {
                                    return;
                                }
                                if req.auth.selected == crate::core::collection::AuthType::ApiKey {
                                    if app.property_editor_row == 2 {
                                        // It's a boolean, don't enter editing mode, just toggle
                                        app.toggle_auth_bool();
                                        return;
                                    }
                                }
                                app.property_editor_field = PropertyEditorField::Value;
                            } else {
                                // For Params and Headers, check if there's a row to edit
                                let items = match app.selected_property_tab {
                                    PropertyTab::Params => &req.params,
                                    PropertyTab::Headers => &req.headers,
                                    _ => &Vec::new(),
                                };
                                if items.is_empty() {
                                    return;
                                }
                            }

                            app.input_mode = InputMode::Editing;
                            let current_val = app.get_kv_editor_value();
                            app.cursor_position = current_val.len();
                        }
                    }
                    _ => {}
                }
            }
        }

        _ => {}
    }
}