| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/chatmarkdown" |
| 5 | "bytes" |
| 6 | "embed" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "html" |
| 10 | "html/template" |
| 11 | "net/http" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "time" |
| 15 | |
| 16 | "amadan.net/rastrillo/rastrillo" |
| 17 | "amadan.net/rastrillo/rastrillo/ui" |
| 18 | |
| 19 | "amadan.net/oficina/equipo/internal/equipo/door" |
| 20 | "amadan.net/oficina/equipo/internal/equipo/icons" |
| 21 | "amadan.net/oficina/equipo/internal/equipo/library" |
| 22 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 23 | "amadan.net/oficina/estilo.git/chrome" |
| 24 | ) |
| 25 | |
| 26 | //go:embed templates static |
| 27 | var appFS embed.FS |
| 28 | |
| 29 | // assets fingerprints static/: {{asset "static/tokens.css"}} in a |
| 30 | // template returns a content-hashed URL, served immutable-cacheable by |
| 31 | // the handler App mounts. |
| 32 | var assets = rastrillo.NewAssets(appFS) |
| 33 | |
| 34 | // pages is one *template.Template per page, each combining layout.html |
| 35 | // with that page's own file — kept separate so every page can define a |
| 36 | // template named "content" without colliding with the others. |
| 37 | var pages = map[string]*template.Template{} |
| 38 | |
| 39 | // signinLook is the sign-in door's pages (package door, templates/door.html). |
| 40 | var signinLook = door.Look{Brand: copyBrand, Oficina: copyText("id.title"), Refusal: copyText("signin.refused"), Forget: setup.ForgetPath, |
| 41 | Render: func(w http.ResponseWriter, status int, p door.Page) { renderStatus(w, status, "door", p) }} |
| 42 | |
| 43 | // pasteBox is the paste box's words with its icons: Copy's, and the tick |
| 44 | // it shows once it has copied. |
| 45 | var pasteBox = func() chatmarkdown.Labels { |
| 46 | l := pasteLabels |
| 47 | l.CopyIcon, l.CopiedIcon = icons.Icon("copy"), icons.Icon("check") |
| 48 | return l |
| 49 | }() |
| 50 | |
| 51 | func init() { |
| 52 | // The framework's partials are parsed once and each page is a clone: |
| 53 | // parsing them for every page made this init the runner's start-up |
| 54 | // cost too (it re-runs as the namespace helper inside each sandbox). |
| 55 | base := template.Must(template.New("layout"). |
| 56 | Funcs(ui.Funcs(ui.WithIcons(icons.Icon, icons.Assets))). |
| 57 | Funcs(template.FuncMap{"chatMarkdown": pasteBox.Render, "chatDraft": pasteBox.RenderDraft, "fileSize": fileSize, "loadingLine": loadingLine, "split": strings.Split, "since": sinceMillis, "elapsed": elapsedText, "short": short, "agoData": agoData, "agoText": agoText, "hostOf": hostOf, "deref": deref, "agoFormats": agoFormats, "projectAllows": library.ProjectAllowed, "asset": assets.Path, "copy": copyText, "taskGroups": func() []string { return []string{"needs-person", "running", "queued", "finished"} }, |
| 58 | "kindKey": func(k ProjectKind) string { return "kind." + string(k) }, |
| 59 | "ownerKey": func(k string) string { return "owner." + k }, |
| 60 | "roleKey": func(k Role) string { return "role." + string(k) }, |
| 61 | }).ParseFS(ui.Templates(), "*.html")) |
| 62 | layout := template.Must(template.Must(base.Clone()).ParseFS(appFS, "templates/layout.html")) |
| 63 | shell := template.Must(template.Must(layout.Clone()).ParseFS(appFS, "templates/chrome.html", "templates/forms.html", "templates/task_card.html", "templates/conversation_card.html", "templates/work_parts.html", "templates/housekeeping_settings.html", "templates/autonomy_settings.html", "templates/settings_nav.html", "templates/merge_policy.html", "templates/placement_chip.html", "templates/access_card.html")) |
| 64 | for _, name := range []string{"github", "placement", "oficina_id", "id_public", "person_devices", "person_device", "teams", "onboarding", "invite", "playbooks", "playbook_form", "playbook_diff", "specialist_form", "template_library", "template_diff", "conversation", "chat", "jobs", "thread", "plan", "coordinator", "preferences", "index", "empty", "projects", "project", "project_form", "people", "settings", "specialist", "errors", "door", "form_error", "runners", "tasks", "task", "task_form", "question", "merge", "connections", "recovery", "recovery_form", "rendered_approval", "notebook", "housekeeping", "work_tasks", "work_task", "work_projects", "work_project", "models", "work_needs", "settings_hub", "team", "usage"} { |
| 65 | var page *template.Template |
| 66 | switch name { |
| 67 | case "id_public": |
| 68 | stage, ok := ui.Layout("stage") |
| 69 | if !ok { |
| 70 | panic(copyMissingStage) |
| 71 | } |
| 72 | page = template.Must(template.Must(base.Clone()).Parse(string(stage))) |
| 73 | case "door": // its own document, around estilo's door |
| 74 | page = template.Must(template.Must(base.Clone()).ParseFS(chrome.Templates(), "templates/*.html")) |
| 75 | case "errors": |
| 76 | page = template.Must(layout.Clone()) |
| 77 | default: |
| 78 | page = template.Must(shell.Clone()) |
| 79 | } |
| 80 | if name == "oficina_id" || name == "id_public" { |
| 81 | page = template.Must(page.ParseFS(appFS, "templates/id_body.html")) |
| 82 | } |
| 83 | pages[name] = template.Must(page.ParseFS(appFS, "templates/"+name+".html")) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // render executes name's layout into a buffer before anything touches |
| 88 | // the wire: a template error becomes a clean 500 instead of garbage |
| 89 | // appended to a half-written page, and headers (flash cookies, say) |
| 90 | // stay settable until the status line is written. |
| 91 | func render(w http.ResponseWriter, name string, data any) { |
| 92 | renderStatus(w, http.StatusOK, name, data) |
| 93 | } |
| 94 | |
| 95 | func renderStatus(w http.ResponseWriter, status int, name string, data any) { |
| 96 | var buf bytes.Buffer |
| 97 | if err := pages[name].ExecuteTemplate(&buf, "layout", data); err != nil { |
| 98 | http.Error(w, copyRenderFailure, http.StatusInternalServerError) |
| 99 | return |
| 100 | } |
| 101 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 102 | w.WriteHeader(status) |
| 103 | buf.WriteTo(w) |
| 104 | } |
| 105 | |
| 106 | // ErrorPage renders the "errors" page — templates/errors.html, which |
| 107 | // wraps ui's error-page partial in the layout — for status inside ref |
| 108 | // (rastrillo.NewRef, or empty when there is none to show). It is the |
| 109 | // rastrillo.ErrorPageFunc cmd/equipo/main.go points Options.ErrorPage at, |
| 110 | // so a panic gets the same page a handled failure would; wire it to |
| 111 | // Ctx.ErrorPage too as the app grows handlers of its own. |
| 112 | func ErrorPage(w http.ResponseWriter, r *http.Request, status int, ref string) { |
| 113 | renderStatus(w, status, "errors", map[string]any{"Status": status, "Ref": ref}) |
| 114 | } |
| 115 | |
| 116 | // sinceMillis is a start time for the client's elapsed clock. |
| 117 | func sinceMillis(v any) int64 { |
| 118 | switch t := v.(type) { |
| 119 | case time.Time: |
| 120 | return t.UnixMilli() |
| 121 | case *time.Time: |
| 122 | if t != nil { |
| 123 | return t.UnixMilli() |
| 124 | } |
| 125 | } |
| 126 | return 0 |
| 127 | } |
| 128 | |
| 129 | // elapsedText is the clock's first reading, before script ticks it: |
| 130 | // "4:12", or "1:04:12" past an hour. |
| 131 | func elapsedText(v any) string { |
| 132 | ms := sinceMillis(v) |
| 133 | if ms == 0 { |
| 134 | return "" |
| 135 | } |
| 136 | s := int(time.Since(time.UnixMilli(ms)) / time.Second) |
| 137 | s = max(s, 0) |
| 138 | if m := s / 60; m >= 60 { |
| 139 | return fmt.Sprintf("%d:%02d:%02d", m/60, m%60, s%60) |
| 140 | } |
| 141 | return fmt.Sprintf("%d:%02d", s/60, s%60) |
| 142 | } |
| 143 | |
| 144 | // agoData marks a relative time for nav-stream.js to keep current: its |
| 145 | // instant and, when the words wrap it ("Changed %s"), the wrapping. |
| 146 | func agoData(v any, key string) template.HTMLAttr { |
| 147 | format := "{t}" |
| 148 | if key != "" { |
| 149 | format = strings.Replace(copyText(key), "%s", "{t}", 1) |
| 150 | } |
| 151 | return template.HTMLAttr(fmt.Sprintf(` data-at="%d" data-format="%s"`, sinceMillis(v), html.EscapeString(format))) |
| 152 | } |
| 153 | |
| 154 | // agoFormats are the relative time's words for the client, with {n} for |
| 155 | // the number. |
| 156 | func agoFormats() string { |
| 157 | out := map[string]string{} |
| 158 | for _, k := range []string{"now", "minutes", "hours", "yesterday", "days"} { |
| 159 | out[k] = strings.Replace(copyText("work.ago."+k), "%d", "{n}", 1) |
| 160 | } |
| 161 | raw, _ := json.Marshal(out) |
| 162 | return string(raw) |
| 163 | } |
| 164 | |
| 165 | // fileSize is a file's size in a few characters: "840 B", "12 KB", "3.4 MB". |
| 166 | func fileSize(n int64) string { |
| 167 | switch { |
| 168 | case n < 1<<10: |
| 169 | return strconv.FormatInt(n, 10) + " B" |
| 170 | case n < 1<<20: |
| 171 | return strconv.FormatInt((n+512)>>10, 10) + " KB" |
| 172 | } |
| 173 | return strconv.FormatFloat(float64(n)/(1<<20), 'f', 1, 64) + " MB" |
| 174 | } |
| 175 | |