Files
notedeck/src/draft.rs
William Casarin 7f234935cc refactor: unify note, post and nav actions
There was a bunch of redundant responses. Let's unify them under
the RenderNavAction enum. We unify all action processing under this
type.

This also centralizes all of our side effects into a single function
instead of scattering them everywhere
2024-11-19 18:43:09 -08:00

47 lines
1.0 KiB
Rust

use crate::ui::note::PostType;
use std::collections::HashMap;
#[derive(Default)]
pub struct Draft {
pub buffer: String,
}
#[derive(Default)]
pub struct Drafts {
replies: HashMap<[u8; 32], Draft>,
quotes: HashMap<[u8; 32], Draft>,
compose: Draft,
}
impl Drafts {
pub fn compose_mut(&mut self) -> &mut Draft {
&mut self.compose
}
pub fn get_from_post_type(&mut self, post_type: &PostType) -> &mut Draft {
match post_type {
PostType::New => self.compose_mut(),
PostType::Quote(note_id) => self.quote_mut(note_id.bytes()),
PostType::Reply(note_id) => self.reply_mut(note_id.bytes()),
}
}
pub fn reply_mut(&mut self, id: &[u8; 32]) -> &mut Draft {
self.replies.entry(*id).or_default()
}
pub fn quote_mut(&mut self, id: &[u8; 32]) -> &mut Draft {
self.quotes.entry(*id).or_default()
}
}
impl Draft {
pub fn new() -> Self {
Draft::default()
}
pub fn clear(&mut self) {
self.buffer = "".to_string();
}
}