this repo has no description
1package pages
2
3import (
4 "embed"
5 "fmt"
6 "html/template"
7 "io"
8 "io/fs"
9 "log"
10 "strings"
11
12 "github.com/sotangled/tangled/appview/auth"
13 "github.com/sotangled/tangled/appview/db"
14)
15
16//go:embed templates/*
17var files embed.FS
18
19type Pages struct {
20 t map[string]*template.Template
21}
22
23func NewPages() *Pages {
24 templates := make(map[string]*template.Template)
25
26 // Walk through embedded templates directory and parse all .html files
27 err := fs.WalkDir(files, "templates", func(path string, d fs.DirEntry, err error) error {
28 if err != nil {
29 return err
30 }
31
32 if !d.IsDir() && strings.HasSuffix(path, ".html") {
33 name := strings.TrimPrefix(path, "templates/")
34 name = strings.TrimSuffix(name, ".html")
35
36 if !strings.HasPrefix(path, "templates/layouts/") {
37 // Add the page template on top of the base
38 tmpl, err := template.New(name).ParseFS(files, path, "templates/layouts/*.html")
39 if err != nil {
40 return fmt.Errorf("setting up template: %w", err)
41 }
42
43 templates[name] = tmpl
44 log.Printf("loaded template: %s", name)
45 }
46
47 return nil
48 }
49 return nil
50 })
51 if err != nil {
52 log.Fatalf("walking template dir: %v", err)
53 }
54
55 log.Printf("total templates loaded: %d", len(templates))
56
57 return &Pages{
58 t: templates,
59 }
60}
61
62type LoginParams struct {
63}
64
65func (p *Pages) execute(name string, w io.Writer, params any) error {
66 return p.t[name].ExecuteTemplate(w, "layouts/base", params)
67}
68
69func (p *Pages) Login(w io.Writer, params LoginParams) error {
70 return p.t["user/login"].ExecuteTemplate(w, "layouts/base", params)
71}
72
73type TimelineParams struct {
74 User *auth.User
75}
76
77func (p *Pages) Timeline(w io.Writer, params TimelineParams) error {
78 return p.execute("timeline", w, params)
79}
80
81type SettingsParams struct {
82 User *auth.User
83 PubKeys []db.PublicKey
84}
85
86func (p *Pages) Settings(w io.Writer, params SettingsParams) error {
87 return p.execute("settings/keys", w, params)
88}
89
90type KnotsParams struct {
91 User *auth.User
92 Registrations []db.Registration
93}
94
95func (p *Pages) Knots(w io.Writer, params KnotsParams) error {
96 return p.execute("knots", w, params)
97}
98
99type KnotParams struct {
100 User *auth.User
101 Registration *db.Registration
102 Members []string
103 IsOwner bool
104}
105
106func (p *Pages) Knot(w io.Writer, params KnotParams) error {
107 return p.execute("knot", w, params)
108}
109
110type NewRepoParams struct {
111 User *auth.User
112}
113
114func (p *Pages) NewRepo(w io.Writer, params NewRepoParams) error {
115 return p.execute("repo/new", w, params)
116}
117
118type ProfilePageParams struct {
119 LoggedInUser *auth.User
120 UserDid string
121 UserHandle string
122 Repos []db.Repo
123}
124
125func (p *Pages) ProfilePage(w io.Writer, params ProfilePageParams) error {
126 return p.execute("user/profile", w, params)
127}