src/app/pages/search.rs
388 lines · 14480 bytes
1use std::{collections::LinkedList, error::Error};
2
3use crossterm::event::KeyCode;
4use invidious::structs::hidden::SearchItem as InvidiousSearchItem;
5use tui::{
6 backend::Backend,
7 layout::Rect,
8 style::{Color, Style},
9 Frame,
10};
11
12use crate::{
13 app::{
14 app::{App, AppNoState},
15 config::{
16 Action, ConstraintTransitional, ItemTransitional, LayoutConfig, RowItemTransitional,
17 RowTransitional, SearchItemTransitional,
18 },
19 },
20 functions::download_all_thumbnails,
21 structs::{Item, ListItem, MiniChannel, MiniPlayList, MiniVideo, Page, WatchHistory},
22 traits::{ItemTrait, PageTrait},
23 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
24};
25
26use super::{
27 channel::{Channel, ChannelPage},
28 global::GlobalItem,
29 item_info::{DisplayItem, ItemInfo},
30 main_menu::textlist_from_video_list,
31};
32
33#[derive(Debug, Clone)]
34pub enum SearchItem {
35 Search {
36 results: Option<LinkedList<ListItem>>,
37 text_list: TextList,
38 },
39}
40
41impl ItemTrait for SearchItem {
42 fn select(&mut self, app: App) -> (App, bool) {
43 (app, true)
44 }
45
46 fn selectable(&self) -> bool {
47 true
48 }
49
50 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
51 let action = match app.config.keybindings.0.get(&key) {
52 Some(action) => action,
53 None => return (false, app),
54 };
55 match self {
56 SearchItem::Search { results, text_list } => match action {
57 Action::Up => text_list.up(),
58 Action::Down => text_list.down(),
59 Action::FirstItem => text_list.first(),
60 Action::LastItem => text_list.last(),
61 Action::Select => {
62 if let Some(results_unwrapped) = results {
63 match results_unwrapped.iter().nth(text_list.selected).unwrap() {
64 ListItem::PageTurner(b) => {
65 let new_page_no;
66 if *b {
67 new_page_no = app.page_no + 1;
68 } else {
69 new_page_no = app.page_no - 1;
70 }
71
72 *results = None;
73 *text_list = TextList::default();
74
75 let state = app.config.layouts.search.clone().into();
76 let mut history = app.history.clone();
77 let search_text = app.search_text.clone();
78 let search_settings = app.search_settings.clone();
79 history.push(app.into());
80
81 return (
82 false,
83 App {
84 history,
85 page: Page::Search,
86 selectable: App::selectable(&state),
87 state,
88 search_text,
89 search_settings,
90 load: true,
91 page_no: new_page_no,
92 ..Default::default()
93 },
94 );
95 }
96
97 ListItem::MiniVideo(video) => {
98 let state = app.config.layouts.item_info.clone().into();
99 let mut history = app.history.clone();
100 history.push(app.into());
101
102 return (
103 false,
104 App {
105 history,
106 page: Page::ItemDisplay(DisplayItem::Video(
107 video.video_id.clone(),
108 )),
109 selectable: App::selectable(&state),
110 state,
111 ..Default::default()
112 },
113 );
114 }
115
116 ListItem::MiniPlayList(playlist) => {
117 let state = app.config.layouts.item_info.clone().into();
118 let mut history = app.history.clone();
119 history.push(app.into());
120
121 return (
122 false,
123 App {
124 history,
125 page: Page::ItemDisplay(DisplayItem::PlayList(
126 playlist.playlist_id.clone(),
127 )),
128 selectable: App::selectable(&state),
129 state,
130 ..Default::default()
131 },
132 );
133 }
134
135 ListItem::MiniChannel(channel) => {
136 let state = app.config.layouts.channel.clone().into();
137 let mut history = app.history.clone();
138 history.push(app.into());
139
140 return (
141 false,
142 App {
143 history,
144 page: Page::Channel(
145 ChannelPage::Home,
146 channel.author_id.clone(),
147 ),
148 selectable: App::selectable(&state),
149 state,
150 ..Default::default()
151 },
152 );
153 }
154
155 _ => {}
156 }
157 }
158 }
159 _ => {}
160 },
161 }
162
163 (true, app)
164 }
165
166 fn load_item(&self, app: &App, _: &mut WatchHistory) -> Result<Item, Box<dyn Error>> {
167 let mut this = self.clone();
168
169 match &mut this {
170 SearchItem::Search { results, text_list } => {
171 // panic!("got here");
172
173 let mut args = vec![app.search_text.clone()];
174 args.extend(app.search_settings.clone().to_vec());
175
176 let invidious_results = app
177 .client
178 .search(Some(&format!(
179 "page={}&q={}",
180 app.page_no,
181 args.join("&").as_str()
182 )))?
183 .items;
184
185 let mut items = LinkedList::new();
186
187 for item in invidious_results {
188 match item {
189 InvidiousSearchItem::Video {
190 title: _,
191 videoId: _,
192 author: _,
193 authorId: _,
194 authorUrl: _,
195 lengthSeconds: _,
196 videoThumbnails: _,
197 description: _,
198 descriptionHtml: _,
199 viewCount: _,
200 published: _,
201 publishedText: _,
202 liveNow: _,
203 paid: _,
204 premium: _,
205 } => {
206 items.push_back(ListItem::MiniVideo(MiniVideo::from(item)));
207 }
208
209 InvidiousSearchItem::Playlist {
210 title: _,
211 playlistId: _,
212 author: _,
213 authorId: _,
214 authorUrl: _,
215 videoCount: _,
216 videos: _,
217 } => {
218 items.push_back(ListItem::MiniPlayList(MiniPlayList::from(item)));
219 }
220
221 InvidiousSearchItem::Channel {
222 author: _,
223 authorId: _,
224 authorUrl: _,
225 authorThumbnails: _,
226 subCount: _,
227 videoCount: _,
228 description: _,
229 descriptionHtml: _,
230 } => {
231 items.push_back(ListItem::MiniChannel(MiniChannel::from(item)));
232 }
233
234 _ => {}
235 }
236 }
237
238 let mut minimum_length = 0;
239
240 if app.page_no != 1 {
241 minimum_length += 1;
242 items.push_front(ListItem::PageTurner(false));
243 }
244
245 if items.len() != minimum_length {
246 items.push_back(ListItem::PageTurner(true));
247 }
248
249 text_list.items = textlist_from_video_list(&items);
250
251 let mut thumbnails = LinkedList::new();
252
253 for item in items.iter() {
254 match item {
255 ListItem::MiniVideo(video) => {
256 thumbnails
257 .push_back((video.video_thumbnail.clone(), video.video_id.clone()));
258 }
259
260 ListItem::MiniPlayList(playlist) => {
261 if let Some(thumbnail) = &playlist.thumbnail {
262 thumbnails
263 .push_back((thumbnail.clone(), playlist.playlist_id.clone()));
264 }
265 }
266
267 ListItem::MiniChannel(channel) => {
268 thumbnails.push_back((
269 format!("https:{}", channel.thumbnail.clone()),
270 channel.author_id.clone(),
271 ));
272
273 // panic!("{:?}", channel.thumbnail.clone());
274 }
275
276 _ => {}
277 }
278 }
279
280 let _ = download_all_thumbnails(thumbnails);
281
282 *results = Some(items);
283 }
284 }
285
286 Ok(Item::Search(this))
287 }
288
289 fn render_item<B: Backend>(
290 &mut self,
291 frame: &mut Frame<B>,
292 rect: Rect,
293 app: AppNoState,
294 selected: bool,
295 hover: bool,
296 popup_focus: bool,
297 popup_render: bool,
298 ) -> (bool, AppNoState) {
299 let out = (false, app);
300
301 if popup_render {
302 return out;
303 }
304
305 match self {
306 SearchItem::Search { results, text_list } => {
307 let split = HorizontalSplit::default()
308 .percentages(vec![60, 40])
309 .border_style(Style::default().fg(if selected {
310 Color::LightBlue
311 } else if hover {
312 Color::LightRed
313 } else {
314 Color::Reset
315 }));
316
317 let chunks = split.inner(rect);
318
319 frame.render_widget(split, rect);
320
321 text_list.area(chunks[0]);
322
323 let mut text_list = text_list.clone();
324
325 if let Some(result) = results {
326 if let Some(item) = result.iter().nth(text_list.selected) {
327 if !popup_focus {
328 frame.render_widget(ItemDisplay { item: item.clone() }, chunks[1]);
329 }
330 }
331 }
332
333 if selected {
334 text_list.selected_style(Style::default().fg(Color::LightRed));
335 } else {
336 text_list.selected_style(Style::default().fg(Color::LightYellow));
337 }
338
339 frame.render_widget(text_list, chunks[0]);
340 }
341 }
342
343 out
344 }
345}
346
347pub struct Search;
348
349impl PageTrait for Search {
350 fn default() -> LayoutConfig {
351 LayoutConfig {
352 layout: vec![
353 RowTransitional {
354 items: vec![
355 RowItemTransitional {
356 item: ItemTransitional::Global(GlobalItem::SearchBar),
357 constraint: ConstraintTransitional::Min(16),
358 },
359 RowItemTransitional {
360 item: ItemTransitional::Global(GlobalItem::SearchSettings),
361 constraint: ConstraintTransitional::Length(5),
362 },
363 ],
364 centered: false,
365 height: ConstraintTransitional::Length(3),
366 },
367 RowTransitional {
368 items: vec![RowItemTransitional {
369 item: ItemTransitional::Search(SearchItemTransitional::Search),
370 constraint: ConstraintTransitional::Percentage(100),
371 }],
372 centered: false,
373 height: ConstraintTransitional::Min(6),
374 },
375 RowTransitional {
376 items: vec![RowItemTransitional {
377 item: ItemTransitional::Global(GlobalItem::MessageBar),
378 constraint: ConstraintTransitional::Percentage(100),
379 }],
380 centered: false,
381 height: ConstraintTransitional::Length(3),
382 },
383 ],
384 min: (45, 12),
385 message: String::from("Loading search results..."),
386 }
387 }
388}