feat: improve compose modal

This commit is contained in:
reya
2025-01-17 09:05:46 +07:00
parent b119722392
commit 8f6bedf70a
14 changed files with 328 additions and 342 deletions
+32 -36
View File
@@ -1,12 +1,11 @@
use chrono::{Duration, Local, TimeZone};
use crate::{constants::NIP96_SERVER, get_client};
use chrono::{Datelike, Local, TimeZone};
use nostr_sdk::prelude::*;
use std::{
collections::HashSet,
hash::{DefaultHasher, Hash, Hasher},
};
use crate::{constants::NIP96_SERVER, get_client};
pub async fn nip96_upload(file: Vec<u8>) -> anyhow::Result<Url, anyhow::Error> {
let client = get_client();
let signer = client.signer().await?;
@@ -42,50 +41,47 @@ pub fn shorted_public_key(public_key: PublicKey) -> String {
format!("{}:{}", &pk[0..4], &pk[pk.len() - 4..])
}
pub fn show_npub(public_key: PublicKey, len: usize) -> String {
let bech32 = public_key.to_bech32().unwrap_or_default();
let separator = " ... ";
let sep_len = separator.len();
let chars_to_show = len - sep_len;
let front_chars = (chars_to_show + 1) / 2; // ceil
let back_chars = chars_to_show / 2; // floor
format!(
"{}{}{}",
&bech32[..front_chars],
separator,
&bech32[bech32.len() - back_chars..]
)
}
pub fn ago(time: Timestamp) -> String {
pub fn message_ago(time: Timestamp) -> String {
let now = Local::now();
let input_time = Local.timestamp_opt(time.as_u64() as i64, 0).unwrap();
let diff = (now - input_time).num_hours();
if diff < 24 {
let duration = now.signed_duration_since(input_time);
format_duration(duration)
if duration.num_seconds() < 60 {
"now".to_string()
} else if duration.num_minutes() == 1 {
"1m".to_string()
} else if duration.num_minutes() < 60 {
format!("{}m", duration.num_minutes())
} else if duration.num_hours() == 1 {
"1h".to_string()
} else if duration.num_hours() < 24 {
format!("{}h", duration.num_hours())
} else if duration.num_days() == 1 {
"1d".to_string()
} else {
format!("{}d", duration.num_days())
}
} else {
input_time.format("%b %d").to_string()
}
}
pub fn format_duration(duration: Duration) -> String {
if duration.num_seconds() < 60 {
"now".to_string()
} else if duration.num_minutes() == 1 {
"1m".to_string()
} else if duration.num_minutes() < 60 {
format!("{}m", duration.num_minutes())
} else if duration.num_hours() == 1 {
"1h".to_string()
} else if duration.num_hours() < 24 {
format!("{}h", duration.num_hours())
} else if duration.num_days() == 1 {
"1d".to_string()
pub fn message_time(time: Timestamp) -> String {
let now = Local::now();
let input_time = Local.timestamp_opt(time.as_u64() as i64, 0).unwrap();
if input_time.day() == now.day() {
format!("Today at {}", input_time.format("%H:%M %p"))
} else if input_time.day() == now.day() - 1 {
format!("Yesterday at {}", input_time.format("%H:%M %p"))
} else {
format!("{}d", duration.num_days())
format!(
"{}, {}",
input_time.format("%d/%m/%y"),
input_time.format("%H:%M %p")
)
}
}