filterbar wip

This commit is contained in:
Robin Appelman 2023-04-11 23:06:48 +02:00
commit e5c9aeb7fe
15 changed files with 1905 additions and 420 deletions

19
src/data/maps.rs Normal file
View file

@ -0,0 +1,19 @@
use crate::Result;
use sqlx::{query, Executor, Postgres};
use tracing::instrument;
#[instrument(skip(connection))]
pub async fn map_list(
connection: impl Executor<'_, Database = Postgres>,
) -> Result<impl Iterator<Item = String>> {
Ok(query!(
r#"SELECT
map as "map!"
FROM map_list
ORDER BY count DESC LIMIT 50"#
)
.fetch_all(connection)
.await?
.into_iter()
.map(|res| res.map))
}

View file

@ -1,5 +1,6 @@
pub mod chat;
pub mod demo;
pub mod maps;
pub mod player;
pub mod steam_id;
pub mod user;

View file

@ -8,6 +8,7 @@ mod session;
pub use crate::config::Config;
use crate::config::Listen;
use crate::data::demo::{Demo, ListDemo};
use crate::data::maps::map_list;
use crate::data::steam_id::SteamId;
use crate::data::user::User;
use crate::pages::about::AboutPage;
@ -127,7 +128,15 @@ async fn main() -> Result<()> {
async fn index(State(app): State<Arc<App>>, session: SessionData) -> Result<Markup> {
let demos = ListDemo::list(&app.connection, None).await?;
Ok(render(Index { demos }, session))
let maps = map_list(&app.connection).await?.collect();
Ok(render(
Index {
demos,
maps,
api: &app.api,
},
session,
))
}
async fn about(State(_app): State<Arc<App>>, session: SessionData) -> Result<Markup> {

View file

@ -1,14 +1,16 @@
use crate::asset::saved_asset_url;
use crate::data::demo::ListDemo;
use crate::pages::Page;
use maud::{html, Markup};
use maud::{html, Markup, Render};
use std::borrow::Cow;
pub struct Index {
pub struct Index<'a> {
pub demos: Vec<ListDemo>,
pub maps: Vec<String>,
pub api: &'a str,
}
impl Page for Index {
impl Page for Index<'_> {
fn title(&self) -> Cow<'static, str> {
"Demos - demos.tf".into()
}
@ -17,7 +19,7 @@ impl Page for Index {
let script = saved_asset_url!("demo_list.js");
html! {
h1 { "Demos" }
.filter-bar {}
#filter-bar data-maps = (MapList(&self.maps)) data-api-base = (self.api) {}
table.demolist {
thead {
tr {
@ -46,3 +48,18 @@ impl Page for Index {
}
}
}
struct MapList<'a>(&'a [String]);
impl Render for MapList<'_> {
fn render_to(&self, buffer: &mut String) {
let mut first = true;
for map in self.0 {
if !first {
buffer.push_str(",");
}
buffer.push_str(&map);
first = false;
}
}
}