Nomos chat fixes (#555)
* Make chat messages unique Since DA will rightfully ignore duplicated messages, we need to add a nonce to make sure every message is unique, so that a user can send the same message twice. This nonce is randomly generated for simplicity. * Clear in_flight status upon error on chat app
This commit is contained in:
parent
285300f365
commit
b7d1fd9256
|
@ -35,4 +35,5 @@ once_cell = "1"
|
|||
crossterm = "0.27"
|
||||
ratatui = "0.24"
|
||||
tui-input = "0.8"
|
||||
ansi-to-tui = "3"
|
||||
ansi-to-tui = "3"
|
||||
rand = "0.8"
|
|
@ -160,9 +160,8 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) {
|
|||
terminal.draw(|f| ui::ui(f, &app)).unwrap();
|
||||
|
||||
if let Ok(update) = app.status_updates.try_recv() {
|
||||
if let Status::Done = update {
|
||||
if let Status::Done | Status::Err(_) = update {
|
||||
app.message_in_flight = false;
|
||||
app.message_status = None;
|
||||
}
|
||||
app.message_status = Some(update);
|
||||
app.last_updated = Instant::now();
|
||||
|
@ -191,6 +190,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) {
|
|||
wire::serialize(&ChatMessage {
|
||||
author: app.username.clone().unwrap(),
|
||||
message: app.input.value().into(),
|
||||
_nonce: rand::random(),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
|
@ -223,6 +223,9 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) {
|
|||
struct ChatMessage {
|
||||
author: String,
|
||||
message: String,
|
||||
// Since DA will rightfully ignore duplicated messages, we need to add a nonce to make sure
|
||||
// every message is unique. This is randomly generated for simplicity.
|
||||
_nonce: u64,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
|
|
@ -124,25 +124,31 @@ fn render_messages(f: &mut Frame, app: &App, rect: Rect) {
|
|||
let messages: Vec<ListItem> = app
|
||||
.messages
|
||||
.iter()
|
||||
.map(|ChatMessage { author, message }| {
|
||||
let content = if author == app.username.as_ref().unwrap() {
|
||||
static MARGIN: usize = 2;
|
||||
// pad to make it appear aligned on the right
|
||||
let pad = " ".repeat(
|
||||
(rect.width as usize)
|
||||
.saturating_sub(message.len())
|
||||
.saturating_sub(MARGIN),
|
||||
);
|
||||
Line::from(vec![Span::raw(pad), Span::raw(message)])
|
||||
} else {
|
||||
Line::from(vec![
|
||||
Span::styled(format!("{author}: "), Style::new().fg(Color::Yellow).bold()),
|
||||
Span::raw(message),
|
||||
])
|
||||
.alignment(Alignment::Left)
|
||||
};
|
||||
ListItem::new(content)
|
||||
})
|
||||
.map(
|
||||
|ChatMessage {
|
||||
author,
|
||||
message,
|
||||
_nonce,
|
||||
}| {
|
||||
let content = if author == app.username.as_ref().unwrap() {
|
||||
static MARGIN: usize = 2;
|
||||
// pad to make it appear aligned on the right
|
||||
let pad = " ".repeat(
|
||||
(rect.width as usize)
|
||||
.saturating_sub(message.len())
|
||||
.saturating_sub(MARGIN),
|
||||
);
|
||||
Line::from(vec![Span::raw(pad), Span::raw(message)])
|
||||
} else {
|
||||
Line::from(vec![
|
||||
Span::styled(format!("{author}: "), Style::new().fg(Color::Yellow).bold()),
|
||||
Span::raw(message),
|
||||
])
|
||||
.alignment(Alignment::Left)
|
||||
};
|
||||
ListItem::new(content)
|
||||
},
|
||||
)
|
||||
.collect();
|
||||
let messages =
|
||||
List::new(messages).block(Block::default().borders(Borders::ALL).title("Messages"));
|
||||
|
|
Loading…
Reference in New Issue