import SwiftUI struct PostGridSearchHistoryView: View { @EnvironmentObject private var manager: BooruManager @EnvironmentObject private var settings: SettingsManager @State private var searchText: String = "" @Binding var selectedTab: Int @Binding var isPresented: Bool @State private var isShowingRemoveAllConfirmation = false var filteredHistory: [BooruSearchQuery] { guard !searchText.isEmpty else { return settings.searchHistory } return settings.searchHistory .filter { query in query.tags .joined(separator: " ") .lowercased() .contains(searchText.lowercased()) } } var body: some View { NavigationStack { VStack { if settings.searchHistory.isEmpty { ContentUnavailableView( "No History", systemImage: "magnifyingglass", description: Text("Recent searches will appear here.") ) } else { List { if filteredHistory.isEmpty, !searchText.isEmpty { Text("No matching history found") } ForEach( filteredHistory.sorted { $0.date > $1.date }, id: \.id ) { query in Button(action: { let previousProvider = settings.preferredBooru settings.preferredBooru = query.provider manager.searchText = query.tags.joined(separator: " ") selectedTab = 0 isPresented.toggle() if previousProvider == settings.preferredBooru { manager.performSearch() } }) { GenericItemView( item: query, removeAction: settings.removeSearchHistoryEntry ) } #if os(macOS) .buttonStyle(.plain) #endif } .onDelete(perform: settings.removeSearchHistoryEntry) } } } } .navigationTitle("Search History") .searchable(text: $searchText) .toolbar { ToolbarItem { Button(action: { isShowingRemoveAllConfirmation = true }) { Label("Remove All Search History", systemImage: "trash") } } } .confirmationDialog( "Are you sure you want to remove all searches? This action cannot be undone.", isPresented: $isShowingRemoveAllConfirmation ) { Button("Remove All Searches") { settings.bookmarks.removeAll() } } } } #Preview { PostGridSearchHistoryView( selectedTab: .constant(0), isPresented: .constant(true) ) .environmentObject(SettingsManager()) .environmentObject(BooruManager(.safebooru)) }