Skip to content

Commit

Permalink
hashtags: click hashtags to open them
Browse files Browse the repository at this point in the history
Fixes: #695
Fixes: #713
Changelog-Added: Add ability to click hashtags
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Feb 6, 2025
1 parent 0cc1d8a commit ac10c7e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 79 deletions.
75 changes: 9 additions & 66 deletions crates/notedeck_columns/src/actionbar.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use crate::{
column::Columns,
route::{Route, Router},
timeline::{ThreadSelection, TimelineCache, TimelineKind},
timeline::{TimelineCache, TimelineKind},
};

use enostr::{NoteId, Pubkey, RelayPool};
use enostr::{NoteId, RelayPool};
use nostrdb::{Ndb, NoteKey, Transaction};
use notedeck::{note::root_note_id_from_selected_id, NoteCache, RootIdError, UnknownIds};
use notedeck::{NoteCache, UnknownIds};
use tracing::error;

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum NoteAction {
Reply(NoteId),
Quote(NoteId),
OpenThread(NoteId),
OpenProfile(Pubkey),
OpenTimeline(TimelineKind),
}

pub struct NewNotes {
Expand All @@ -26,52 +25,6 @@ pub enum TimelineOpenResult {
NewNotes(NewNotes),
}

/// open_thread is called when a note is selected and we need to navigate
/// to a thread It is responsible for managing the subscription and
/// making sure the thread is up to date. In a sense, it's a model for
/// the thread view. We don't have a concept of model/view/controller etc
/// in egui, but this is the closest thing to that.
#[allow(clippy::too_many_arguments)]
fn open_thread(
ndb: &Ndb,
txn: &Transaction,
router: &mut Router<Route>,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
timeline_cache: &mut TimelineCache,
selected_note: &[u8; 32],
) -> Option<TimelineOpenResult> {
router.route_to(Route::thread(
ThreadSelection::from_note_id(ndb, note_cache, txn, NoteId::new(*selected_note)).ok()?,
));

match root_note_id_from_selected_id(ndb, note_cache, txn, selected_note) {
Ok(root_id) => timeline_cache.open(
ndb,
note_cache,
txn,
pool,
&TimelineKind::Thread(ThreadSelection::from_root_id(root_id.to_owned())),
),

Err(RootIdError::NoteNotFound) => {
error!(
"open_thread: note not found: {}",
hex::encode(selected_note)
);
None
}

Err(RootIdError::NoRootId) => {
error!(
"open_thread: note has no root id: {}",
hex::encode(selected_note)
);
None
}
}
}

impl NoteAction {
#[allow(clippy::too_many_arguments)]
pub fn execute(
Expand All @@ -89,19 +42,9 @@ impl NoteAction {
None
}

NoteAction::OpenThread(note_id) => open_thread(
ndb,
txn,
router,
note_cache,
pool,
timeline_cache,
note_id.bytes(),
),

NoteAction::OpenProfile(pubkey) => {
router.route_to(Route::profile(*pubkey));
timeline_cache.open(ndb, note_cache, txn, pool, &TimelineKind::Profile(*pubkey))
NoteAction::OpenTimeline(kind) => {
router.route_to(Route::Timeline(kind.to_owned()));
timeline_cache.open(ndb, note_cache, txn, pool, kind)
}

NoteAction::Quote(note_id) => {
Expand All @@ -114,7 +57,7 @@ impl NoteAction {
/// Execute the NoteAction and process the TimelineOpenResult
#[allow(clippy::too_many_arguments)]
pub fn execute_and_process_result(
self,
&self,
ndb: &Ndb,
columns: &mut Columns,
col: usize,
Expand Down
2 changes: 1 addition & 1 deletion crates/notedeck_columns/src/timeline/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl TimelineKind {
TimelineKind::Hashtag(hashtag) => FilterState::ready(vec![Filter::new()
.kinds([1])
.limit(filter::default_limit())
.tags([hashtag.clone()], 't')
.tags([hashtag.to_lowercase()], 't')
.build()]),

TimelineKind::Algo(algo_timeline) => match algo_timeline {
Expand Down
2 changes: 1 addition & 1 deletion crates/notedeck_columns/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl Timeline {
let filter = Filter::new()
.kinds([1])
.limit(filter::default_limit())
.tags([hashtag.clone()], 't')
.tags([hashtag.to_lowercase()], 't')
.build();

Timeline::new(
Expand Down
6 changes: 4 additions & 2 deletions crates/notedeck_columns/src/ui/mention.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ui;
use crate::{actionbar::NoteAction, profile::get_display_name};
use crate::{actionbar::NoteAction, profile::get_display_name, timeline::TimelineKind};
use egui::Sense;
use enostr::Pubkey;
use nostrdb::{Ndb, Transaction};
Expand Down Expand Up @@ -89,7 +89,9 @@ fn mention_ui(

let note_action = if resp.clicked() {
ui::show_pointer(ui);
Some(NoteAction::OpenProfile(Pubkey::new(*pk)))
Some(NoteAction::OpenTimeline(TimelineKind::profile(
Pubkey::new(*pk),
)))
} else if resp.hovered() {
ui::show_pointer(ui);
None
Expand Down
13 changes: 10 additions & 3 deletions crates/notedeck_columns/src/ui/note/contents.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::actionbar::NoteAction;
use crate::images::ImageType;
use crate::ui::{
self,
note::{NoteOptions, NoteResponse},
ProfilePic,
};
use crate::{actionbar::NoteAction, images::ImageType, timeline::TimelineKind};
use egui::{Color32, Hyperlink, Image, RichText};
use nostrdb::{BlockType, Mention, Ndb, Note, NoteKey, Transaction};
use tracing::warn;
Expand Down Expand Up @@ -198,7 +197,15 @@ fn render_note_contents(
BlockType::Hashtag => {
#[cfg(feature = "profiling")]
puffin::profile_scope!("hashtag contents");
ui.colored_label(link_color, format!("#{}", block.as_str()));
let resp = ui.colored_label(link_color, format!("#{}", block.as_str()));

if resp.clicked() {
note_action = Some(NoteAction::OpenTimeline(TimelineKind::Hashtag(
block.as_str().to_string(),
)));
} else if resp.hovered() {
ui::show_pointer(ui);
}
}

BlockType::Url => {
Expand Down
25 changes: 19 additions & 6 deletions crates/notedeck_columns/src/ui/note/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use reply_description::reply_desc;
use crate::{
actionbar::NoteAction,
profile::get_display_name,
timeline::{ThreadSelection, TimelineKind},
ui::{self, View},
};

Expand Down Expand Up @@ -354,8 +355,9 @@ impl<'a> NoteView<'a> {
ui.vertical(|ui| {
ui.horizontal(|ui| {
if self.pfp(note_key, &profile, ui).clicked() {
note_action =
Some(NoteAction::OpenProfile(Pubkey::new(*self.note.pubkey())));
note_action = Some(NoteAction::OpenTimeline(TimelineKind::profile(
Pubkey::new(*self.note.pubkey()),
)));
};

let size = ui.available_size();
Expand Down Expand Up @@ -415,7 +417,7 @@ impl<'a> NoteView<'a> {
ui.add(&mut contents);

if let Some(action) = contents.action() {
note_action = Some(*action);
note_action = Some(action.clone());
}

if self.options().has_actionbar() {
Expand All @@ -430,7 +432,9 @@ impl<'a> NoteView<'a> {
// main design
ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
if self.pfp(note_key, &profile, ui).clicked() {
note_action = Some(NoteAction::OpenProfile(Pubkey::new(*self.note.pubkey())));
note_action = Some(NoteAction::OpenTimeline(TimelineKind::Profile(
Pubkey::new(*self.note.pubkey()),
)));
};

ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| {
Expand Down Expand Up @@ -480,7 +484,7 @@ impl<'a> NoteView<'a> {
ui.add(&mut contents);

if let Some(action) = contents.action() {
note_action = Some(*action);
note_action = Some(action.clone());
}

if self.options().has_actionbar() {
Expand All @@ -496,7 +500,16 @@ impl<'a> NoteView<'a> {
};

let note_action = if note_hitbox_clicked(ui, hitbox_id, &response.rect, maybe_hitbox) {
Some(NoteAction::OpenThread(NoteId::new(*self.note.id())))
if let Ok(selection) = ThreadSelection::from_note_id(
self.ndb,
self.note_cache,
self.note.txn().unwrap(),
NoteId::new(*self.note.id()),
) {
Some(NoteAction::OpenTimeline(TimelineKind::Thread(selection)))
} else {
None
}
} else {
note_action
};
Expand Down

0 comments on commit ac10c7e

Please sign in to comment.