Skip to main content

tui/app/
filter.rs

1//! Filtering the Transactions tab from the palette and from the Accounts tab.
2
3use cli_core::forms::Fetched;
4use forms::{FormValues, Source, TRANSACTION_QUERY};
5
6use super::App;
7use super::lookup::Goal;
8use super::pending::rejection;
9use crate::form::Form;
10use crate::tabs::list::ListTab;
11use crate::tabs::paging::filter_summary;
12use crate::view::Tab;
13use crate::window::WindowId;
14
15impl App {
16    /// Filter the Transactions tab by `filter`, whose `account` may be a label.
17    pub fn filter_transactions(&mut self, filter: FormValues) {
18        let window = self.window_or_open(Tab::Transactions).window;
19        self.start_lookup(Goal::Filter(window), &TRANSACTION_QUERY, filter);
20    }
21
22    /// Drop the Transactions filter and show the window.
23    pub fn show_all_transactions(&mut self) {
24        let window = self.window_or_open(Tab::Transactions).window;
25        self.clear_transaction_filter(window);
26        self.activate_window(window);
27    }
28
29    /// Filter the Transactions tab by the account selected on the Accounts tab.
30    pub fn filter_transactions_of_selected_account(&mut self) {
31        let Some(id) = self.selected_id(Tab::Accounts) else {
32            self.set_status("no account selected");
33            return;
34        };
35        let mut filter = FormValues::default();
36        filter.set("account", &id);
37        self.filter_transactions(filter);
38    }
39
40    pub(super) fn next_filter_seq(&mut self, window: WindowId) -> Option<u64> {
41        self.win_mut::<ListTab>(window)
42            .map(|list| list.filter_seq.advance())
43    }
44
45    pub(super) fn apply_resolved_filter(
46        &mut self,
47        window: WindowId,
48        filter: &FormValues,
49        fetched: &Fetched,
50    ) {
51        let label = filter
52            .text("account")
53            .and_then(|id| {
54                let (_, options) = fetched.iter().find(|(s, _)| *s == Source::Accounts)?;
55                options.iter().find(|option| option.id == id)
56            })
57            .map_or("", |option| option.label.as_str());
58        let summary = filter_summary(label, filter);
59        self.submit_transaction_filter(window, filter, summary);
60    }
61
62    /// Adopt the filter of an answered query, or report the server's refusal
63    /// and keep the old one; a reply of a superseded request is dropped.
64    pub(super) fn deliver_filter_reply(
65        &mut self,
66        window: WindowId,
67        seq: u64,
68        filter: &FormValues,
69        summary: String,
70        wire: &str,
71    ) {
72        let awaited = self
73            .win::<ListTab>(window)
74            .is_some_and(|list| list.filter_seq.awaits(seq));
75        if !awaited {
76            return;
77        }
78        if let Err(e) = cli_core::render::parse_wire(wire) {
79            let mut form = Form::transaction_filter(filter);
80            form.show_errors(&rejection(&e));
81            self.set_status(format!("filter failed: {}", form.errors.join("; ")));
82            return;
83        }
84        if let Some(list) = self.win_mut::<ListTab>(window) {
85            list.set_filter(filter, summary);
86        }
87        self.deliver_transactions_reply(window, wire);
88        self.show_window_if_free(window);
89    }
90}