| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | "net/http" |
| 9 | "net/url" |
| 10 | "strings" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | // Settings (Paul, 2026-11-09: "settings could probably get a submenu on |
| 15 | // mobile and its own menu sidebar on desktop"): a section per URL under |
| 16 | // /conversation/settings, with a secondary menu of the sections. On a |
| 17 | // desktop the menu stands beside the section, the app's rail stays; on a |
| 18 | // phone /conversation/settings is the menu as full-row drill-ins and each |
| 19 | // section is its own page with a way back to Settings. Neither Rastrillo |
| 20 | // v0.30.1 nor Fiesta has a secondary navigation or settings layout, so |
| 21 | // it is Equipo's own (settings_nav.html, equipo.css). |
| 22 | // |
| 23 | // Usage is not a section yet: there is no usage view to put there. |
| 24 | |
| 25 | // Paul, 2026-11-10: "The settings nav should probably replace the main |
| 26 | // nav on this view, right?" On a desktop the rail itself becomes the |
| 27 | // Settings nav ("‹ Equipo", then the sections), so Settings is two |
| 28 | // columns; the phone keeps its drill-in. The swap is the same rail with a |
| 29 | // calm slide of its nav, only when the person crosses into or out of |
| 30 | // Settings (the Referer says from where), never between sections. |
| 31 | func crossedSettings(r *http.Request) bool { |
| 32 | from, err := url.Parse(r.Referer()) |
| 33 | if r.Referer() == "" || err != nil || from.Host != r.Host { |
| 34 | return false |
| 35 | } |
| 36 | in := func(p string) bool { |
| 37 | return p == "/conversation/settings" || strings.HasPrefix(p, "/conversation/settings/") || p == "/teams" |
| 38 | } |
| 39 | return in(from.Path) != in(r.URL.Path) |
| 40 | } |
| 41 | |
| 42 | // settingsLink is one entry of the Settings menu. |
| 43 | type settingsLink struct { |
| 44 | Key, Href, CopyKey, Icon string |
| 45 | Current bool |
| 46 | } |
| 47 | |
| 48 | var settingsSections = []settingsLink{ |
| 49 | {Key: "github", Href: "/conversation/settings/github", CopyKey: "github.title", Icon: "plug"}, |
| 50 | {Key: "autonomy", Href: "/conversation/settings/autonomy", CopyKey: "autonomy.title", Icon: "list-checks"}, |
| 51 | {Key: "oficina-id", Href: "/conversation/settings/oficina-id", CopyKey: "id.title", Icon: "users"}, |
| 52 | {Key: "thinking", Href: "/conversation/settings/thinking", CopyKey: "thinking.title", Icon: "type"}, |
| 53 | {Key: "connections", Href: "/conversation/settings/connections", CopyKey: "settings.connections", Icon: "plug"}, |
| 54 | {Key: "machines", Href: "/conversation/settings/machines", CopyKey: "runner.title", Icon: "monitor"}, |
| 55 | {Key: "housekeeping", Href: "/conversation/settings/housekeeping", CopyKey: "settings.housekeeping", Icon: "list-checks"}, |
| 56 | {Key: "copy", Href: "/conversation/settings/copy", CopyKey: "copy.title", Icon: "type"}, |
| 57 | {Key: "places", Href: "/conversation/settings/places", CopyKey: "settings.places", Icon: "list-checks"}, |
| 58 | {Key: "notebook", Href: "/conversation/settings/notebook", CopyKey: "settings.notebook_open", Icon: "notebook"}, |
| 59 | {Key: "teams", Href: "/teams", CopyKey: "settings.teams_open", Icon: "users"}, |
| 60 | } |
| 61 | |
| 62 | // settingsHubView is a Settings section: which one, whether it is the |
| 63 | // root (the menu on a phone, the first section on a desktop), and the |
| 64 | // section's own data. |
| 65 | type settingsHubView struct { |
| 66 | CopyProjects []wire.Project |
| 67 | // Places are the Places page's lines, state first (read-only). |
| 68 | Places []placeView |
| 69 | Section string |
| 70 | Root bool |
| 71 | Thinking thinkingView |
| 72 | } |
| 73 | |
| 74 | // settingsPage is a page in the Settings section: the app's rail shows |
| 75 | // Settings, the secondary menu shows section. |
| 76 | func (a *application) settingsPage(r *http.Request, section string, root bool) (pageData, error) { |
| 77 | data, err := a.page(r, "settings-hub") |
| 78 | if err != nil { |
| 79 | return data, err |
| 80 | } |
| 81 | for _, s := range settingsSections { |
| 82 | if s.Key == "github" && (!data.Owner || a.store.forge == nil || !a.store.forge.Enabled) { |
| 83 | continue |
| 84 | } |
| 85 | if s.Key == "autonomy" && !data.Owner { |
| 86 | continue |
| 87 | } |
| 88 | if s.Key == "oficina-id" && a.home == nil { |
| 89 | continue |
| 90 | } |
| 91 | s.Current = s.Key == section |
| 92 | data.SettingsNav = append(data.SettingsNav, s) |
| 93 | } |
| 94 | data.Settings.Section, data.Settings.Root = section, root |
| 95 | data.Title = "nav.settings" |
| 96 | return data, nil |
| 97 | } |
| 98 | |
| 99 | // settingsHub is GET /conversation/settings: the menu on a phone, the |
| 100 | // first section (Thinking words) beside it on a desktop. |
| 101 | func (a *application) settingsHub(w http.ResponseWriter, r *http.Request) { |
| 102 | a.settingsSection(w, r, "thinking", true) |
| 103 | } |
| 104 | |
| 105 | // settingsSectionHandler serves /conversation/settings/{thinking,housekeeping}. |
| 106 | func (a *application) settingsSectionHandler(section string) http.HandlerFunc { |
| 107 | return func(w http.ResponseWriter, r *http.Request) { a.settingsSection(w, r, section, false) } |
| 108 | } |
| 109 | |
| 110 | func (a *application) settingsSection(w http.ResponseWriter, r *http.Request, section string, root bool) { |
| 111 | w.Header().Set("Cache-Control", "no-store") |
| 112 | ctx, address, ok := sessionContext(r) |
| 113 | if !ok { |
| 114 | a.failure(w, r, ErrForbidden) |
| 115 | return |
| 116 | } |
| 117 | data, err := a.settingsPage(r, section, root) |
| 118 | if err != nil { |
| 119 | a.failure(w, r, err) |
| 120 | return |
| 121 | } |
| 122 | if section == "autonomy" && !data.Owner { |
| 123 | a.failure(w, r, ErrForbidden) |
| 124 | return |
| 125 | } |
| 126 | switch section { |
| 127 | case "copy", "autonomy": |
| 128 | projects, err := a.store.Chat().Projects(ctx, address) |
| 129 | if err != nil { |
| 130 | a.failure(w, r, chatError(err)) |
| 131 | return |
| 132 | } |
| 133 | data.Settings.CopyProjects = projects.Items |
| 134 | case "thinking": |
| 135 | words, err := a.store.Chat().Thinking(ctx, address) |
| 136 | if err != nil { |
| 137 | a.failure(w, r, chatError(err)) |
| 138 | return |
| 139 | } |
| 140 | data.Settings.Thinking = thinkingView{ThinkingWords: words, JSON: thinkingJSON(words.Sets)} |
| 141 | case "places": |
| 142 | places, err := a.store.Chat().Places(ctx, address) |
| 143 | if err != nil { |
| 144 | a.failure(w, r, chatError(err)) |
| 145 | return |
| 146 | } |
| 147 | data.Settings.Places = placeViews(places) |
| 148 | case "housekeeping": |
| 149 | h, err := a.store.Chat().Housekeeping(ctx, address) |
| 150 | if err != nil { |
| 151 | a.failure(w, r, chatError(err)) |
| 152 | return |
| 153 | } |
| 154 | data.HousekeepingSettings = housekeepingSettings(h) |
| 155 | } |
| 156 | render(w, "settings_hub", data) |
| 157 | } |
| 158 | |
| 159 | // movedTo answers an old Settings URL with its section's. |
| 160 | func movedTo(path string) http.HandlerFunc { |
| 161 | return func(w http.ResponseWriter, r *http.Request) { |
| 162 | http.Redirect(w, r, path, http.StatusSeeOther) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // agoText is a quiet relative time, "4 days ago", for a template. |
| 167 | func agoText(v any) string { |
| 168 | ms := sinceMillis(v) |
| 169 | if ms == 0 { |
| 170 | return "" |
| 171 | } |
| 172 | return ago(time.Now(), time.UnixMilli(ms)) |
| 173 | } |
| 174 | |
| 175 | // hostOf is an instance address as a person reads it: its host, without |
| 176 | // the scheme. |
| 177 | func hostOf(origin string) string { |
| 178 | if u, err := url.Parse(origin); err == nil && u.Host != "" { |
| 179 | return u.Host |
| 180 | } |
| 181 | return origin |
| 182 | } |
| 183 | |
| 184 | // deref is the number behind a known count. |
| 185 | func deref(n *int) int { |
| 186 | if n == nil { |
| 187 | return 0 |
| 188 | } |
| 189 | return *n |
| 190 | } |
| 191 | |
| 192 | // placeView is one project's lines on the Places page: what it is, then |
| 193 | // where it stands, then its deploy key. |
| 194 | type placeView struct { |
| 195 | Line, State, Deploy string |
| 196 | Lost []string |
| 197 | } |
| 198 | |
| 199 | // placeViews says each site in the page's words; names come from CARLOS |
| 200 | // and the repository and are escaped by the template. |
| 201 | func placeViews(places []chat.SitePlace) []placeView { |
| 202 | out := make([]placeView, 0, len(places)) |
| 203 | for _, p := range places { |
| 204 | var v placeView |
| 205 | switch { |
| 206 | case p.Console == "" && p.Pending: |
| 207 | v.Line = fmt.Sprintf(copyText("places.pending"), p.Project) |
| 208 | case p.Console == "": |
| 209 | v.Line = fmt.Sprintf(copyText("places.unplaced"), p.Project, p.App) |
| 210 | default: |
| 211 | v.Line = fmt.Sprintf(copyText("places.site_line"), p.Project, p.App, p.Console, p.Account) |
| 212 | when := "" |
| 213 | if p.At != nil { |
| 214 | when = p.At.UTC().Format("2 January 2006, 15:04 UTC") |
| 215 | } |
| 216 | switch { |
| 217 | case p.State == wire.PublishPublished && p.URL != "": |
| 218 | v.State = fmt.Sprintf(copyText("places.site_live"), p.URL, when) |
| 219 | case p.State == wire.PublishUnconfirmed: |
| 220 | v.State = fmt.Sprintf(copyText("places.site_unconfirmed"), when) |
| 221 | default: |
| 222 | v.State = copyText("places.site_not_yet") |
| 223 | } |
| 224 | switch { |
| 225 | case p.Deploy == "held": |
| 226 | v.Deploy = fmt.Sprintf(copyText("places.deploy_held"), p.DeployName) |
| 227 | case len(p.Lost) == 0: |
| 228 | v.Deploy = copyText("places.deploy_none") |
| 229 | } |
| 230 | for _, name := range p.Lost { |
| 231 | v.Lost = append(v.Lost, fmt.Sprintf(copyText("places.deploy_lost"), name)) |
| 232 | } |
| 233 | } |
| 234 | out = append(out, v) |
| 235 | } |
| 236 | return out |
| 237 | } |
| 238 | |