diff --git a/Cargo.lock b/Cargo.lock index 46d3ab8c..162ecd07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -804,6 +804,17 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "fancy-duration" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5260c33bfcf92e6eb34b72343cbfcd74da5d793a082fc39b5af7fd14db68639a" +dependencies = [ + "anyhow", + "lazy_static", + "regex", +] + [[package]] name = "fastrand" version = "1.9.0" @@ -3237,6 +3248,7 @@ version = "0.1.0" dependencies = [ "base64 0.21.2", "console_error_panic_hook", + "fancy-duration", "futures", "getrandom", "gloo-dialogs", @@ -3253,6 +3265,7 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "web-sys", + "web-time", "yew", "yew-router", "yewdux", @@ -3963,6 +3976,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "web-time" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee269d72cc29bf77a2c4bc689cc750fb39f5cbd493d2205bbb3f5c7779cf7b0" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "webpki" version = "0.22.4" diff --git a/taxy-webui/Cargo.toml b/taxy-webui/Cargo.toml index c86cd5f3..307fbb59 100644 --- a/taxy-webui/Cargo.toml +++ b/taxy-webui/Cargo.toml @@ -25,3 +25,5 @@ url = "2.4.0" base64 = "0.21.2" time = { version = "0.3.23", features = ["formatting"] } gloo-events = "0.1.2" +web-time = "1.0.0" +fancy-duration = "0.9.1" diff --git a/taxy-webui/src/format.rs b/taxy-webui/src/format.rs new file mode 100644 index 00000000..fa4c123e --- /dev/null +++ b/taxy-webui/src/format.rs @@ -0,0 +1,23 @@ +use fancy_duration::FancyDuration; +use time::{format_description::well_known::Rfc3339, OffsetDateTime}; +use wasm_bindgen::UnwrapThrowExt; +use web_time::{Duration, SystemTime, UNIX_EPOCH}; + +pub fn format_expiry(unix_time: i64) -> String { + let time = OffsetDateTime::from_unix_timestamp(unix_time).unwrap_throw(); + let timestamp = time.format(&Rfc3339).unwrap_throw(); + let date = timestamp.split('T').next().unwrap_throw().to_string(); + + let time = UNIX_EPOCH + .checked_add(Duration::from_secs(unix_time as u64)) + .unwrap_throw(); + + let duration = if let Ok(duration) = time.duration_since(SystemTime::now()) { + let duration = FancyDuration::new(duration).to_string(); + duration.split(' ').next().unwrap_throw().to_string() + } else { + "Expired".to_string() + }; + + format!("{} ({})", date, duration) +} diff --git a/taxy-webui/src/main.rs b/taxy-webui/src/main.rs index 26a67154..7821449c 100644 --- a/taxy-webui/src/main.rs +++ b/taxy-webui/src/main.rs @@ -8,6 +8,7 @@ use yew_router::prelude::*; mod auth; mod components; mod event; +mod format; mod pages; mod store; diff --git a/taxy-webui/src/pages/cert_list.rs b/taxy-webui/src/pages/cert_list.rs index 0937a97d..e964df7c 100644 --- a/taxy-webui/src/pages/cert_list.rs +++ b/taxy-webui/src/pages/cert_list.rs @@ -1,4 +1,5 @@ use crate::auth::use_ensure_auth; +use crate::format::format_expiry; use crate::pages::Route; use crate::store::{AcmeStore, CertStore}; use crate::API_ENDPOINT; @@ -164,6 +165,9 @@ pub fn cert_list() -> Html {