| 1 | package library |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 6 | "context" |
| 7 | ) |
| 8 | |
| 9 | type NavItem struct{ Key, Group, Href, CopyKey, Icon string } |
| 10 | |
| 11 | func Navigation() []NavItem { |
| 12 | return []NavItem{ |
| 13 | {"teams", "", "/teams", "teams.title", "users"}, |
| 14 | {"conversation", "", "/conversation", "nav.conversation", "message-circle"}, |
| 15 | {"tasks", "", "/tasks", "nav.tasks", "check-circle"}, |
| 16 | {"needs-you", "", "/needs-you", "nav.needs-you", "inbox"}, |
| 17 | {"journey", "", "/journey", "nav.journey", "route"}, |
| 18 | {"projects", "", "/projects", "nav.projects", "folder"}, |
| 19 | {"product", "specialists", "/specialists/product", "nav.product", "lightbulb"}, |
| 20 | {"design", "specialists", "/specialists/design", "nav.design", "pencil"}, |
| 21 | {"copy", "specialists", "/specialists/copy", "nav.copy", "type"}, |
| 22 | {"engineering", "specialists", "/specialists/engineering", "nav.engineering", "code"}, |
| 23 | {"review", "specialists", "/specialists/review", "nav.review", "check-circle"}, |
| 24 | {"playbooks", "team", "/playbooks", "playbooks.title", "route"}, |
| 25 | {"templates", "team", "/templates", "templates.title", "plus"}, |
| 26 | {"preferences", "team", "/preferences", "preferences.title", "sliders"}, |
| 27 | {"coordinator", "team", "/settings/coordinator", "coordinator.settings", "message-circle"}, |
| 28 | {"connections", "team", "/connections", "nav.connections", "plug"}, |
| 29 | {"people", "team", "/people", "nav.people", "users"}, |
| 30 | {"settings", "team", "/settings", "nav.settings", "sliders"}, |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func WithSpecialists(rows []Specialist) ([]NavItem, map[string]string) { |
| 35 | nav := []NavItem{} |
| 36 | active := map[string]bool{} |
| 37 | for _, row := range rows { |
| 38 | active[row.Key] = true |
| 39 | } |
| 40 | for _, item := range Navigation() { |
| 41 | if item.Group != "specialists" || item.Key == "templates" || active[item.Key] { |
| 42 | nav = append(nav, item) |
| 43 | } |
| 44 | } |
| 45 | names := map[string]string{} |
| 46 | for _, row := range rows { |
| 47 | names[row.Key] = row.Name |
| 48 | } |
| 49 | for i, item := range nav { |
| 50 | if item.Key == "playbooks" { |
| 51 | custom := []NavItem{} |
| 52 | for _, row := range rows { |
| 53 | if row.Role == "" { |
| 54 | custom = append(custom, NavItem{row.Key, "specialists", "/specialists/" + row.Key, "", row.Icon}) |
| 55 | } |
| 56 | } |
| 57 | nav = append(nav[:i], append(custom, nav[i:]...)...) |
| 58 | break |
| 59 | } |
| 60 | } |
| 61 | return nav, names |
| 62 | } |
| 63 | |
| 64 | func NavigationCounts(counts map[string]planning.SpecialistWorkView, rows []Specialist, err error) map[string]planning.SpecialistWorkView { |
| 65 | if err == nil { |
| 66 | return counts |
| 67 | } |
| 68 | unknown := make(map[string]planning.SpecialistWorkView, len(rows)) |
| 69 | for _, row := range rows { |
| 70 | unknown[row.Key] = planning.SpecialistWorkView{} |
| 71 | } |
| 72 | return unknown |
| 73 | } |
| 74 | |
| 75 | type NavigationView struct { |
| 76 | Counts map[string]planning.SpecialistWorkView |
| 77 | NavProjects, NavNeeds *int |
| 78 | } |
| 79 | |
| 80 | type NavigationStore interface { |
| 81 | SpecialistCounts(context.Context, kernel.Actor) (map[string]planning.SpecialistWorkView, error) |
| 82 | Projects(context.Context) ([]kernel.ProjectGroup, error) |
| 83 | Journey(context.Context, kernel.Actor) ([]planning.TaskViewData, error) |
| 84 | } |
| 85 | |
| 86 | func (v *NavigationView) Load(ctx context.Context, store NavigationStore, actor kernel.Actor, rows []Specialist) { |
| 87 | if v.Counts != nil { |
| 88 | return |
| 89 | } |
| 90 | counts, err := store.SpecialistCounts(ctx, actor) |
| 91 | v.Counts = NavigationCounts(counts, rows, err) |
| 92 | groups, err := store.Projects(ctx) |
| 93 | if err == nil { |
| 94 | n := 0 |
| 95 | for _, group := range groups { |
| 96 | n += len(group.Projects) |
| 97 | } |
| 98 | v.NavProjects = &n |
| 99 | } |
| 100 | tasks, err := store.Journey(ctx, actor) |
| 101 | if err == nil { |
| 102 | n := 0 |
| 103 | for _, task := range tasks { |
| 104 | if task.NeedsYou { |
| 105 | n++ |
| 106 | } |
| 107 | } |
| 108 | v.NavNeeds = &n |
| 109 | } |
| 110 | } |
| 111 | |