| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "amadan.net/oficina/estilo.git/chrome" |
| 7 | "amadan.net/oficina/estilo.git/menu" |
| 8 | ) |
| 9 | |
| 10 | // NavItem is one entry in the rail. |
| 11 | type NavItem struct { |
| 12 | Label, Href, Icon string |
| 13 | Current bool |
| 14 | // Holds marks the place the page on screen belongs to when the |
| 15 | // page has a sidebar row of its own (side.go's light): the open |
| 16 | // sidebar lights the row, and the rail, which hides it, the place. |
| 17 | Holds bool |
| 18 | } |
| 19 | |
| 20 | // nav is the sidebar's places, the same on every page (level-up §3.2). |
| 21 | func nav(current string) []NavItem { |
| 22 | items := []NavItem{ |
| 23 | {Label: "Today", Href: "/", Icon: "calendar"}, |
| 24 | {Label: "Search", Href: "/search", Icon: "search"}, |
| 25 | {Label: "All pages", Href: "/pages", Icon: "files"}, |
| 26 | {Label: "Scratch", Href: "/scratch", Icon: "file"}, |
| 27 | } |
| 28 | for index := range items { |
| 29 | items[index].Current = items[index].Href == current |
| 30 | } |
| 31 | return items |
| 32 | } |
| 33 | |
| 34 | // view is what every shell page is drawn from. A handler fills Title, |
| 35 | // Nav's current place and its own Data; page fills the rest. |
| 36 | type view struct { |
| 37 | Title string |
| 38 | Nav []NavItem |
| 39 | Account *chrome.Account |
| 40 | Theme string |
| 41 | Viewer Viewer |
| 42 | Data any |
| 43 | // Chrome is what the handler says about its screen (frame.go); nil |
| 44 | // for a list or form screen. |
| 45 | Chrome *pageChrome |
| 46 | // Frame is the bar and the sidebar, built by show. |
| 47 | Frame frame |
| 48 | // Return is this page's own path, for the view forms to come back |
| 49 | // to. |
| 50 | Return string |
| 51 | name string |
| 52 | } |
| 53 | |
| 54 | // show renders a shell page for the signed-in viewer. |
| 55 | func (app *app) show(writer http.ResponseWriter, request *http.Request, |
| 56 | status int, name string, page view) { |
| 57 | address, _ := app.identity(request) |
| 58 | page.Account = app.account(request, address) |
| 59 | page.Theme = themeAttr(request) |
| 60 | page.name, page.Return = name, returnTarget(request) |
| 61 | if panel := historyIn(request); panel != nil { |
| 62 | page.Frame.History = panel |
| 63 | } |
| 64 | if err := app.buildFrame(request, &page); err != nil { |
| 65 | app.fail(writer, request, err) |
| 66 | return |
| 67 | } |
| 68 | writer.Header().Set("Cache-Control", "no-store") |
| 69 | speculate(writer) |
| 70 | if status != http.StatusOK { |
| 71 | writer.WriteHeader(status) |
| 72 | } |
| 73 | render(writer, request, name, page) |
| 74 | } |
| 75 | |
| 76 | // account is the suite's account menu at the bar's end (estilo chrome): |
| 77 | // who is signed in, "All apps" with a hub directory, Appearance, Sign out. |
| 78 | // Import from Notion is File's alone (level-up build note 87). |
| 79 | func (app *app) account(request *http.Request, |
| 80 | address string) *chrome.Account { |
| 81 | account := &chrome.Account{ |
| 82 | TriggerLabel: "Account: " + address, |
| 83 | Initials: chrome.Initials("", address), |
| 84 | Address: address, |
| 85 | Labels: chrome.AccountLabels{SignedInAs: "Signed in as", |
| 86 | Appearance: "Appearance", Help: "Help"}, |
| 87 | Appearance: &chrome.AppearanceMenu{Action: "/appearance", |
| 88 | Return: returnTarget(request), |
| 89 | Options: chrome.AppearanceOptions(chrome.Appearance(request), |
| 90 | "Light", "Dark", "System")}, |
| 91 | SignOut: chrome.SignOut{Action: "/signout", Label: "Sign out"}, |
| 92 | } |
| 93 | account.Items = []menu.Item{{Href: "/settings", Label: "Settings"}, |
| 94 | {Href: "/deleted", Label: "Deleted pages"}} |
| 95 | if app.config.Directory != "" { |
| 96 | account.Items = append(account.Items, menu.Item{ |
| 97 | Href: app.config.Directory + "/", Label: "All apps"}) |
| 98 | } |
| 99 | return account |
| 100 | } |
| 101 | |
| 102 | // returnTarget is where Appearance brings a person back to: this page if |
| 103 | // it is one, else Today. |
| 104 | func returnTarget(request *http.Request) string { |
| 105 | if request.Method != http.MethodGet && request.Method != http.MethodHead { |
| 106 | return "/" |
| 107 | } |
| 108 | return request.URL.RequestURI() |
| 109 | } |
| 110 | |
| 111 | // fail is a 500 with the error logged and nothing about it shown. |
| 112 | func (app *app) fail(writer http.ResponseWriter, request *http.Request, |
| 113 | err error) { |
| 114 | app.logger.Error("request failed", "path", request.URL.Path, "err", err) |
| 115 | ErrorPage(writer, request, http.StatusInternalServerError, "") |
| 116 | } |
| 117 | |