Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): format post creation time dynamically #231

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ members = [
"crates/server",
"crates/test",
"crates/types",
"crates/web"
"crates/web",
]
default-members = ["crates/cli"]
resolver = "2"

[workspace.dependencies]
anyhow = "1.0.86"
http-auth-basic = "0.3.3"
async-graphql = { version = "7.0.5", features = ["chrono", "decimal", "tracing"] }
async-graphql = { version = "7.0.5", features = [
"chrono",
"decimal",
"tracing",
] }
async-graphql-axum = "7.0.5"
async-trait = "0.1.80"
axum = "0.7.5"
axum-extra = "0.9.3"
base64 = "0.22.1"
chrono = { version = "0.4.38", default-features = false }
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
cookie = "0.18.1"
dotenv = "0.15.0"
fake = "2.9.2"
Expand All @@ -38,7 +42,10 @@ rand = "0.8.5"
regex = "1.9.3"
reqwest = "0.11"
rust-argon2 = "2.1.0"
rust-s3 = { version = "0.34.0", features = ["tokio-rustls-tls", "fail-on-err"], default-features = false }
rust-s3 = { version = "0.34.0", features = [
"tokio-rustls-tls",
"fail-on-err",
], default-features = false }
sea-orm = "0.12"
sea-orm-cli = { version = "0.12", default-features = false }
sea-orm-migration = "0.12"
Expand Down
3 changes: 2 additions & 1 deletion crates/web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ name = "townhall-web"
path = "src/bin/main.rs"

[dependencies]
chrono = { workspace = true }
anyhow = { workspace = true }
leptos = { workspace = true, features = ["csr"] }
leptos = { workspace = true, features = ["csr"] }
leptos_meta = { workspace = true, features = ["csr"] }
leptos_router = { workspace = true, features = ["csr"] }
leptos-use = { workspace = true }
Expand Down
30 changes: 29 additions & 1 deletion crates/web/src/components/feed/post.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
use chrono::{TimeZone, Utc};
use leptos::{component, view, IntoView};

use townhall_client::post::posts::posts::PostsPostsEdgesNode;

const LONG_TO_SHORT_CUTOFF_DAYS: i64 = 3;

#[component]
pub fn Post(post: PostsPostsEdgesNode) -> impl IntoView {
let post_created_at_date_time = Utc::from_utc_datetime(&Utc, &post.created_at.naive_utc());
let now_date = Utc::now();

let diff_in_seconds = now_date
.signed_duration_since(post_created_at_date_time)
.num_seconds();

let format_string = match diff_in_seconds {
// More than 3 days
_ if diff_in_seconds > 86400 * LONG_TO_SHORT_CUTOFF_DAYS => "%m/%d/%y".to_string(), // More than 3 days
diff_in_seconds if diff_in_seconds > 86400 => {
format!("{} Days ago", diff_in_seconds / 86400)
}
// Within 3 days
diff_in_seconds if diff_in_seconds > 3600 => {
format!("{} hours ago", diff_in_seconds / 3600)
} // Within 1 hour
diff_in_seconds if diff_in_seconds > 60 => {
format!("{} minutes ago", diff_in_seconds / 60)
} // Within 1 minute
_ => format!("{} seconds ago", diff_in_seconds),
};

let formatted_date = post_created_at_date_time.format(&format_string).to_string();

Comment on lines +10 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could use something like: https://docs.rs/humantime/latest/humantime/ for this?

view! {
<li class="feed-post bg-white rounded-md p-4">
<div>
Expand All @@ -13,7 +41,7 @@ pub fn Post(post: PostsPostsEdgesNode) -> impl IntoView {
</figure>
<div class="flex flex-col ml-2">
<strong>{post.author.username}</strong>
<time class="text-sm text-gray-400">2 hours ago</time>
<time class="text-sm text-gray-400">{formatted_date}</time>
</div>
</article>
</div>
Expand Down
Loading