htmgo/examples/chat/pages/chat.$id.go

109 lines
2.3 KiB
Go
Raw Normal View History

2024-10-01 01:32:42 +00:00
package pages
import (
2024-10-01 03:49:03 +00:00
"chat/chat"
2024-10-01 03:08:52 +00:00
"fmt"
"github.com/go-chi/chi/v5"
2024-10-01 01:32:42 +00:00
"github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/js"
)
func ChatRoom(ctx *h.RequestContext) *h.Page {
2024-10-01 03:08:52 +00:00
roomId := chi.URLParam(ctx.Request, "id")
2024-10-01 01:32:42 +00:00
return h.NewPage(
RootPage(
h.Div(
h.JoinExtensions(
h.TriggerChildren(),
h.HxExtension("ws"),
),
2024-10-01 17:09:22 +00:00
2024-10-01 03:08:52 +00:00
h.Attribute("ws-connect", fmt.Sprintf("/ws/chat/%s", roomId)),
2024-10-01 17:09:22 +00:00
h.HxOnWsOpen(
js.ConsoleLog("Connected to chat room"),
),
h.HxOnWsClose(
js.EvalJs(`
const reason = e.detail.event.reason
if(['invalid room', 'no session'].includes(reason)) {
window.location.href = '/';
} else {
console.error('Connection closed:', e.detail.event)
}
`),
),
2024-10-01 03:49:03 +00:00
h.Class("flex flex-row min-h-screen bg-neutral-100"),
2024-10-01 03:08:52 +00:00
// Sidebar for connected users
UserSidebar(),
// Chat Area
2024-10-01 01:32:42 +00:00
h.Div(
2024-10-01 03:49:03 +00:00
h.Class("flex flex-col flex-grow gap-4 bg-white rounded p-4"),
2024-10-01 03:08:52 +00:00
2024-10-01 17:09:22 +00:00
h.HxAfterWsMessage(
js.EvalJsOnSibling("#messages",
// language=JavaScript
`element.scrollTop = element.scrollHeight;`),
),
2024-10-01 03:08:52 +00:00
// Chat Messages
2024-10-01 01:32:42 +00:00
h.Div(
h.Id("messages"),
2024-10-01 03:08:52 +00:00
h.Class("flex flex-col gap-2 overflow-auto grow w-full"),
),
// Chat Input at the bottom
h.Div(
h.Class("mt-auto"),
Form(ctx),
2024-10-01 01:32:42 +00:00
),
),
),
),
)
}
2024-10-01 03:08:52 +00:00
func UserSidebar() *h.Element {
return h.Div(
2024-10-01 03:49:03 +00:00
h.Class("w-48 bg-slate-200 p-4 flex flex-col gap-3 rounded-l-lg"),
2024-10-01 03:08:52 +00:00
h.H2F("Connected Users", h.Class("text-lg font-bold")),
2024-10-01 03:49:03 +00:00
chat.ConnectedUsers(""),
2024-10-01 03:08:52 +00:00
)
}
2024-10-01 01:32:42 +00:00
func MessageInput() *h.Element {
return h.Input("text",
h.Id("message-input"),
h.Required(),
2024-10-01 03:08:52 +00:00
h.Class("p-4 rounded-md border border-slate-200 w-full"),
2024-10-01 01:32:42 +00:00
h.Name("message"),
2024-10-01 03:08:52 +00:00
h.Placeholder("Type a message..."),
2024-10-01 17:09:22 +00:00
h.HxAfterWsSend(
2024-10-01 01:32:42 +00:00
js.SetValue(""),
),
)
}
func Form(ctx *h.RequestContext) *h.Element {
return h.Div(
2024-10-01 03:08:52 +00:00
h.Class("flex gap-4 items-center"),
2024-10-01 01:32:42 +00:00
h.Form(
h.Attribute("ws-send", ""),
2024-10-01 03:08:52 +00:00
h.Class("flex flex-grow"),
2024-10-01 01:32:42 +00:00
MessageInput(),
),
)
}
func Spinner(children ...h.Ren) *h.Element {
return h.Div(
h.Children(children...),
2024-10-01 03:08:52 +00:00
h.Class("spinner spinner-border animate-spin w-4 h-4 border-2 border-t-transparent"),
2024-10-01 01:32:42 +00:00
h.Attribute("role", "status"),
)
}