Skip to main content

max / makenotwork

4.0 KB · 120 lines History Blame Raw
1 //! home-screen key-input handler (split out of tui/input.rs).
2
3 use super::{
4 App, AppEvent, KeyCode, KeyEvent, MnwApiClient, Path, Screen, load_analytics, load_collections,
5 load_home_data, load_project_items, load_promo_codes, load_settings, load_staged_files, mpsc,
6 };
7
8 pub(crate) async fn handle_home_input(
9 key: KeyEvent,
10 app: &mut App,
11 screen: &mut Screen,
12 api: &MnwApiClient,
13 tx: &mpsc::Sender<AppEvent>,
14 staging_dir: &Path,
15 ) {
16 match key.code {
17 KeyCode::Char('j') | KeyCode::Down => app.move_down(screen),
18 KeyCode::Char('k') | KeyCode::Up => app.move_up(screen),
19 KeyCode::Enter if !app.projects.is_empty() => {
20 let idx = app.selected_index;
21 let project_id = app.projects[idx].id.clone();
22 let user_id = app.user.user_id.clone();
23 *screen = Screen::Project(idx);
24 app.items.clear();
25 app.selected_index = 0;
26 app.loading = true;
27
28 let api = api.clone();
29 let tx = tx.clone();
30 tokio::spawn(async move {
31 load_project_items(&api, &project_id, &user_id, &tx).await;
32 });
33 }
34 KeyCode::Char('u' | 'U') => {
35 *screen = Screen::Upload;
36 app.selected_index = 0;
37 app.loading = true;
38 app.upload_status = None;
39 app.editing_field = None;
40
41 let staging_dir = staging_dir.to_path_buf();
42 let api = api.clone();
43 let user_id = app.user.user_id.clone();
44 let tx = tx.clone();
45 tokio::spawn(async move {
46 load_staged_files(&staging_dir, &api, &user_id, &tx).await;
47 });
48 }
49 KeyCode::Char('a' | 'A') => {
50 *screen = Screen::Analytics;
51 app.analytics_data = None;
52 app.analytics_status = None;
53 app.analytics_show_transactions = false;
54 app.selected_index = 0;
55 app.loading = true;
56
57 let api = api.clone();
58 let user_id = app.user.user_id.clone();
59 let range = app.analytics_range.clone();
60 let tx = tx.clone();
61 tokio::spawn(async move {
62 load_analytics(&api, &user_id, &range, &tx).await;
63 });
64 }
65 KeyCode::Char('p' | 'P') => {
66 *screen = Screen::Promo;
67 app.promo_codes.clear();
68 app.promo_status = None;
69 app.promo_editing_step = None;
70 app.selected_index = 0;
71 app.loading = true;
72
73 let api = api.clone();
74 let user_id = app.user.user_id.clone();
75 let tx = tx.clone();
76 tokio::spawn(async move {
77 load_promo_codes(&api, &user_id, &tx).await;
78 });
79 }
80 KeyCode::Char('c' | 'C') => {
81 *screen = Screen::Collections;
82 app.collections.clear();
83 app.collections_status = None;
84 app.selected_index = 0;
85 app.loading = true;
86
87 let api = api.clone();
88 let user_id = app.user.user_id.clone();
89 let tx = tx.clone();
90 tokio::spawn(async move {
91 load_collections(&api, &user_id, &tx).await;
92 });
93 }
94 KeyCode::Char('s' | 'S') => {
95 *screen = Screen::Settings;
96 app.ssh_keys.clear();
97 app.settings_status = None;
98 app.selected_index = 0;
99 app.loading = true;
100
101 let api = api.clone();
102 let user_id = app.user.user_id.clone();
103 let tx = tx.clone();
104 tokio::spawn(async move {
105 load_settings(&api, &user_id, &tx).await;
106 });
107 }
108 KeyCode::Char('r' | 'R') => {
109 app.loading = true;
110 let api = api.clone();
111 let user_id = app.user.user_id.clone();
112 let tx = tx.clone();
113 tokio::spawn(async move {
114 load_home_data(&api, &user_id, &tx).await;
115 });
116 }
117 _ => {}
118 }
119 }
120