labelbadges.go
⎇
Raw
1package views
2
3import (
4 "strconv"
5
6 "hearthforge/internal/db"
7 "hearthforge/internal/util"
8
9 g "maragu.dev/gomponents"
10 . "maragu.dev/gomponents/html"
11)
12
13// LabelBadges shows the labels of an issue or patch, with add and remove
14// forms when the viewer may manage them.
15func LabelBadges(labels, repoLabels []db.Label, canManage bool, baseURL string) g.Node {
16 if len(labels) == 0 && !canManage {
17 return nil
18 }
19 var available []db.Label
20 for _, l := range repoLabels {
21 attached := false
22 for _, al := range labels {
23 if al.ID == l.ID {
24 attached = true
25 break
26 }
27 }
28 if !attached {
29 available = append(available, l)
30 }
31 }
32 return Div(Class("issue-labels-row"),
33 g.Map(labels, func(label db.Label) g.Node {
34 return Span(Class("label-badge-wrap"),
35 Span(Class("label-badge"),
36 Style("background:"+label.Color+";color:"+util.LabelTextColor(label.Color)),
37 g.Text(label.Name)),
38 g.If(canManage, Form(Method("POST"), Action(baseURL+"/labels/remove"),
39 Class("label-remove-form"),
40 Input(Type("hidden"), Name("label_id"), Value(strconv.FormatInt(label.ID, 10))),
41 Button(Type("submit"), Class("label-remove-btn"), Title("Remove label"), g.Text("×")),
42 )),
43 )
44 }),
45 g.If(canManage && len(available) > 0,
46 Form(Method("POST"), Action(baseURL+"/labels/add"), Class("label-add-inline-form"),
47 Select(Name("label_id"), Class("label-select"),
48 g.Map(available, func(label db.Label) g.Node {
49 return Option(Value(strconv.FormatInt(label.ID, 10)), g.Text(label.Name))
50 }),
51 ),
52 Button(Type("submit"), Class("btn btn-sm btn-secondary"), g.Text("Add label")),
53 )),
54 )
55}
56