Skip to content

Commit 88da4e0

Browse files
authored
refactor(admin): dashboard lists share one table, badge and message fields (#599)
Part of #544. Stacked on #564 (`fix/550-notice-icons`), which also edits `crates/ui/src/admin`: retarget to `main` once it merges. The dashboard's components wrote three things out again and again: - **Tables.** Each list repeated the same empty line, `table-wrap`, `thead` and `tbody` around its rows: keys, deliveries, clients, accounts, an account's sessions, invitations, announcements and audit checkpoints. Now `data_table` takes the headings, the empty line and whether there are rows, and each list writes only its rows. - **Badges.** Seven places built a `(tone, label)` tuple and read it as `.0` and `.1`. Now they return a named `Badge`, and the `badge` component renders it. A client's listed status keeps its own match, since a typed `ClientStatus` there is still an open decision. - **Message fields.** The message page and the announcement form copied the same subject and text inputs. Now both use `message_fields`, which focuses the subject only on the message page, as before. The alert card's own `badge` function becomes `alert_badge`, because the component now has that name. `data_table`'s child makes the audit page's view slightly larger, which pushed its render future over Clippy's `large_futures` limit, so `web/admin/audit.rs` boxes that render, as the dashboard does for its other large pages. - Tests merged or removed: none. - Files edited outside the Admin area: none. - Proof that the markup is unchanged: a scratch test, not committed, rendered 69 dashboard pages against the scratch database before and after. Together with #593's change, this covered every list touched here, filled and empty, the account page with its sessions, the message page and the announcement form. After normalizing IDs, timestamps and the order of rows created in the same instant, the HTML is identical. A second scratch render in `sourceorigin-ui` covered the alert cards in every state (needing attention, investigating, monitoring, cleared), the previous alerts and the update fields' select. The output is byte-identical. Checks: `just fmt`; `cargo clippy -p sourceorigin-ui -p auth-service --all-targets` with and without `--all-features` (`-D warnings`); `cargo doc -D warnings -p sourceorigin-ui`; `cargo test --doc -p sourceorigin-ui`; `just test -p sourceorigin-ui --all-features`; `just db test -E 'test(/^web::admin::/)'`; `just rust unused`; `just docs screenshots`: it rewrote only `admin/overview.png`, the known nondeterministic one, and `account/data.png`, which is already stale on `main` (its top bar lacks the search box and bell the settings pages now have); neither page is touched here. > [!NOTE] > Written by Claude Opus 5.5 in Claude Code and reviewed by Claude Opus 5.5 (via T3 Code)
1 parent c6dd59e commit 88da4e0

10 files changed

Lines changed: 331 additions & 349 deletions

File tree

‎apps/auth-service/src/web/admin/audit.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ pub(super) async fn verify(cx: &Cx, form: Option<Form<IgnoredAny>>) -> Result<Re
4747
later: later,
4848
)
4949
};
50-
App::render(cx, StatusCode::OK, "Audit", view).await
50+
// A large page: its future is boxed, off the handler's stack.
51+
Box::pin(App::render(cx, StatusCode::OK, "Audit", view)).await
5152
}

‎crates/ui/src/admin/alerts.rs‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The alerts administrators are shown, and the updates they post on them.
22
3-
use super::{Action, Change, confirm, moment};
3+
use super::{Action, Badge, Change, badge, confirm, moment};
44
use crate::layout::page_head;
55
use sourceorigin_common::alerts::{
66
Alert, AlertAuthor, AlertDetail, AlertState, AlertStatus, AlertUpdate,
@@ -20,22 +20,24 @@ pub struct AlertCard<'a> {
2020
pub update: Option<Action>,
2121
}
2222

