diff --git a/crates/ely_app/src/shell/internal_pages/reading_list.rs b/crates/ely_app/src/shell/internal_pages/reading_list.rs index ca85d8d..a7a04e6 100644 --- a/crates/ely_app/src/shell/internal_pages/reading_list.rs +++ b/crates/ely_app/src/shell/internal_pages/reading_list.rs @@ -143,6 +143,7 @@ impl ElyShell { .child(progress_label(entry.progress())), ) .child(render_progress_action(index, entry_id, progress, cx)) + .child(render_remove_action(index, entry.id().clone(), cx)) .into_any_element() } } @@ -210,6 +211,23 @@ fn render_progress_action( .into_any_element() } +fn render_remove_action( + index: usize, + entry_id: ReadingListId, + cx: &mut Context, +) -> AnyElement { + Button::new(("remove-reading-list", index)) + .danger() + .xsmall() + .icon(IconName::Delete) + .label("Remove") + .tooltip("Remove From Reading List") + .on_click(cx.listener(move |shell, _, _, cx| { + shell.remove_reading_list_entry(&entry_id, cx); + })) + .into_any_element() +} + fn reading_list_count_label(count: usize) -> String { match count { 1 => "1 item".to_string(), diff --git a/crates/ely_app/src/shell/reading_list.rs b/crates/ely_app/src/shell/reading_list.rs index c2ed2d8..7eb6485 100644 --- a/crates/ely_app/src/shell/reading_list.rs +++ b/crates/ely_app/src/shell/reading_list.rs @@ -16,4 +16,16 @@ impl ElyShell { cx.notify(); } } + + pub(super) fn remove_reading_list_entry( + &mut self, + entry_id: &ReadingListId, + cx: &mut Context, + ) { + if let ShellState::Ready(core) = &mut self.state + && core.remove_reading_list_entry(entry_id).is_ok() + { + cx.notify(); + } + } } diff --git a/crates/ely_browser_core/src/state/reading_list.rs b/crates/ely_browser_core/src/state/reading_list.rs index 5f9cb22..f168c6a 100644 --- a/crates/ely_browser_core/src/state/reading_list.rs +++ b/crates/ely_browser_core/src/state/reading_list.rs @@ -36,6 +36,12 @@ impl BrowserCore { Ok(()) } + pub fn remove_reading_list_entry(&mut self, entry_id: &ReadingListId) -> Result<(), CoreError> { + let index = self.reading_list_entry_index(entry_id)?; + self.reading_list.remove(index); + Ok(()) + } + pub(super) fn find_reading_list_match(&self, query: &str) -> Option { let normalized_query = query.trim().to_lowercase(); if normalized_query.is_empty() { @@ -62,9 +68,14 @@ impl BrowserCore { &mut self, entry_id: &ReadingListId, ) -> Result<&mut ReadingListEntry, CoreError> { + let index = self.reading_list_entry_index(entry_id)?; + Ok(&mut self.reading_list[index]) + } + + fn reading_list_entry_index(&self, entry_id: &ReadingListId) -> Result { self.reading_list - .iter_mut() - .find(|entry| entry.id() == entry_id) + .iter() + .position(|entry| entry.id() == entry_id) .ok_or_else(|| CoreError::ReadingListEntryNotFound { id: entry_id.clone() }) } } diff --git a/crates/ely_browser_core/tests/reading_list.rs b/crates/ely_browser_core/tests/reading_list.rs index ed02454..2cfab78 100644 --- a/crates/ely_browser_core/tests/reading_list.rs +++ b/crates/ely_browser_core/tests/reading_list.rs @@ -70,6 +70,32 @@ fn missing_reading_list_progress_update_returns_error() -> Result<(), Box Result<(), Box> { + let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?; + core.open_tab(UrlText::parse("https://example.com/long-read")?); + let entry_id = core.save_active_tab_to_reading_list()?; + + core.remove_reading_list_entry(&entry_id)?; + let snapshot = core.snapshot()?; + + assert!(snapshot.reading_list.is_empty()); + Ok(()) +} + +#[test] +fn missing_reading_list_remove_returns_error() -> Result<(), Box> { + let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?; + let missing_id = ReadingListId::new(); + + let Err(error) = core.remove_reading_list_entry(&missing_id) else { + return Err("expected missing reading list entry error".into()); + }; + + assert_eq!(error, ely_browser_core::CoreError::ReadingListEntryNotFound { id: missing_id }); + Ok(()) +} + #[test] fn reading_list_scoped_search_opens_matching_entry() -> Result<(), Box> { let mut core = BrowserCore::new(InitialBrowserConfig::ely_defaults()?)?;