Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

refactor(admin): pass checkbox and number options as structs

Hampus48ed7723616a
diff --git a/fluxer_admin/src/templates/components/archive.rs b/fluxer_admin/src/templates/components/archive.rs
index b1c9e96e0..c9b811f21 100644
--- a/fluxer_admin/src/templates/components/archive.rs
+++ b/fluxer_admin/src/templates/components/archive.rs
@@ -3,7 +3,7 @@
use crate::utils::timestamps::format_admin_timestamp;
use crate::{
api::types::Archive,
- templates::components::form::{checkbox, csrf_input, submit_button},
+ templates::components::form::{Checkbox, csrf_input, submit_button},
};
use maud::{Markup, html};
@@ -25,7 +25,13 @@ pub fn archives_tab(
action=(action)
class="flex flex-wrap items-center gap-3" {
(csrf_input(csrf_token))
- (checkbox("include_attachments", "true", "Include attachments", false, true))
+ (Checkbox {
+ name: "include_attachments",
+ value: "true",
+ label: "Include attachments",
+ checked: false,
+ enabled: true,
+ }.render())
(submit_button("Trigger Archive"))
}
}
diff --git a/fluxer_admin/src/templates/components/form.rs b/fluxer_admin/src/templates/components/form.rs
index c0a45970b..ea952cbd0 100644
--- a/fluxer_admin/src/templates/components/form.rs
+++ b/fluxer_admin/src/templates/components/form.rs
@@ -182,6 +182,44 @@ pub fn form_field_group(
}
}
+pub(crate) struct NumberField<'a> {
+ pub name: &'a str,
+ pub label: &'a str,
+ pub value: &'a str,
+ pub min: Option<u32>,
+ pub max: Option<u32>,
+ pub step: &'a str,
+ pub helper: Option<&'a str>,
+ pub placeholder: Option<&'a str>,
+}
+
+impl NumberField<'_> {
+ pub(crate) fn render(self) -> Markup {
+ let Self {
+ name,
+ label,
+ value,
+ min,
+ max,
+ step,
+ helper,
+ placeholder,
+ } = self;
+ form_field_group(
+ label,
+ name,
+ false,
+ None,
+ helper,
+ html! {
+ input type="number" id=(name) name=(name) step=(step)
+ value=(value) placeholder=[placeholder]
+ min=[min] max=[max] class=(FORM_INPUT_CLASS);
+ },
+ )
+ }
+}
+
pub fn textarea_input(
name: &str,
label: &str,
@@ -209,20 +247,37 @@ pub fn textarea_input(
const CHECKMARK_SVG: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" style="stroke-width:32"><polyline points="40 144 96 200 224 72" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/></svg>"#;
-pub fn checkbox(name: &str, value: &str, label: &str, checked: bool, enabled: bool) -> Markup {
- html! {
- label class={
- "group flex w-full items-center gap-2 "
- (if enabled { "cursor-pointer" } else { "cursor-not-allowed opacity-60" })
- } {
- input type="checkbox" name=(name) value=(value)
- checked[checked]
- disabled[!enabled]
- class="hidden";
- div class="checkbox-custom" {
- (PreEscaped(CHECKMARK_SVG))
+pub(crate) struct Checkbox<'a> {
+ pub name: &'a str,
+ pub value: &'a str,
+ pub label: &'a str,
+ pub checked: bool,
+ pub enabled: bool,
+}
+
+impl Checkbox<'_> {
+ pub fn render(self) -> Markup {
+ let Self {
+ name,
+ value,
+ label,
+ checked,
+ enabled,
+ } = self;
+ html! {
+ label class={
+ "group flex w-full items-center gap-2 "
+ (if enabled { "cursor-pointer" } else { "cursor-not-allowed opacity-60" })
+ } {
+ input type="checkbox" name=(name) value=(value)
+ checked[checked]
+ disabled[!enabled]
+ class="hidden";
+ div class="checkbox-custom" {
+ (PreEscaped(CHECKMARK_SVG))
+ }
+ span class="min-w-0 [overflow-wrap:anywhere] text-sm leading-5 text-neutral-900" { (label) }
}
- span class="min-w-0 [overflow-wrap:anywhere] text-sm leading-5 text-neutral-900" { (label) }
}
}
}
@@ -230,7 +285,13 @@ pub fn checkbox(name: &str, value: &str, label: &str, checked: bool, enabled: bo
pub fn opt_out_checkbox(name: &str, label: &str) -> Markup {
html! {
input type="hidden" name={(name) "_present"} value="1";
- (checkbox(name, "true", label, true, true))
+ (Checkbox {
+ name,
+ value: "true",
+ label,
+ checked: true,
+ enabled: true,
+ }.render())
}
}
diff --git a/fluxer_admin/src/templates/components/voice.rs b/fluxer_admin/src/templates/components/voice.rs
index e373e0efb..f7c53292d 100644
--- a/fluxer_admin/src/templates/components/voice.rs
+++ b/fluxer_admin/src/templates/components/voice.rs
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-use super::form::checkbox;
+use super::form::Checkbox;
use maud::{Markup, html};
pub fn voice_status_badges(vip_only: bool, has_features: bool, has_guild_ids: bool) -> Markup {
@@ -72,7 +72,13 @@ pub fn voice_restriction_fields(
html! {
div class="space-y-3 border-neutral-200 border-t pt-3" {
h4 class="font-medium text-neutral-700 text-sm" { "Access Restrictions" }
- (checkbox("vip_only", "true", "VIP Only", vip_only, true))
+ (Checkbox {
+ name: "vip_only",
+ value: "true",
+ label: "VIP Only",
+ checked: vip_only,
+ enabled: true,
+ }.render())
div class="space-y-1" {
label class="block text-sm font-medium text-neutral-700" { "Required Guild Features" }
input type="text" name="required_guild_features" value=(required_features)
diff --git a/fluxer_admin/src/templates/pages/admin_api_keys.rs b/fluxer_admin/src/templates/pages/admin_api_keys.rs
index 471184436..4e26541d3 100644
--- a/fluxer_admin/src/templates/pages/admin_api_keys.rs
+++ b/fluxer_admin/src/templates/pages/admin_api_keys.rs
@@ -9,7 +9,7 @@ use crate::{
components::{
alert::{AlertVariant, alert},
badge::{BadgeVariant, badge},
- form::{checkbox, csrf_input, form_actions},
+ form::{Checkbox, csrf_input, form_actions},
page_container::page_header,
section_card::section_card,
table::empty_state,
@@ -96,7 +96,13 @@ fn create_form(base: &str, csrf_token: &str, acls: &[&str]) -> Markup {
}
div class="grid grid-cols-1 gap-3 md:grid-cols-2" {
@for acl in acls {
- (checkbox("acls", acl, acl, false, true))
+ (Checkbox {
+ name: "acls",
+ value: acl,
+ label: acl,
+ checked: false,
+ enabled: true,
+ }.render())
}
}
}
diff --git a/fluxer_admin/src/templates/pages/bulk_actions.rs b/fluxer_admin/src/templates/pages/bulk_actions.rs
index 444557e41..0ed63dae9 100644
--- a/fluxer_admin/src/templates/pages/bulk_actions.rs
+++ b/fluxer_admin/src/templates/pages/bulk_actions.rs
@@ -13,7 +13,7 @@ use crate::{
internal_note_field, radio_group, reason_select,
},
form::{
- checkbox, csrf_input, danger_button, form_actions, form_field_group, submit_button,
+ Checkbox, csrf_input, danger_button, form_actions, form_field_group, submit_button,
text_input, textarea_input,
},
page_container::page_header,
@@ -68,7 +68,13 @@ fn guild_feature_checkbox_grid(prefix: &str, include_deprecated: bool) -> Markup
div class="grid grid-cols-1 gap-3 sm:grid-cols-2" {
@for feature in GUILD_FEATURES {
@if include_deprecated || !DEPRECATED_GUILD_FEATURES.contains(feature) {
- (checkbox(prefix, feature, &guild_feature_label(feature), false, true))
+ (Checkbox {
+ name: prefix,
+ value: feature,
+ label: &guild_feature_label(feature),
+ checked: false,
+ enabled: true,
+ }.render())
}
}
}
@@ -79,7 +85,13 @@ fn user_flag_checkbox_grid(prefix: &str) -> Markup {
html! {
div class="grid grid-cols-1 gap-3 sm:grid-cols-2" {
@for flag in crate::admin_flags::USER_FLAGS {
- (checkbox(prefix, &flag.value.to_string(), flag.name, false, true))
+ (Checkbox {
+ name: prefix,
+ value: &flag.value.to_string(),
+ label: flag.name,
+ checked: false,
+ enabled: true,
+ }.render())
}
}
}
diff --git a/fluxer_admin/src/templates/pages/guild_detail_tabs/features.rs b/fluxer_admin/src/templates/pages/guild_detail_tabs/features.rs
index dd371b0b3..eb96b3921 100644
--- a/fluxer_admin/src/templates/pages/guild_detail_tabs/features.rs
+++ b/fluxer_admin/src/templates/pages/guild_detail_tabs/features.rs
@@ -6,7 +6,7 @@ use crate::{
api::types::GuildInfo,
config::AdminConfig,
templates::components::{
- form::{checkbox, csrf_input, form_actions, submit_button},
+ form::{Checkbox, csrf_input, form_actions, submit_button},
page_container::card_with_header,
},
};
@@ -60,13 +60,13 @@ pub fn features_tab(
(csrf_input(csrf_token))
div class="space-y-3" {
@for &feature in features_list {
- (checkbox(
- "features[]",
- feature,
- &feature_label(feature),
- guild.features.iter().any(|f| f == feature),
- true,
- ))
+ (Checkbox {
+ name: "features[]",
+ value: feature,
+ label: &feature_label(feature),
+ checked: guild.features.iter().any(|f| f == feature),
+ enabled: true,
+ }.render())
}
}
div class="mt-6 border-t border-neutral-200 pt-6 space-y-2" {
diff --git a/fluxer_admin/src/templates/pages/guild_detail_tabs/settings.rs b/fluxer_admin/src/templates/pages/guild_detail_tabs/settings.rs
index a71afd247..b35b62882 100644
--- a/fluxer_admin/src/templates/pages/guild_detail_tabs/settings.rs
+++ b/fluxer_admin/src/templates/pages/guild_detail_tabs/settings.rs
@@ -5,7 +5,7 @@ use crate::{
api::types::GuildDetailInfo,
config::AdminConfig,
templates::components::{
- form::{checkbox, csrf_input, form_actions, submit_button},
+ form::{Checkbox, csrf_input, form_actions, submit_button},
page_container::card_with_header,
},
};
@@ -105,20 +105,20 @@ pub fn settings_tab(
input type="hidden" name="nsfw_submitted" value="1";
input type="hidden" name="content_warning_submitted" value="1";
div class="flex flex-wrap gap-6" {
- (checkbox(
- "nsfw",
- "on",
- "Adult content (18+)",
- guild.nsfw.unwrap_or(false),
- true,
- ))
- (checkbox(
- "content_warning_level",
- "on",
- "Show a content warning",
- guild.content_warning_level == Some(1),
- true,
- ))
+ (Checkbox {
+ name: "nsfw",
+ value: "on",
+ label: "Adult content (18+)",
+ checked: guild.nsfw.unwrap_or(false),
+ enabled: true,
+ }.render())
+ (Checkbox {
+ name: "content_warning_level",
+ value: "on",
+ label: "Show a content warning",
+ checked: guild.content_warning_level == Some(1),
+ enabled: true,
+ }.render())
}
div class="space-y-2" {
label for="guild-content-warning-text"
@@ -152,13 +152,13 @@ pub fn settings_tab(
(csrf_input(csrf_token))
div class="space-y-3" {
@for &(name, value) in DISABLED_OPERATIONS {
- (checkbox(
- "disabled_operations[]",
- &value.to_string(),
- name,
- guild.disabled_operations.unwrap_or(0) & value == value,
- true,
- ))
+ (Checkbox {
+ name: "disabled_operations[]",
+ value: &value.to_string(),
+ label: name,
+ checked: guild.disabled_operations.unwrap_or(0) & value == value,
+ enabled: true,
+ }.render())
}
}
div class="mt-6 border-t border-neutral-200 pt-6" {
@@ -175,10 +175,34 @@ pub fn settings_tab(
(csrf_input(csrf_token))
div class="space-y-4" {
div class="space-y-2" {
- (checkbox("fields[]", "icon", "Icon", false, true))
- (checkbox("fields[]", "banner", "Banner", false, true))
- (checkbox("fields[]", "splash", "Splash", false, true))
- (checkbox("fields[]", "embed_splash", "Embed Splash", false, true))
+ (Checkbox {
+ name: "fields[]",
+ value: "icon",
+ label: "Icon",
+ checked: false,
+ enabled: true,
+ }.render())
+ (Checkbox {
+ name: "fields[]",
+ value: "banner",
+ label: "Banner",
+ checked: false,
+ enabled: true,
+ }.render())
+ (Checkbox {
+ name: "fields[]",
+ value: "splash",
+ label: "Splash",
+ checked: false,
+ enabled: true,
+ }.render())
+ (Checkbox {
+ name: "fields[]",
+ value: "embed_splash",
+ label: "Embed Splash",
+ checked: false,
+ enabled: true,
+ }.render())
}
(form_actions(html! {
(submit_button("Clear Selected Fields"))
diff --git a/fluxer_admin/src/templates/pages/report_detail.rs b/fluxer_admin/src/templates/pages/report_detail.rs
index 4a78a6b3a..f295375bc 100644
--- a/fluxer_admin/src/templates/pages/report_detail.rs
+++ b/fluxer_admin/src/templates/pages/report_detail.rs
@@ -24,7 +24,7 @@ use crate::{
},
enforcement_fields::{TakeAction, can_take_action, take_action_href},
form::{
- FORM_CONTROL_CLASS, FORM_LABEL_CLASS, checkbox, csrf_input, danger_button,
+ Checkbox, FORM_CONTROL_CLASS, FORM_LABEL_CLASS, csrf_input, danger_button,
opt_out_checkbox, select_chevron,
},
media::user_avatar_url,
@@ -96,20 +96,12 @@ fn reported_user_label(report: &ReportEntry) -> String {
)
}
-fn admin_acls(auth: &AuthContext) -> &[String] {
- auth.admin_user
- .as_ref()
- .map(|admin| admin.acls.as_slice())
- .unwrap_or(&[])
-}
-
fn has_acl(auth: &AuthContext, permission: &str) -> bool {
acl::has_permission(admin_acls(auth), permission)
}
fn basic_info_section(config: &AdminConfig, auth: &AuthContext, report: &ReportEntry) -> Markup {
let base = &config.base_path;
- let show_pii = has_acl(auth, acl::REPORT_VIEW_REPORTER_PII);
html! {
(section_card(Some("Basic Information"), None, None, html! {
div class="grid grid-cols-1 sm:grid-cols-2 gap-4" {
@@ -1090,7 +1082,13 @@ fn delete_report_card(
input type="text" id="delete_audit_log_reason" name="audit_log_reason" maxlength="512"
placeholder="Why this report is being deleted"
class={(FORM_CONTROL_CLASS) " h-8 px-3 py-1.5"};
- (checkbox("confirm", "true", DELETE_REPORT_CONFIRMATION, false, true))
+ (Checkbox {
+ name: "confirm",
+ value: "true",
+ label: DELETE_REPORT_CONFIRMATION,
+ checked: false,
+ enabled: true,
+ }.render())
(danger_button("Delete Report"))
}
}
@@ -1218,24 +1216,3 @@ pub fn report_detail_fragment(config: &AdminConfig, report: &ReportEntry) -> Mar
}
}
}
-
-fn basic_info_section_fragment(config: &AdminConfig, report: &ReportEntry) -> Markup {
- let base = &config.base_path;
- html! {
- (section_card(Some("Basic Information"), None, None, html! {
- div class="grid grid-cols-1 sm:grid-cols-2 gap-4" {
- (data_field_mono("Report ID", &report.report_id))
- (data_field_text("Reported At", &format_admin_timestamp(&report.reported_at)))
- (data_field_text("Type", report_type(report.report_type).0))
- (category_field(report))
- (reason_field(report))
- @if let Some(ref reporter_id) = report.reporter_id {
- (data_field_link_mono("Reporter", &format!("{base}/users/{reporter_id}"), &reporter_label(report)))
- } @else {
- (data_field_text("Reporter", &reporter_label(report)))
- }
- (data_field("Status", status_badge(report.status)))
- }
- }))
- }
-}
diff --git a/fluxer_admin/src/templates/pages/url_domain_bans.rs b/fluxer_admin/src/templates/pages/url_domain_bans.rs
index 688b41be6..5ef82aa77 100644
--- a/fluxer_admin/src/templates/pages/url_domain_bans.rs
+++ b/fluxer_admin/src/templates/pages/url_domain_bans.rs
@@ -7,7 +7,7 @@ use crate::{
templates::{
components::{
badge::{BadgeVariant, badge},
- form::{checkbox, csrf_input},
+ form::{Checkbox, csrf_input},
page_container::page_header,
table::{data_table, empty_state, table_cell, table_row},
},
@@ -64,7 +64,13 @@ fn ban_card(base: &str, csrf_token: &str) -> Markup {
csrf_token,
html! {
(blocklist_text_field("domain", "Domain or pattern", "example.com or *shop*.example.com", true))
- (checkbox("match_subdomains", "true", "Match subdomains (e.g. sub.example.com)", true, true))
+ (Checkbox {
+ name: "match_subdomains",
+ value: "true",
+ label: "Match subdomains (e.g. sub.example.com)",
+ checked: true,
+ enabled: true,
+ }.render())
(blocklist_text_field("audit_log_reason", "Private reason (audit log, optional)", "Why is this ban being applied?", false))
},
"Ban",
diff --git a/fluxer_admin/src/templates/pages/voice_regions.rs b/fluxer_admin/src/templates/pages/voice_regions.rs
index 92242f525..b40d4d9b3 100644
--- a/fluxer_admin/src/templates/pages/voice_regions.rs
+++ b/fluxer_admin/src/templates/pages/voice_regions.rs
@@ -12,7 +12,7 @@ use crate::{
data_field::data_field_text,
empty_state::empty_state_full,
error_display::error_alert,
- form::{checkbox, csrf_input},
+ form::{Checkbox, csrf_input},
page_container::{card, page_header_with_actions},
voice::{
voice_features_list, voice_guild_ids_list, voice_restriction_fields,
@@ -213,7 +213,13 @@ fn edit_region_form(config: &AdminConfig, region: &VoiceRegion, csrf_token: &str
(form_field("Latitude", "region-latitude", "latitude", "number", &lat, "Geographic latitude", false))
(form_field("Longitude", "region-longitude", "longitude", "number", &lng, "Geographic longitude", false))
}
- (checkbox("is_default", "true", "Set as default region", is_default, true))
+ (Checkbox {
+ name: "is_default",
+ value: "true",
+ label: "Set as default region",
+ checked: is_default,
+ enabled: true,
+ }.render())
(voice_restriction_fields("edit", vip_only, &features_csv, &guild_ids_csv))
button type="submit"
class="inline-flex items-center justify-center gap-2 font-medium \
@@ -244,7 +250,13 @@ fn create_region_form(config: &AdminConfig, csrf_token: &str) -> Markup {
(form_field("Latitude", "new-region-latitude", "latitude", "number", "", "40.7128", true))
(form_field("Longitude", "new-region-longitude", "longitude", "number", "", "-74.0060", true))
}
- (checkbox("is_default", "true", "Set as default region", false, true))
+ (Checkbox {
+ name: "is_default",
+ value: "true",
+ label: "Set as default region",
+ checked: false,
+ enabled: true,
+ }.render())
(voice_restriction_fields("create", false, "", ""))
button type="submit"
class="inline-flex items-center justify-center gap-2 font-medium \
diff --git a/fluxer_admin/src/templates/pages/voice_servers_forms.rs b/fluxer_admin/src/templates/pages/voice_servers_forms.rs
index dfeaa70da..92f580bd9 100644
--- a/fluxer_admin/src/templates/pages/voice_servers_forms.rs
+++ b/fluxer_admin/src/templates/pages/voice_servers_forms.rs
@@ -6,7 +6,7 @@ use crate::{
api::types::VoiceServer,
config::AdminConfig,
templates::components::{
- form::{checkbox, csrf_input},
+ form::{Checkbox, csrf_input},
page_container::card,
voice::voice_restriction_fields,
},
@@ -90,7 +90,13 @@ pub fn edit_server_form(
"LiveKit API secret (leave blank to keep unchanged)",
))
div class="space-y-2" {
- (checkbox("is_active", "true", "Server is active", is_active, true))
+ (Checkbox {
+ name: "is_active",
+ value: "true",
+ label: "Server is active",
+ checked: is_active,
+ enabled: true,
+ }.render())
}
(voice_restriction_fields(&id_prefix, vip_only, &features_csv, &guild_ids_csv))
button type="submit"
@@ -130,7 +136,13 @@ pub fn create_server_form(config: &AdminConfig, region_id: &str, csrf_token: &st
))
}
div class="space-y-3" {
- (checkbox("is_active", "true", "Server is active", true, true))
+ (Checkbox {
+ name: "is_active",
+ value: "true",
+ label: "Server is active",
+ checked: true,
+ enabled: true,
+ }.render())
}
(voice_restriction_fields("create", false, "", ""))
button type="submit"