Skip to content

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

TypeScript fluxerfluxerapp

fix(admin): open tab links as full pages (#3734)

Hampus659a341ed7ca
diff --git a/fluxer_admin/src/middleware/htmx.rs b/fluxer_admin/src/middleware/htmx.rs
index 98d9f64f6..743af14b0 100644
--- a/fluxer_admin/src/middleware/htmx.rs
+++ b/fluxer_admin/src/middleware/htmx.rs
@@ -1,11 +1,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-use crate::middleware::flash::{self, FlashData};
+use crate::{
+ middleware::flash::{self, FlashData},
+ templates::components::detail_tabs::detail_page_href,
+};
use axum::{
extract::Request,
http::{HeaderMap, HeaderName, HeaderValue, Method, StatusCode, header},
middleware::Next,
- response::{IntoResponse, Response},
+ response::{IntoResponse, Redirect, Response},
};
const HX_RESWAP: HeaderName = HeaderName::from_static("hx-reswap");
@@ -19,6 +22,46 @@ pub fn is_htmx_request(headers: &HeaderMap) -> bool {
.is_some_and(|value| value == "true")
}
+pub fn is_history_restore(headers: &HeaderMap) -> bool {
+ headers
+ .get("HX-History-Restore-Request")
+ .and_then(|value| value.to_str().ok())
+ .is_some_and(|value| value == "true")
+}
+
+pub fn tab_page_redirect(
+ headers: &HeaderMap,
+ query: Option<&str>,
+ base_url: &str,
+ entity_id: &str,
+ tab: &str,
+) -> Option<Response> {
+ if is_htmx_request(headers) && !is_history_restore(headers) {
+ return None;
+ }
+ let plain = |value: &str| {
+ !value.is_empty()
+ && value
+ .bytes()
+ .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-')
+ };
+ if !plain(entity_id) {
+ return None;
+ }
+ let tab = if plain(tab) { tab } else { "overview" };
+ let mut href = detail_page_href(base_url, entity_id, tab);
+ let extra: Vec<&str> = query
+ .unwrap_or("")
+ .split('&')
+ .filter(|pair| !pair.is_empty() && pair.split('=').next().is_some_and(|key| key != "tab"))
+ .collect();
+ if !extra.is_empty() {
+ href.push(if href.contains('?') { '&' } else { '?' });
+ href.push_str(&extra.join("&"));
+ }
+ Some(Redirect::to(&href).into_response())
+}
+
pub fn htmx_target(headers: &HeaderMap) -> Option<String> {
headers
.get("HX-Target")
diff --git a/fluxer_admin/src/routes/guilds.rs b/fluxer_admin/src/routes/guilds.rs
index ffb45656f..5092524a6 100644
--- a/fluxer_admin/src/routes/guilds.rs
+++ b/fluxer_admin/src/routes/guilds.rs
@@ -535,6 +535,15 @@ async fn guild_tab(
request: Request,
) -> Response {
let config = state.config();
+ if let Some(redirect) = htmx::tab_page_redirect(
+ request.headers(),
+ request.uri().query(),
+ &format!("{}/guilds", config.base_path),
+ &guild_id,
+ &tab,
+ ) {
+ return redirect;
+ }
let csrf_token = csrf::get_csrf_token(&request);
let client = AdminApiClient::new(state.http_client(), config, &auth.0.session);
let admin_acls = auth.0.acls();
diff --git a/fluxer_admin/src/routes/users.rs b/fluxer_admin/src/routes/users.rs
index 4102dca6c..39c644a37 100644
--- a/fluxer_admin/src/routes/users.rs
+++ b/fluxer_admin/src/routes/users.rs
@@ -20,7 +20,7 @@ use crate::{
use axum::{
Router,
extract::{Path, Query, Request, State},
- http::HeaderMap,
+ http::{HeaderMap, Uri},
response::{Html, IntoResponse, Response},
routing::get,
};
@@ -312,8 +312,19 @@ async fn user_tab(
auth: axum::Extension<AuthContext>,
csrf: axum::Extension<CsrfToken>,
Path((user_id, tab)): Path<(String, String)>,
+ headers: HeaderMap,
+ uri: Uri,
) -> Response {
let config = state.config();
+ if let Some(redirect) = htmx::tab_page_redirect(
+ &headers,
+ uri.query(),
+ &format!("{}/users", config.base_path),
+ &user_id,
+ &tab,
+ ) {
+ return redirect;
+ }
let client = AdminApiClient::new(state.http_client(), config, &auth.0.session);
let user = client
.get_user_by_id(&user_id)
diff --git a/fluxer_admin/src/templates/components/detail_tabs.rs b/fluxer_admin/src/templates/components/detail_tabs.rs
index 3a91249a4..7899a894e 100644
--- a/fluxer_admin/src/templates/components/detail_tabs.rs
+++ b/fluxer_admin/src/templates/components/detail_tabs.rs
@@ -19,23 +19,24 @@ pub fn build_detail_tabs(
) -> Vec<DetailTab> {
tabs.iter()
.filter(|(id, _)| visible(id))
- .map(|(id, label)| {
- let href = if *id == "overview" {
- format!("{base_url}/{entity_id}")
- } else {
- format!("{base_url}/{entity_id}?tab={id}")
- };
- DetailTab {
- id: id.to_string(),
- label: label.to_string(),
- tab_url: format!("{base_url}/{entity_id}/tabs/{id}"),
- href,
- active: *id == active_tab,
- }
+ .map(|(id, label)| DetailTab {
+ id: id.to_string(),
+ label: label.to_string(),
+ tab_url: format!("{base_url}/{entity_id}/tabs/{id}"),
+ href: detail_page_href(base_url, entity_id, id),
+ active: *id == active_tab,
})
.collect()
}
+pub fn detail_page_href(base_url: &str, entity_id: &str, tab: &str) -> String {
+ if tab == "overview" {
+ format!("{base_url}/{entity_id}")
+ } else {
+ format!("{base_url}/{entity_id}?tab={tab}")
+ }
+}
+
const MOBILE_TAB_BASE: &str = "inline-flex min-h-[44px] flex-shrink-0 items-center \
whitespace-nowrap rounded-md px-3 py-2 font-medium text-sm transition-colors \
focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-primary \