Brand Icons

/app/lib/icons/library.go (1.1 KB)

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package icons

import (
"fmt"
"strings"
"sync"

"github.com/samber/lo"
"golang.org/x/exp/maps"

"{{{ .Package }}}/app/util"
)

type Library struct {
Icons map[string]*Icon `json:"icons"`
Keys map[string]string `json:"keys,omitempty"`
}

func NewLibrary(icons ...*Icon) *Library {
ret := &Library{Icons: map[string]*Icon{}, Keys: map[string]string{}}
lo.ForEach(icons, func(x *Icon, _ int) {
ret.AddIcon(x)
})
return ret
}

func (l *Library) AddIcon(bi *Icon) {
l.Icons[bi.Key] = bi
l.Keys[bi.Title] = bi.Key
for _, x := range bi.Aliases {
l.Keys[x] = bi.Key
}
}

func (l *Library) SortedKeys() []string {
return util.ArraySorted(maps.Keys(l.Icons))
}

func (l *Library) HTML(key string) string {
key = strings.TrimPrefix(key, "brand-")
ret, ok := l.Icons[key]
if !ok {
return fmt.Sprintf("<!-- unknown brand icon [%s] -->", key)
}
return ret.HTML("brand-")
}

var (
brandLibCache *Library
brandLibMu sync.Mutex
)

func BrandLibrary() *Library {
if brandLibCache == nil {
brandLibMu.Lock()
defer brandLibMu.Unlock()
brandLibCache = NewLibrary(BrandIcons...)
}
return brandLibCache
}