refactor(ui): all table search and filter functionality is now available directly through the ManagarrTable widget to make life easier moving forward

This commit is contained in:
2024-12-05 19:07:03 -07:00
parent 9d0948e124
commit 5abed23cf2
14 changed files with 609 additions and 346 deletions
+39 -226
View File
@@ -1,19 +1,27 @@
use crate::app::App;
use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, HISTORY_BLOCKS};
use crate::models::sonarr_models::{SonarrHistoryData, SonarrHistoryEventType, SonarrHistoryItem};
use crate::models::sonarr_models::{SonarrHistoryEventType, SonarrHistoryItem};
use crate::models::Route;
use crate::ui::styles::ManagarrStyle;
use crate::ui::utils::{get_width_from_percentage, layout_block_top_border};
use crate::ui::widgets::managarr_table::ManagarrTable;
use crate::ui::widgets::message::Message;
use crate::ui::widgets::popup::{Popup, Size};
use crate::ui::{draw_input_box_popup, draw_popup_over, DrawUi};
use crate::ui::DrawUi;
use ratatui::layout::{Alignment, Constraint, Rect};
use ratatui::style::{Style, Stylize};
use ratatui::text::{Line, Text};
use ratatui::style::Style;
use ratatui::text::Text;
use ratatui::widgets::{Cell, Row};
use ratatui::Frame;
use super::sonarr_ui_utils::{
create_download_failed_history_event_details,
create_download_folder_imported_history_event_details,
create_episode_file_deleted_history_event_details,
create_episode_file_renamed_history_event_details, create_grabbed_history_event_details,
create_no_data_history_event_details,
};
#[cfg(test)]
#[path = "history_ui_tests.rs"]
mod history_ui_tests;
@@ -32,40 +40,12 @@ impl DrawUi for HistoryUi {
fn draw(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
if let Route::Sonarr(active_sonarr_block, _) = app.get_current_route() {
match active_sonarr_block {
ActiveSonarrBlock::History | ActiveSonarrBlock::HistorySortPrompt => {
draw_history_table(f, app, area)
}
ActiveSonarrBlock::SearchHistory => draw_popup_over(
f,
app,
area,
draw_history_table,
draw_history_search_box,
Size::InputBox,
),
ActiveSonarrBlock::SearchHistoryError => {
let popup = Popup::new(Message::new("History item not found!")).size(Size::Message);
draw_history_table(f, app, area);
f.render_widget(popup, f.area());
}
ActiveSonarrBlock::FilterHistory => draw_popup_over(
f,
app,
area,
draw_history_table,
draw_filter_history_box,
Size::InputBox,
),
ActiveSonarrBlock::FilterHistoryError => {
let popup = Popup::new(Message::new(
"No history items found matching the given filter!",
))
.size(Size::Message);
draw_history_table(f, app, area);
f.render_widget(popup, f.area());
}
ActiveSonarrBlock::History
| ActiveSonarrBlock::HistorySortPrompt
| ActiveSonarrBlock::SearchHistory
| ActiveSonarrBlock::SearchHistoryError
| ActiveSonarrBlock::FilterHistory
| ActiveSonarrBlock::FilterHistoryError => draw_history_table(f, app, area),
ActiveSonarrBlock::HistoryItemDetails => {
draw_history_table(f, app, area);
draw_history_item_details_popup(f, app);
@@ -120,6 +100,10 @@ fn draw_history_table(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
.loading(app.is_loading)
.footer(history_table_footer)
.sorting(active_sonarr_block == ActiveSonarrBlock::HistorySortPrompt)
.searching(active_sonarr_block == ActiveSonarrBlock::SearchHistory)
.search_produced_empty_results(active_sonarr_block == ActiveSonarrBlock::SearchHistoryError)
.filtering(active_sonarr_block == ActiveSonarrBlock::FilterHistory)
.filter_produced_empty_results(active_sonarr_block == ActiveSonarrBlock::FilterHistoryError)
.headers(["Source Title", "Event Type", "Language", "Quality", "Date"])
.constraints([
Constraint::Percentage(40),
@@ -129,6 +113,15 @@ fn draw_history_table(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
Constraint::Percentage(20),
]);
if [
ActiveSonarrBlock::SearchHistory,
ActiveSonarrBlock::FilterHistory,
]
.contains(&active_sonarr_block)
{
history_table.show_cursor(f, area);
}
f.render_widget(history_table, area);
}
}
@@ -141,18 +134,20 @@ fn draw_history_item_details_popup(f: &mut Frame<'_>, app: &mut App<'_>) {
};
let line_vec = match current_selection.event_type {
SonarrHistoryEventType::Unknown => create_unknown_event_vec(current_selection),
SonarrHistoryEventType::Grabbed => create_grabbed_history_event_details(current_selection),
SonarrHistoryEventType::DownloadFolderImported => {
create_download_folder_imported_event_vec(current_selection)
create_download_folder_imported_history_event_details(current_selection)
}
SonarrHistoryEventType::DownloadFailed => {
create_download_failed_history_event_details(current_selection)
}
SonarrHistoryEventType::DownloadFailed => create_download_failed_event_vec(current_selection),
SonarrHistoryEventType::EpisodeFileDeleted => {
create_episode_file_deleted_event_vec(current_selection)
create_episode_file_deleted_history_event_details(current_selection)
}
SonarrHistoryEventType::EpisodeFileRenamed => {
create_episode_file_renamed_event_vec(current_selection)
create_episode_file_renamed_history_event_details(current_selection)
}
_ => create_no_data_event_vec(current_selection),
_ => create_no_data_history_event_details(current_selection),
};
let text = Text::from(line_vec);
@@ -163,185 +158,3 @@ fn draw_history_item_details_popup(f: &mut Frame<'_>, app: &mut App<'_>) {
f.render_widget(Popup::new(message).size(Size::NarrowMessage), f.area());
}
fn create_unknown_event_vec(history_item: SonarrHistoryItem) -> Vec<Line<'static>> {
let SonarrHistoryItem {
source_title, data, ..
} = history_item;
let SonarrHistoryData {
indexer,
release_group,
series_match_type,
nzb_info_url,
download_client_name,
age,
published_date,
..
} = data;
vec![
Line::from(vec![
"Source Title: ".bold().secondary(),
source_title.text.secondary(),
]),
Line::from(vec![
"Indexer: ".bold().secondary(),
indexer.unwrap_or_default().secondary(),
]),
Line::from(vec![
"Release Group: ".bold().secondary(),
release_group.unwrap_or_default().secondary(),
]),
Line::from(vec![
"Series Match Type: ".bold().secondary(),
series_match_type.unwrap_or_default().secondary(),
]),
Line::from(vec![
"NZB Info URL: ".bold().secondary(),
nzb_info_url.unwrap_or_default().secondary(),
]),
Line::from(vec![
"Download Client Name: ".bold().secondary(),
download_client_name.unwrap_or_default().secondary(),
]),
Line::from(vec![
"Age: ".bold().secondary(),
format!("{} days", age.unwrap_or("0".to_owned())).secondary(),
]),
Line::from(vec![
"Published Date: ".bold().secondary(),
published_date.unwrap_or_default().to_string().secondary(),
]),
]
}
fn create_download_folder_imported_event_vec(
history_item: SonarrHistoryItem,
) -> Vec<Line<'static>> {
let SonarrHistoryItem {
source_title, data, ..
} = history_item;
let SonarrHistoryData {
dropped_path,
imported_path,
..
} = data;
vec![
Line::from(vec![
"Source Title: ".bold().secondary(),
source_title.text.secondary(),
]),
Line::from(vec![
"Dropped Path: ".bold().secondary(),
dropped_path.unwrap_or_default().secondary(),
]),
Line::from(vec![
"Imported Path: ".bold().secondary(),
imported_path.unwrap_or_default().secondary(),
]),
]
}
fn create_download_failed_event_vec(history_item: SonarrHistoryItem) -> Vec<Line<'static>> {
let SonarrHistoryItem {
source_title, data, ..
} = history_item;
let SonarrHistoryData { message, .. } = data;
vec![
Line::from(vec![
"Source Title: ".bold().secondary(),
source_title.text.secondary(),
]),
Line::from(vec![
"Message: ".bold().secondary(),
message.unwrap_or_default().secondary(),
]),
]
}
fn create_episode_file_deleted_event_vec(history_item: SonarrHistoryItem) -> Vec<Line<'static>> {
let SonarrHistoryItem {
source_title, data, ..
} = history_item;
let SonarrHistoryData { reason, .. } = data;
vec![
Line::from(vec![
"Source Title: ".bold().secondary(),
source_title.text.secondary(),
]),
Line::from(vec![
"Reason: ".bold().secondary(),
reason.unwrap_or_default().secondary(),
]),
]
}
fn create_episode_file_renamed_event_vec(history_item: SonarrHistoryItem) -> Vec<Line<'static>> {
let SonarrHistoryItem {
source_title, data, ..
} = history_item;
let SonarrHistoryData {
source_path,
source_relative_path,
path,
relative_path,
..
} = data;
vec![
Line::from(vec![
"Source Title: ".bold().secondary(),
source_title.text.secondary(),
]),
Line::from(vec![
"Source Path: ".bold().secondary(),
source_path.unwrap_or_default().secondary(),
]),
Line::from(vec![
"Source Relative Path: ".bold().secondary(),
source_relative_path.unwrap_or_default().secondary(),
]),
Line::from(vec![
"Destination Path: ".bold().secondary(),
path.unwrap_or_default().secondary(),
]),
Line::from(vec![
"Destination Relative Path: ".bold().secondary(),
relative_path.unwrap_or_default().secondary(),
]),
]
}
fn create_no_data_event_vec(history_item: SonarrHistoryItem) -> Vec<Line<'static>> {
let SonarrHistoryItem { source_title, .. } = history_item;
vec![
Line::from(vec![
"Source Title: ".bold().secondary(),
source_title.text.secondary(),
]),
Line::from(vec![String::new().secondary()]),
Line::from(vec!["No additional data available".bold().secondary()]),
]
}
fn draw_history_search_box(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
draw_input_box_popup(
f,
area,
"Search",
app.data.sonarr_data.history.search.as_ref().unwrap(),
);
}
fn draw_filter_history_box(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
draw_input_box_popup(
f,
area,
"Filter",
app.data.sonarr_data.history.filter.as_ref().unwrap(),
)
}
+6 -4
View File
@@ -2,6 +2,7 @@
mod tests {
use crate::models::servarr_data::sonarr::sonarr_data::{
ActiveSonarrBlock, ADD_SERIES_BLOCKS, DELETE_SERIES_BLOCKS, EDIT_SERIES_BLOCKS,
SERIES_DETAILS_BLOCKS,
};
use crate::models::{
servarr_data::sonarr::sonarr_data::LIBRARY_BLOCKS, sonarr_models::SeriesStatus,
@@ -26,12 +27,13 @@ mod tests {
library_ui_blocks.extend(ADD_SERIES_BLOCKS);
library_ui_blocks.extend(DELETE_SERIES_BLOCKS);
library_ui_blocks.extend(EDIT_SERIES_BLOCKS);
library_ui_blocks.extend(SERIES_DETAILS_BLOCKS);
ActiveSonarrBlock::iter().for_each(|active_radarr_block| {
if library_ui_blocks.contains(&active_radarr_block) {
assert!(LibraryUi::accepts(active_radarr_block.into()));
ActiveSonarrBlock::iter().for_each(|active_sonarr_block| {
if library_ui_blocks.contains(&active_sonarr_block) {
assert!(LibraryUi::accepts(active_sonarr_block.into()));
} else {
assert!(!LibraryUi::accepts(active_radarr_block.into()));
assert!(!LibraryUi::accepts(active_sonarr_block.into()));
}
});
}