phoenix/views/pages/index.go
Ivan R. 9a9a1af63e
feat: tile mode (#39)
Experimental mode for displaying links as tiles.
For consistency, I made the globe the default icon.
2024-04-01 22:38:23 +05:00

50 lines
1.1 KiB
Go

package pages
import (
"net/http"
"time"
"github.com/ordinary-dev/phoenix/database"
log "github.com/sirupsen/logrus"
)
func ShowMainPage(w http.ResponseWriter, r *http.Request) {
groups, err := database.GetGroupsWithLinks()
if err != nil {
ShowError(w, http.StatusInternalServerError, err)
return
}
// Get desired style.
style := r.FormValue("style")
if style == "tiles" || style == "list" {
// If a valid style is specified in the url -
// save the value in a cookie.
http.SetCookie(w, &http.Cookie{
Name: "phoenix-style",
Value: style,
Expires: time.Now().Add(time.Hour * 24 * 30 * 12 * 10),
})
} else {
// The style is not specified or has an incorrect type, check the cookies.
styleCookie, err := r.Cookie("phoenix-style")
if err == nil {
style = styleCookie.Value
}
}
if style != "tiles" && style != "list" {
style = "list"
}
err = Render("index.html.tmpl", w, map[string]any{
"description": "Self-hosted start page.",
"groups": groups,
"style": style,
})
if err != nil {
log.Error(err)
}
}