Merge pull request 'Fix chat messages not appearing without refresh' (#7) from fix/chat-append-messages into main
Some checks failed
CI / Deploy / lint (push) Has been cancelled
CI / Deploy / deploy (push) Has been cancelled
CI / Deploy / test (push) Has been cancelled

This commit was merged in pull request #7.
This commit is contained in:
2026-03-03 19:41:52 +00:00
3 changed files with 26 additions and 11 deletions

View File

@@ -23,10 +23,8 @@ type Config struct {
StopKeyPropagation bool StopKeyPropagation bool
} }
templ Chat(messages []chat.Message, cfg Config) { // ChatMessage renders a single chat message. Used for appending new messages via SSE.
<div id={ cfg.CSSPrefix + "-chat" } class={ cfg.CSSPrefix + "-chat" }> templ ChatMessage(m chat.Message, cfg Config) {
<div class={ cfg.CSSPrefix + "-chat-history" }>
for _, m := range messages {
<div class={ cfg.CSSPrefix + "-chat-msg" }> <div class={ cfg.CSSPrefix + "-chat-msg" }>
<span style={ fmt.Sprintf("color:%s;font-weight:bold;", cfg.Color(m.Slot)) }> <span style={ fmt.Sprintf("color:%s;font-weight:bold;", cfg.Color(m.Slot)) }>
{ m.Nickname + ": " } { m.Nickname + ": " }
@@ -34,6 +32,13 @@ templ Chat(messages []chat.Message, cfg Config) {
<span>{ m.Message }</span> <span>{ m.Message }</span>
</div> </div>
} }
templ Chat(messages []chat.Message, cfg Config) {
<div id={ cfg.CSSPrefix + "-chat" } class={ cfg.CSSPrefix + "-chat" }>
<div id={ cfg.CSSPrefix + "-chat-history" } class={ cfg.CSSPrefix + "-chat-history" }>
for _, m := range messages {
@ChatMessage(m, cfg)
}
</div> </div>
<div class={ cfg.CSSPrefix + "-chat-input" } data-morph-ignore> <div class={ cfg.CSSPrefix + "-chat-input" } data-morph-ignore>
if cfg.StopKeyPropagation { if cfg.StopKeyPropagation {

View File

@@ -148,8 +148,13 @@ func HandleGameEvents(store *connect4.Store, nc *nats.Conn, sm *scs.SessionManag
return return
} }
case msg := <-chatCh: case msg := <-chatCh:
room.Receive(msg.Data) chatMsg, _ := room.Receive(msg.Data)
if err := patchAll(); err != nil { err := sse.PatchElementTempl(
chatcomponents.ChatMessage(chatMsg, chatCfg),
datastar.WithSelectorID("c4-chat-history"),
datastar.WithModeAppend(),
)
if err != nil {
return return
} }
} }

View File

@@ -172,8 +172,13 @@ func HandleSnakeEvents(snakeStore *snake.SnakeStore, nc *nats.Conn, sm *scs.Sess
if msg == nil { if msg == nil {
continue continue
} }
room.Receive(msg.Data) chatMsg, _ := room.Receive(msg.Data)
if err := patchAll(); err != nil { err := sse.PatchElementTempl(
chatcomponents.ChatMessage(chatMsg, chatCfg),
datastar.WithSelectorID("snake-chat-history"),
datastar.WithModeAppend(),
)
if err != nil {
return return
} }
} }