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 "github.com/sotangled/tangled/types"
15)
16
17//go:embed templates/*
18var files embed.FS
19
20type Pages struct {
21 t map[string]*template.Template
22}
23
24func NewPages() *Pages {
25 templates := make(map[string]*template.Template)
26
27 // Walk through embedded templates directory and parse all .html files
28 err := fs.WalkDir(files, "templates", func(path string, d fs.DirEntry, err error) error {
29 if err != nil {
30 return err
31 }
32
33 if !d.IsDir() && strings.HasSuffix(path, ".html") {
34 name := strings.TrimPrefix(path, "templates/")
35 name = strings.TrimSuffix(name, ".html")
36
37 if !strings.HasPrefix(path, "templates/layouts/") {
38 // Add the page template on top of the base
39 tmpl, err := template.New(name).ParseFS(files, path, "templates/layouts/*.html")
40 if err != nil {
41 return fmt.Errorf("setting up template: %w", err)
42 }
43
44 templates[name] = tmpl
45 log.Printf("loaded template: %s", name)
46 }
47
48 return nil
49 }
50 return nil
51 })
52 if err != nil {
53 log.Fatalf("walking template dir: %v", err)
54 }
55
56 log.Printf("total templates loaded: %d", len(templates))
57
58 return &Pages{
59 t: templates,
60 }
61}
62
63type LoginParams struct {
64}
65
66func (p *Pages) execute(name string, w io.Writer, params any) error {
67 return p.t[name].ExecuteTemplate(w, "layouts/base", params)
68}
69
70func (p *Pages) Login(w io.Writer, params LoginParams) error {
71 return p.t["user/login"].ExecuteTemplate(w, "layouts/base", params)
72}
73
74type TimelineParams struct {
75 User *auth.User
76}
77
78func (p *Pages) Timeline(w io.Writer, params TimelineParams) error {
79 return p.execute("timeline", w, params)
80}
81
82type SettingsParams struct {
83 User *auth.User
84 PubKeys []db.PublicKey
85}
86
87func (p *Pages) Settings(w io.Writer, params SettingsParams) error {
88 return p.execute("settings/keys", w, params)
89}
90
91type KnotsParams struct {
92 User *auth.User
93 Registrations []db.Registration
94}
95
96func (p *Pages) Knots(w io.Writer, params KnotsParams) error {
97 return p.execute("knots", w, params)
98}
99
100type KnotParams struct {
101 User *auth.User
102 Registration *db.Registration
103 Members []string
104 IsOwner bool
105}
106
107func (p *Pages) Knot(w io.Writer, params KnotParams) error {
108 return p.execute("knot", w, params)
109}
110
111type NewRepoParams struct {
112 User *auth.User
113}
114
115func (p *Pages) NewRepo(w io.Writer, params NewRepoParams) error {
116 return p.execute("repo/new", w, params)
117}
118
119type ProfilePageParams struct {
120 LoggedInUser *auth.User
121 UserDid string
122 UserHandle string
123 Repos []db.Repo
124}
125
126func (p *Pages) ProfilePage(w io.Writer, params ProfilePageParams) error {
127 return p.execute("user/profile", w, params)
128}
129
130type RepoInfo struct {
131 Name string
132 OwnerDid string
133 OwnerHandle string
134}
135
136func (r RepoInfo) OwnerWithAt() string {
137 if r.OwnerHandle != "" {
138 return fmt.Sprintf("@%s", r.OwnerHandle)
139 } else {
140 return r.OwnerDid
141 }
142}
143
144type RepoIndexParams struct {
145 LoggedInUser *auth.User
146 RepoInfo RepoInfo
147 types.RepoIndexResponse
148}
149
150func (p *Pages) RepoIndexPage(w io.Writer, params RepoIndexParams) error {
151 return p.execute("repo/index", w, params)
152}
153
154type RepoLogParams struct {
155 LoggedInUser *auth.User
156 RepoInfo RepoInfo
157 types.RepoLogResponse
158}
159
160func (p *Pages) RepoLog(w io.Writer, params RepoLogParams) error {
161 return p.execute("repo/log", w, params)
162}