Skip to content

Commit

Permalink
fix: load info into a static (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
javierEd authored Jan 26, 2025
1 parent 07f2fb8 commit 5f52c1e
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 32 deletions.
2 changes: 1 addition & 1 deletion mango3-core/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ lazy_static! {
pub(crate) static ref REGEX_USERNAME: Regex = Regex::new(r"\A[-_.]?([[:alnum:]]+[-_.]?)+\z").unwrap();
}

pub static ALLOWED_POST_REACTION_EMOJIS: LazyLock<[&str; 16]> = LazyLock::new(|| {
pub static REACTION_EMOJIS: LazyLock<[&str; 16]> = LazyLock::new(|| {
[
"😀", "😂", "🥹", "🙂", "🙃", "🙁", "😢", "😡", "🤯", "🤔", "😦", "🤡", "💩", "🖕", "👍", "👎",
]
Expand Down
32 changes: 32 additions & 0 deletions mango3-core/src/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::process::Command;
use std::sync::LazyLock;

use crate::constants::REACTION_EMOJIS;

pub static INFO: LazyLock<Info> = LazyLock::new(Info::load);

#[derive(Clone)]
pub struct Info {
pub git_commit_hash: String,
pub git_commit_short_hash: String,
pub reaction_emojis: Vec<String>,
pub version: String,
}

impl Info {
fn load() -> Self {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.expect("could not get git commit hash");

let git_commit_hash = String::from_utf8(output.stdout).expect("could not parse git commit hash");

Self {
git_commit_hash: git_commit_hash.clone(),
git_commit_short_hash: git_commit_hash[0..7].to_owned(),
reaction_emojis: REACTION_EMOJIS.clone().iter().map(|s| (*s).to_owned()).collect(),
version: env!("CARGO_PKG_VERSION").to_owned(),
}
}
}
1 change: 1 addition & 0 deletions mango3-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod test_utils;
pub mod config;
pub mod constants;
pub mod enums;
pub mod info;
pub mod jobs;
pub mod locales;
pub mod models;
Expand Down
4 changes: 2 additions & 2 deletions mango3-core/src/models/post_reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sqlx::{query, query_as};

use super::{Post, User};

use crate::constants::ALLOWED_POST_REACTION_EMOJIS;
use crate::constants::REACTION_EMOJIS;
use crate::enums::{Input, InputError};
use crate::validator::{ValidationErrors, Validator};
use crate::CoreContext;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl PostReaction {
let mut validator = Validator::default();

validator.custom_validation(Input::Emoji, InputError::IsInvalid, &|| {
ALLOWED_POST_REACTION_EMOJIS.contains(&emoji)
REACTION_EMOJIS.contains(&emoji)
});

if let Ok(reaction) = query_as!(
Expand Down
12 changes: 0 additions & 12 deletions mango3-leptos-utils/build.rs

This file was deleted.

3 changes: 2 additions & 1 deletion mango3-leptos-utils/src/components/app_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use codee::string::FromToStringCodec;
use leptos::prelude::*;

use crate::constants::COOKIE_NAME_LANGUAGE;
use crate::context::*;
use crate::context::{provide_basic_config, provide_current_user_resource, provide_info, use_language_cookie_options};
use crate::i18n::I18nContextProvider;

#[component]
pub fn AppProvider(children: Children) -> impl IntoView {
provide_basic_config();
provide_current_user_resource();
provide_info();

let language_cookie_options = use_language_cookie_options::<FromToStringCodec>();

Expand Down
9 changes: 5 additions & 4 deletions mango3-leptos-utils/src/components/bottom_bar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use leptos::prelude::*;
use leptos_use::{ColorMode, UseColorModeReturn};

use crate::context::{use_basic_config, use_color_mode_with_options, UseColorModeOptions};
use crate::context::{use_basic_config, use_color_mode_with_options, use_info, UseColorModeOptions};
use crate::i18n::{t, t_string, use_i18n, Locale};
use crate::icons::{ChevronUpMini, ComputerOutlined, MoonOutlined, SunOutlined};

Expand All @@ -14,6 +14,7 @@ pub fn BottomBar(
#[prop(default = "dark".to_owned(), into)] dark_theme: String,
) -> impl IntoView {
let basic_config = use_basic_config();
let info = use_info();
let UseColorModeReturn { mode, set_mode, .. } = use_color_mode_with_options(
UseColorModeOptions::default()
.light_theme(light_theme)
Expand Down Expand Up @@ -70,14 +71,14 @@ pub fn BottomBar(
</Show>

<a
href=format!("https://github.com/mangocubed/mango3/tree/{}", env!("GIT_COMMIT_HASH"))
href=format!("https://github.com/mangocubed/mango3/tree/{}", info.git_commit_hash)
target="_blank"
title=move || t_string!(i18n, shared.view_source_code)
>
"v"
{env!("CARGO_PKG_VERSION")}
{info.version}
" ("
{env!("GIT_COMMIT_SHORT_HASH")}
{info.git_commit_short_hash}
")"
</a>
</nav>
Expand Down
34 changes: 33 additions & 1 deletion mango3-leptos-utils/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use leptos::prelude::*;
use leptos_router::params::ParamsMap;

use crate::constants::KEY_PARAM_NAME;
use crate::models::{BasicConfigResp, UserResp};
use crate::models::{BasicConfigResp, InfoResp, UserResp};
use crate::server_functions::get_current_user;

mod use_color_mode;
Expand Down Expand Up @@ -37,6 +37,24 @@ pub fn provide_basic_config() {
provide_context(basic_config.into_inner());
}

pub fn provide_info() {
let info = SharedValue::<InfoResp>::new(move || {
#[cfg(feature = "ssr")]
{
use mango3_core::info::INFO;

INFO.clone().into()
}

#[cfg(not(feature = "ssr"))]
{
InfoResp::default()
}
});

provide_context(info.into_inner());
}

pub fn provide_current_user_resource() {
provide_context(Resource::new_blocking(|| (), |_| get_current_user()))
}
Expand All @@ -58,3 +76,17 @@ pub fn use_basic_config() -> BasicConfigResp {
pub fn use_current_user_resource() -> Resource<Result<Option<UserResp>, ServerFnError>> {
use_context::<Resource<Result<Option<UserResp>, ServerFnError>>>().unwrap()
}

pub fn use_info() -> InfoResp {
#[cfg(feature = "ssr")]
{
use mango3_core::info::INFO;

INFO.clone().into()
}

#[cfg(not(feature = "ssr"))]
{
use_context::<InfoResp>().unwrap()
}
}
24 changes: 24 additions & 0 deletions mango3-leptos-utils/src/models/info_resp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use serde::{Deserialize, Serialize};

#[cfg(feature = "ssr")]
use mango3_core::info::Info;

#[derive(Clone, Default, Deserialize, Serialize)]
pub struct InfoResp {
pub git_commit_hash: String,
pub git_commit_short_hash: String,
pub reaction_emojis: Vec<String>,
pub version: String,
}

#[cfg(feature = "ssr")]
impl From<Info> for InfoResp {
fn from(info: Info) -> Self {
Self {
git_commit_hash: info.git_commit_hash.clone(),
git_commit_short_hash: info.git_commit_short_hash,
reaction_emojis: info.reaction_emojis,
version: info.version,
}
}
}
2 changes: 2 additions & 0 deletions mango3-leptos-utils/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod action_form_resp;
mod basic_config_resp;
mod blob_resp;
mod hashtag_resp;
mod info_resp;
mod navigation_item_resp;
mod post_comment_resp;
mod post_resp;
Expand All @@ -33,6 +34,7 @@ pub use action_form_resp::ActionFormResp;
pub use basic_config_resp::BasicConfigResp;
pub use blob_resp::BlobResp;
pub use hashtag_resp::HashtagResp;
pub use info_resp::InfoResp;
pub use navigation_item_resp::NavigationItemResp;
pub use post_comment_resp::PostCommentResp;
pub use post_resp::{PostPreviewResp, PostResp};
Expand Down
18 changes: 7 additions & 11 deletions mango3-websites/src/components/post_reactions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::sync::LazyLock;

use leptos::either::Either;
use leptos::prelude::*;

use mango3_leptos_utils::components::{CurrentUser, LoadingSpinner};
use mango3_leptos_utils::context::use_current_user_resource;
use mango3_leptos_utils::context::{use_current_user_resource, use_info};
use mango3_leptos_utils::i18n::{t, use_i18n};
use mango3_leptos_utils::icons::PlusOutlined;

Expand All @@ -13,12 +11,6 @@ use crate::server_functions::{
get_post_reaction_emojis_count,
};

pub static REACTION_EMOJIS: LazyLock<[&str; 16]> = LazyLock::new(|| {
[
"😀", "😂", "🥹", "🙂", "🙃", "🙁", "😢", "😡", "🤯", "🤔", "😦", "🤡", "💩", "🖕", "👍", "👎",
]
});

#[component]
pub fn PostReactions(post_id: String) -> impl IntoView {
let i18n = use_i18n();
Expand Down Expand Up @@ -54,6 +46,7 @@ pub fn PostReactions(post_id: String) -> impl IntoView {
.get()
.and_then(|result| result.ok())
.map(|emojis_count| {
let info = use_info();
let insert_reaction_action = Action::new(move |emoji: &String| {
let post_id = post_id_store.read_value().clone();
let emoji = emoji.clone();
Expand All @@ -79,7 +72,10 @@ pub fn PostReactions(post_id: String) -> impl IntoView {

<div class="dropdown-content bg-base-100 rounded-box z-[1] p-2 shadow flex flex-wrap w-[296px]">
<For
each=move || REACTION_EMOJIS.to_vec()
each={
let reaction_emojis = info.reaction_emojis.clone();
move || reaction_emojis.clone()
}
key=|emoji| emoji.to_owned()
let:emoji
>
Expand All @@ -89,7 +85,7 @@ pub fn PostReactions(post_id: String) -> impl IntoView {
insert_reaction_action.dispatch(emoji.to_owned());
}
>
{emoji}
{emoji.clone()}
</button>
</For>
</div>
Expand Down

0 comments on commit 5f52c1e

Please sign in to comment.