Skip to main content

rumtk_web/components/
socials.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21use crate::utils::defaults::{DEFAULT_TEXT_ITEM, PARAMS_CSS_CLASS, SECTION_SOCIALS,
22};
23use crate::utils::types::{RUMString, SharedAppState, URLParams, URLPath};
24use crate::{rumtk_web_get_config, rumtk_web_get_config_section, rumtk_web_get_text_item, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe};
25use rumtk_core::strings::rumtk_format;
26
27#[derive(Debug, Clone)]
28struct Social {
29    name: RUMString,
30    icon: RUMString,
31    url: RUMString,
32}
33
34type SocialsList = Vec<Social>;
35
36#[derive(RUMWebTemplate, Debug, Clone)]
37#[template(
38    source = "
39        {% if custom_css_enabled %}
40            <link href='/static/components/socials.css' rel='stylesheet'>
41        {% endif %}
42        <div class='socials-{{ css_class }}-container gap-10'>
43          {% for icon in icons %}
44            <a href='{{icon.url}}' aria-label='link-{{icon.name}}' class='f20 {{icon.icon}}'> </a>
45          {% endfor %}
46        </div>
47    ",
48    ext = "html"
49)]
50pub struct Socials {
51    icons: SocialsList,
52    css_class: RUMString,
53    custom_css_enabled: bool,
54}
55
56impl RUMWebTemplateSafe for Socials {}
57
58fn get_social_list(social_list: &str, state: &SharedAppState) -> SocialsList {
59    let data = social_list.to_lowercase();
60    let sl_names = data.split(',').collect::<Vec<&str>>();
61    let sl_urls = rumtk_web_get_config_section!(state, SECTION_SOCIALS);
62    let mut sl: SocialsList = SocialsList::with_capacity(sl_names.len());
63
64    for name in sl_names {
65        if name.is_empty() {
66            continue;
67        }
68
69        let url = rumtk_web_get_text_item!(&sl_urls, name, "").to_string();
70        sl.push(Social {
71            name: RUMString::from(name),
72            icon: rumtk_format!("fa-brands fa-{}", name),
73            url,
74        })
75    }
76
77    sl
78}
79
80pub fn socials<'a>(_path_components: URLPath<'a, 'a>, params: URLParams<'a>, state: SharedAppState) -> ComponentResult<Socials> {
81    let social_list = rumtk_web_get_config!(state).footer_conf.socials_list.to_string();
82    let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
83
84    let custom_css_enabled = rumtk_web_get_config!(state).flags.custom_css;
85
86    let icons = get_social_list(&social_list, &state);
87
88    Ok(Socials {
89        icons,
90        css_class,
91        custom_css_enabled
92    })
93}