File size: 10,334 Bytes
ffcad5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0878a6
ffcad5d
 
 
 
 
d0878a6
 
 
 
 
 
 
 
 
 
 
ffcad5d
 
 
 
 
 
 
 
 
 
 
 
d0878a6
ffcad5d
 
 
 
 
 
 
d0878a6
ffcad5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0878a6
ffcad5d
 
 
 
 
 
 
 
 
 
d0878a6
 
ffcad5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0878a6
 
ffcad5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import React, { useState, useRef, useEffect, useLayoutEffect, useCallback } from 'react';
import { ChevronLeft, ChevronRight } from 'lucide-react';
import MessageBubble from './MessageBubble';
import { computeLayout, GAP, PREFERRED_SLIDE } from '../utils/advisorCarouselLayout';

const findScrollParent = (node) => {
  let el = node?.parentElement;
  while (el) {
    const { overflowY } = window.getComputedStyle(el);
    if (overflowY === 'auto' || overflowY === 'scroll' || overflowY === 'overlay') {
      return el;
    }
    el = el.parentElement;
  }
  return null;
};

/**

 * Show as many advisor answers as fit side-by-side; carousel when they don't.

 * Messages should already be in start-of-stream order (first token first).

 *

 * Controls sit just to the right of the visible answer(s) when that fits.

 * If not, they overlay the last visible card (or sit below) so they stay on-screen.

 * They stay vertically centered in the visible chat pane.

 */
