internal/httpd/topics.go

419f6dfdc5489a0c6374e36dd1ebbfca68040056
gitbay/internal/httpd/topics.go history · blame · raw

40 lines · 977 bytes

 1package httpd
 2
 3import (
 4	"net/url"
 5	"sort"
 6)
 7
 8// topicFacets counts the topics across repos for explore's column. The
 9// links are the ones topic chips already use, ?q=<topic>; the active
10// topic links to explore with no query.
11func topicFacets(repos []describedRepo, q string) facetGroup {
12	counts := map[string]int64{}
13	for _, r := range repos {
14		for _, t := range r.Topics {
15			counts[t]++
16		}
17	}
18	names := make([]string, 0, len(counts))
19	for t := range counts {
20		names = append(names, t)
21	}
22	sort.Slice(names, func(i, j int) bool {
23		if counts[names[i]] != counts[names[j]] {
24			return counts[names[i]] > counts[names[j]]
25		}
26		return names[i] < names[j]
27	})
28	if len(names) > 20 {
29		names = names[:20]
30	}
31	g := facetGroup{Title: "Topics"}
32	for _, t := range names {
33		item := facetItem{Label: t, Count: counts[t], Href: "/explore?q=" + url.QueryEscape(t), Active: t == q}
34		if item.Active {
35			item.Href = "/explore"
36		}
37		g.Items = append(g.Items, item)
38	}
39	return g
40}