Files
MujicaUI/account/api_key_manager.go
T
ZacharyZhang-NYandClaude Opus 5.5 bcce34cda1 Upgrade MyGo to v0.5.5
MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:

- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
  elements expire and Context methods are inert: read IsDisabled into a
  local during the build, keep a ui.Handle for Focus, use c.Services()
  for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
  content builds, so same-frame autofocus lost focus restore on close.
  It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
  c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
  split button tooltip no longer reorders its open menu; disabled
  menubar rows pass presses to the panel; Form defers blur-started
  async checks so the growing message no longer moves Submit away
  from the release; Combobox drops its disabled copy now that MyGo
  blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
  time.Sleep/time.Now and read element state inside the view. The web
  mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.

Snapshot goldens are darwin-only and were not re-verified here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:37:54 -04:00

209 lines
7.6 KiB
Go

package account
import (
"fmt"
"log/slog"
"slices"
"strings"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/feedback"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/overlay"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// APIKey contains public metadata and the visible prefix.
type APIKey struct {
ID, Name, Prefix string
Created, LastUsed time.Time
}
// ApiKeyManagerState holds creation, confirmation and one-time reveal state.
type ApiKeyManagerState struct {
creating, pending bool
name, secret string
err string
revoking string
keys []APIKey
index map[string]APIKey
}
// Reveal presents the generated secret until dismissal.
func (s *ApiKeyManagerState) Reveal(secret string) {
if !s.pending || secret == "" {
panic("mujica: ApiKeyManagerState.Reveal needs a create in progress and a secret")
}
s.pending, s.secret, s.err = false, secret, ""
slog.Debug("mujica: api_key_manager revealed")
}
// Fail displays a caller-provided creation error.
func (s *ApiKeyManagerState) Fail(err string) {
if !s.pending {
panic("mujica: ApiKeyManagerState.Fail needs a create in progress")
}
s.creating, s.pending, s.err = true, false, err
}
// ApiKeyManagerOptions configure an ApiKeyManager.
type ApiKeyManagerOptions struct {
Disabled bool
}
// ApiKeyManagerResult reports creation and revocation requests.
type ApiKeyManagerResult struct {
Element ui.Element
create, revoke string
}
// Create returns the requested name; finish with Reveal or Fail.
func (r ApiKeyManagerResult) Create() (string, bool) { return r.create, r.create != "" }
// Revoked returns the confirmed key ID.
func (r ApiKeyManagerResult) Revoked() (string, bool) { return r.revoke, r.revoke != "" }
// ApiKeyManager masks stored keys and confirms revocation.
func ApiKeyManager(c *ui.Context, s *ApiKeyManagerState, keys []APIKey, opts ApiKeyManagerOptions) ApiKeyManagerResult {
k := core.Tokens(c)
d := core.Density(c)
var r ApiKeyManagerResult
r.Element = ui.Column(c).Gap(d.Unit() * 3).MinWidth(460).Disabled(opts.Disabled)
disabled := r.Element.IsDisabled()
if disabled {
s.creating = s.creating && (s.pending || s.err != "")
s.revoking, s.secret = "", ""
}
if s.index == nil || !slices.Equal(s.keys, keys) {
s.keys, s.index = slices.Clone(keys), map[string]APIKey{}
for _, key := range keys {
if key.ID == "" || key.Name == "" || key.Prefix == "" {
panic("mujica: APIKey needs an ID, a Name and a Prefix")
}
if _, ok := s.index[key.ID]; ok {
panic("mujica: APIKey IDs must be unique")
}
s.index[key.ID] = key
}
}
byID := s.index
r.Element.Children(func() {
ui.Row(c).Children(func() {
if input.Button(c, core.Msg(c, "api_key_manager.create"), input.ButtonOptions{Icon: icons.Must("plus"), Disabled: disabled}).Clicked() && !disabled {
s.creating, s.name, s.err = true, "", ""
}
})
if len(keys) == 0 {
feedback.EmptyState(c, feedback.EmptyStateOptions{Title: core.Msg(c, "api_key_manager.empty")})
}
accountRows(c, len(keys), func(i int) any { return keys[i].ID }, func(i int) {
key := keys[i]
ui.Row(c.Key(key.ID)).MinHeight(d.ControlHeight()+d.Unit()*4).Gap(d.Unit()*3).AlignItems(ui.Center).
BorderWidth(0, 0, theme.BorderWidth, 0).BorderColor(k.Border).Children(func() {
ui.Column(c).Grow(1).MinWidth(0).Gap(2).Children(func() {
ui.Text(c, key.Name).FontWeight(600).SingleLine()
ui.Text(c, apiKeyMasked(key.Prefix)).Font(theme.MonoFont).TextColor(k.TextMuted).SingleLine()
ui.Text(c, apiKeyDates(c, key)).TextColor(k.TextMuted).FontSize(core.FontSize(c, theme.CaptionSize))
})
b := input.Button(c, core.Msg(c, "api_key_manager.revoke"), input.ButtonOptions{Variant: input.Secondary, Disabled: disabled}).
Label(fmt.Sprintf(core.Msg(c, "api_key_manager.revoke_named"), key.Name))
if b.Clicked() && !disabled {
s.revoking = key.ID
}
})
})
})
if name, ok := apiKeyCreateDialog(c, s, disabled); ok && !disabled {
r.create = name
slog.Debug("mujica: api_key_manager create")
}
victim, exists := byID[s.revoking]
open := s.revoking != "" && exists
res := overlay.AlertDialog(c, &open, overlay.AlertDialogOptions{Title: fmt.Sprintf(core.Msg(c, "api_key_manager.revoke_title"), victim.Name),
Description: core.Msg(c, "api_key_manager.revoke_text"), ConfirmLabel: core.Msg(c, "api_key_manager.revoke")})
if res.Confirmed && !disabled {
r.revoke = victim.ID
slog.Debug("mujica: api_key_manager revoke")
}
if !open {
s.revoking = ""
}
return r
}
// apiKeyMasked shows the prefix and hides the rest.
func apiKeyMasked(prefix string) string { return prefix + strings.Repeat("•", 12) }
// apiKeyDates says when a key was made and last used.
func apiKeyDates(c *ui.Context, key APIKey) string {
used := core.Msg(c, "api_key_manager.never")
if !key.LastUsed.IsZero() {
used = key.LastUsed.Format("2006-01-02")
}
return fmt.Sprintf(core.Msg(c, "api_key_manager.dates"), key.Created.Format("2006-01-02"), used)
}
// apiKeyCreateDialog collects a name and reveals the generated secret.
func apiKeyCreateDialog(c *ui.Context, s *ApiKeyManagerState, disabled bool) (string, bool) {
k := core.Tokens(c)
d := core.Density(c)
open := !disabled && (s.creating || s.pending || s.secret != "")
was := open
asked := false
title := core.Msg(c, "api_key_manager.create_title")
if s.secret != "" {
title = core.Msg(c, "api_key_manager.secret_title")
}
overlay.Dialog(c, &open, overlay.DialogOptions{Title: title, KeepOnBackdrop: true, KeepOnEscape: s.pending, HideClose: s.pending, Actions: func() {
switch {
case s.secret != "":
if input.Button(c, core.Msg(c, "api_key_manager.done"), input.ButtonOptions{}).Clicked() {
open = false
}
default:
if input.Button(c, core.Msg(c, "api_key_manager.cancel"), input.ButtonOptions{Variant: input.Secondary, Disabled: s.pending}).Clicked() {
open = false
}
ok := strings.TrimSpace(s.name) != ""
if input.Button(c, core.Msg(c, "api_key_manager.create"), input.ButtonOptions{Loading: s.pending, Disabled: !ok}).Clicked() && ok && !s.pending {
asked = true
}
}
}}, func() {
if s.secret != "" {
ui.Row(c).Gap(d.Unit() * 2).AlignItems(ui.Start).Padding(d.Unit() * 3).Radius(theme.ControlRadius).Background(k.Warning.Alpha(0.12)).Children(func() {
ui.Icon(c, icons.Must("triangle-alert")).Size(16, 16).TextColor(k.Warning).Shrink(0)
ui.Text(c, core.Msg(c, "api_key_manager.once")).Grow(1)
})
ui.Row(c).Gap(d.Unit()*2).AlignItems(ui.Center).Padding(d.Unit()*2, d.Unit()*3).Radius(theme.ControlRadius).
Background(k.Background).Border(theme.BorderWidth, k.Border).Children(func() {
ui.Text(c, s.secret).Font(theme.MonoFont).Selectable().Label(core.Msg(c, "api_key_manager.secret_title")).Grow(1).MinWidth(0)
if core.IconAction(c, icons.Must("copy"), core.Msg(c, "api_key_manager.copy")).Clicked() {
c.WriteClipboard(s.secret)
c.Announce(core.Msg(c, "api_key_manager.copied"))
}
})
return
}
res := input.TextInput(c, &s.name, input.TextInputOptions{Label: core.Msg(c, "api_key_manager.name"), Placeholder: core.Msg(c, "api_key_manager.name"),
Disabled: s.pending, Error: s.err})
core.AutoFocus(c, res.Input)
if res.Submitted() && strings.TrimSpace(s.name) != "" && !s.pending {
asked = true
}
})
if was && !open && !s.pending {
// Dismissing wipes the one-time secret.
s.creating, s.secret, s.name = false, "", ""
}
if asked {
s.pending, s.err = true, ""
return strings.TrimSpace(s.name), true
}
return "", false
}