eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
//! Status update actions.
//!
//! Handles setting status entries, summaries, errors, and workflow context.

use crate::app::{actions::Action, state::AppState};
use crate::app::error_guidance::ErrorGuidance;
use crate::app::workflow::WorkflowContext;

/// Handle status update actions.
pub fn handle_status_updates(state: &mut AppState, action: &Action) -> bool {
    match action {
        Action::SetStatus(raw) => {
            state.last_status = raw.clone();
            true
        }
        Action::SetStatusLines(lines) => {
            state.status_lines = lines.clone();
            true
        }
        Action::SetStatusEntries(entries) => {
            // Preserve the selected path before updating entries
            // (files can move positions when status changes, e.g., untracked -> staged)
            let preserved_path = state.status_selected_path.clone();
            
            // Try to find the preserved path in the new entries list
            // This ensures selection persists even if file moves position after staging
            if let Some(found_idx) = entries.iter()
                .position(|e| e.path == preserved_path) {
                state.status_selected = found_idx;
            } else {
                // Path not found (file might have been removed) - use index with bounds check
                let max_idx = entries.len().saturating_sub(1);
                state.status_selected = state.status_selected.min(max_idx);
            }
            state.status_entries = entries.clone();
            // Update workflow context when status changes
            state.workflow_context = Some(WorkflowContext::detect(&*state));
            true
        }
        Action::SetStatusSummary(summary) => {
            state.status_summary = summary.clone();
            true
        }
        Action::SetStatusError(err) => {
            state.last_status_error = err.clone();
            // Generate error guidance if error exists
            if let Some(ref error_msg) = err {
                state.error_guidance = Some(ErrorGuidance::from_error(error_msg));
            } else {
                state.error_guidance = None;
            }
            true
        }
        Action::SetErrorGuidance(guidance) => {
            state.error_guidance = guidance.clone();
            true
        }
        Action::UpdateWorkflowContext => {
            state.workflow_context = Some(WorkflowContext::detect(&*state));
            true
        }
        _ => false,
    }
}