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

View File

@@ -859,35 +859,142 @@ td.col-actions {
} }
/* ============================================================ /* ============================================================
HOSTS PAGE MODAL
============================================================ */ ============================================================ */
.hosts-page h1 { .modal-backdrop {
margin-bottom: var(--size-lg); 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); background: var(--bg-surface);
border: 1px solid var(--border); border: 1px solid var(--border);
border-radius: var(--radius-lg); border-radius: var(--radius-lg);
box-shadow: var(--shadow-sm); box-shadow: var(--shadow-lg);
padding: var(--size-lg); padding: var(--size-lg) var(--size-xl);
margin-bottom: var(--size-md); width: 90%;
max-width: 440px;
animation: modal-in var(--transition-base) both;
} }
.hosts-page .add-form h2 { @keyframes modal-in {
margin-bottom: var(--size-md); from { opacity: 0; transform: translateY(-12px) scale(0.97); }
to { opacity: 1; transform: translateY(0) scale(1); }
} }
.add-form__fields { .modal__header {
display: grid; display: flex;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); 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); gap: var(--size-md);
align-items: end;
} }
.add-form__fields button[type="submit"] { .modal .add-form__fields label {
align-self: end; 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 */ /* Delete button inside hosts table */