Skip to content

Commit

Permalink
feat: add page title #6
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Aug 12, 2024
1 parent 6b6e281 commit 016ab68
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use thousands::Separable;
use crate::db_client::{
DbClient, Direction, PopularFilter, PopularKind, PopularSort, RepoFilter, RepoMetrics, RepoSort,
};
use crate::types::HtmlRes;
use crate::types::{AppError, HtmlRes};
use crate::AppState;

#[derive(Debug)]
Expand All @@ -34,14 +34,20 @@ fn maybe_url(item: &(String, Option<String>)) -> Markup {
}

fn base(state: &Arc<AppState>, navs: Vec<(String, Option<String>)>, inner: Markup) -> Markup {
let brand = format!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
let (app_name, app_version) = (env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));

let last_release = state.last_release.lock().unwrap().clone();
let is_new_release = last_release != env!("CARGO_PKG_VERSION");
let is_new_release = last_release != app_version;

let title = match navs.len() {
0 => app_name,
_ => &format!("{} · {}", navs.last().unwrap().0, app_name),
};

html!(
html {
head {
title { (title) }
link rel="stylesheet" href="https://unpkg.com/@picocss/pico@2.0.6/css/pico.min.css" {}
script src="https://unpkg.com/chart.js@4.4.3/dist/chart.umd.js" {}
script src="https://unpkg.com/htmx.org@2.0.1" {}
Expand Down Expand Up @@ -71,7 +77,7 @@ fn base(state: &Arc<AppState>, navs: Vec<(String, Option<String>)>, inner: Marku
style="font-size: 18px;"
target="_blank"
{
(brand)
(format!("{} v{}", app_name, app_version))
}
}
}
Expand Down Expand Up @@ -216,7 +222,7 @@ pub async fn repo_page(

let totals = match db.get_repo_totals(&repo).await? {
Some(x) => x,
None => return Ok(base(&state, vec![], html!(h1 { "Repo not found" }))),
None => return AppError::not_found(),
};

let metrics = db.get_metrics(&repo).await?;
Expand Down
15 changes: 13 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ pub type HtmlRes = Result<maud::Markup, AppError>;

pub struct AppError(anyhow::Error);

impl AppError {
pub fn not_found() -> HtmlRes {
Err(Self(anyhow::anyhow!(axum::http::StatusCode::NOT_FOUND)))
}
}

impl axum::response::IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
let code = axum::http::StatusCode::INTERNAL_SERVER_ERROR;
(code, format!("Something went wrong: {}", self.0)).into_response()
match self.0.downcast_ref::<axum::http::StatusCode>() {
Some(code) => (*code, self.0.to_string()).into_response(),
None => {
let code = axum::http::StatusCode::INTERNAL_SERVER_ERROR;
(code, format!("Something went wrong: {}", self.0)).into_response()
}
}
}
}

Expand Down

0 comments on commit 016ab68

Please sign in to comment.