Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkgs/id/src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::markdown::prosemirror_to_markdown;
use super::templates::{
render_binary_viewer, render_editor, render_editor_page, render_file_list,
render_file_list_content, render_main_page_wrapper, render_media_viewer, render_page,
render_peers, render_settings,
render_peers, render_peers_content, render_settings,
};
use crate::fileops::{FileOpError, FileOps, claim_archive_name, snapshot_provenance_tags};
use crate::tags::TagStore;
Expand Down Expand Up @@ -1055,7 +1055,10 @@ async fn peers_partial_handler(State(state): State<AppState>) -> impl IntoRespon
Vec::new()
};

Html(render_peers(&peers_data))
// Return only the inner content: fetchPartial assigns this as
// innerHTML of #peers-content, so the wrapper must not be included
// (issue #6956).
Html(render_peers_content(&peers_data))
}

/// Static assets handler.
Expand Down
41 changes: 40 additions & 1 deletion pkgs/id/src/web/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,19 @@ fn render_program_shell(title: &str, content: &str, assets: &AssetUrls) -> Strin
pub fn render_peers(peers: &[(String, String, u64, u64)]) -> String {
let mut html = String::with_capacity(2048);
html.push_str("<div id=\"peers-content\" data-auto-refresh=\"10\">\n");
html.push_str(&render_peers_content(peers));
html.push_str("</div>");
html
}

/// Render just the peers content (card) without the `#peers-content` wrapper.
///
/// This is the replaceable inner content of the peers page.
/// Used by `/api/peers` for auto-refresh partial updates: the client's
/// `fetchPartial` assigns the response as `innerHTML` of `#peers-content`,
/// so the partial must not carry the wrapper itself (issue #6956).
pub fn render_peers_content(peers: &[(String, String, u64, u64)]) -> String {
let mut html = String::with_capacity(2048);
html.push_str("<div class=\"card bg-base-200 border border-base-300\">\n");
html.push_str(" <div class=\"px-3 py-2 border-b border-base-300 text-sm font-bold\">Discovered Peers</div>\n");
html.push_str(" <div class=\"p-4\">\n");
Expand Down Expand Up @@ -1134,7 +1147,6 @@ pub fn render_peers(peers: &[(String, String, u64, u64)]) -> String {

html.push_str(" </div>\n");
html.push_str("</div>\n");
html.push_str("</div>");

html
}
Expand Down Expand Up @@ -1539,6 +1551,33 @@ mod tests {
assert!(html.contains("30s ago"));
}

#[test]
fn test_render_peers_content_has_no_wrapper() {
// Regression for #6956: the /api/peers partial is assigned as
// innerHTML of #peers-content, so the inner renderer must not
// carry the wrapper (duplicate id + data-auto-refresh nesting).
let peers = vec![(
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".to_owned(),
"test-node".to_owned(),
42,
30,
)];
for html in [render_peers_content(&[]), render_peers_content(&peers)] {
assert!(!html.contains("peers-content"), "inner partial must not contain #peers-content wrapper: {html}");
assert!(!html.contains("data-auto-refresh"), "inner partial must not contain data-auto-refresh: {html}");
assert!(html.contains("Discovered Peers"));
}
assert!(render_peers_content(&peers).contains("test-node"));
assert!(render_peers_content(&[]).contains("No peers discovered yet"));
}

#[test]
fn test_render_peers_wrapper_exactly_once() {
let html = render_peers(&[]);
assert_eq!(html.matches("id=\"peers-content\"").count(), 1);
assert_eq!(html.matches("data-auto-refresh").count(), 1);
}

#[test]
fn test_format_age() {
assert_eq!(format_age(5), "5s ago");
Expand Down
Loading