Skip to content

Commit

Permalink
sort challenges, use animated for (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
PenguinWithATie authored Nov 30, 2024
1 parent 13bbb7e commit 75a7d23
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
69 changes: 52 additions & 17 deletions apis/src/components/organisms/challenges.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
use crate::common::UserStatus;
use crate::i18n::*;
use crate::providers::online_users::{OnlineUsersSignal, OnlineUsersState};
use crate::{
components::molecules::challenge_row::ChallengeRow,
providers::{challenges::ChallengeStateSignal, AuthContext},
responses::ChallengeResponse,
};
use leptos::*;

fn challenge_order(
a: &ChallengeResponse,
b: &ChallengeResponse,
online_users: &OnlineUsersState,
) -> std::cmp::Ordering {
let online_a = online_users.username_status.get(&a.challenger.username);
let online_a = matches!(online_a, Some(UserStatus::Online));
let online_b = online_users.username_status.get(&b.challenger.username);
let online_b = matches!(online_b, Some(UserStatus::Online));
if a.challenge_id.0.cmp(&b.challenge_id.0) == std::cmp::Ordering::Equal {
std::cmp::Ordering::Equal
} else if online_a && !online_b {
std::cmp::Ordering::Less
} else if !online_a && online_b {
std::cmp::Ordering::Greater
} else if a.time_base == b.time_base {
let a_incr = a.time_increment.unwrap_or(i32::MAX);
let b_incr = b.time_increment.unwrap_or(i32::MAX);
a_incr.cmp(&b_incr)
} else {
let a_base = a.time_base.unwrap_or(i32::MAX);
let b_base = b.time_base.unwrap_or(i32::MAX);
a_base.cmp(&b_base)
}
}

#[component]
pub fn Challenges() -> impl IntoView {
let i18n = use_i18n();
let th_class =
"py-1 px-1 md:py-2 md:px-2 lg:px-3 font-bold uppercase max-h-[80vh] max-w-screen";
let challenges = expect_context::<ChallengeStateSignal>();
let online_users = expect_context::<OnlineUsersSignal>().signal;
let auth_context = expect_context::<AuthContext>();
let user = move || {
if let Some(Ok(Some(user))) = (auth_context.user)() {
Expand All @@ -20,8 +49,8 @@ pub fn Challenges() -> impl IntoView {
None
}
};
let direct = move || {
if let Some(user) = user() {
let direct = Signal::derive(move || {
let mut ret = if let Some(user) = user() {
// Get the challenges direct at the current user
challenges
.signal
Expand All @@ -39,11 +68,13 @@ pub fn Challenges() -> impl IntoView {
.values()
.cloned()
.collect::<Vec<ChallengeResponse>>()
}
};
};
ret.sort_by(|a, b| challenge_order(a, b, &online_users.get()));
ret
});

let own = move || {
if let Some(user) = user() {
let own = Signal::derive(move || {
let mut ret = if let Some(user) = user() {
challenges
.signal
.get()
Expand All @@ -54,11 +85,13 @@ pub fn Challenges() -> impl IntoView {
.collect::<Vec<ChallengeResponse>>()
} else {
Vec::new()
}
};
};
ret.sort_by(|a, b| challenge_order(a, b, &online_users.get()));
ret
});

let public = move || {
if let Some(user) = user() {
let public = Signal::derive(move || {
let mut ret = if let Some(user) = user() {
challenges
.signal
.get()
Expand All @@ -71,8 +104,10 @@ pub fn Challenges() -> impl IntoView {
.collect::<Vec<ChallengeResponse>>()
} else {
Vec::new()
}
};
};
ret.sort_by(|a, b| challenge_order(a, b, &online_users.get()));
ret
});
let has_games = move |list: Vec<ChallengeResponse>| !list.is_empty();
let not_hidden =
Memo::new(move |_| has_games(direct()) || has_games(own()) || has_games(public()));
Expand All @@ -92,15 +127,15 @@ pub fn Challenges() -> impl IntoView {
</tr>
</thead>
<tbody>
<For each=move || { direct() } key=|c| c.challenge_id.to_owned() let:challenge>
<ChallengeRow challenge=store_value(challenge.to_owned()) single=false />
</For>
<For each=direct key=|c| c.challenge_id.to_owned() let:challenge>
<ChallengeRow challenge=store_value(challenge.to_owned()) single=false />
</For>
<tr class="h-2"></tr>
<For each=move || { own() } key=|c| c.challenge_id.to_owned() let:challenge>
<For each=own key=|c| c.challenge_id.to_owned() let:challenge>
<ChallengeRow challenge=store_value(challenge.to_owned()) single=false />
</For>
<tr class="h-2"></tr>
<For each=move || { public() } key=|c| c.challenge_id.to_owned() let:challenge>
<For each=public key=|c| c.challenge_id.to_owned() let:challenge>
<ChallengeRow challenge=store_value(challenge.to_owned()) single=false />
</For>
</tbody>
Expand Down
16 changes: 8 additions & 8 deletions apis/src/pages/profile_view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::common::UserAction;
use crate::components::atoms::rating::icon_for_speed;
use crate::components::molecules::user_row::UserRow;
use crate::i18n::*;
use crate::{
Expand All @@ -10,21 +11,20 @@ use crate::{
ApiRequests,
},
};
use leptos_icons::*;
use hive_lib::Color;
use leptix_primitives::{
radio_group::{RadioGroupItem, RadioGroupRoot},
toggle_group::{ToggleGroupItem, ToggleGroupKind, ToggleGroupMultiple, ToggleGroupRoot},
};
use leptos::*;
use leptos_icons::*;
use leptos_router::*;
use leptos_use::{
core::ConnectionReadyState, signal_debounced, use_infinite_scroll_with_options,
UseInfiniteScrollOptions,
};
use shared_types::{GameProgress, GameSpeed, GamesContextToUpdate, GamesQueryOptions, ResultType};
use std::str::FromStr;
use crate::components::atoms::rating::icon_for_speed;

#[derive(Params, PartialEq, Eq)]
struct UsernameParams {
Expand Down Expand Up @@ -163,22 +163,22 @@ fn Controls(username: Signal<String>) -> impl IntoView {
>

<ToggleGroupItem value="Bullet" attr:class=toggle_classes>
<Icon icon=icon_for_speed(&GameSpeed::Bullet) />
<Icon icon=icon_for_speed(&GameSpeed::Bullet) />
</ToggleGroupItem>
<ToggleGroupItem value="Blitz" attr:class=toggle_classes>
<Icon icon=icon_for_speed(&GameSpeed::Blitz) />
<Icon icon=icon_for_speed(&GameSpeed::Blitz) />
</ToggleGroupItem>
<ToggleGroupItem value="Rapid" attr:class=toggle_classes>
<Icon icon=icon_for_speed(&GameSpeed::Rapid) />
<Icon icon=icon_for_speed(&GameSpeed::Rapid) />
</ToggleGroupItem>
<ToggleGroupItem value="Classic" attr:class=toggle_classes>
<Icon icon=icon_for_speed(&GameSpeed::Classic) />
<Icon icon=icon_for_speed(&GameSpeed::Classic) />
</ToggleGroupItem>
<ToggleGroupItem value="Correspondence" attr:class=toggle_classes>
<Icon icon=icon_for_speed(&GameSpeed::Correspondence) />
<Icon icon=icon_for_speed(&GameSpeed::Correspondence) />
</ToggleGroupItem>
<ToggleGroupItem value="Untimed" attr:class=toggle_classes>
<Icon icon=icon_for_speed(&GameSpeed::Untimed) />
<Icon icon=icon_for_speed(&GameSpeed::Untimed) />
</ToggleGroupItem>
</ToggleGroupRoot>
</div>
Expand Down
2 changes: 1 addition & 1 deletion shared_types/src/game_speed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Debug, Serialize, PartialEq, Eq, Deserialize, Clone, Hash)]
#[derive(Debug, Serialize, PartialEq, Eq, Deserialize, Clone, Hash, PartialOrd, Ord)]
pub enum GameSpeed {
Bullet,
Blitz,
Expand Down

0 comments on commit 75a7d23

Please sign in to comment.