package main
import (
"context"
"embed"
"net/http"
"strings"
"github.com/theplant/htmlgo"
)
// embed ui files
//go:embed ui/dist/*
var dist embed.FS
func inviteTreeHandler(w http.ResponseWriter, r *http.Request) {
content := inviteTreePageHTML(r.Context(), InviteTreePageParams{})
htmlgo.Fprint(w, baseHTML(content), r.Context())
}
func reportsViewerHandler(w http.ResponseWriter, r *http.Request) {
// var formattedReportsData template.HTML = ""
// events, _ := db.QueryEvents(context.Background(), nostr.Filter{
// Kinds: []int{1984},
// Limit: 52,
// })
// type Report struct {
// ID string
// ByUser string
// AboutUser string
// AboutEvent string
// Type string
// Content string
// }
// for ev := range events {
// pTag := ev.Tags.GetFirst([]string{"p"})
// eTag := ev.Tags.GetFirst([]string{"e"})
// if pTag != nil {
// typeReport := eTag.Relay()[6:]
// if typeReport == "" {
// typeReport = pTag.Relay()[6:]
// }
// report := Report{
// ID: ev.ID,
// ByUser: ev.PubKey,
// AboutUser: pTag.Value(),
// AboutEvent: eTag.Value(),
// Type: typeReport,
// Content: ev.Content,
// }
// // get AboutEvent content, note1 ect
// formattedReportsData += template.HTML(fmt.Sprintf(`
//
//
Report %v
//
By User: %v
//
About User: %v
`,
// report.ID,
// getUserInfo(context.Background(), report.ByUser).Npub,
// getUserInfo(context.Background(), report.ByUser).Name,
// getUserInfo(context.Background(), report.AboutUser).Npub,
// getUserInfo(context.Background(), report.AboutUser).Name,
// ))
// if report.AboutEvent != "" {
// // fetch event data
// aboutEvents, _ := db.QueryEvents(context.TODO(), nostr.Filter{
// IDs: []string{report.AboutEvent},
// })
// for aboutEvent := range aboutEvents {
// formattedReportsData += template.HTML(fmt.Sprintf(`
//
// About Event:
// Kind: %v
// Tags: %v
// Content: %v
//
// `,
// template.HTMLEscaper(aboutEvent.Kind),
// template.HTMLEscaper(aboutEvent.Tags),
// template.HTMLEscaper(aboutEvent.Content),
// ))
// }
// }
// formattedReportsData += template.HTML(fmt.Sprintf(`
//
Type: %v
`,
// report.Type,
// ))
// if report.Content != "" {
// formattedReportsData += template.HTML(fmt.Sprintf(`
//
Content: %v
//
//
//
//
//
//
//
`,
// template.HTMLEscaper(report.Content),
// template.HTMLEscaper(report.ID),
// template.HTMLEscaper(report.AboutUser),
// template.HTMLEscaper(report.ID),
// template.HTMLEscaper(report.ByUser),
// ))
// }
// }
// }
// data := map[string]interface{}{
// "Relayname": s.RelayName,
// "Relaydescription": s.RelayDescription,
// "Pagetitle": "Reports Viewer",
// "Pagecontent": formattedReportsData,
// }
// tmpl, err := template.ParseFS(dist, "ui/dist/index.html")
// if err != nil {
// http.Error(w, "Error parsing template: "+err.Error(), http.StatusInternalServerError)
// return
// }
// // Execute the template with the provided data and write it to the response
// err = tmpl.Execute(w, data)
// if err != nil {
// http.Error(w, "Error executing template: "+err.Error(), http.StatusInternalServerError)
// return
// }
}
func homePageHandler(w http.ResponseWriter, r *http.Request) {
content := homePageHTML(r.Context(), HomePageParams{
RelayOwnerInfo: getUserInfo(context.Background(), s.RelayPubkey),
})
htmlgo.Fprint(w, baseHTML(content), r.Context())
}
func staticHandler(prefix string, w http.ResponseWriter, r *http.Request) {
path := prefix + r.URL.Path
data, err := dist.ReadFile(path)
if err != nil {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
contentType := http.DetectContentType(data)
if strings.HasSuffix(r.URL.Path, ".js") {
contentType = "application/javascript"
} else if strings.HasSuffix(r.URL.Path, ".css") {
contentType = "text/css"
}
w.Header().Set("Content-Type", contentType)
if _, err := w.Write(data); err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}