incognitolm commited on
Commit
c8ae980
·
1 Parent(s): 8c493a5

Update chat.js

Browse files
Files changed (1) hide show
  1. public/js/chat.js +32 -26
public/js/chat.js CHANGED
@@ -40,12 +40,14 @@ on('ws:connected', () => {
40
 
41
  /**
42
  * Extract a flat array from the tree-based message structure.
43
- * The server stores messages in a tree with versions; each version has a tail.
44
- * This extracts the current branch's messages as a flat array.
45
  */
46
  function extractFlatHistoryFromTree(rootMessage) {
47
  if (!rootMessage) return [];
48
 
 
 
49
  const ensureValidContent = (msg) => {
50
  if (msg.content === undefined || msg.content === null) {
51
  msg.content = '';
@@ -53,31 +55,35 @@ function extractFlatHistoryFromTree(rootMessage) {
53
  return msg;
54
  };
55
 
56
- const history = [ensureValidContent(rootMessage)];
57
- const currentVerIdx = rootMessage.currentVersionIdx ?? 0;
58
-
59
- if (!Array.isArray(rootMessage.versions)) {
60
- return history;
61
- }
62
-
63
- if (currentVerIdx >= rootMessage.versions.length) {
64
- return history;
65
- }
66
-
67
- const currentTail = rootMessage.versions[currentVerIdx]?.tail;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- if (currentTail && Array.isArray(currentTail)) {
70
- const walkTail = (tail) => {
71
- for (const msg of tail) {
72
- history.push(ensureValidContent(msg));
73
- const ver = msg.versions?.[msg.currentVersionIdx ?? 0];
74
- if (ver?.tail && Array.isArray(ver.tail)) {
75
- walkTail(ver.tail);
76
- }
77
- }
78
- };
79
- walkTail(currentTail);
80
- }
81
  return history;
82
  }
83
 
 
40
 
41
  /**
42
  * Extract a flat array from the tree-based message structure.
43
+ * Only follows the currently selected version branch at each level.
44
+ * Each message's version has a tail; only the selected version's tail is included.
45
  */
46
  function extractFlatHistoryFromTree(rootMessage) {
47
  if (!rootMessage) return [];
48
 
49
+ const history = [];
50
+
51
  const ensureValidContent = (msg) => {
52
  if (msg.content === undefined || msg.content === null) {
53
  msg.content = '';
 
55
  return msg;
56
  };
57
 
58
+ // Recursively extract only the currently selected version's branch
59
+ const extractBranch = (message) => {
60
+ history.push(ensureValidContent(message));
61
+
62
+ // Get the currently selected version index for this message
63
+ const currentVersionIdx = message.currentVersionIdx ?? 0;
64
+ const versions = message.versions || [];
65
+
66
+ // Validate version index is in bounds
67
+ if (currentVersionIdx >= versions.length) {
68
+ return;
69
+ }
70
+
71
+ const currentVersion = versions[currentVersionIdx];
72
+ if (!currentVersion) return;
73
+
74
+ // Only process the selected version's tail if it exists and has items
75
+ const tail = currentVersion.tail;
76
+ if (!Array.isArray(tail) || tail.length === 0) {
77
+ return;
78
+ }
79
+
80
+ // Recursively extract each message in the current version's tail
81
+ for (const tailMessage of tail) {
82
+ extractBranch(tailMessage);
83
+ }
84
+ };
85
 
86
+ extractBranch(rootMessage);
 
 
 
 
 
 
 
 
 
 
 
87
  return history;
88
  }
89