All files / apps/chatbot/src/utils blockGrouping.ts

100% Statements 14/14
100% Branches 4/4
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26            3x 15x 15x 15x 17x 12x 12x     15x 15x   2x 2x     15x 15x    
import { BUBBLE_BLOCK_TYPES } from "@/constants/chat";
 
import { Block } from "@/types/chat";
 
export type BlockGroup = { kind: "bubble"; blocks: Block[] } | { kind: "standalone"; block: Block };
 
export const groupBlocks = (blocks: Block[]): BlockGroup[] => {
  const result: BlockGroup[] = [];
  let currentBubble: Block[] = [];
  const flushBubble = () => {
    if (currentBubble.length > 0) {
      result.push({ kind: "bubble", blocks: [...currentBubble] });
      currentBubble = [];
    }
  };
  for (const block of blocks) {
    if (BUBBLE_BLOCK_TYPES.includes(block.type)) currentBubble.push(block);
    else {
      flushBubble();
      result.push({ kind: "standalone", block });
    }
  }
  flushBubble();
  return result;
};