From 2a98187d08b16555aaf3554ad7eafdb91ee2bf89 Mon Sep 17 00:00:00 2001 From: Blake Rain Date: Wed, 20 Nov 2024 17:52:16 +0000 Subject: [PATCH] feat: add counts to tabs --- src/app/handlers/index.rs | 20 +++++++++++++++++--- src/app/handlers/teams.rs | 18 +++++++++++++++++- src/model/team.rs | 18 ++++++++++++++++++ src/model/user.rs | 4 ++-- style/components/label.css | 7 +++++++ style/components/tabs.css | 8 +++++--- style/main.css | 1 + templates/uploads/deleted.html | 2 +- templates/uploads/list.html | 2 +- templates/uploads/tabs.html | 6 ++++-- 10 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 style/components/label.css diff --git a/src/app/handlers/index.rs b/src/app/handlers/index.rs index dd9d1c8..0720a4a 100644 --- a/src/app/handlers/index.rs +++ b/src/app/handlers/index.rs @@ -12,7 +12,7 @@ use crate::{ }, env::Env, model::{ - team::TeamTab, + team::{HomeTab, TeamTab}, upload::{UploadList, UploadStats}, }, }; @@ -25,8 +25,13 @@ pub async fn get_index( SessionUser(user): SessionUser, Query(query): Query, ) -> poem::Result> { - // Get the tab data for the teams the user belongs to. This is used by the nested 'teams.html' - // template, which will render the tabs for the teams. + let home = HomeTab::get_for_user(&env.pool, user.id) + .await + .map_err(|err| { + tracing::error!(%user.id, ?err, "Unable to get home tab for user"); + poem::error::InternalServerError(err) + })?; + let tabs = TeamTab::get_for_user(&env.pool, user.id) .await .map_err(|err| { @@ -54,6 +59,7 @@ pub async fn get_index( "index.html", minijinja::context! { query, + home, tabs, stats, uploads, @@ -71,6 +77,13 @@ pub async fn get_tab( SessionUser(user): SessionUser, Query(query): Query, ) -> poem::Result { + let home = HomeTab::get_for_user(&env.pool, user.id) + .await + .map_err(|err| { + tracing::error!(%user.id, ?err, "Unable to get home tab for user"); + poem::error::InternalServerError(err) + })?; + let tabs = TeamTab::get_for_user(&env.pool, user.id) .await .map_err(|err| { @@ -96,6 +109,7 @@ pub async fn get_tab( "tab.html", minijinja::context! { query, + home, tabs, stats, uploads, diff --git a/src/app/handlers/teams.rs b/src/app/handlers/teams.rs index f6fa55b..5b7c22e 100644 --- a/src/app/handlers/teams.rs +++ b/src/app/handlers/teams.rs @@ -14,7 +14,7 @@ use crate::{ }, env::Env, model::{ - team::{Team, TeamMember, TeamTab}, + team::{HomeTab, Team, TeamMember, TeamTab}, upload::{UploadList, UploadStats}, }, }; @@ -50,6 +50,13 @@ pub async fn get_team( return Err(poem::Error::from_status(StatusCode::FORBIDDEN)); }; + let home = HomeTab::get_for_user(&env.pool, user.id) + .await + .map_err(|err| { + tracing::error!(%user.id, ?err, "Unable to get home tab for user"); + InternalServerError(err) + })?; + let tabs = TeamTab::get_for_user(&env.pool, user.id) .await .map_err(|err| { @@ -73,6 +80,7 @@ pub async fn get_team( minijinja::context! { query, tabs, + home, team, membership, stats, @@ -112,6 +120,13 @@ pub async fn get_tab( return Err(poem::Error::from_status(StatusCode::FORBIDDEN)); }; + let home = HomeTab::get_for_user(&env.pool, user.id) + .await + .map_err(|err| { + tracing::error!(%user.id, ?err, "Unable to get home tab for user"); + InternalServerError(err) + })?; + let tabs = TeamTab::get_for_user(&env.pool, user.id) .await .map_err(|err| { @@ -135,6 +150,7 @@ pub async fn get_tab( minijinja::context! { query, tabs, + home, team, membership, stats, diff --git a/src/model/team.rs b/src/model/team.rs index bc878c3..4525d0f 100644 --- a/src/model/team.rs +++ b/src/model/team.rs @@ -189,6 +189,24 @@ impl TeamTab { } } +#[derive(Debug, FromRow, Serialize)] +pub struct HomeTab { + pub count: i64, +} + +impl HomeTab { + pub async fn get_for_user(pool: &SqlitePool, user: Key) -> sqlx::Result { + sqlx::query_as( + "SELECT COUNT(uploads.id) AS count \ + FROM uploads \ + WHERE uploads.owner_user = $1", + ) + .bind(user) + .fetch_one(pool) + .await + } +} + #[derive(Debug, FromRow, Serialize)] pub struct TeamList { pub id: Key, diff --git a/src/model/user.rs b/src/model/user.rs index ea9cb43..ec4f14c 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -280,13 +280,13 @@ impl User { #[derive(Debug, FromRow, Serialize)] pub struct UserStats { - pub total: i32, + pub count: i32, pub enabled: i32, } impl UserStats { pub async fn get(pool: &SqlitePool) -> sqlx::Result { - sqlx::query_as("SELECT COUNT(*) AS total, SUM(enabled) AS enabled FROM users") + sqlx::query_as("SELECT COUNT(*) AS count, SUM(enabled) AS enabled FROM users") .fetch_one(pool) .await } diff --git a/style/components/label.css b/style/components/label.css new file mode 100644 index 0000000..b73dae7 --- /dev/null +++ b/style/components/label.css @@ -0,0 +1,7 @@ +@layer components { + .label { + @apply rounded-full bg-blue-300 text-blue-900 dark:bg-blue-600 dark:text-blue-100; + @apply text-sm leading-none px-2 pt-[0.2rem] pb-[0.3rem]; + } +} + diff --git a/style/components/tabs.css b/style/components/tabs.css index 641f0e6..52d83f0 100644 --- a/style/components/tabs.css +++ b/style/components/tabs.css @@ -3,14 +3,16 @@ @apply flex flex-row; .tab { - @apply flex flex-row gap-1 rounded-t-lg bg-transparent cursor-pointer px-4 py-2; + @apply flex flex-row items-center gap-1 rounded-t-md bg-transparent cursor-pointer px-4 py-2; @apply hover:bg-neutral-200 dark:hover:bg-gray-800/25; + @apply border border-b-0 border-transparent; + + margin-bottom: -1px; &.active { - @apply border border-b-0 border-slate-400 dark:border-gray-700; + @apply border-slate-400 dark:border-gray-700; @apply bg-neutral-100 dark:bg-gray-800; - margin-bottom: -1px; z-index: 1; } } diff --git a/style/main.css b/style/main.css index b89479d..d34b5f2 100644 --- a/style/main.css +++ b/style/main.css @@ -4,6 +4,7 @@ @import "components/drop.css"; @import "components/dropdown.css"; @import "components/form.css"; +@import "components/label.css"; @import "components/modal.css"; @import "components/panel.css"; @import "components/select.css"; diff --git a/templates/uploads/deleted.html b/templates/uploads/deleted.html index 51d4611..443a13a 100644 --- a/templates/uploads/deleted.html +++ b/templates/uploads/deleted.html @@ -1,5 +1,5 @@ {% include "uploads/stats.html" %} -{% if stats.total == 0 %} +{% if stats.count == 0 %}
You have not uploaded any files yet.
diff --git a/templates/uploads/list.html b/templates/uploads/list.html index 70cfa9a..8fe719c 100644 --- a/templates/uploads/list.html +++ b/templates/uploads/list.html @@ -40,7 +40,7 @@ - {% if stats.total == 0 %} + {% if stats.count == 0 %}
You have not uploaded any files yet.
diff --git a/templates/uploads/tabs.html b/templates/uploads/tabs.html index ff017a5..a7fbb60 100644 --- a/templates/uploads/tabs.html +++ b/templates/uploads/tabs.html @@ -1,7 +1,8 @@