Skip to content

Commit

Permalink
convert templates to .templ
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jan 14, 2025
1 parent be73687 commit a64a12e
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 209 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ users.json
khatru-pyramid
.env
db
*_templ.go
45 changes: 0 additions & 45 deletions components.go

This file was deleted.

18 changes: 4 additions & 14 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import (

"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/theplant/htmlgo"
)

func inviteTreeHandler(w http.ResponseWriter, r *http.Request) {
loggedUser := getLoggedUser(r)
content := inviteTreePageHTML(r.Context(), InviteTreePageParams{
loggedUser: loggedUser,
})
htmlgo.Fprint(w, baseHTML(content, loggedUser), r.Context())
inviteTreePage(loggedUser).Render(r.Context(), w)

Check failure on line 13 in handler.go

View workflow job for this annotation

GitHub Actions / build

undefined: inviteTreePage
}

func addToWhitelistHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -35,8 +31,7 @@ func addToWhitelistHandler(w http.ResponseWriter, r *http.Request) {
return
}

content := inviteTreeComponent(r.Context(), "", loggedUser)
htmlgo.Fprint(w, content, r.Context())
inviteTreeComponent("", loggedUser).Render(r.Context(), w)

Check failure on line 34 in handler.go

View workflow job for this annotation

GitHub Actions / build

undefined: inviteTreeComponent
}

func removeFromWhitelistHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -46,8 +41,7 @@ func removeFromWhitelistHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "failed to remove from whitelist: "+err.Error(), 500)
return
}
content := inviteTreeComponent(r.Context(), "", loggedUser)
htmlgo.Fprint(w, content, r.Context())
inviteTreeComponent("", loggedUser).Render(r.Context(), w)

Check failure on line 44 in handler.go

View workflow job for this annotation

GitHub Actions / build

undefined: inviteTreeComponent
}

// this deletes all events from users not in the relay anymore
Expand Down Expand Up @@ -95,11 +89,7 @@ func reportsViewerHandler(w http.ResponseWriter, r *http.Request) {
return
}

content := reportsPageHTML(r.Context(), ReportsPageParams{
reports: events,
loggedUser: getLoggedUser(r),
})
htmlgo.Fprint(w, content, r.Context())
reportsPage(events, getLoggedUser(r)).Render(r.Context(), w)

Check failure on line 92 in handler.go

View workflow job for this annotation

GitHub Actions / build

undefined: reportsPage
}

func joubleHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
69 changes: 69 additions & 0 deletions invite_tree.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"fmt"

"github.com/nbd-wtf/go-nostr/sdk"
)

templ inviteTreePage(loggedUser string) {
@layout(loggedUser) {
<div>
if loggedUser != "" && (loggedUser == s.RelayPubkey || !hasInvitedAtLeast(loggedUser, s.MaxInvitesPerPerson)) {
<form
hx-post="/add-to-whitelist"
hx-trigger="submit"
hx-target="#tree"
_="on htmx:afterRequest(elt, successful) if successful and elt is I call I.reset()"
class="flex justify-center"
>
<input
type="text"
name="pubkey"
placeholder="npub1..."
class="w-96 rounded-md border-0 p-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600"
/>
<button
type="submit"
class="rounded-md text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 ml-2 p-2 bg-white hover:bg-gray-50"
>
invite
</button>
</form>
}
<div id="tree" class="mt-3 flex justify-center">
@inviteTreeComponent("", loggedUser)
</div>
</div>
}
}

templ inviteTreeComponent(inviter string, loggedUser string) {
<ul>
for pubkey, invitedBy := range whitelist {
if invitedBy == inviter {
<li class="ml-6">
@userNameComponent(sys.FetchProfileMetadata(ctx, pubkey))
if isAncestorOf(loggedUser, pubkey) && loggedUser != "" {
<button
class="rounded-md text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 px-2 ml-2 bg-red-100 hover:bg-red-300"
hx-post="/remove-from-whitelist"
hx-trigger="click"
hx-target="#tree"
hx-vals={ fmt.Sprintf(`{"pubkey": "%s"}`, pubkey) }
>
remove
</button>
}
@inviteTreeComponent(pubkey, loggedUser)
</li>
}
}
</ul>
}

templ userNameComponent(profile sdk.ProfileMetadata) {
<a href={ templ.URL("https://nosta.me/" + profile.Npub()) } target="_blank" class="font-mono py-1">
<span title={ profile.Npub() }>{ profile.ShortName() }</span>
</a>
}
7 changes: 5 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
dev:
ag -l --go | entr -r godotenv go run .
fd 'go|templ' | entr -r bash -c 'just templ && godotenv go run .'

build:
build: templ
CC=musl-gcc go build -ldflags='-linkmode external -extldflags "-static"' -o ./khatru-pyramid

templ:
templ generate

deploy target: build
ssh root@{{target}} 'systemctl stop pyramid';
scp khatru-pyramid {{target}}:pyramid/khatru-invite
Expand Down
59 changes: 59 additions & 0 deletions layout.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

templ layout(loggedUser string) {
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>{ s.RelayName }</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>
</head>
<body class="max-w-screen-lg px-3 mx-auto">
<div class="mx-auto my-6 text-center">
<h1 class="font-bold text-2xl">{ s.RelayName }</h1>
if s.RelayDescription != "" {
<p class="text-lg">{ s.RelayDescription }</p>
}
</div>
<nav class="flex flex-1 items-center justify-center">
<a href="/" class="text-gray-600 hover:bg-gray-200 rounded-md px-3 py-2 font-medium" hx-boost="true" hx-target="main" hx-select="main">invite tree</a>
<a href="/browse" class="text-gray-600 hover:bg-gray-200 rounded-md px-3 py-2 font-medium">browse</a>
<a href="/reports" class="text-gray-600 hover:bg-gray-200 rounded-md px-3 py-2 font-medium" hx-boost="true" hx-target="main" hx-select="main">reports</a>
if loggedUser == s.RelayPubkey {
<a href="/cleanup" class="text-gray-600 hover:bg-gray-200 rounded-md px-3 py-2 font-medium">clear stuff</a>
}
<a
href="#"
class="text-gray-600 hover:bg-gray-200 rounded-md px-3 py-2 font-medium"
_="
on click if my innerText is equal to 'login'
get window.nostr.signEvent({created_at: Math.round(Date.now()/1000), kind: 27235, tags: [['domain', '{ s.Domain }']], content: ''})
then get JSON.stringify(it)
then set cookies['nip98'] to it
otherwise
call cookies.clear('nip98')
end
then call location.reload()
on load
get cookies['nip98']
then if it is undefined
set my innerText to 'login'
otherwise
set my innerText to 'logout'
"
></a>
</nav>
<main class="m-4">
{ children... }
</main>
<p class="text-end my-4 text-sm">
powered by
<a href="https://github.com/github-tijlxyz/khatru-pyramid" class="hover:underline cursor-pointer text-blue-500">khatru-pyramid</a>
</p>
</body>
</html>
}
148 changes: 0 additions & 148 deletions pages.go

This file was deleted.

Loading

0 comments on commit a64a12e

Please sign in to comment.