Files
radicle-tui/bin/commands/inbox/select.rs
T

316 lines
8.9 KiB
Rust

#[path = "select/ui.rs"]
mod ui;
use std::str::FromStr;
use anyhow::Result;
use radicle::identity::Project;
use radicle::node::notifications::NotificationId;
use radicle::storage::ReadRepository;
use radicle::storage::ReadStorage;
use radicle::storage::git::Repository;
use radicle::Profile;
use radicle_tui as tui;
use tui::cob::inbox::{self};
use tui::store;
use tui::store::StateValue;
use tui::task::{self, Interrupted};
use tui::terminal;
use tui::ui::items::{Filter, NotificationItem, NotificationItemFilter};
use tui::ui::Frontend;
use tui::Exit;
use ui::ListPage;
use super::common::SelectionMode;
use super::common::{Mode, RepositoryMode};
type Selection = tui::Selection<NotificationId>;
pub struct Context {
pub profile: Profile,
pub repository: Repository,
pub mode: Mode,
pub filter: inbox::Filter,
pub sort_by: inbox::SortBy,
}
pub struct App {
context: Context,
}
#[derive(Clone, Debug)]
pub struct UIState {
page_size: usize,
show_search: bool,
}
impl Default for UIState {
fn default() -> Self {
Self {
page_size: 1,
show_search: false,
}
}
}
#[derive(Clone, Debug)]
pub struct NotificationsState {
items: Vec<NotificationItem>,
selected: Option<usize>,
}
#[derive(Clone, Debug)]
pub struct HelpState {
show: bool,
progress: usize,
}
#[derive(Clone, Debug)]
pub struct State {
notifications: NotificationsState,
help: HelpState,
mode: Mode,
project: Project,
filter: NotificationItemFilter,
search: StateValue<String>,
ui: UIState,
}
impl State {
pub fn shortcuts(&self) -> Vec<(&str, &str)> {
if self.ui.show_search {
vec![("esc", "cancel"), ("enter", "apply")]
} else if self.help.show {
vec![("?", "close")]
} else {
match self.mode.selection() {
SelectionMode::Id => vec![("enter", "select"), ("/", "search")],
SelectionMode::Operation => vec![
("enter", "show"),
("c", "clear"),
("/", "search"),
("?", "help"),
],
}
}
}
pub fn notifications(&self) -> Vec<NotificationItem> {
self.notifications
.items
.iter()
.filter(|patch| self.filter.matches(patch))
.cloned()
.collect()
}
}
impl TryFrom<&Context> for State {
type Error = anyhow::Error;
fn try_from(context: &Context) -> Result<Self, Self::Error> {
let doc = context.repository.identity_doc()?;
let project = doc.project()?;
let search = StateValue::new(String::new());
let filter = NotificationItemFilter::from_str(&search.read()).unwrap_or_default();
let mut notifications = match &context.mode.repository() {
RepositoryMode::All => {
let mut repos = context.profile.storage.repositories()?;
repos.sort_by_key(|r| r.rid);
let mut notifs = vec![];
for repo in repos {
let repo = context.profile.storage.repository(repo.rid)?;
let items = inbox::all(&repo, &context.profile)?
.iter()
.map(|notif| NotificationItem::new(&context.profile, &repo, notif))
.filter_map(|item| item.ok())
.flatten()
.collect::<Vec<_>>();
notifs.extend(items);
}
notifs
}
RepositoryMode::Contextual => {
let notifs = inbox::all(&context.repository, &context.profile)?;
notifs
.iter()
.map(|notif| {
NotificationItem::new(&context.profile, &context.repository, notif)
})
.filter_map(|item| item.ok())
.flatten()
.collect::<Vec<_>>()
}
RepositoryMode::ByRepo((rid, _)) => {
let repo = context.profile.storage.repository(*rid)?;
let notifs = inbox::all(&repo, &context.profile)?;
notifs
.iter()
.map(|notif| NotificationItem::new(&context.profile, &repo, notif))
.filter_map(|item| item.ok())
.flatten()
.collect::<Vec<_>>()
}
};
// Set project name
let mode = match &context.mode.repository() {
RepositoryMode::ByRepo((rid, _)) => {
let project = context
.profile
.storage
.repository(*rid)?
.identity_doc()?
.project()?;
let name = project.name().to_string();
context
.mode
.clone()
.with_repository(RepositoryMode::ByRepo((*rid, Some(name))))
}
_ => context.mode.clone(),
};
// Apply sorting
match context.sort_by.field {
"timestamp" => notifications.sort_by(|a, b| a.timestamp.cmp(&b.timestamp)),
"id" => notifications.sort_by(|a, b| a.id.cmp(&b.id)),
_ => {}
}
if context.sort_by.reverse {
notifications.reverse();
}
// Sort by project if all notifications are shown
if let RepositoryMode::All = mode.repository() {
notifications.sort_by(|a, b| a.project.cmp(&b.project));
}
Ok(Self {
notifications: NotificationsState {
items: notifications,
selected: Some(0),
},
help: HelpState {
show: false,
progress: 0,
},
mode: mode.clone(),
project,
filter,
search,
ui: UIState::default(),
})
}
}
pub enum Action {
Exit { selection: Option<Selection> },
Select { selected: Option<usize> },
PageSize(usize),
OpenSearch,
UpdateSearch { value: String },
ApplySearch,
CloseSearch,
OpenHelp,
CloseHelp,
ScrollHelp { progress: usize },
}
impl store::State<Action, Selection> for State {
fn tick(&self) {}
fn handle_action(&mut self, action: Action) -> Option<Exit<Selection>> {
match action {
Action::Exit { selection } => Some(Exit { value: selection }),
Action::Select { selected } => {
self.notifications.selected = selected;
None
}
Action::PageSize(size) => {
self.ui.page_size = size;
None
}
Action::OpenSearch => {
self.ui.show_search = true;
None
}
Action::UpdateSearch { value } => {
self.search.write(value);
self.filter =
NotificationItemFilter::from_str(&self.search.read()).unwrap_or_default();
None
}
Action::ApplySearch => {
self.search.apply();
self.ui.show_search = false;
None
}
Action::CloseSearch => {
self.search.reset();
self.ui.show_search = false;
self.filter =
NotificationItemFilter::from_str(&self.search.read()).unwrap_or_default();
None
}
Action::OpenHelp => {
self.help.show = true;
None
}
Action::CloseHelp => {
self.help.show = false;
None
}
Action::ScrollHelp { progress } => {
self.help.progress = progress;
None
}
}
}
}
impl App {
pub fn new(context: Context) -> Self {
Self { context }
}
pub async fn run(&self) -> Result<Option<Selection>> {
let (terminator, mut interrupt_rx) = task::create_termination();
let (store, state_rx) = store::Store::<Action, State, Selection>::new();
let (frontend, action_rx) = Frontend::<Action>::new();
let state = State::try_from(&self.context)?;
tokio::try_join!(
store.main_loop(state, terminator, action_rx, interrupt_rx.resubscribe()),
frontend.main_loop::<State, ListPage<terminal::Backend>, Selection>(
state_rx,
interrupt_rx.resubscribe()
),
)?;
if let Ok(reason) = interrupt_rx.recv().await {
match reason {
Interrupted::User { payload } => Ok(payload),
Interrupted::OsSignal => anyhow::bail!("exited because of an os sig int"),
}
} else {
anyhow::bail!("exited because of an unexpected error");
}
}
}