package web import ( "context" "fmt" "html/template" "net/http" "strings" "time" "amadan.net/oficina/base/internal/catalog" "amadan.net/oficina/base/internal/store" ) type step struct { Label string Done bool } // tile is one app on Home: in the grid of apps or in the dock of Base's own // pages. Its icon is an image (Image, a path in the static tree for // {{asset}}: an Oficina app's) or line art on a tint (Icon and Tint). type tile struct { // ID names the app: an Oficina app's id, a catalogue app's, or the // page's ("store", "people", "settings"). Kind is "oficina", // "installed" or "own". ID, Kind string Name string Href string Tint string Icon template.HTML Image string NewTab bool // an Oficina app, which opens in a new tab Own bool // one of Base's own pages, which start a row of their own } type homePage struct { Name string // the Base's name Greeting string Initials string Lede string Steps []step // Apps are the team's Oficina apps (BASE_OFICINA_APPS) in their order, // then the installed Store apps; Dock is Base's own pages. Home draws // them all, in that order, in one grid. Apps, Dock []tile // Ask is the chat docked beside Home, nil with the chat off. AskOpen // draws it open on a wide screen: the member has not closed it. Ask *askView AskOpen bool } // Icons for the built-in tiles, in the mockup's style. const ( iconStore = `` iconPeople = `` iconSettings = `` ) // displayName is the member's name, or the capitalised local part of their // email address. func displayName(m store.Member) string { if m.Name != "" { return m.Name } local, _, _ := strings.Cut(m.Email, "@") if local == "" { return m.Email } r := []rune(local) return strings.ToUpper(string(r[:1])) + string(r[1:]) } func initials(name string) string { f := strings.Fields(name) switch len(f) { case 0: return "" case 1: return strings.ToUpper(string([]rune(f[0])[:1])) } return strings.ToUpper(string([]rune(f[0])[:1]) + string([]rune(f[len(f)-1])[:1])) } // greeting says hello by the time of day where the Base is. Between midnight // and 05:00 "good morning" and "good evening" are both wrong, so it says a // plain hello. A nil loc uses t as given. func greeting(t time.Time, loc *time.Location, name string) string { if loc != nil { t = t.In(loc) } switch h := t.Hour(); { case h < 5: return "Hello, " + name + "." case h < 12: return "Good morning, " + name + "." case h < 18: return "Good afternoon, " + name + "." } return "Good evening, " + name + "." } // homeSnapshotWait is how long Home waits for Equipo to read the docked // chat's conversation before it draws the chat without it. const homeSnapshotWait = 1500 * time.Millisecond // homeContent is what Home shows a member, apart from the chat: the // greeting, the steps and every app in Home's order. Home and GET // /api/v1/home both draw from it, so they cannot disagree. type homeContent struct { Greeting, Initials, Lede string Steps []step Apps, Dock []tile } func (a *app) homeContent(ctx context.Context, me store.Member) (homeContent, error) { ms, err := store.Members(ctx, a.db) if err != nil { return homeContent{}, fmt.Errorf("members: %w", err) } installs, err := store.Installs(ctx, a.db) if err != nil { return homeContent{}, fmt.Errorf("installs: %w", err) } name := displayName(me) c := homeContent{Greeting: greeting(time.Now(), a.cfg.Timezone, name), Initials: initials(name)} c.Dock = []tile{ {ID: "store", Kind: "own", Name: "Store", Href: "/store", Tint: "teal", Icon: iconStore, Own: true}, {ID: "people", Kind: "own", Name: "People", Href: "/people", Tint: "violet", Icon: iconPeople, Own: true}, {ID: "settings", Kind: "own", Name: "Settings", Href: "/settings", Tint: "mist", Icon: iconSettings, Own: true}, } for _, o := range a.cfg.OficinaApps { 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}) } installedOwn := false for _, in := range installs { e, ok := catalog.Get(in.App) if !ok || in.State != "installed" { continue } // The host the app was installed at: config may name another now. 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)}) if !e.Default { installedOwn = true } } c.Steps = []step{ {"Signed in", true}, {"Invite your team", len(ms) > 1}, {"Install your first app", installedOwn}, } left := 0 for _, s := range c.Steps { if !s.Done { left++ } } switch left { case 0: c.Lede = a.cfg.Name + "'s Base is ready." case 1: c.Lede = "One step until " + a.cfg.Name + "'s Base is ready." default: c.Lede = "Two steps until " + a.cfg.Name + "'s Base is ready." } return c, nil } func (a *app) home(w http.ResponseWriter, r *http.Request) { me, _ := CurrentMember(r) c, err := a.homeContent(r.Context(), me) if err != nil { a.logger.Error("home", "err", err) http.Error(w, "unavailable", http.StatusServiceUnavailable) return } page := homePage{Name: a.cfg.Name, Greeting: c.Greeting, Initials: c.Initials, Lede: c.Lede, Steps: c.Steps, Apps: c.Apps, Dock: c.Dock} if a.ask != nil { l, sec, state, err := a.askLink(r.Context(), me) if err != nil { a.logger.Error("home: equipo link", "err", err) state = askUnavailable } v := a.newAskView(homeState(state), "home") v.Notice = takeOficinaNote(w, r) // Docked open, the conversation is on Home from the first paint, // if Equipo answers in time: Home never waits on it. Closed, it // is not read; the panel's script reads it when the chat opens. switch { case state != askReady: case me.AskClosed: v.Unread = true default: a.askSnapshotWithin(r, me, l, sec.Token, &v, homeSnapshotWait) } page.Ask, page.AskOpen = &v, !me.AskClosed } w.Header().Set("Cache-Control", "no-store") a.render(w, r, "home", page) }