htmgo/examples/chat/chat/component.go

49 lines
1.1 KiB
Go
Raw Normal View History

2024-09-30 22:31:09 +00:00
package chat
2024-10-01 03:08:52 +00:00
import (
2024-10-01 03:49:03 +00:00
"fmt"
2024-10-01 03:08:52 +00:00
"github.com/maddalax/htmgo/framework/h"
2024-10-01 03:49:03 +00:00
"strings"
2024-10-01 03:08:52 +00:00
"time"
)
2024-09-30 22:31:09 +00:00
2024-10-01 03:08:52 +00:00
func MessageRow(message *Message) *h.Element {
2024-09-30 22:31:09 +00:00
return h.Div(
h.Attribute("hx-swap-oob", "beforeend"),
2024-10-01 17:42:01 +00:00
h.Class("flex flex-col gap-4 w-full"),
2024-09-30 22:31:09 +00:00
h.Id("messages"),
2024-10-01 03:08:52 +00:00
h.Div(
2024-10-01 17:42:01 +00:00
h.Class("flex flex-col gap-1"),
h.Div(
h.Class("flex gap-2 items-center"),
h.Pf(message.UserName, h.Class("font-bold")),
h.Pf(message.CreatedAt.In(time.Local).Format("01/02 03:04 PM")),
),
h.P(h.Text(message.Message)),
2024-10-01 03:08:52 +00:00
),
2024-09-30 22:31:09 +00:00
)
}
2024-10-01 03:49:03 +00:00
func ConnectedUsers(username string) *h.Element {
return h.Ul(
h.Attribute("hx-swap", "none"),
h.Attribute("hx-swap-oob", "beforeend"),
h.Id("connected-users"),
2024-10-01 17:42:01 +00:00
h.Class("flex flex-col"),
2024-10-01 03:49:03 +00:00
// This would be populated dynamically with connected users
ConnectedUser(username, false),
)
}
func ConnectedUser(username string, remove bool) *h.Element {
id := fmt.Sprintf("connected-user-%s", strings.ReplaceAll(username, "#", "-"))
if remove {
return h.Div(h.Id(id), h.Attribute("hx-swap-oob", "delete"))
}
return h.Li(
h.Id(id),
2024-10-01 17:42:01 +00:00
h.Class("truncate text-slate-700"),
2024-10-01 03:49:03 +00:00
h.Text(username),
)
}