97 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			97 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
package views
 | 
						|
 | 
						|
import "git.32bit.cafe/32bitcafe/guestbook/internal/models"
 | 
						|
import "strconv"
 | 
						|
import "fmt"
 | 
						|
import "strings"
 | 
						|
 | 
						|
type CommonData struct {
 | 
						|
	CurrentYear      int
 | 
						|
	Flash            string
 | 
						|
	IsAuthenticated  bool
 | 
						|
	CSRFToken        string
 | 
						|
	CurrentUser      *models.User
 | 
						|
	IsHtmx           bool
 | 
						|
	RootUrl          string
 | 
						|
	LocalAuthEnabled bool
 | 
						|
	OIDCEnabled      bool
 | 
						|
}
 | 
						|
 | 
						|
func shortIdToSlug(shortId uint64) string {
 | 
						|
	return strconv.FormatUint(shortId, 36)
 | 
						|
}
 | 
						|
 | 
						|
func slugToShortId(slug string) uint64 {
 | 
						|
	id, _ := strconv.ParseUint(slug, 36, 64)
 | 
						|
	return id
 | 
						|
}
 | 
						|
 | 
						|
func externalUrl(url string) string {
 | 
						|
	if !strings.HasPrefix(url, "http") {
 | 
						|
		return "http://" + url
 | 
						|
	}
 | 
						|
	return url
 | 
						|
}
 | 
						|
 | 
						|
templ commonHeader() {
 | 
						|
	<header>
 | 
						|
		<h1><a href="/">webweav.ing</a></h1>
 | 
						|
	</header>
 | 
						|
}
 | 
						|
 | 
						|
templ topNav(data CommonData) {
 | 
						|
	{{ hxHeaders := fmt.Sprintf("{\"X-CSRF-Token\": \"%s\"}", data.CSRFToken) }}
 | 
						|
	<nav aria-label="Site navigation">
 | 
						|
		<div class="nav-welcome">
 | 
						|
			<span>
 | 
						|
				if data.IsAuthenticated {
 | 
						|
					Welcome, { data.CurrentUser.Username }
 | 
						|
				}
 | 
						|
			</span>
 | 
						|
		</div>
 | 
						|
		<ul class="nav-links">
 | 
						|
			if data.IsAuthenticated {
 | 
						|
				<li><a href="/guestbooks">All Guestbooks</a></li>
 | 
						|
				<li><a href="/websites">My Websites</a></li>
 | 
						|
				<li><a href="/users/settings">Settings</a></li>
 | 
						|
				<li><a href="#" hx-post="/users/logout" hx-headers={ hxHeaders }>Logout</a></li>
 | 
						|
			} else {
 | 
						|
				if data.LocalAuthEnabled {
 | 
						|
					<li><a href="/users/register">Create an Account</a></li> | 
 | 
						|
				}
 | 
						|
				<li><a href="/users/login">Login</a></li>
 | 
						|
			}
 | 
						|
		</ul>
 | 
						|
	</nav>
 | 
						|
}
 | 
						|
 | 
						|
templ commonFooter() {
 | 
						|
	<footer>
 | 
						|
		<p>A <a href="https://32bit.cafe" rel="noopener">32bit.cafe</a> Project</p>
 | 
						|
	</footer>
 | 
						|
}
 | 
						|
 | 
						|
templ base(title string, data CommonData) {
 | 
						|
	<!DOCTYPE html>
 | 
						|
	<html lang="en">
 | 
						|
		<head>
 | 
						|
			<title>{ title } - webweav.ing</title>
 | 
						|
			<meta charset="UTF-8"/>
 | 
						|
			<meta name="viewport" content="width=device-width, initial-scale=1"/>
 | 
						|
			<link href="/static/css/style.css" rel="stylesheet"/>
 | 
						|
			<script src="/static/js/htmx.min.js"></script>
 | 
						|
		</head>
 | 
						|
		<body>
 | 
						|
			@commonHeader()
 | 
						|
			@topNav(data)
 | 
						|
			<main role="main">
 | 
						|
				if data.Flash != "" {
 | 
						|
					<div class="notice flash">{ data.Flash }</div>
 | 
						|
				}
 | 
						|
				{ children... }
 | 
						|
			</main>
 | 
						|
			@commonFooter()
 | 
						|
		</body>
 | 
						|
	</html>
 | 
						|
}
 |