notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

thread.rs (3641B)


      1 use crate::{
      2     actionbar::NoteAction,
      3     timeline::{ThreadSelection, TimelineCache, TimelineKind},
      4 };
      5 
      6 use nostrdb::Transaction;
      7 use notedeck::{MuteFun, RootNoteId, UnknownIds};
      8 use tracing::error;
      9 
     10 use super::{
     11     note::{contents::NoteContext, NoteOptions},
     12     timeline::TimelineTabView,
     13 };
     14 
     15 pub struct ThreadView<'a, 'd> {
     16     timeline_cache: &'a mut TimelineCache,
     17     unknown_ids: &'a mut UnknownIds,
     18     selected_note_id: &'a [u8; 32],
     19     note_options: NoteOptions,
     20     id_source: egui::Id,
     21     is_muted: &'a MuteFun,
     22     note_context: &'a mut NoteContext<'d>,
     23 }
     24 
     25 impl<'a, 'd> ThreadView<'a, 'd> {
     26     #[allow(clippy::too_many_arguments)]
     27     pub fn new(
     28         timeline_cache: &'a mut TimelineCache,
     29         unknown_ids: &'a mut UnknownIds,
     30         selected_note_id: &'a [u8; 32],
     31         note_options: NoteOptions,
     32         is_muted: &'a MuteFun,
     33         note_context: &'a mut NoteContext<'d>,
     34     ) -> Self {
     35         let id_source = egui::Id::new("threadscroll_threadview");
     36         ThreadView {
     37             timeline_cache,
     38             unknown_ids,
     39             selected_note_id,
     40             note_options,
     41             id_source,
     42             is_muted,
     43             note_context,
     44         }
     45     }
     46 
     47     pub fn id_source(mut self, id: egui::Id) -> Self {
     48         self.id_source = id;
     49         self
     50     }
     51 
     52     pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<NoteAction> {
     53         let txn = Transaction::new(self.note_context.ndb).expect("txn");
     54 
     55         ui.label(
     56             egui::RichText::new("Threads ALPHA! It's not done. Things will be broken.")
     57                 .color(egui::Color32::RED),
     58         );
     59 
     60         egui::ScrollArea::vertical()
     61             .id_salt(self.id_source)
     62             .animated(false)
     63             .auto_shrink([false, false])
     64             .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysVisible)
     65             .show(ui, |ui| {
     66                 let root_id = match RootNoteId::new(
     67                     self.note_context.ndb,
     68                     self.note_context.note_cache,
     69                     &txn,
     70                     self.selected_note_id,
     71                 ) {
     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.note_context.ndb,
     84                         self.note_context.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.note_context.ndb,
     96                     &txn,
     97                     self.unknown_ids,
     98                     self.note_context.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.is_muted,
    110                     self.note_context,
    111                 )
    112                 .show(ui)
    113             })
    114             .inner
    115     }
    116 }