23-
/// Where an alert's handling stands, as a badge's tone and label: monitoring
24-
/// means the fix is in, so it reads as good.
25-
const fn status_badge(status: AlertStatus) -> (&'static str, &'static str) {
23+
/// Where an alert's handling stands, as a badge: monitoring means the fix is
24+
/// in, so it reads as good.
25+
const fn status_badge(status: AlertStatus) -> Badge {
2626
match status {
27-
AlertStatus::Investigating => ("warning", "Investigating"),
28-
AlertStatus::Fixing => ("warning", "Fixing"),
29-
AlertStatus::Monitoring => ("ok", "Monitoring"),
27+
AlertStatus::Investigating => Badge::new("warning", "Investigating"),
28+
AlertStatus::Fixing => Badge::new("warning", "Fixing"),
29+
AlertStatus::Monitoring => Badge::new("ok", "Monitoring"),
3030
}
3131
}
3232

33-
/// An alert's badge, as a tone and a label: whether it needs attention, the
34-
/// status its latest update gave, or that it cleared.
35-
fn badge(detail: &AlertDetail) -> (&'static str, &'static str) {
33+
/// An alert's badge: whether it needs attention, the status its latest update
34+
/// gave, or that it cleared.
35+
fn alert_badge(detail: &AlertDetail) -> Badge {
3636
match (detail.alert.state(), detail.updates.last()) {
37-
(AlertState::Cleared, _) => ("neutral", "Cleared"),
38-
(AlertState::Open, _) | (AlertState::Acknowledged, None) => ("danger", "Needs attention"),
37+
(AlertState::Cleared, _) => Badge::new("neutral", "Cleared"),
38+
(AlertState::Open, _) | (AlertState::Acknowledged, None) => {
39+
Badge::new("danger", "Needs attention")
40+
}
3941
(AlertState::Acknowledged, Some(update)) => status_badge(update.status),
4042
}
4143
}
@@ -103,13 +105,12 @@ pub async fn previous_alerts<'a>(
103105
async fn alert_card<'a>(card: &'a AlertCard<'a>) -> Result<impl View> {
104106
let detail = card.detail;
105107
let alert = &detail.alert;
106-
let (tone, label) = badge(detail);
107108
let subject = alert.alert.as_ref().map_or(Alert::UNKNOWN, Alert::subject);
108109
Ok(view! {
109110
<section class="card">
110111
<header>
111112
<h2>(subject)</h2>
112-
<span class=(class!("badge", tone))>(label)</span>
113+
badge(shown: alert_badge(detail))
113114
</header>
114115
<div class="body">
115116
if let Some(says) = &alert.alert {
@@ -134,9 +135,9 @@ async fn alert_card<'a>(card: &'a AlertCard<'a>) -> Result<impl View> {
134135
for update in detail.updates.iter().rev() {
135136
<li>
136137
moment(at: update.posted_at)
137-
<span class=(class!("dot", status_badge(update.status).0))></span>
138+
<span class=(class!("dot", status_badge(update.status).tone))></span>
138139
<div class="text">
139-
<strong>(status_badge(update.status).1)</strong>
140+
<strong>(status_badge(update.status).label)</strong>
140141
(format!(" by {}", author(update)))
141142
if let Some(note) = &update.note {
142143
<small class="wrap">(note.as_str())</small>
@@ -164,7 +165,7 @@ pub async fn alert_update_fields<'a>(
164165
"Status"
165166
<select name="status" required="">
166167
for each in AlertStatus::ALL {
167-
<option value=(each.as_str()) selected=(status == Some(each))>(status_badge(each).1)</option>
168+
<option value=(each.as_str()) selected=(status == Some(each))>(status_badge(each).label)</option>
168169
}
169170
</select>
170171
</label>

‎crates/ui/src/admin/announcements.rs‎

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Announcements to accounts: the list, one announcement with its email as its
22
//! recipients read it, and the form drafting one.
33
4-
use super::{Action, Change, confirm, moment};
4+
use super::{Action, Change, confirm, data_table, message_fields, moment};
55
use crate::icons::{Icon, icon};
66
use crate::{
77
clients::choice,
@@ -72,31 +72,23 @@ pub async fn announcement_list<'a>(
7272
actions: view! { <a class="button" href=(create)>"New announcement"</a> },
7373
)
7474
<section class="card">
75-
if rows.is_empty() {
76-
<p class="empty">"No announcement yet."</p>
77-
} else {
78-
<div class="table-wrap">
79-
<table>
80-
<thead>
81-
<tr><th>"subject"</th><th>"to"</th><th>"sent"</th></tr>
82-
</thead>
83-
<tbody>
84-
for row in rows {
85-
<tr>
86-
<td><a href=(row.page.as_str())>(row.announcement.subject.as_str())</a></td>
87-
<td>(audience(row.announcement))</td>
88-
<td>
89-
match row.announcement.sent_at {
90-
Some(at) => moment(at: at),
91-
None => <span class="badge warning">"Draft"</span>,
92-
}
93-
</td>
94-
</tr>
75+
data_table(
76+
headings: &["subject", "to", "sent"],
77+
empty: "No announcement yet.",
78+
filled: !rows.is_empty(),
79+
for row in rows {
80+
<tr>
81+
<td><a href=(row.page.as_str())>(row.announcement.subject.as_str())</a></td>
82+
<td>(audience(row.announcement))</td>
83+
<td>
84+
match row.announcement.sent_at {
85+
Some(at) => moment(at: at),
86+
None => <span class="badge warning">"Draft"</span>,
9587
}
96-
</tbody>
97-
</table>
98-
</div>
99-
}
88+
</td>
89+
</tr>
90+
}
91+
)
10092
</section>
10193
})
10294
}
@@ -251,15 +243,7 @@ pub async fn announcement_form<'a>(
251243
alert_notice(message: refusal)
252244
<section class="card">
253245
<form class="stack" method="post" data-submit-once="" data-unconfirmed="" data-confirming=(sending.as_str())>
254-
<label>
255-
"Subject"
256-
<input name="subject" required="" maxlength="150" value=(form.subject.as_str())>
257-
</label>
258-
<label>
259-
"Text"
260-
<textarea name="body" required="" rows="14" maxlength="20000">(form.body.as_str())</textarea>
261-
<small>"The email starts with “Hello,” and ends with “SourceOrigin”. Separate paragraphs with a blank line. Links are written out in full: mail apps make them clickable."</small>
262-
</label>
246+
message_fields(subject: form.subject.as_str(), body: form.body.as_str())
263247
<fieldset>
264248
<legend>"Send it to"</legend>
265249
choice(

‎crates/ui/src/admin/audit.rs‎

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! The audit chain's state and its checkpoints.
22
3-
use super::moment;
3+
use super::{Badge, badge, data_table, moment};
44
use crate::layout::{alert_notice, page_head};
55
use sourceorigin_common::admin::{AuditPage, ChainPoint, Pruning, VerificationKeys};
66
use topcoat::{
77
Result,
8-
view::{View, class, component, view},
8+
view::{View, component, view},
99
};
1010

1111
/// The audit chain, as stored and as last verified, why the last attempt to
@@ -47,38 +47,30 @@ pub async fn audit_page<'a>(
4747
which auth never holds, so run the command shown as the owner. It verifies the \
4848
chain again first, and refuses if it breaks at or before that checkpoint."
4949
</p>
50-
if page.checkpoints.is_empty() {
51-
<p class="empty">"No checkpoint to show."</p>
52-
} else {
53-
<div class="table-wrap">
54-
<table>
55-
<thead>
56-
<tr><th>"position"</th><th>"signed"</th><th>"pruning"</th></tr>
57-
</thead>
58-
<tbody>
59-
for checkpoint in &page.checkpoints {
60-
<tr>
61-
<td><code title=(checkpoint.point.hash.as_str())>(checkpoint.point.position.to_string())</code></td>
62-
<td>moment(at: checkpoint.signed_at)</td>
63-
<td>
64-
match &checkpoint.pruning {
65-
Pruning::Prunable => {
66-
<code>(format!("auth-service audit prune --through {}{key}", checkpoint.point.position))</code>
67-
},
68-
Pruning::Pruned { .. } => {
69-
<span class="muted">"Already pruned"</span>
70-
},
71-
Pruning::Broken(broken) => {
72-
<span class="muted">(format!("Not prunable: the chain breaks at {}", broken.position))</span>
73-
},
74-
}
75-
</td>
76-
</tr>
50+
data_table(
51+
headings: &["position", "signed", "pruning"],
52+
empty: "No checkpoint to show.",
53+
filled: !page.checkpoints.is_empty(),
54+
for checkpoint in &page.checkpoints {
55+
<tr>
56+
<td><code title=(checkpoint.point.hash.as_str())>(checkpoint.point.position.to_string())</code></td>
57+
<td>moment(at: checkpoint.signed_at)</td>
58+
<td>
59+
match &checkpoint.pruning {
60+
Pruning::Prunable => {
61+
<code>(format!("auth-service audit prune --through {}{key}", checkpoint.point.position))</code>
62+
},
63+
Pruning::Pruned { .. } => {
64+
<span class="muted">"Already pruned"</span>
65+
},
66+
Pruning::Broken(broken) => {
67+
<span class="muted">(format!("Not prunable: the chain breaks at {}", broken.position))</span>
68+
},
7769
}
78-
</tbody>
79-
</table>
80-
</div>
81-
}
70+
</td>
71+
</tr>
72+
}
73+
)
8274
</section>
8375
if let Some(later) = later {
8476
<div class="row"><a class="button secondary" href=(later)>"Later checkpoints"</a></div>
@@ -108,10 +100,10 @@ async fn chain_card<'a>(page: &'a AuditPage, uncommitted: bool) -> Result<impl V
108100
let since = verification
109101
.filter(|verification| verification.broken.is_none())
110102
.map(|verification| position(&chain.newest).saturating_sub(position(&verification.newest)));
111-
let (badge, status) = match (verification, broken) {
112-
(None, _) => ("warning", "Not verified"),
113-
(Some(_), Some(_)) => ("danger", "Broken at last check"),
114-
(Some(_), None) => ("ok", "Intact at last check"),
103+
let status = match (verification, broken) {
104+
(None, _) => Badge::new("warning", "Not verified"),
105+
(Some(_), Some(_)) => Badge::new("danger", "Broken at last check"),
106+
(Some(_), None) => Badge::new("ok", "Intact at last check"),
115107
};
116108
let broken = broken.map(|broken| {
117109
format!(
@@ -124,7 +116,7 @@ async fn chain_card<'a>(page: &'a AuditPage, uncommitted: bool) -> Result<impl V
124116
<section class="card">
125117
<header>
126118
<h2>"Chain"</h2>
127-
<span class=(class!("badge", badge))>(status)</span>
119+
badge(shown: status)
128120
</header>
129121
<div class="body">
130122
alert_notice(message: broken.as_deref())

‎crates/ui/src/admin/clients.rs‎

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//! developer console's pieces: its settings form, with the inputs only
44
//! operators fill in added, its status badges, and its secrets.
55
6-
use super::{Action, Change, REASON, confirm, moment, reason_input, refused_action};
6+
use super::{
7+
Action, Badge, Change, REASON, badge, confirm, data_table, moment, reason_input, refused_action,
8+
};
79
use crate::{
810
clients::{
911
ClientDraft, ClientRow, ClientStatus, Revealed, SecretRow, choice, client_form,
@@ -19,7 +21,7 @@ use serde::Deserialize;
1921
use sourceorigin_common::{UserId, admin::Client, clients::ClientKind, protocol::oauth::Scope};
2022
use topcoat::{
2123
Result,
22-
view::{View, class, component, view},
24+
view::{View, component, view},
2325
};
2426

2527
/// A client in the list, and its page.
@@ -277,51 +279,41 @@ pub async fn client_list<'a>(rows: &'a [AdminClientRow<'a>], new: &'a str) -> Re
277279
},
278280
)
279281
<section class="card">
280-
if rows.is_empty() {
281-
<p class="empty">"No client is registered."</p>
282-
} else {
283-
<div class="table-wrap">
284-
<table>
285-
<thead>
286-
<tr><th>"name"</th><th>"ID"</th><th>"managed by"</th><th>"status"</th><th></th></tr>
287-
</thead>
288-
<tbody>
289-
for row in rows {
290-
let client = row.client;
291-
<tr>
292-
<td>
293-
<a href=(row.href.as_str())>
294-
(if client.display_name.is_empty() { "Deleted client" } else { client.display_name.as_str() })
295-
</a>
296-
</td>
297-
<td><code>(client.id.as_str())</code></td>
298-
<td>(if client.owner_id.is_some() { "An account" } else { "Operators" })</td>
299-
<td>listed_status(client: client)</td>
300-
<td class="right">
301-
if client.trusted { <span class="badge">"Trusted"</span> }
302-
</td>
303-
</tr>
304-
}
305-
</tbody>
306-
</table>
307-
</div>
308-
}
282+
data_table(
283+
headings: &["name", "ID", "managed by", "status", ""],
284+
empty: "No client is registered.",
285+
filled: !rows.is_empty(),
286+
for row in rows {
287+
let client = row.client;
288+
<tr>
289+
<td>
290+
<a href=(row.href.as_str())>
291+
(if client.display_name.is_empty() { "Deleted client" } else { client.display_name.as_str() })
292+
</a>
293+
</td>
294+
<td><code>(client.id.as_str())</code></td>
295+
<td>(if client.owner_id.is_some() { "An account" } else { "Operators" })</td>
296+
<td>listed_status(client: client)</td>
297+
<td class="right">
298+
if client.trusted { <span class="badge">"Trusted"</span> }
299+
</td>
300+
</tr>
301+
}
302+
)
309303
</section>
310304
})
311305
}
312306

313307
/// A listed client's status, as a badge.
314308
#[component]
315309
async fn listed_status<'a>(client: &'a Client) -> Result<impl View> {
316-
let (tone, label) = match client.status() {
317-
"enabled" => ("ok", "Enabled"),
318-
"suspended" => ("danger", "Suspended"),
319-
"deleted" => ("neutral", "Deleted"),
320-
_ => ("warning", "Disabled"),
310+
let shown = match client.status() {
311+
"enabled" => Badge::new("ok", "Enabled"),
312+
"suspended" => Badge::new("danger", "Suspended"),
313+
"deleted" => Badge::new("neutral", "Deleted"),
314+
_ => Badge::new("warning", "Disabled"),
321315
};
322-
Ok(view! {
323-
<span class=(class!("badge", tone))>(label)</span>
324-
})
316+
Ok(view! { badge(shown: shown) })
325317
}
326318

327319
/// One client's page as of `now`: what it is, why it does not sign users in,

0 commit comments

Comments
 (0)