From 5726252825b171a4237e840ccf45d01b5e491b5e Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Sun, 27 Sep 2026 14:54:02 +0200 Subject: [PATCH] Open the quota form on the branch, instead of asking which branch again The tree was rendered and then, underneath it, a form with a dropdown asking which of those rows you meant. The tree already said. Picking a branch, reading its current numbers, scrolling past the tree and selecting the same branch from a list is three steps to do one thing, and two of them are a chance to pick the wrong row. A quota belongs to a branch, so the form opens on it: each row links to ?edit=scope:id, that row renders its form inline, pre-filled with what is set there now and naming the branch in full. By link rather than by disclosure on every row: a collapsed form per branch would put a form in the markup for every group on a page whose job is to be read as a tree. One is open at a time, and the rest keep their link. Clearing moved into that form as a second submit, so a row that is not being edited has one control and not two. Saving closes it. Re-rendering the form over the row it just changed hides the result and invites submitting the same thing twice. The note about a quota being shared came out of the footer: the open form says it, at the point where somebody is about to do it. The dropdown's parsing went with it rather than being left unreachable. Co-Authored-By: Claude Opus 5 (1M context) --- internal/web/admin_quotas.go | 28 ++++++--- internal/web/admin_quotas_test.go | 66 +++++++++++++------- internal/web/static/base.css | 5 ++ internal/web/templates/admin_quotas.html | 79 +++++++++++------------- 4 files changed, 105 insertions(+), 73 deletions(-) diff --git a/internal/web/admin_quotas.go b/internal/web/admin_quotas.go index 6f50efd..9c5d4f5 100644 --- a/internal/web/admin_quotas.go +++ b/internal/web/admin_quotas.go @@ -45,6 +45,13 @@ type quotaRow struct { Percent int HasPct bool + // Editing is true for the one row whose form is open. + // + // One at a time, chosen by a link rather than opened by script. A + // disclosure on every row would put twenty collapsed forms in the markup of + // a page whose job is to be read as a tree. + Editing bool + // Shadowed names an ancestor whose limit is stricter, which means the value // written here does nothing. // @@ -57,7 +64,10 @@ type quotaRow struct { } type adminQuotasData struct { - Rows []quotaRow + Rows []quotaRow + // Editing is the "scope:id" whose form is open, so the template can keep + // the link on every other row. + Editing string Since string Message string Error string @@ -108,15 +118,9 @@ func (p *AccountPages) RegisterAdminQuotas(mux *http.ServeMux, s QuotaAdmin) { // applyQuota writes one scope's limits, and says what it did. func (p *AccountPages) applyQuota(r *http.Request, s QuotaAdmin, admin store.User) (string, error) { - // "scope:id", from the chooser, or the two fields the Clear buttons post. + // Carried by the form the row opened, so the branch a quota lands on is the + // one that was clicked rather than one somebody named again in a box. scope, id := store.Scope(r.FormValue("scope")), r.FormValue("id") - if t := r.FormValue("target"); t != "" { - sc, i, ok := strings.Cut(t, ":") - if !ok { - return "", errors.New("choose where the quota goes") - } - scope, id = store.Scope(sc), i - } switch scope { case store.ScopeOrg, store.ScopeTeam, store.ScopeProject: default: @@ -189,6 +193,8 @@ func (p *AccountPages) renderQuotas(w http.ResponseWriter, r *http.Request, Now: time.Now().UTC().Format(time.RFC3339), } + d.Editing = r.URL.Query().Get("edit") + limits, err := s.AllLimits(r.Context()) if err != nil { p.log.Error("could not read the limits", "err", err) @@ -229,6 +235,10 @@ func (p *AccountPages) renderQuotas(w http.ResponseWriter, r *http.Request, } } markShadowed(d.Rows, limits, orgs, teamsByOrg, projects, pathOf) + for i := range d.Rows { + d.Rows[i].Editing = d.Editing == + string(d.Rows[i].Scope)+":"+d.Rows[i].ID + } render(w, "admin_quotas", d) } diff --git a/internal/web/admin_quotas_test.go b/internal/web/admin_quotas_test.go index e582941..92b2b99 100644 --- a/internal/web/admin_quotas_test.go +++ b/internal/web/admin_quotas_test.go @@ -245,36 +245,58 @@ func TestSomebodyWhoIsNotAnAdminSeesNoQuotas(t *testing.T) { } } -func TestTheChooserPicksTheGroupWithoutTypingAnId(t *testing.T) { - s := oneTeam() - mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ - "csrf": {"tok"}, "target": {"team:team-1"}, "rpm": {"60"}, - }) +func TestAFormOpensOnTheBranchThatWasClicked(t *testing.T) { + body := mirrorGet(t, quotasPage(t, oneTeam(), true), + "/admin/quotas?edit=team:team-1").Body.String() - // Ids are random so two organizations can both have a "platform" team, - // which makes them impossible to remember and a typo impossible to notice: - // it would write a quota onto nothing and report success. - if len(s.put) != 1 || s.put[0].ScopeID != "team-1" || s.put[0].Scope != store.ScopeTeam { - t.Fatalf("the chooser value did not resolve: %+v", s.put) + // The tree is the interface: a quota belongs to a branch, so the form opens + // on that branch rather than asking for its id again. Ids are random — two + // organizations can both have a "platform" team — so typing one is a typo + // waiting to write a quota onto nothing and report success. + if !strings.Contains(body, `name="id" value="team-1"`) { + t.Errorf("the open form should carry the branch it belongs to; got %s", body) + } + if !strings.Contains(body, "Scania / platform") { + t.Error("the form should name the branch in full") } } -func TestAChooserValueThatIsNotAPairIsRefused(t *testing.T) { - s := oneTeam() - mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ - "csrf": {"tok"}, "target": {"team-1"}, "rpm": {"60"}, - }) - if len(s.put) != 0 { - t.Error("nothing should have been written") +func TestOnlyTheClickedBranchOpens(t *testing.T) { + body := mirrorGet(t, quotasPage(t, oneTeam(), true), + "/admin/quotas?edit=team:team-1").Body.String() + + // One form at a time. A disclosure on every row would put a form in the + // markup for each branch of a page whose job is to be read as a tree. + if n := strings.Count(body, `name="csrf"`); n != 1 { + t.Errorf("expected exactly one form, found %d", n) + } + if !strings.Contains(body, `href="/admin/quotas?edit=project:proj-1"`) { + t.Error("every other branch should still offer to open") } } -func TestTheChooserOffersEveryGroupByItsFullPath(t *testing.T) { +func TestABranchWithNoQuotaSaysSoOnItsLink(t *testing.T) { body := mirrorGet(t, quotasPage(t, oneTeam(), true), "/admin/quotas").Body.String() - // "platform" alone is ambiguous the moment a second organization has one. - if !strings.Contains(body, `value="team:team-1"`) || - !strings.Contains(body, "Scania / platform") { - t.Errorf("the chooser should name the whole path; got %s", body) + // "change" on a branch that has nothing set would suggest there is + // something to change. + if !strings.Contains(body, "set quota") { + t.Errorf("an unlimited branch should invite setting one; got %s", body) + } +} + +func TestSavingClosesTheFormSoTheResultIsVisible(t *testing.T) { + s := oneTeam() + body := mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "csrf": {"tok"}, "scope": {"team"}, "id": {"team-1"}, "rpm": {"60"}, + }).Body.String() + + // Re-rendering the form you just submitted hides the row you changed behind + // it, and invites a second submission of the same thing. + if strings.Contains(body, `name="rpm"`) { + t.Error("the form should be closed after a save") + } + if !strings.Contains(body, "rpm 60") { + t.Errorf("the page should confirm what was written; got %s", body) } } diff --git a/internal/web/static/base.css b/internal/web/static/base.css index 28ff668..5e5fc82 100644 --- a/internal/web/static/base.css +++ b/internal/web/static/base.css @@ -277,3 +277,8 @@ tr:has(+ .url-row) td, tr:has(+ .error-row) td { border-bottom: none; } .field-row { display: flex; gap: 1rem; flex-wrap: wrap; } .field-row label { flex: 1 1 12rem; } .opt { font-weight: 400; color: var(--muted); font-size: .8rem; } + +/* The row whose quota form is open. Tinted rather than outlined: a border + inside a table cell breaks the ruling of every column beside it. */ +tr.editing > td { background: color-mix(in srgb, var(--accent) 8%, var(--card)); } +tr.editing form { margin: .4rem 0; } diff --git a/internal/web/templates/admin_quotas.html b/internal/web/templates/admin_quotas.html index ec6d01b..500ae66 100644 --- a/internal/web/templates/admin_quotas.html +++ b/internal/web/templates/admin_quotas.html @@ -14,10 +14,13 @@ {{if .Message}}

{{.Message}}

{{end}} {{if .Rows}} +{{/* The tree is the interface. A quota belongs to a branch, so the form to set + one opens on that branch rather than asking somebody to name it again in a + box at the bottom. */}} {{range .Rows}} - + {{if .Shadowed}} @@ -45,6 +43,37 @@ {{end}} {{if .Models}}{{end}} + +{{if .Editing}} + +{{end}} {{end}}
WhereRPMTPMBudgetSpentUsed
{{if eq .Indent 0}}{{.Path}}{{else}}{{.Path}}{{end}} {{.Scope}} @@ -28,13 +31,8 @@ {{.Spend}} {{if .HasPct}}{{.Percent}}%{{else}}—{{end}} -
- - - - - -
+ {{if .Editing}}close + {{else}}{{if .HasAny}}change{{else}}set quota{{end}}{{end}}
written here, but {{.Shadowed}}
only: {{.Models}}
+
+ + + +

Quota for {{.Crumb}} — shared by everything + beneath it. Whatever is set here replaces what was there; a field left blank + is no limit of that kind, not a limit of zero.

+
+ + + + +
+ + + +
+
{{else}} @@ -52,40 +81,6 @@ accounts page first.

{{end}} -

Set a quota

-
- - {{/* Chosen, not typed. Ids are random so that two organizations can both - have a "platform" team, which makes them impossible to remember and a - typo impossible to notice — it would write a quota onto nothing. */}} - -
- - - - -
- - -
- -

A quota written here is shared by everything beneath it: two -people on one team spend the same budget, which is the number that will refuse -them. Whatever is set is replaced wholesale — a field left blank means no limit -of that kind at this level, not "leave what was there".

-

Limits compose downward by taking the most restrictive value, so a team cannot raise what its organization set and a project cannot raise its team's. Only an organization's own limit is bounded by nothing. Model lists