From 72b8b19f52e8aa92b083b8d673d696fa5ee5ac01 Mon Sep 17 00:00:00 2001 From: objz Date: Sun, 26 Jul 2026 22:00:07 +0200 Subject: [PATCH] fix(discovery): guard unsupported vanilla content --- src/net/modrinth.rs | 31 +++++++++++++++++++++ src/tui/input.rs | 30 ++++++++++---------- src/tui/widgets/content/discovery.rs | 41 ++++++++++++++++++++++++++++ src/tui/widgets/content/tabs.rs | 25 +++++++++++++++++ 4 files changed, 113 insertions(+), 14 deletions(-) diff --git a/src/net/modrinth.rs b/src/net/modrinth.rs index a373e7a..b4dce7f 100644 --- a/src/net/modrinth.rs +++ b/src/net/modrinth.rs @@ -80,6 +80,17 @@ impl DiscoveryKind { Self::Shader => "shader", } } + + pub fn unavailable_message(self, loader: ModLoader) -> Option<&'static str> { + if loader != ModLoader::Vanilla { + return None; + } + match self { + Self::Mod => Some("Vanilla does not support mods."), + Self::Shader => Some("Vanilla does not support shaders."), + Self::ResourcePack => None, + } + } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -589,6 +600,26 @@ mod tests { ); } + #[test] + fn vanilla_discovery_only_supports_resource_packs() { + assert_eq!( + DiscoveryKind::Mod.unavailable_message(ModLoader::Vanilla), + Some("Vanilla does not support mods.") + ); + assert_eq!( + DiscoveryKind::Shader.unavailable_message(ModLoader::Vanilla), + Some("Vanilla does not support shaders.") + ); + assert_eq!( + DiscoveryKind::ResourcePack.unavailable_message(ModLoader::Vanilla), + None + ); + assert_eq!( + DiscoveryKind::Mod.unavailable_message(ModLoader::Fabric), + None + ); + } + #[test] fn compatible_mod_versions_filter_by_game_and_loader() { let url = content_versions_url( diff --git a/src/tui/input.rs b/src/tui/input.rs index 90d87d3..e3bc483 100644 --- a/src/tui/input.rs +++ b/src/tui/input.rs @@ -632,21 +632,15 @@ impl App { let Some(instance) = self.instances_state.selected_instance().cloned() else { return; }; - let (needs_search, search_due) = match self.content_tab { - widgets::content::ContentTab::Mods => ( - self.mods_discovery_state.needs_search(&instance), - self.mods_discovery_state.search_due(), - ), - widgets::content::ContentTab::ResourcePacks => ( - self.resource_packs_discovery_state.needs_search(&instance), - self.resource_packs_discovery_state.search_due(), - ), - widgets::content::ContentTab::Shaders => ( - self.shaders_discovery_state.needs_search(&instance), - self.shaders_discovery_state.search_due(), - ), - _ => (false, false), + let Some(state) = self.active_discovery_state_mut() else { + return; }; + if state.unavailable_message(&instance).is_some() { + state.set_unavailable(&instance); + return; + } + let needs_search = state.needs_search(&instance); + let search_due = state.search_due(); if needs_search || search_due { self.spawn_active_discovery_search(); } else { @@ -996,6 +990,10 @@ impl App { widgets::content::ContentTab::Shaders => &mut self.shaders_discovery_state, _ => return, }; + if state.unavailable_message(&instance).is_some() { + state.set_unavailable(&instance); + return; + } let kind = state.kind; let query = state.search.query.clone(); let request = state.begin_search(&instance); @@ -1031,6 +1029,10 @@ impl App { widgets::content::ContentTab::Shaders => &mut self.shaders_discovery_state, _ => return, }; + if state.unavailable_message(&instance).is_some() { + state.set_unavailable(&instance); + return; + } let kind = state.kind; let query = state.search.query.clone(); let Some(request) = state.begin_next_page() else { diff --git a/src/tui/widgets/content/discovery.rs b/src/tui/widgets/content/discovery.rs index d2af67d..34eb469 100644 --- a/src/tui/widgets/content/discovery.rs +++ b/src/tui/widgets/content/discovery.rs @@ -215,6 +215,22 @@ impl DiscoveryState { self.context.as_deref() != Some(discovery_context(instance).as_str()) } + pub fn unavailable_message(&self, instance: &InstanceConfig) -> Option<&'static str> { + self.kind.unavailable_message(instance.loader) + } + + pub fn set_unavailable(&mut self, instance: &InstanceConfig) { + let context = discovery_context(instance); + if self.context.as_deref() == Some(&context) && self.list.entries.is_empty() { + return; + } + self.context = None; + drop(self.begin_search(instance)); + self.stream = None; + self.page_loading = false; + self.exhausted = true; + } + pub fn begin_search(&mut self, instance: &InstanceConfig) -> DiscoveryRequest { self.generation = self.generation.wrapping_add(1); self.project_page = None; @@ -1252,6 +1268,31 @@ mod tests { assert!(state.needs_search(&second)); } + #[test] + fn unavailable_vanilla_discovery_clears_cached_results() { + let mut state = DiscoveryState::new(DiscoveryKind::Mod); + state.list.entries.push(project_entry( + DiscoveryProject { + id: "cached".to_owned(), + slug: "cached".to_owned(), + title: "Cached".to_owned(), + description: String::new(), + downloads: 0, + icon_url: None, + icon_bytes: None, + }, + None, + )); + let mut vanilla = instance("vanilla", "1.21.1"); + vanilla.loader = ModLoader::Vanilla; + + state.set_unavailable(&vanilla); + + assert!(state.list.entries.is_empty()); + assert!(!state.page_loading); + assert!(state.exhausted); + } + #[test] fn changing_instance_compatibility_invalidates_results() { let mut state = DiscoveryState::new(DiscoveryKind::Mod); diff --git a/src/tui/widgets/content/tabs.rs b/src/tui/widgets/content/tabs.rs index a384499..066c46a 100644 --- a/src/tui/widgets/content/tabs.rs +++ b/src/tui/widgets/content/tabs.rs @@ -223,11 +223,25 @@ pub fn render( ContentTab::Shaders => shaders_discovery_state.project_page_open(), _ => false, }; + let discovery_unavailable = mode == ContentMode::Discover + && instance.is_some_and(|instance| match tab { + ContentTab::Mods => mods_discovery_state.unavailable_message(instance).is_some(), + ContentTab::ResourcePacks => resource_packs_discovery_state + .unavailable_message(instance) + .is_some(), + ContentTab::Shaders => shaders_discovery_state + .unavailable_message(instance) + .is_some(), + _ => false, + }); // keybinds change depending on which tab is active and whether // the content panel or instances panel has focus let kb: Option<&[(&str, &str)]> = if is_focused { Some(match (mode, tab) { + (ContentMode::Discover, _) if discovery_unavailable => { + &[("h/l", " tabs"), ("Tab", " installed")] + } (ContentMode::Discover, _) if discovery_page_open => { &[("j/k", " scroll"), ("g/G", " top/bottom"), ("h", " back")] } @@ -347,6 +361,7 @@ pub fn render( frame, content_area, mods_discovery_state, + instance, is_focused, "Searching Modrinth...", picker, @@ -390,6 +405,7 @@ pub fn render( frame, content_area, resource_packs_discovery_state, + instance, is_focused, "Searching Modrinth...", picker, @@ -433,6 +449,7 @@ pub fn render( frame, content_area, shaders_discovery_state, + instance, is_focused, "Searching Modrinth...", picker, @@ -533,11 +550,19 @@ fn render_discovery( frame: &mut Frame, area: Rect, state: &mut DiscoveryState, + instance: &crate::instance::InstanceConfig, is_focused: bool, loading_text: &str, picker: &ratatui_image::picker::Picker, ) { state.set_viewport_rows(area.height); + if let Some(message) = state.unavailable_message(instance) { + frame.render_widget( + Paragraph::new(message).style(Style::default().fg(THEME.as_ref().text_dim())), + area, + ); + return; + } if let Some(page) = state.project_page.as_mut() { if let Some(error) = page.error.as_deref() { frame.render_widget(