feat(hosts): replace inline add form with modal dialog

The add-host form is now opened via a "+ Add host" button in the page
header. The modal closes on Cancel, backdrop click, × button, or
automatically after a successful creation.

Adds modal CSS with backdrop blur and entry animation, .btn-primary /
.btn-secondary shared button styles, and a .page-header flex layout
reusable across list pages.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-15 23:57:08 +02:00
parent 042793f385
commit a4fc5b176f
2 changed files with 210 additions and 60 deletions

View File

@@ -1,15 +1,14 @@
// client/hosts.rs — Hosts list page
//
// Displays all hosts across every network with:
// - Add form : create a host inside a chosen network
// - Add button : opens a modal form to create a host inside a chosen network
// - Filter bar : name (substring), network, open port, application
// - Table : name, IP, network, port count, application count, delete
// - Pagination : configurable page size (15 / 25 / 50 / 100 / All)
//
// Each sub-component calls `.into_any()` on its view to return `AnyView`
// (a type-erased wrapper). This prevents Rust from composing all the nested
// generic types into a single enormous type in the parent's monomorphization,
// which would otherwise overflow the compiler's query depth limit.
// Sub-components call `.into_any()` on their views to erase the concrete
// Leptos type, preventing the parent from accumulating a deeply-nested
// generic type that overflows the compiler's query depth limit.
use leptos::prelude::*;
use leptos::form::ActionForm;
@@ -28,16 +27,36 @@ const PER_PAGE_OPTIONS: &[(i64, &str)] = &[
(0, "All"),
];
// ─── Add host form ────────────────────────────────────────────────────────────
// ─── Add host modal ───────────────────────────────────────────────────────────
#[component]
fn AddHostForm(
fn AddHostModal(
create_action: ServerAction<CreateHost>,
networks_res: Resource<Result<Vec<crate::models::Network>, ServerFnError>>,
show_modal: RwSignal<bool>,
) -> impl IntoView {
// Close the modal automatically after a successful creation.
Effect::new(move |_| {
if let Some(Ok(_)) = create_action.value().get() {
show_modal.set(false);
}
});
view! {
<section class="add-form">
// Backdrop — click outside the card to close
<div class="modal-backdrop" on:click=move |_| show_modal.set(false)>
// stop_propagation keeps clicks inside the card from closing the modal
<div class="modal" on:click=move |e| e.stop_propagation()>
<div class="modal__header">
<h2>"Add a host"</h2>
<button
class="modal__close"
type="button"
aria-label="Close"
on:click=move |_| show_modal.set(false)
>"×"</button>
</div>
<ActionForm action=create_action>
<div class="add-form__fields">
<label>
@@ -60,14 +79,24 @@ fn AddHostForm(
}
</select>
</label>
<button type="submit">"Add"</button>
</div>
<div class="modal__actions">
<button
class="btn-secondary"
type="button"
on:click=move |_| show_modal.set(false)
>"Cancel"</button>
<button type="submit">"Add host"</button>
</div>
</ActionForm>
{move || create_action.value().get()
.and_then(|r| r.err())
.map(|e| view! { <p class="error">{e.to_string()}</p> })
}
</section>
</div>
</div>
}.into_any()
}
@@ -194,7 +223,7 @@ fn PaginationBar(
{move || format!("Page {} of {}", page.get(), total_pages.get().max(1))}
</span>
<button
disabled=move || page.get() >= total_pages.get()
disabled={move || page.get() >= total_pages.get()}
on:click=move |_| {
let max = total_pages.get_untracked();
page.update(|p| *p = (*p + 1).min(max));
@@ -209,7 +238,6 @@ fn PaginationBar(
// ─── Host table ───────────────────────────────────────────────────────────────
// Separate component for the table body to further reduce type depth in HostsPage.
#[component]
fn HostTable(
hosts: Resource<Result<HostsPageData, ServerFnError>>,
@@ -284,6 +312,9 @@ pub fn HostsPage() -> impl IntoView {
let create_action = ServerAction::<CreateHost>::new();
let delete_action = ServerAction::<DeleteHost>::new();
// Controls the add-host modal
let show_modal = RwSignal::new(false);
// Filter signals (0 / "" = no filter)
let name_filter = RwSignal::new(String::new());
let network_id_filter = RwSignal::new(0i64);
@@ -323,9 +354,21 @@ pub fn HostsPage() -> impl IntoView {
view! {
<div class="hosts-page">
<div class="page-header">
<h1>"Hosts"</h1>
<button class="btn-primary" on:click=move |_| show_modal.set(true)>
"+ Add host"
</button>
</div>
<AddHostForm create_action=create_action networks_res=networks_res/>
// Modal — only rendered when show_modal is true
{move || show_modal.get().then(|| view! {
<AddHostModal
create_action=create_action
networks_res=networks_res
show_modal=show_modal
/>
})}
<FilterBar
networks_res=networks_res

View File

@@ -859,35 +859,142 @@ td.col-actions {
}
/* ============================================================
HOSTS PAGE
MODAL
============================================================ */
.hosts-page h1 {
margin-bottom: var(--size-lg);
.modal-backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.45);
backdrop-filter: blur(3px);
display: flex;
align-items: center;
justify-content: center;
z-index: 200;
animation: backdrop-in var(--transition-base) both;
}
.hosts-page .add-form {
@keyframes backdrop-in {
from { opacity: 0; }
to { opacity: 1; }
}
.modal {
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius-lg);
box-shadow: var(--shadow-sm);
padding: var(--size-lg);
margin-bottom: var(--size-md);
box-shadow: var(--shadow-lg);
padding: var(--size-lg) var(--size-xl);
width: 90%;
max-width: 440px;
animation: modal-in var(--transition-base) both;
}
.hosts-page .add-form h2 {
margin-bottom: var(--size-md);
@keyframes modal-in {
from { opacity: 0; transform: translateY(-12px) scale(0.97); }
to { opacity: 1; transform: translateY(0) scale(1); }
}
.add-form__fields {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
.modal__header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: var(--size-lg);
}
.modal__header h2 {
margin: 0;
font-size: var(--font-lg);
}
.modal__close {
background: none;
border: none;
font-size: 1.4rem;
line-height: 1;
cursor: pointer;
color: var(--text-secondary);
padding: 2px 6px;
border-radius: var(--radius-sm);
transition: color var(--transition-fast), background var(--transition-fast);
}
.modal__close:hover {
color: var(--text);
background: var(--bg-hover);
}
/* Form fields inside modal — single column stack */
.modal .add-form__fields {
display: flex;
flex-direction: column;
gap: var(--size-md);
align-items: end;
}
.add-form__fields button[type="submit"] {
align-self: end;
.modal .add-form__fields label {
display: flex;
flex-direction: column;
gap: var(--size-xs);
font-size: var(--font-sm);
font-weight: 500;
color: var(--text-secondary);
}
/* Cancel / Submit button row */
.modal__actions {
display: flex;
justify-content: flex-end;
gap: var(--size-sm);
margin-top: var(--size-lg);
}
.btn-secondary {
background: var(--bg-hover);
color: var(--text);
border: 1px solid var(--border);
border-radius: var(--radius-sm);
padding: 7px var(--size-md);
font-size: var(--font-sm);
font-weight: 500;
cursor: pointer;
transition: background var(--transition-fast);
}
.btn-secondary:hover {
background: var(--bg-surface2);
}
/* ============================================================
HOSTS PAGE
============================================================ */
/* Header row: title on the left, "Add host" button on the right */
.page-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: var(--size-lg);
}
.page-header h1 {
margin: 0;
}
.btn-primary {
background: var(--accent);
color: var(--text-on-accent);
border: none;
border-radius: var(--radius-sm);
padding: 8px var(--size-md);
font-size: var(--font-sm);
font-weight: 500;
cursor: pointer;
transition: background var(--transition-fast);
white-space: nowrap;
}
.btn-primary:hover {
background: var(--accent-hover);
}
/* Delete button inside hosts table */