const AdvisorCarousel = ({

  messages = [],

  onReply,

  onExpand,

  onClick,

  onSearchReferences,

  onReferenceSearchOpened,

  userQuestion = '',

  userAvatarId,

  userAvatarOptions,

}) => {
  const [activeIndex, setActiveIndex] = useState(0);
  const [layout, setLayout] = useState({
    visible: 1,
    slideW: PREFERRED_SLIDE,
    cardsWidth: PREFERRED_SLIDE,
    controlsMode: 'none',
  });
  const shellRef = useRef(null);
  const stageRef = useRef(null);
  const controlsColRef = useRef(null);
  const controlsInnerRef = useRef(null);

  const firstMessageId = messages[0]?.id;
  const visibleCount = Math.min(layout.visible, messages.length || 1);
  const maxIndex = Math.max(0, messages.length - visibleCount);
  const showAll = visibleCount >= messages.length && messages.length > 1;
  const showControls = layout.controlsMode !== 'none' && messages.length > visibleCount;

  useEffect(() => {
    setActiveIndex(0);
  }, [firstMessageId]);

  useEffect(() => {
    setActiveIndex((i) => Math.min(i, maxIndex));
  }, [maxIndex]);

  const goPrev = useCallback(() => {
    setActiveIndex((i) => Math.max(0, i - 1));
  }, []);

  const goNext = useCallback(() => {
    setActiveIndex((i) => Math.min(maxIndex, i + 1));
  }, [maxIndex]);

  const measurePane = useCallback(() => {
    const shell = shellRef.current;
    if (!shell) return;
    const available = Math.floor(shell.clientWidth);
    if (available <= 0) return;
    const next = computeLayout(available, messages.length);
    setLayout((prev) => (
      prev.visible === next.visible
      && prev.slideW === next.slideW
      && prev.controlsMode === next.controlsMode
        ? prev
        : next
    ));
  }, [messages.length]);

  const updateControlPosition = useCallback(() => {
    const col = controlsColRef.current;
    const inner = controlsInnerRef.current;
    const stage = stageRef.current;
    const shell = shellRef.current;
    if (!col || !inner || !stage) return;

    if (layout.controlsMode === 'below') {
      inner.style.top = '';
      return;
    }

    const scrollParent = findScrollParent(stage);
    const colRect = col.getBoundingClientRect();
    const viewRect = scrollParent
      ? scrollParent.getBoundingClientRect()
      : { top: 0, bottom: window.innerHeight };

    const inputEl = document.querySelector('.floating-input-area');
    const inputTop = inputEl ? inputEl.getBoundingClientRect().top : viewRect.bottom;
    const viewTop = viewRect.top;
    const viewBottom = Math.min(viewRect.bottom, inputTop);

    const overlapTop = Math.max(colRect.top, viewTop);
    const overlapBottom = Math.min(colRect.bottom, viewBottom);
    const innerH = inner.offsetHeight || 0;
    const colH = col.offsetHeight || 0;

    if (overlapBottom > overlapTop && innerH > 0 && colH > 0) {
      const mid = (overlapTop + overlapBottom) / 2;
      const maxTop = Math.max(0, colH - innerH);
      let top = Math.max(0, Math.min(maxTop, mid - colRect.top - innerH / 2));

      if (shell) {
        const shellRect = shell.getBoundingClientRect();
        const innerTopAbs = colRect.top + top;
        if (innerTopAbs < shellRect.top) {
          top = Math.max(0, shellRect.top - colRect.top);
        }
        const innerBottomAbs = colRect.top + top + innerH;
        if (innerBottomAbs > shellRect.bottom) {
          top = Math.max(0, Math.min(maxTop, shellRect.bottom - colRect.top - innerH));
        }
      }

      inner.style.top = `${Math.round(top)}px`;
    }
  }, [layout.controlsMode]);

  useLayoutEffect(() => {
    measurePane();
    const shell = shellRef.current;
    const ro = typeof ResizeObserver !== 'undefined' ? new ResizeObserver(measurePane) : null;
    if (shell) ro?.observe(shell);
    const scrollParent = shell ? findScrollParent(shell) : null;
    if (scrollParent) ro?.observe(scrollParent);
    const chatArea = shell?.closest('.main-chat-area, .messages-scroll');
    if (chatArea && chatArea !== scrollParent && chatArea !== shell) {
      ro?.observe(chatArea);
    }
    window.addEventListener('resize', measurePane);
    window.visualViewport?.addEventListener('resize', measurePane);
    return () => {
      window.removeEventListener('resize', measurePane);
      window.visualViewport?.removeEventListener('resize', measurePane);
      ro?.disconnect();
    };
  }, [measurePane]);

  useLayoutEffect(() => {
    if (!showControls) return undefined;

    updateControlPosition();
    const raf = window.requestAnimationFrame(() => updateControlPosition());

    const stage = stageRef.current;
    const scrollParent = stage ? findScrollParent(stage) : null;
    const onScrollOrResize = () => updateControlPosition();

    scrollParent?.addEventListener('scroll', onScrollOrResize, { passive: true });
    window.addEventListener('resize', onScrollOrResize);

    const ro = typeof ResizeObserver !== 'undefined'
      ? new ResizeObserver(onScrollOrResize)
      : null;
    if (stage) ro?.observe(stage);
    if (scrollParent) ro?.observe(scrollParent);

    return () => {
      window.cancelAnimationFrame(raf);
      scrollParent?.removeEventListener('scroll', onScrollOrResize);
      window.removeEventListener('resize', onScrollOrResize);
      ro?.disconnect();
    };
  }, [showControls, messages.length, activeIndex, firstMessageId, visibleCount, updateControlPosition]);

  if (messages.length === 1) {
    return (
      <div className="single-response-wide">

        <MessageBubble

          message={messages[0]}

          onReply={onReply}

          onExpand={onExpand}

          onClick={onClick}

          onSearchReferences={onSearchReferences}

          onReferenceSearchOpened={onReferenceSearchOpened}

          userQuestion={userQuestion}

          showReplyButton={true}

          userAvatarId={userAvatarId}

          userAvatarOptions={userAvatarOptions}

        />

      </div>
    );
  }

  const viewportWidth = visibleCount * layout.slideW + GAP * (visibleCount - 1);
  const offset = showAll ? 0 : activeIndex * (layout.slideW + GAP);
  const controlsMode = showControls ? layout.controlsMode : 'none';

  return (
    <div

      className={`advisor-carousel carousel-mode controls-${controlsMode}${showAll ? ' showing-all' : ''}`}

      ref={shellRef}

      data-visible={visibleCount}

      data-controls={controlsMode}

      data-slide-w={layout.slideW}

    >

      <div className="carousel-stage" ref={stageRef}>

        <div

          className="carousel-viewport"

          style={{ width: `${viewportWidth}px` }}

        >

          <div

            className="carousel-track"

            style={{

              gap: `${GAP}px`,

              transform: `translateX(-${offset}px)`,

            }}

          >

            {messages.map((message) => (

              <div

                key={message.id}

                className="carousel-slide"

                style={{ width: `${layout.slideW}px`, flex: `0 0 ${layout.slideW}px` }}

              >

                <MessageBubble

                  message={message}

                  onReply={onReply}

                  onExpand={onExpand}

                  onClick={onClick}

                  onSearchReferences={onSearchReferences}

                  onReferenceSearchOpened={onReferenceSearchOpened}

                  userQuestion={userQuestion}

                  showReplyButton={true}

                  inlineAvatar={true}

                  userAvatarId={userAvatarId}

                  userAvatarOptions={userAvatarOptions}

                />

              </div>

            ))}

          </div>

        </div>



        {showControls && (

          <div className="carousel-controls" ref={controlsColRef}>

            <div className="carousel-controls-inner" ref={controlsInnerRef}>

              <button

                type="button"

                className="carousel-arrow carousel-prev"

                onClick={goPrev}

                disabled={activeIndex === 0}

                aria-label="Previous advisor"

              >

                <ChevronLeft size={28} strokeWidth={2.4} />

              </button>

              <button

                type="button"

                className="carousel-arrow carousel-next"

                onClick={goNext}

                disabled={activeIndex >= maxIndex}

                aria-label="Next advisor"

              >

                <ChevronRight size={28} strokeWidth={2.4} />

              </button>

            </div>

          </div>

        )}

      </div>



      {showControls && (

        <div className="carousel-dots" role="tablist" aria-label="Advisor answers">

          {Array.from({ length: maxIndex + 1 }, (_, i) => (

            <button

              key={messages[i].id}

              type="button"

              className={`carousel-dot ${i === activeIndex ? 'active' : ''}`}

              onClick={() => setActiveIndex(i)}

              aria-label={`Show answers starting at ${i + 1}`}

              aria-selected={i === activeIndex}

              role="tab"

            />

          ))}

        </div>

      )}

    </div>
  );
};

export default AdvisorCarousel;