use std::sync::Arc;
use egaku::{DiffView, KeyMap};
use metsuke::{Forge, ForgeReview, Involvement, PrSummary, Verdict};
use pauta_spec::{Catalog, Effect};
use crate::board::Board;
use crate::feed::PrFeed;
pub type Action = Effect;
pub enum Panel {
Board,
Diff(Box<DiffView>),
Pending { pr: PrSummary, verdict: Verdict },
Message(String),
}
pub struct PautaApp<F: Forge, R: ForgeReview> {
forge: Arc<F>,
review: R,
board: Board,
panel: Panel,
catalog: Catalog,
keys: KeyMap<Effect>,
done: bool,
feed: Option<PrFeed>,
source_label: String,
}
impl<F: Forge, R: ForgeReview> PautaApp<F, R> {
pub fn try_new(
forge: impl Into<Arc<F>>,
review: R,
lane: Involvement,
source_label: impl Into<String>,
) -> Result<Self, pauta_spec::SpecError> {
let catalog = Catalog::load()?;
Ok(Self {
forge: forge.into(),
review,
board: Board::new(lane),
panel: Panel::Board,
keys: crate::keys::keymap(&catalog),
catalog,
done: false,
feed: None,
source_label: source_label.into(),
})
}
#[must_use]
pub fn catalog(&self) -> &Catalog {
&self.catalog
}
#[must_use]
pub fn with_feed(mut self, interval: std::time::Duration) -> Self
where
F: Send + Sync + 'static,
{
self.feed = Some(PrFeed::start(
Arc::clone(&self.forge),
self.board.lane(),
crate::feed::DEFAULT_LIMIT,
interval,
));
self
}
#[must_use]
pub fn feed(&self) -> Option<&PrFeed> {
self.feed.as_ref()
}
#[must_use]
pub fn board(&self) -> &Board {
&self.board
}
#[must_use]
pub fn panel(&self) -> &Panel {
&self.panel
}
#[must_use]
pub fn source_label(&self) -> &str {
&self.source_label
}
#[must_use]
pub fn keymap(&self) -> &KeyMap<Effect> {
&self.keys
}
#[must_use]
pub fn should_quit(&self) -> bool {
self.done
}
#[must_use]
pub fn review_env(&self) -> &R {
&self.review
}
pub fn apply_feed(&mut self) {
if let Some(rows) = self.feed.as_ref().map(PrFeed::rows) {
self.board.set_rows(rows);
}
}
pub fn refresh(&mut self) -> metsuke::Result<()> {
let rows = self
.forge
.search(self.board.lane(), crate::feed::DEFAULT_LIMIT)?;
self.board.set_rows(rows);
Ok(())
}
pub fn dispatch(&mut self, action: Action) {
match action {
Action::Quit => self.done = true,
Action::SelectNext => self.board.view_mut().select_next(),
Action::SelectPrev => self.board.view_mut().select_prev(),
Action::CycleLane => self.cycle_lane(),
Action::OpenDiff => self.open_diff(),
Action::CloseDiff => self.panel = Panel::Board,
Action::ToggleDiffMode => self.toggle_diff_mode(),
Action::Approve => self.stage(Effect::Approve, Verdict::Approve(String::new())),
Action::RequestChanges => {
self.stage(
Effect::RequestChanges,
Verdict::RequestChanges(String::new()),
);
}
Action::Confirm => self.commit_pending(),
Action::Cancel => self.panel = Panel::Board,
}
}
fn cycle_lane(&mut self) {
let lanes = Involvement::all();
let here = lanes
.iter()
.position(|l| *l == self.board.lane())
.unwrap_or(0);
let next = lanes[(here + 1) % lanes.len()];
self.board.set_lane(next);
self.board.set_rows(Vec::new());
}
fn open_diff(&mut self) {
let Some(pr) = self.board.selected().cloned() else {
return;
};
match self.forge.files(&pr.repo, pr.number) {
Ok(files) => {
let mut unified = String::new();
for f in &files {
unified.push_str("diff --git a/");
unified.push_str(f.previous_path.as_deref().unwrap_or(&f.path));
unified.push_str(" b/");
unified.push_str(&f.path);
unified.push('\n');
match &f.patch {
Some(p) => {
unified.push_str("--- a/");
unified.push_str(f.previous_path.as_deref().unwrap_or(&f.path));
unified.push('\n');
unified.push_str("+++ b/");
unified.push_str(&f.path);
unified.push('\n');
unified.push_str(p);
if !p.ends_with('\n') {
unified.push('\n');
}
}
None => unified.push_str("Binary files differ\n"),
}
}
self.panel = Panel::Diff(Box::new(DiffView::parse(&unified)));
}
Err(e) => self.panel = Panel::Message(e.to_string()),
}
}
fn toggle_diff_mode(&mut self) {
if let Panel::Diff(v) = &mut self.panel {
v.toggle_mode();
}
}
fn stage(&mut self, effect: Effect, verdict: Verdict) {
let Some(pr) = self.board.selected().cloned() else {
return;
};
if self.catalog.is_binding(effect) {
self.panel = Panel::Pending { pr, verdict };
} else {
self.submit(&pr.repo.clone(), pr.number, &verdict);
}
}
fn commit_pending(&mut self) {
let Panel::Pending { pr, verdict } = &self.panel else {
return;
};
let (repo, number) = (pr.repo.clone(), pr.number);
let v = verdict.clone();
self.submit(&repo, number, &v);
}
fn submit(&mut self, repo: &metsuke::RepoId, number: metsuke::PrNumber, v: &Verdict) {
self.panel = match self.review.submit_review(repo, number, v) {
Ok(()) => {
let mut m = String::from("submitted ");
m.push_str(match *v {
Verdict::Approve(_) => "approval",
Verdict::RequestChanges(_) => "request-changes",
Verdict::Comment(_) => "comment",
});
m.push_str(" on ");
m.push_str(&repo.to_string());
m.push('#');
m.push_str(&number.0.to_string());
Panel::Message(m)
}
Err(e) => Panel::Message(e.to_string()),
};
}
}