threads: add initial thread support

This is a really dumb and broken version of threads, but it will be our
foundation for future changes.

All it currently does is load whatever notes we have locally for a
thread in chronological order. It currently does not open any
subscriptions. It is not clear what is replying to what, but hey, its a
start.

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-07-16 12:48:57 -07:00
parent 1024affdbd
commit 33e5b6886b
5 changed files with 189 additions and 7 deletions

85
src/ui/thread.rs Normal file
View File

@@ -0,0 +1,85 @@
use crate::{ui, Damus};
use nostrdb::{Note, NoteReply};
use tracing::warn;
pub struct ThreadView<'a> {
app: &'a mut Damus,
timeline: usize,
selected_note: &'a Note<'a>,
}
impl<'a> ThreadView<'a> {
pub fn new(app: &'a mut Damus, timeline: usize, selected_note: &'a Note<'a>) -> Self {
ThreadView {
app,
timeline,
selected_note,
}
}
pub fn ui(&mut self, ui: &mut egui::Ui) {
let txn = self.selected_note.txn().unwrap();
let key = self.selected_note.key().unwrap();
let scroll_id = egui::Id::new((
"threadscroll",
self.app.timelines[self.timeline].selected_view,
self.timeline,
key,
));
ui.label(
egui::RichText::new("Threads ALPHA! It's not done. Things will be broken.")
.color(egui::Color32::RED),
);
egui::ScrollArea::vertical()
.id_source(scroll_id)
.animated(false)
.auto_shrink([false, false])
.scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysVisible)
.show(ui, |ui| {
let root_id = NoteReply::new(self.selected_note.tags())
.root()
.map_or_else(|| self.selected_note.id(), |nr| nr.id);
let (len, list) = {
let thread = self.app.threads.thread_mut(&self.app.ndb, txn, root_id);
let len = thread.view.notes.len();
(len, &mut thread.view.list)
};
list.clone()
.borrow_mut()
.ui_custom_layout(ui, len, |ui, start_index| {
ui.spacing_mut().item_spacing.y = 0.0;
ui.spacing_mut().item_spacing.x = 4.0;
let note_key = {
let thread = self.app.threads.thread_mut(&self.app.ndb, txn, root_id);
thread.view.notes[start_index].key
};
let note = if let Ok(note) = self.app.ndb.get_note_by_key(txn, note_key) {
note
} else {
warn!("failed to query note {:?}", note_key);
return 0;
};
ui::padding(8.0, ui, |ui| {
let textmode = self.app.textmode;
let resp = ui::NoteView::new(self.app, &note)
.note_previews(!textmode)
.show(ui);
if let Some(action) = resp.action {
action.execute(self.app, self.timeline, note.id());
}
});
ui::hline(ui);
//ui.add(egui::Separator::default().spacing(0.0));
1
});
});
}
}