From a356546fc706b2ade758e7f42069e6d669330421 Mon Sep 17 00:00:00 2001
From: Owen Jacobson <owen@grimoire.ca>
Date: Wed, 30 Oct 2024 18:50:01 -0400
Subject: Coalesce adjacent messages by the same author into runs, to
 consolidate message display.

---
 ui/lib/components/ActiveChannel.svelte | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

(limited to 'ui/lib/components/ActiveChannel.svelte')

diff --git a/ui/lib/components/ActiveChannel.svelte b/ui/lib/components/ActiveChannel.svelte
index ece9f55..1b23bc1 100644
--- a/ui/lib/components/ActiveChannel.svelte
+++ b/ui/lib/components/ActiveChannel.svelte
@@ -1,10 +1,33 @@
 <script>
     import { messages } from '$lib/store';
     import Message from './Message.svelte';
+    import MessageRun from './MessageRun.svelte';
 
     export let channel = null;
     $: messageList = channel !== null ? $messages.inChannel(channel) : [];
 
+    function *chunkBy(xs, fn) {
+        let chunk;
+        let key;
+        for (let x of xs) {
+            let newKey = fn(x);
+            if (key !== newKey) {
+                if (chunk !== undefined) {
+                    yield [key, chunk];
+                }
+
+                chunk = [x];
+                key = newKey;
+            } else {
+                chunk.push(x);
+            }
+        }
+        if (chunk !== undefined) {
+            yield [key, chunk];
+        }
+    }
+
+
     let container;
     // TODO: eventually, store scroll height/last unread in channel? scroll there?
 
@@ -14,9 +37,9 @@
 </script>
 
 <div class="container" bind:this={container}>
-    {#each messageList as message}
+    {#each chunkBy(messageList, msg => msg.sender) as [sender, messages]}
         <div use:scroll>
-            <Message {...message} />
+            <MessageRun {sender} {messages} />
         </div>
     {/each}
 </div>
-- 
cgit v1.2.3

