Skip to main content

gitkraft_gui/features/stash/
update.rs

1//! Update logic for stash-related messages.
2
3use iced::Task;
4
5use crate::message::Message;
6use crate::state::GitKraft;
7
8use super::commands;
9
10/// Handle all stash-related messages, returning a [`Task`] for any follow-up
11/// async work.
12pub fn update(state: &mut GitKraft, message: Message) -> Task<Message> {
13    match message {
14        Message::StashMessageChanged(msg) => {
15            state.active_tab_mut().stash_message = msg;
16            Task::none()
17        }
18
19        Message::StashSave => {
20            // Derive the optional stash message before the macro borrows state.
21            let msg = {
22                let tab = state.active_tab();
23                if tab.stash_message.trim().is_empty() {
24                    None
25                } else {
26                    Some(tab.stash_message.trim().to_string())
27                }
28            };
29            with_repo!(state, loading, "Saving stash…".into(), |repo_path| {
30                state.active_tab_mut().stash_message.clear();
31                commands::stash_save(repo_path, msg)
32            })
33        }
34
35        Message::StashPop(index) => with_repo!(
36            state,
37            loading,
38            format!("Popping stash@{{{index}}}…"),
39            |repo_path| commands::stash_pop(repo_path, index)
40        ),
41
42        Message::StashDrop(index) => with_repo!(
43            state,
44            loading,
45            format!("Dropping stash@{{{index}}}…"),
46            |repo_path| commands::stash_drop(repo_path, index)
47        ),
48
49        Message::StashApply(index) => with_repo!(
50            state,
51            loading,
52            format!("Applying stash@{{{index}}}…"),
53            |repo_path| commands::stash_apply(repo_path, index)
54        ),
55
56        Message::ViewStashDiff(index) => {
57            state.active_tab_mut().context_menu = None;
58            with_repo!(state, "Loading stash diff…".into(), |repo_path| {
59                commands::load_stash_diff(repo_path, index)
60            })
61        }
62
63        Message::StashDiffLoaded(result) => {
64            match result {
65                Ok(diffs) => {
66                    let tab = state.active_tab_mut();
67                    tab.selected_diff = diffs.first().cloned();
68                    tab.commit_files = diffs
69                        .iter()
70                        .map(|d| gitkraft_core::DiffFileEntry {
71                            old_file: d.old_file.clone(),
72                            new_file: d.new_file.clone(),
73                            status: d.status.clone(),
74                        })
75                        .collect();
76                    tab.selected_file_index = Some(0);
77                    tab.status_message =
78                        Some(format!("{} file(s) in stash", tab.commit_files.len()));
79                }
80                Err(e) => {
81                    state.active_tab_mut().error_message =
82                        Some(format!("Failed to load stash diff: {e}"));
83                }
84            }
85            Task::none()
86        }
87
88        Message::StashUpdated(result) => {
89            state.active_tab_mut().is_loading = false;
90            match result {
91                Ok(stashes) => {
92                    {
93                        let tab = state.active_tab_mut();
94                        tab.stashes = stashes;
95                        tab.status_message = Some("Stash updated.".into());
96                    }
97                    state.refresh_active_tab()
98                }
99                Err(e) => {
100                    let tab = state.active_tab_mut();
101                    tab.error_message = Some(format!("Stash operation failed: {e}"));
102                    tab.status_message = None;
103                    Task::none()
104                }
105            }
106        }
107
108        _ => Task::none(),
109    }
110}