Skip to content

Commit

Permalink
Add post form
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Jan 3, 2024
1 parent 69b1aef commit 5d13750
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .config/config.json5
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"<q>": "Quit", // Quit the application
"<Ctrl-d>": "Quit", // Another way to quit
"<Ctrl-c>": "Quit", // Yet another way to quit
"<Ctrl-z>": "Suspend" // Suspend the application
"<Ctrl-z>": "Suspend", // Suspend the application
"<n>": "NewTextNote" // Show the text note input form
}
},
"relays": [
Expand Down
6 changes: 5 additions & 1 deletion src/action.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fmt, string::ToString};

use crossterm::event::KeyEvent;
use nostr_sdk::prelude::*;
use serde::{
de::{self, Deserializer, Visitor},
Expand All @@ -18,7 +19,7 @@ pub enum Action {
Refresh,
Error(String),
Help,
ReceiveEvent(nostr_sdk::Event),
ReceiveEvent(Event),
ScrollUp,
ScrollDown,
ScrollToTop,
Expand All @@ -28,4 +29,7 @@ pub enum Action {
Repost,
SendRepost(EventId),
Unselect,
NewTextNote,
SendTextNote(String),
Key(KeyEvent),
}
2 changes: 2 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ impl App {
tui::Event::Render => action_tx.send(Action::Render)?,
tui::Event::Resize(x, y) => action_tx.send(Action::Resize(x, y))?,
tui::Event::Key(key) => {
action_tx.send(Action::Key(key))?;

if let Some(keymap) = self.config.keybindings.get(&self.mode) {
if let Some(action) = keymap.get(&vec![key]) {
log::info!("Got action: {action:?}");
Expand Down
58 changes: 50 additions & 8 deletions src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use nostr_sdk::prelude::*;
use ratatui::{prelude::*, widgets, widgets::*};
use sorted_vec::ReverseSortedSet;
use tokio::sync::mpsc::UnboundedSender;
use tui_textarea::{Input, Key, TextArea};

use super::{Component, Frame};
use crate::{
Expand All @@ -15,7 +16,7 @@ use crate::{
};

#[derive(Default)]
pub struct Home {
pub struct Home<'a> {
command_tx: Option<UnboundedSender<Action>>,
config: Config,
list_state: ListState,
Expand All @@ -24,9 +25,11 @@ pub struct Home {
reactions: HashMap<EventId, HashSet<Event>>,
reposts: HashMap<EventId, HashSet<Event>>,
zap_receipts: HashMap<EventId, HashSet<Event>>,
show_input: bool,
input: TextArea<'a>,
}

impl Home {
impl<'a> Home<'a> {
pub fn new() -> Self {
Self::default()
}
Expand Down Expand Up @@ -127,7 +130,7 @@ impl Home {
}
}

impl Component for Home {
impl<'a> Component for Home<'a> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
self.command_tx = Some(tx);
Ok(())
Expand All @@ -148,10 +151,26 @@ impl Component for Home {
Kind::ZapReceipt => self.append_zap_receipt(ev),
_ => {}
},
Action::ScrollUp => self.scroll_up(),
Action::ScrollDown => self.scroll_down(),
Action::ScrollToTop => self.scroll_to_top(),
Action::ScrollToBottom => self.scroll_to_bottom(),
Action::ScrollUp => {
if !self.show_input {
self.scroll_up()
}
}
Action::ScrollDown => {
if !self.show_input {
self.scroll_down()
}
}
Action::ScrollToTop => {
if !self.show_input {
self.scroll_to_top()
}
}
Action::ScrollToBottom => {
if !self.show_input {
self.scroll_to_bottom()
}
}
Action::React => {
if let (Some(i), Some(tx)) = (self.list_state.selected(), &self.command_tx) {
let event = self.notes.get(i).expect("failed to get target event");
Expand All @@ -166,6 +185,15 @@ impl Component for Home {
}
Action::Unselect => {
self.list_state.select(None);
self.show_input = false;
}
Action::NewTextNote => {
self.show_input = true;
}
Action::Key(key) => {
if self.show_input {
self.input.input(key);
}
}
_ => {}
}
Expand All @@ -188,11 +216,25 @@ impl Component for Home {

f.render_stateful_widget(list, area, &mut self.list_state);

if self.show_input {
let mut input_area = f.size();
input_area.height /= 2;
input_area.y = input_area.height;
f.render_widget(Clear, input_area);

self.input.set_block(
widgets::Block::default()
.borders(Borders::ALL)
.title("New note: Press ESC to close"),
);
f.render_widget(self.input.widget(), input_area);
}

Ok(())
}
}

impl ScrollableList<Event> for Home {
impl<'a> ScrollableList<Event> for Home<'a> {
fn select(&mut self, index: Option<usize>) {
self.list_state.select(index);
}
Expand Down

0 comments on commit 5d13750

Please sign in to comment.