use crate::app::{Action, reducer};
use crate::app::actions::LogAction;
use crate::errors::ApplicationError;
use super::super::HandlerContext;
pub async fn handle(action: &Action, ctx: &mut HandlerContext<'_>) -> Result<bool, ApplicationError> {
let Action::ExecuteLogAction(log_action) = action else {
return Ok(false);
};
let commit_hash = ctx.app.state.log_action_commit_hash.clone();
ctx.app.state = reducer(ctx.app.state.clone(), Action::HideLogActionMenu);
let Some(hash) = commit_hash else {
return Ok(true);
};
let transformed = match log_action {
LogAction::ShowDetail => Action::ShowCommitDetail(hash),
LogAction::CherryPick => {
if let Some(commit) = ctx.app.state.commits.iter().find(|c| c.hash == hash) {
let msg = format!("Cherry-pick {} - {} ?", commit.short_hash, commit.message);
Action::ShowConfirm(Box::new(Action::CherryPickCommit(commit.short_hash.clone())), msg)
} else { return Ok(true); }
}
LogAction::Revert => {
if let Some(commit) = ctx.app.state.commits.iter().find(|c| c.hash == hash) {
let msg = format!("Revert {} - {} ?", commit.short_hash, commit.message);
Action::ShowConfirm(Box::new(Action::RevertCommit(commit.short_hash.clone())), msg)
} else { return Ok(true); }
}
LogAction::Amend => Action::StartAmend,
LogAction::ResetSoft => Action::RequestResetSoft,
LogAction::ResetMixed => Action::RequestResetMixed,
LogAction::ResetHard => Action::RequestResetHard,
LogAction::CreateBranch => Action::StartBranchCreate,
LogAction::CreateTag => {
ctx.app.state = reducer(ctx.app.state.clone(), Action::SetStatusError(Some("Tag creation not yet implemented".to_string())));
return Ok(true);
}
};
ctx.app.state = reducer(ctx.app.state.clone(), transformed);
Ok(true)
}