| 1 | package web |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "html/template" |
| 7 | "net/http" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/base/internal/catalog" |
| 12 | "amadan.net/oficina/base/internal/store" |
| 13 | ) |
| 14 | |
| 15 | type step struct { |
| 16 | Label string |
| 17 | Done bool |
| 18 | } |
| 19 | |
| 20 | // tile is one app on Home: in the grid of apps or in the dock of Base's own |
| 21 | // pages. Its icon is an image (Image, a path in the static tree for |
| 22 | // {{asset}}: an Oficina app's) or line art on a tint (Icon and Tint). |
| 23 | type tile struct { |
| 24 | // ID names the app: an Oficina app's id, a catalogue app's, or the |
| 25 | // page's ("store", "people", "settings"). Kind is "oficina", |
| 26 | // "installed" or "own". |
| 27 | ID, Kind string |
| 28 | Name string |
| 29 | Href string |
| 30 | Tint string |
| 31 | Icon template.HTML |
| 32 | Image string |
| 33 | NewTab bool // an Oficina app, which opens in a new tab |
| 34 | Own bool // one of Base's own pages, which start a row of their own |
| 35 | } |
| 36 | |
| 37 | type homePage struct { |
| 38 | Name string // the Base's name |
| 39 | Greeting string |
| 40 | Initials string |
| 41 | Lede string |
| 42 | Steps []step |
| 43 | // Apps are the team's Oficina apps (BASE_OFICINA_APPS) in their order, |
| 44 | // then the installed Store apps; Dock is Base's own pages. Home draws |
| 45 | // them all, in that order, in one grid. |
| 46 | Apps, Dock []tile |
| 47 | // Ask is the chat docked beside Home, nil with the chat off. AskOpen |
| 48 | // draws it open on a wide screen: the member has not closed it. |
| 49 | Ask *askView |
| 50 | AskOpen bool |
| 51 | } |
| 52 | |
| 53 | // Icons for the built-in tiles, in the mockup's style. |
| 54 | const ( |
| 55 | iconStore = `<svg viewBox="0 0 24 24"><path d="M5 8h14l-1.2 11.2a1 1 0 0 1-1 .8H7.2a1 1 0 0 1-1-.8z"/><path d="M9 8V6.5a3 3 0 0 1 6 0V8"/></svg>` |
| 56 | iconPeople = `<svg viewBox="0 0 24 24"><circle cx="9" cy="9" r="3"/><circle cx="16.5" cy="10.5" r="2.5"/><path d="M3.5 19a5.5 5.5 0 0 1 11 0M14 19a4 4 0 0 1 7 0"/></svg>` |
| 57 | iconSettings = `<svg viewBox="0 0 24 24"><path d="M4 7h10M18 7h2M4 17h2M10 17h10"/><circle cx="16" cy="7" r="2"/><circle cx="8" cy="17" r="2"/></svg>` |
| 58 | ) |
| 59 | |
| 60 | // displayName is the member's name, or the capitalised local part of their |
| 61 | // email address. |
| 62 | func displayName(m store.Member) string { |
| 63 | if m.Name != "" { |
| 64 | return m.Name |
| 65 | } |
| 66 | local, _, _ := strings.Cut(m.Email, "@") |
| 67 | if local == "" { |
| 68 | return m.Email |
| 69 | } |
| 70 | r := []rune(local) |
| 71 | return strings.ToUpper(string(r[:1])) + string(r[1:]) |
| 72 | } |
| 73 | |
| 74 | func initials(name string) string { |
| 75 | f := strings.Fields(name) |
| 76 | switch len(f) { |
| 77 | case 0: |
| 78 | return "" |
| 79 | case 1: |
| 80 | return strings.ToUpper(string([]rune(f[0])[:1])) |
| 81 | } |
| 82 | return strings.ToUpper(string([]rune(f[0])[:1]) + string([]rune(f[len(f)-1])[:1])) |
| 83 | } |
| 84 | |
| 85 | // greeting says hello by the time of day where the Base is. Between midnight |
| 86 | // and 05:00 "good morning" and "good evening" are both wrong, so it says a |
| 87 | // plain hello. A nil loc uses t as given. |
| 88 | func greeting(t time.Time, loc *time.Location, name string) string { |
| 89 | if loc != nil { |
| 90 | t = t.In(loc) |
| 91 | } |
| 92 | switch h := t.Hour(); { |
| 93 | case h < 5: |
| 94 | return "Hello, " + name + "." |
| 95 | case h < 12: |
| 96 | return "Good morning, " + name + "." |
| 97 | case h < 18: |
| 98 | return "Good afternoon, " + name + "." |
| 99 | } |
| 100 | return "Good evening, " + name + "." |
| 101 | } |
| 102 | |
| 103 | // homeSnapshotWait is how long Home waits for Equipo to read the docked |
| 104 | // chat's conversation before it draws the chat without it. |
| 105 | const homeSnapshotWait = 1500 * time.Millisecond |
| 106 | |
| 107 | // homeContent is what Home shows a member, apart from the chat: the |
| 108 | // greeting, the steps and every app in Home's order. Home and GET |
| 109 | // /api/v1/home both draw from it, so they cannot disagree. |
| 110 | type homeContent struct { |
| 111 | Greeting, Initials, Lede string |
| 112 | Steps []step |
| 113 | Apps, Dock []tile |
| 114 | } |
| 115 | |
| 116 | func (a *app) homeContent(ctx context.Context, me store.Member) (homeContent, error) { |
| 117 | ms, err := store.Members(ctx, a.db) |
| 118 | if err != nil { |
| 119 | return homeContent{}, fmt.Errorf("members: %w", err) |
| 120 | } |
| 121 | installs, err := store.Installs(ctx, a.db) |
| 122 | if err != nil { |
| 123 | return homeContent{}, fmt.Errorf("installs: %w", err) |
| 124 | } |
| 125 | name := displayName(me) |
| 126 | c := homeContent{Greeting: greeting(time.Now(), a.cfg.Timezone, name), Initials: initials(name)} |
| 127 | c.Dock = []tile{ |
| 128 | {ID: "store", Kind: "own", Name: "Store", Href: "/store", Tint: "teal", Icon: iconStore, Own: true}, |
| 129 | {ID: "people", Kind: "own", Name: "People", Href: "/people", Tint: "violet", Icon: iconPeople, Own: true}, |
| 130 | {ID: "settings", Kind: "own", Name: "Settings", Href: "/settings", Tint: "mist", Icon: iconSettings, Own: true}, |
| 131 | } |
| 132 | for _, o := range a.cfg.OficinaApps { |
| 133 | c.Apps = append(c.Apps, tile{ID: o.ID, Kind: "oficina", Name: o.Name, Href: o.Origin + "/", Image: "static/oficina/" + o.ID + ".svg", NewTab: true}) |
| 134 | } |
| 135 | installedOwn := false |
| 136 | for _, in := range installs { |
| 137 | e, ok := catalog.Get(in.App) |
| 138 | if !ok || in.State != "installed" { |
| 139 | continue |
| 140 | } |
| 141 | // The host the app was installed at: config may name another now. |
| 142 | c.Apps = append(c.Apps, tile{ID: in.App, Kind: "installed", Name: e.Name, Href: "https://" + in.Host + "/", Tint: e.Tint, Icon: template.HTML(e.Icon)}) |
| 143 | if !e.Default { |
| 144 | installedOwn = true |
| 145 | } |
| 146 | } |
| 147 | c.Steps = []step{ |
| 148 | {"Signed in", true}, |
| 149 | {"Invite your team", len(ms) > 1}, |
| 150 | {"Install your first app", installedOwn}, |
| 151 | } |
| 152 | left := 0 |
| 153 | for _, s := range c.Steps { |
| 154 | if !s.Done { |
| 155 | left++ |
| 156 | } |
| 157 | } |
| 158 | switch left { |
| 159 | case 0: |
| 160 | c.Lede = a.cfg.Name + "'s Base is ready." |
| 161 | case 1: |
| 162 | c.Lede = "One step until " + a.cfg.Name + "'s Base is ready." |
| 163 | default: |
| 164 | c.Lede = "Two steps until " + a.cfg.Name + "'s Base is ready." |
| 165 | } |
| 166 | return c, nil |
| 167 | } |
| 168 | |
| 169 | func (a *app) home(w http.ResponseWriter, r *http.Request) { |
| 170 | me, _ := CurrentMember(r) |
| 171 | c, err := a.homeContent(r.Context(), me) |
| 172 | if err != nil { |
| 173 | a.logger.Error("home", "err", err) |
| 174 | http.Error(w, "unavailable", http.StatusServiceUnavailable) |
| 175 | return |
| 176 | } |
| 177 | page := homePage{Name: a.cfg.Name, Greeting: c.Greeting, Initials: c.Initials, Lede: c.Lede, Steps: c.Steps, Apps: c.Apps, Dock: c.Dock} |
| 178 | if a.ask != nil { |
| 179 | l, sec, state, err := a.askLink(r.Context(), me) |
| 180 | if err != nil { |
| 181 | a.logger.Error("home: equipo link", "err", err) |
| 182 | state = askUnavailable |
| 183 | } |
| 184 | v := a.newAskView(homeState(state), "home") |
| 185 | v.Notice = takeOficinaNote(w, r) |
| 186 | // Docked open, the conversation is on Home from the first paint, |
| 187 | // if Equipo answers in time: Home never waits on it. Closed, it |
| 188 | // is not read; the panel's script reads it when the chat opens. |
| 189 | switch { |
| 190 | case state != askReady: |
| 191 | case me.AskClosed: |
| 192 | v.Unread = true |
| 193 | default: |
| 194 | a.askSnapshotWithin(r, me, l, sec.Token, &v, homeSnapshotWait) |
| 195 | } |
| 196 | page.Ask, page.AskOpen = &v, !me.AskClosed |
| 197 | } |
| 198 | w.Header().Set("Cache-Control", "no-store") |
| 199 | a.render(w, r, "home", page) |
| 200 | } |
| 201 | |