use crate::app::{Action, reducer};
use crate::async_result::AsyncResult;
use crate::errors::ApplicationError;
use super::super::HandlerContext;
pub async fn handle(action: &Action, ctx: &mut HandlerContext<'_>) -> Result<bool, ApplicationError> {
match action {
Action::ShowCommitDetail(hash) => {
let path = ctx.app.state.repo_path.clone();
let hash_clone = hash.clone();
let git_service = ctx.app.git_service.clone();
let async_tx = ctx.app.async_tx.clone();
ctx.app.state = reducer(ctx.app.state.clone(), Action::SetCommitDetail(Some("Loading commit details...".to_string())));
tokio::task::spawn_blocking(move || {
match git_service.show_commit(&path, &hash_clone) {
Ok(detail) => { let _ = async_tx.send(AsyncResult::CommitDetailLoaded(detail)); }
Err(e) => { let _ = async_tx.send(AsyncResult::CommitDetailError(format!("show commit error: {e}"))); }
}
});
Ok(true)
}
Action::CommitUp | Action::CommitDown if ctx.app.state.commit_detail.is_some() => {
if let Some(commit) = ctx.app.state.commits.get(ctx.app.state.commit_selected) {
let hash = commit.hash.clone();
let path = ctx.app.state.repo_path.clone();
let git_service = ctx.app.git_service.clone();
let async_tx = ctx.app.async_tx.clone();
ctx.app.state = reducer(ctx.app.state.clone(), Action::SetCommitDetail(Some("Loading commit details...".to_string())));
tokio::task::spawn_blocking(move || {
match git_service.show_commit(&path, &hash) {
Ok(detail) => { let _ = async_tx.send(AsyncResult::CommitDetailLoaded(detail)); }
Err(e) => { let _ = async_tx.send(AsyncResult::CommitDetailError(format!("show commit error: {e}"))); }
}
});
}
Ok(false) }
_ => Ok(false),
}
}