satoru/internal/webui/home.go

56 lines
1.3 KiB
Go

package webui
import (
"context"
"fmt"
"html"
"io"
"time"
"github.com/a-h/templ"
"satoru/internal/store"
)
func Home(now time.Time, user store.User) templ.Component {
return templ.ComponentFunc(func(_ context.Context, w io.Writer) error {
role := "Operator"
if user.IsAdmin {
role = "Admin"
}
var body string
if user.ID == 0 {
body = `<p class="muted">Sign in to continue.</p>
<div class="row">
<a class="button" href="/signin">Sign in</a>
<a class="button ghost" href="/signup">Sign up</a>
</div>`
} else {
body = fmt.Sprintf(`<p class="muted">Signed in as <strong>%s</strong> (%s).</p>
<p class="muted">Current server time: %s</p>
<form method="post" action="/signout">
<button class="button ghost" type="submit">Sign out</button>
</form>`, html.EscapeString(user.Username), role, now.Format(time.RFC1123))
}
_, err := io.WriteString(w, fmt.Sprintf(`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Satoru</title>
<link rel="stylesheet" href="/static/app.css" />
</head>
<body>
<main class="card">
<p class="eyebrow">Satoru</p>
<h1>Backup Control Plane</h1>
%s
</main>
</body>
</html>`, body))
return err
})
}