thread.rs (3671B)
1 use crate::{ 2 actionbar::NoteAction, 3 timeline::{ThreadSelection, TimelineCache, TimelineKind}, 4 ui::note::NoteOptions, 5 }; 6 7 use nostrdb::{Ndb, Transaction}; 8 use notedeck::{Images, MuteFun, NoteCache, RootNoteId, UnknownIds}; 9 use tracing::error; 10 11 use super::timeline::TimelineTabView; 12 13 pub struct ThreadView<'a> { 14 timeline_cache: &'a mut TimelineCache, 15 ndb: &'a Ndb, 16 note_cache: &'a mut NoteCache, 17 unknown_ids: &'a mut UnknownIds, 18 img_cache: &'a mut Images, 19 selected_note_id: &'a [u8; 32], 20 note_options: NoteOptions, 21 id_source: egui::Id, 22 is_muted: &'a MuteFun, 23 } 24 25 impl<'a> ThreadView<'a> { 26 #[allow(clippy::too_many_arguments)] 27 pub fn new( 28 timeline_cache: &'a mut TimelineCache, 29 ndb: &'a Ndb, 30 note_cache: &'a mut NoteCache, 31 unknown_ids: &'a mut UnknownIds, 32 img_cache: &'a mut Images, 33 selected_note_id: &'a [u8; 32], 34 note_options: NoteOptions, 35 is_muted: &'a MuteFun, 36 ) -> Self { 37 let id_source = egui::Id::new("threadscroll_threadview"); 38 ThreadView { 39 timeline_cache, 40 ndb, 41 note_cache, 42 unknown_ids, 43 img_cache, 44 selected_note_id, 45 note_options, 46 id_source, 47 is_muted, 48 } 49 } 50 51 pub fn id_source(mut self, id: egui::Id) -> Self { 52 self.id_source = id; 53 self 54 } 55 56 pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<NoteAction> { 57 let txn = Transaction::new(self.ndb).expect("txn"); 58 59 ui.label( 60 egui::RichText::new("Threads ALPHA! It's not done. Things will be broken.") 61 .color(egui::Color32::RED), 62 ); 63 64 egui::ScrollArea::vertical() 65 .id_salt(self.id_source) 66 .animated(false) 67 .auto_shrink([false, false]) 68 .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysVisible) 69 .show(ui, |ui| { 70 let root_id = 71 match RootNoteId::new(self.ndb, self.note_cache, &txn, self.selected_note_id) { 72 Ok(root_id) => root_id, 73 74 Err(err) => { 75 ui.label(format!("Error loading thread: {:?}", err)); 76 return None; 77 } 78 }; 79 80 let thread_timeline = self 81 .timeline_cache 82 .notes( 83 self.ndb, 84 self.note_cache, 85 &txn, 86 &TimelineKind::Thread(ThreadSelection::from_root_id(root_id.to_owned())), 87 ) 88 .get_ptr(); 89 90 // TODO(jb55): skip poll if ThreadResult is fresh? 91 92 let reversed = true; 93 // poll for new notes and insert them into our existing notes 94 if let Err(err) = thread_timeline.poll_notes_into_view( 95 self.ndb, 96 &txn, 97 self.unknown_ids, 98 self.note_cache, 99 reversed, 100 ) { 101 error!("error polling notes into thread timeline: {err}"); 102 } 103 104 TimelineTabView::new( 105 thread_timeline.current_view(), 106 true, 107 self.note_options, 108 &txn, 109 self.ndb, 110 self.note_cache, 111 self.img_cache, 112 self.is_muted, 113 ) 114 .show(ui) 115 }) 116 .inner 117 } 118 }