Files
llmgw/internal/web/nav.go
T
Ilker ManapandClaude Opus 5.5 c9cd25efc2 Draw one menu on every page
Each page wrote its own nav: the same links in a different order, some
missing, the current page sometimes listed and sometimes not. A shared
"nav" template now draws it everywhere — Cluster, People, Quotas, Usage,
Mirror for an administrator, then Chat, Your keys and Sign out — with
the current page marked by aria-current. Pages rendered outside the
account pages (the dashboard, node logs) get it through the request
context their admin wrapper sets. The admin listener's own page keeps
its metrics/pprof/api links.

The chat and key pages said "project proj-3kq7…"; they now show the
project's path, and the key page no longer repeats the email.

A test renders several admin pages and requires the same menu on each.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 17:55:36 +02:00

76 lines
2.3 KiB
Go

package web
import (
"context"
"net/http"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// Nav is the menu at the top of every signed-in page.
//
// One menu, drawn by one template, because each page used to write its own:
// the same links in a different order, some missing, and the page you were on
// sometimes listed and sometimes not. Moving between pages then read as moving
// between different programs.
type Nav struct {
// Current is the page being shown, so its link is marked rather than
// offered: "cluster", "people", "quotas", "usage", "mirror", "chat", "keys".
Current string
// Admin shows the administration links.
Admin bool
// Mirror and Quotas are whether those pages were mounted at all; usage is
// mounted with quotas.
Mirror, Quotas bool
// CSRF signs the sign-out form.
CSRF string
// Project is where the signed-in person's keys live, as a path. Carried
// here because every page that says whose session it is wants it, and the
// id it replaces ("project proj-3kq7…") told nobody anything.
Project string
}
// projectPaths is the part of the store that can name a project.
type projectPaths interface {
AllProjects(ctx context.Context) ([]store.ProjectPath, error)
}
type navKey struct{}
// navFor builds the menu for a signed-in person.
func (p *AccountPages) navFor(r *http.Request, user store.User, current string) Nav {
n := Nav{
Current: current,
Admin: user.IsAdmin,
Mirror: p.hasMirror,
Quotas: p.hasQuotas,
CSRF: csrfFor(r),
Project: user.ProjectID,
}
if pp, ok := p.keys.(projectPaths); ok && user.ProjectID != "" {
if all, err := pp.AllProjects(r.Context()); err == nil {
for _, x := range all {
if x.ID == user.ProjectID {
n.Project = x.Path
}
}
}
}
return n
}
// withNav carries the menu to a page rendered by a handler that has no
// session of its own — the dashboard and the node logs are built without the
// account pages and wrapped by them.
func (p *AccountPages) withNav(r *http.Request, user store.User) *http.Request {
return r.WithContext(context.WithValue(r.Context(), navKey{}, p.navFor(r, user, "")))
}
// navFrom reads the menu withNav stored, marked for the page being drawn.
// Zero when there is none, which draws no admin links.
func navFrom(r *http.Request, current string) Nav {
n, _ := r.Context().Value(navKey{}).(Nav)
n.Current = current
return n
}