Home

ugit @main - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / html / repo_breadcrumb.go
- raw
 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
package html

import (
	"fmt"
	"path"
	"strings"

	. "maragu.dev/gomponents"
	. "maragu.dev/gomponents/html"
)

type RepoBreadcrumbComponentContext struct {
	Repo string
	Ref  string
	Path string
}

type breadcrumb struct {
	label string
	href  string
	end   bool
}

func (r RepoBreadcrumbComponentContext) crumbs() []breadcrumb {
	parts := strings.Split(r.Path, "/")
	breadcrumbs := []breadcrumb{
		{
			label: r.Repo,
			href:  fmt.Sprintf("/%s/tree/%s/", r.Repo, r.Ref),
		},
	}
	for idx, part := range parts {
		breadcrumbs = append(breadcrumbs, breadcrumb{
			label: part,
			href:  path.Join(breadcrumbs[idx].href, part),
		})
	}
	breadcrumbs[len(breadcrumbs)-1].end = true
	return breadcrumbs
}

func repoBreadcrumbComponent(rbcc RepoBreadcrumbComponentContext) Node {
	if rbcc.Path == "" {
		return nil
	}
	return Div(Class("inline-block text-text"),
		Map(rbcc.crumbs(), func(crumb breadcrumb) Node {
			if crumb.end {
				return Span(Text(crumb.label))
			}
			return Group([]Node{
				A(Class("underline decoration-text/50 decoration-dashed hover:decoration-solid"), Href(crumb.href), Text(crumb.label)),
				Text(" / "),
			})
		}),
	)
}