@adatechnology/conversations-ui 0.14.1 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +39 -12
- package/dist/styles.css +23 -0
- package/package.json +2 -2
- package/src/hooks/useLoadOlderMessagesScroll.test.ts +26 -11
- package/src/hooks/useLoadOlderMessagesScroll.ts +47 -17
- package/src/styles.css +19 -0
- package/src/workspace/ConversationPane.tsx +18 -4
package/dist/index.js
CHANGED
|
@@ -5653,9 +5653,19 @@ import { useCallback as useCallback14, useEffect as useEffect18, useRef as useRe
|
|
|
5653
5653
|
|
|
5654
5654
|
// src/hooks/useLoadOlderMessagesScroll.ts
|
|
5655
5655
|
import { useCallback as useCallback11, useLayoutEffect as useLayoutEffect2, useRef as useRef13 } from "react";
|
|
5656
|
-
var
|
|
5657
|
-
function
|
|
5658
|
-
return
|
|
5656
|
+
var PREFETCH_MINIMUM_DISTANCE_PX = 600;
|
|
5657
|
+
function computePrefetchDistance(clientHeight) {
|
|
5658
|
+
return Math.max(PREFETCH_MINIMUM_DISTANCE_PX, clientHeight);
|
|
5659
|
+
}
|
|
5660
|
+
function shouldPrefetchOlderMessages(scrollTop, clientHeight) {
|
|
5661
|
+
return scrollTop <= computePrefetchDistance(clientHeight);
|
|
5662
|
+
}
|
|
5663
|
+
function computeScrollTopAfterPrepend(currentScrollTop, anchorOffsetTopBefore, anchorOffsetTopAfter) {
|
|
5664
|
+
return currentScrollTop + (anchorOffsetTopAfter - anchorOffsetTopBefore);
|
|
5665
|
+
}
|
|
5666
|
+
var MESSAGE_ID_ATTRIBUTE = "data-message-id";
|
|
5667
|
+
function findMessageElement(container, messageId) {
|
|
5668
|
+
return container.querySelector(`[${MESSAGE_ID_ATTRIBUTE}="${CSS.escape(messageId)}"]`);
|
|
5659
5669
|
}
|
|
5660
5670
|
function useLoadOlderMessagesScroll({
|
|
5661
5671
|
containerRef,
|
|
@@ -5664,18 +5674,20 @@ function useLoadOlderMessagesScroll({
|
|
|
5664
5674
|
loading,
|
|
5665
5675
|
onLoadOlder
|
|
5666
5676
|
}) {
|
|
5667
|
-
const
|
|
5677
|
+
const pendingAnchorRef = useRef13(null);
|
|
5668
5678
|
const previousOldestMessageIdRef = useRef13(oldestMessageId);
|
|
5669
5679
|
const triggerLoadOlder = useCallback11(() => {
|
|
5670
5680
|
if (loading || !hasMore) return;
|
|
5671
5681
|
const container = containerRef.current;
|
|
5672
5682
|
if (!container) return;
|
|
5673
|
-
|
|
5683
|
+
const anchorElement = oldestMessageId ? findMessageElement(container, oldestMessageId) : null;
|
|
5684
|
+
pendingAnchorRef.current = oldestMessageId && anchorElement ? { messageId: oldestMessageId, offsetTop: anchorElement.offsetTop } : null;
|
|
5674
5685
|
onLoadOlder();
|
|
5675
|
-
}, [containerRef, hasMore, loading, onLoadOlder]);
|
|
5686
|
+
}, [containerRef, hasMore, loading, oldestMessageId, onLoadOlder]);
|
|
5676
5687
|
const handleScroll = useCallback11(
|
|
5677
5688
|
(event) => {
|
|
5678
|
-
|
|
5689
|
+
const target = event.currentTarget;
|
|
5690
|
+
if (!shouldPrefetchOlderMessages(target.scrollTop, target.clientHeight)) return;
|
|
5679
5691
|
triggerLoadOlder();
|
|
5680
5692
|
},
|
|
5681
5693
|
[triggerLoadOlder]
|
|
@@ -5684,10 +5696,12 @@ function useLoadOlderMessagesScroll({
|
|
|
5684
5696
|
const idChanged = previousOldestMessageIdRef.current !== oldestMessageId;
|
|
5685
5697
|
previousOldestMessageIdRef.current = oldestMessageId;
|
|
5686
5698
|
const container = containerRef.current;
|
|
5687
|
-
const pending =
|
|
5699
|
+
const pending = pendingAnchorRef.current;
|
|
5688
5700
|
if (!idChanged || !container || !pending) return;
|
|
5689
|
-
|
|
5690
|
-
|
|
5701
|
+
pendingAnchorRef.current = null;
|
|
5702
|
+
const anchorElement = findMessageElement(container, pending.messageId);
|
|
5703
|
+
if (!anchorElement) return;
|
|
5704
|
+
container.scrollTop = computeScrollTopAfterPrepend(container.scrollTop, pending.offsetTop, anchorElement.offsetTop);
|
|
5691
5705
|
}, [oldestMessageId, containerRef]);
|
|
5692
5706
|
return { handleScroll, triggerLoadOlder };
|
|
5693
5707
|
}
|
|
@@ -6276,11 +6290,24 @@ function ConversationPane({
|
|
|
6276
6290
|
},
|
|
6277
6291
|
className: "cv-workspace-transcript",
|
|
6278
6292
|
children: [
|
|
6279
|
-
hasMoreOlderMessages
|
|
6293
|
+
hasMoreOlderMessages && loadingOlderMessages ? /* @__PURE__ */ jsxs28(
|
|
6294
|
+
"div",
|
|
6295
|
+
{
|
|
6296
|
+
className: "cv-workspace-load-older cv-workspace-load-older--loading",
|
|
6297
|
+
role: "status",
|
|
6298
|
+
"aria-live": "polite",
|
|
6299
|
+
"aria-label": labels.loadingOlderMessages,
|
|
6300
|
+
children: [
|
|
6301
|
+
/* @__PURE__ */ jsx32("span", { className: "cv-skeleton-line cv-skeleton-bubble cv-skeleton-bubble--incoming", "aria-hidden": "true" }),
|
|
6302
|
+
/* @__PURE__ */ jsx32("span", { className: "cv-skeleton-line cv-skeleton-bubble cv-skeleton-bubble--outgoing", "aria-hidden": "true" }),
|
|
6303
|
+
/* @__PURE__ */ jsx32("span", { className: "cv-skeleton-line cv-skeleton-bubble cv-skeleton-bubble--incoming cv-skeleton-bubble--short", "aria-hidden": "true" })
|
|
6304
|
+
]
|
|
6305
|
+
}
|
|
6306
|
+
) : hasMoreOlderMessages ? /* @__PURE__ */ jsx32("div", { className: "cv-workspace-load-older", children: /* @__PURE__ */ jsx32("button", { type: "button", onClick: loadOlder.triggerLoadOlder, children: labels.loadOlderMessages }) }) : null,
|
|
6280
6307
|
messages.map((message, index) => {
|
|
6281
6308
|
const previous = index > 0 ? messages[index - 1] : void 0;
|
|
6282
6309
|
const startsNewDay = !previous || new Date(message.timestamp).toDateString() !== new Date(previous.timestamp).toDateString();
|
|
6283
|
-
return /* @__PURE__ */ jsxs28("div", { children: [
|
|
6310
|
+
return /* @__PURE__ */ jsxs28("div", { "data-message-id": message.id, children: [
|
|
6284
6311
|
startsNewDay ? /* @__PURE__ */ jsx32(DateDivider, { iso: message.timestamp }) : null,
|
|
6285
6312
|
/* @__PURE__ */ jsx32(
|
|
6286
6313
|
MessageBubble,
|
package/dist/styles.css
CHANGED
|
@@ -484,6 +484,7 @@
|
|
|
484
484
|
flex: 1;
|
|
485
485
|
min-height: 0;
|
|
486
486
|
overflow-y: auto;
|
|
487
|
+
overflow-anchor: none;
|
|
487
488
|
padding: 0.75rem 1rem;
|
|
488
489
|
}
|
|
489
490
|
.cv-workspace-failure__link {
|
|
@@ -542,6 +543,28 @@
|
|
|
542
543
|
.cv-workspace-load-older button:disabled {
|
|
543
544
|
opacity: 0.6;
|
|
544
545
|
}
|
|
546
|
+
.cv-workspace-load-older--loading {
|
|
547
|
+
flex-direction: column;
|
|
548
|
+
align-items: stretch;
|
|
549
|
+
gap: 0.5rem;
|
|
550
|
+
}
|
|
551
|
+
.cv-skeleton-bubble {
|
|
552
|
+
display: block;
|
|
553
|
+
height: 2.5rem;
|
|
554
|
+
width: 55%;
|
|
555
|
+
border-radius: 0.75rem;
|
|
556
|
+
}
|
|
557
|
+
.cv-skeleton-bubble--incoming {
|
|
558
|
+
align-self: flex-start;
|
|
559
|
+
}
|
|
560
|
+
.cv-skeleton-bubble--outgoing {
|
|
561
|
+
align-self: flex-end;
|
|
562
|
+
width: 40%;
|
|
563
|
+
}
|
|
564
|
+
.cv-skeleton-bubble--short {
|
|
565
|
+
width: 30%;
|
|
566
|
+
height: 1.75rem;
|
|
567
|
+
}
|
|
545
568
|
.dark .cv-workspace-notice {
|
|
546
569
|
border-top-color: #374151;
|
|
547
570
|
background: #1f2937;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adatechnology/conversations-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "WhatsApp conversation UI components — parametrizável por endpoint, tema e feature flags",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"lucide-react": "^1.21.0",
|
|
37
37
|
"tailwind-merge": "^3.6.0",
|
|
38
38
|
"@adatechnology/conversation-contracts": "0.7.0",
|
|
39
|
-
"@adatechnology/meta-whatsapp-contracts": "0.6.
|
|
39
|
+
"@adatechnology/meta-whatsapp-contracts": "0.6.1"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@xyflow/react": "^12",
|
|
@@ -1,21 +1,36 @@
|
|
|
1
1
|
import { describe, expect, it } from 'bun:test'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
computePrefetchDistance,
|
|
5
|
+
computeScrollTopAfterPrepend,
|
|
6
|
+
shouldPrefetchOlderMessages,
|
|
7
|
+
} from './useLoadOlderMessagesScroll'
|
|
4
8
|
|
|
5
9
|
describe('computeScrollTopAfterPrepend', () => {
|
|
6
|
-
it('
|
|
7
|
-
//
|
|
8
|
-
|
|
9
|
-
expect(computeScrollTopAfterPrepend(before, 2800)).toBe(1100)
|
|
10
|
+
it('soma ao scrollTop atual o quanto a mensagem-âncora desceu com a página prependida', () => {
|
|
11
|
+
// Âncora estava a 40px do topo do conteúdo; a página anterior a empurrou para 840px.
|
|
12
|
+
expect(computeScrollTopAfterPrepend(300, 40, 840)).toBe(1100)
|
|
10
13
|
})
|
|
11
14
|
|
|
12
|
-
it('não
|
|
13
|
-
|
|
14
|
-
expect(computeScrollTopAfterPrepend(
|
|
15
|
+
it('usa a posição atual, não a do disparo: o operador pode ter rolado enquanto a página chegava', () => {
|
|
16
|
+
// Disparou em 500px, rolou até 120px durante a busca; a página somou 900px por cima da âncora.
|
|
17
|
+
expect(computeScrollTopAfterPrepend(120, 60, 960)).toBe(1020)
|
|
15
18
|
})
|
|
16
19
|
|
|
17
|
-
it('
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
it('não desloca a rolagem quando nada entrou por cima da âncora', () => {
|
|
21
|
+
expect(computeScrollTopAfterPrepend(300, 40, 40)).toBe(300)
|
|
22
|
+
})
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
describe('prefetch de mensagens anteriores', () => {
|
|
26
|
+
it('começa a buscar a cerca de uma tela do topo', () => {
|
|
27
|
+
expect(computePrefetchDistance(900)).toBe(900)
|
|
28
|
+
expect(shouldPrefetchOlderMessages(850, 900)).toBe(true)
|
|
29
|
+
expect(shouldPrefetchOlderMessages(950, 900)).toBe(false)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('nunca usa menos que o piso em telas baixas', () => {
|
|
33
|
+
expect(computePrefetchDistance(300)).toBe(600)
|
|
34
|
+
expect(shouldPrefetchOlderMessages(550, 300)).toBe(true)
|
|
20
35
|
})
|
|
21
36
|
})
|
|
@@ -1,20 +1,45 @@
|
|
|
1
1
|
import { useCallback, useLayoutEffect, useRef, type RefObject, type UIEvent } from 'react'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* A busca da página anterior começa antes de o operador chegar ao topo: a cerca de uma tela de
|
|
5
|
+
* distância (nunca menos que este piso). Esperar o topo deixava o operador parado olhando o
|
|
6
|
+
* esqueleto a cada 50 mensagens; com a folga, a página costuma chegar enquanto ele ainda lê.
|
|
7
7
|
*/
|
|
8
|
-
const
|
|
8
|
+
const PREFETCH_MINIMUM_DISTANCE_PX = 600
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
/** Distância do topo a partir da qual a página anterior é pedida. */
|
|
11
|
+
export function computePrefetchDistance(clientHeight: number): number {
|
|
12
|
+
return Math.max(PREFETCH_MINIMUM_DISTANCE_PX, clientHeight)
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
export function shouldPrefetchOlderMessages(scrollTop: number, clientHeight: number): boolean {
|
|
16
|
+
return scrollTop <= computePrefetchDistance(clientHeight)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A mensagem mais antiga já exibida serve de âncora: o que entra por cima a empurra para baixo, e
|
|
21
|
+
* somar esse deslocamento ao `scrollTop` *atual* devolve o operador exatamente à linha que estava
|
|
22
|
+
* lendo — inclusive se ele continuou rolando enquanto a página chegava (o que o prefetch torna
|
|
23
|
+
* comum) ou se uma mensagem nova entrou no fim da conversa nesse meio tempo.
|
|
24
|
+
*/
|
|
25
|
+
export function computeScrollTopAfterPrepend(
|
|
26
|
+
currentScrollTop: number,
|
|
27
|
+
anchorOffsetTopBefore: number,
|
|
28
|
+
anchorOffsetTopAfter: number,
|
|
29
|
+
): number {
|
|
30
|
+
return currentScrollTop + (anchorOffsetTopAfter - anchorOffsetTopBefore)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Atributo que identifica cada mensagem no transcript — é por ele que a âncora é reencontrada. */
|
|
34
|
+
export const MESSAGE_ID_ATTRIBUTE = 'data-message-id'
|
|
35
|
+
|
|
36
|
+
type PendingAnchor = {
|
|
37
|
+
readonly messageId: string
|
|
38
|
+
readonly offsetTop: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function findMessageElement(container: HTMLElement, messageId: string): HTMLElement | null {
|
|
42
|
+
return container.querySelector<HTMLElement>(`[${MESSAGE_ID_ATTRIBUTE}="${CSS.escape(messageId)}"]`)
|
|
18
43
|
}
|
|
19
44
|
|
|
20
45
|
export type UseLoadOlderMessagesScrollParams = {
|
|
@@ -40,7 +65,7 @@ export function useLoadOlderMessagesScroll({
|
|
|
40
65
|
loading,
|
|
41
66
|
onLoadOlder,
|
|
42
67
|
}: UseLoadOlderMessagesScrollParams): UseLoadOlderMessagesScrollResult {
|
|
43
|
-
const
|
|
68
|
+
const pendingAnchorRef = useRef<PendingAnchor | null>(null)
|
|
44
69
|
const previousOldestMessageIdRef = useRef(oldestMessageId)
|
|
45
70
|
|
|
46
71
|
const triggerLoadOlder = useCallback(() => {
|
|
@@ -48,13 +73,16 @@ export function useLoadOlderMessagesScroll({
|
|
|
48
73
|
const container = containerRef.current
|
|
49
74
|
if (!container) return
|
|
50
75
|
|
|
51
|
-
|
|
76
|
+
const anchorElement = oldestMessageId ? findMessageElement(container, oldestMessageId) : null
|
|
77
|
+
pendingAnchorRef.current =
|
|
78
|
+
oldestMessageId && anchorElement ? { messageId: oldestMessageId, offsetTop: anchorElement.offsetTop } : null
|
|
52
79
|
onLoadOlder()
|
|
53
|
-
}, [containerRef, hasMore, loading, onLoadOlder])
|
|
80
|
+
}, [containerRef, hasMore, loading, oldestMessageId, onLoadOlder])
|
|
54
81
|
|
|
55
82
|
const handleScroll = useCallback(
|
|
56
83
|
(event: UIEvent<HTMLDivElement>) => {
|
|
57
|
-
|
|
84
|
+
const target = event.currentTarget
|
|
85
|
+
if (!shouldPrefetchOlderMessages(target.scrollTop, target.clientHeight)) return
|
|
58
86
|
triggerLoadOlder()
|
|
59
87
|
},
|
|
60
88
|
[triggerLoadOlder],
|
|
@@ -65,11 +93,13 @@ export function useLoadOlderMessagesScroll({
|
|
|
65
93
|
previousOldestMessageIdRef.current = oldestMessageId
|
|
66
94
|
|
|
67
95
|
const container = containerRef.current
|
|
68
|
-
const pending =
|
|
96
|
+
const pending = pendingAnchorRef.current
|
|
69
97
|
if (!idChanged || !container || !pending) return
|
|
70
98
|
|
|
71
|
-
|
|
72
|
-
|
|
99
|
+
pendingAnchorRef.current = null
|
|
100
|
+
const anchorElement = findMessageElement(container, pending.messageId)
|
|
101
|
+
if (!anchorElement) return
|
|
102
|
+
container.scrollTop = computeScrollTopAfterPrepend(container.scrollTop, pending.offsetTop, anchorElement.offsetTop)
|
|
73
103
|
}, [oldestMessageId, containerRef])
|
|
74
104
|
|
|
75
105
|
return { handleScroll, triggerLoadOlder }
|
package/src/styles.css
CHANGED
|
@@ -432,6 +432,9 @@
|
|
|
432
432
|
flex: 1;
|
|
433
433
|
min-height: 0;
|
|
434
434
|
overflow-y: auto;
|
|
435
|
+
/* A SDK reposiciona a rolagem sozinha ao prepender o histórico (useLoadOlderMessagesScroll). Com a
|
|
436
|
+
ancoragem do navegador ligada, os dois ajustes se somavam (Firefox) e o operador pulava. */
|
|
437
|
+
overflow-anchor: none;
|
|
435
438
|
padding: 0.75rem 1rem;
|
|
436
439
|
}
|
|
437
440
|
|
|
@@ -501,6 +504,22 @@
|
|
|
501
504
|
opacity: 0.6;
|
|
502
505
|
}
|
|
503
506
|
|
|
507
|
+
.cv-workspace-load-older--loading {
|
|
508
|
+
flex-direction: column;
|
|
509
|
+
align-items: stretch;
|
|
510
|
+
gap: 0.5rem;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
.cv-skeleton-bubble {
|
|
514
|
+
display: block;
|
|
515
|
+
height: 2.5rem;
|
|
516
|
+
width: 55%;
|
|
517
|
+
border-radius: 0.75rem;
|
|
518
|
+
}
|
|
519
|
+
.cv-skeleton-bubble--incoming { align-self: flex-start; }
|
|
520
|
+
.cv-skeleton-bubble--outgoing { align-self: flex-end; width: 40%; }
|
|
521
|
+
.cv-skeleton-bubble--short { width: 30%; height: 1.75rem; }
|
|
522
|
+
|
|
504
523
|
.dark .cv-workspace-notice {
|
|
505
524
|
border-top-color: #374151;
|
|
506
525
|
background: #1f2937;
|
|
@@ -371,10 +371,24 @@ export function ConversationPane({
|
|
|
371
371
|
}}
|
|
372
372
|
className="cv-workspace-transcript"
|
|
373
373
|
>
|
|
374
|
-
{
|
|
374
|
+
{/* Carregando: balões-esqueleto no lugar do botão, alternando cliente/bot como numa conversa
|
|
375
|
+
real — o texto de um botão desabilitado passava despercebido e o operador achava que a
|
|
376
|
+
conversa acabava ali. Fora do carregamento o botão fica como alternativa à rolagem. */}
|
|
377
|
+
{hasMoreOlderMessages && loadingOlderMessages ? (
|
|
378
|
+
<div
|
|
379
|
+
className="cv-workspace-load-older cv-workspace-load-older--loading"
|
|
380
|
+
role="status"
|
|
381
|
+
aria-live="polite"
|
|
382
|
+
aria-label={labels.loadingOlderMessages}
|
|
383
|
+
>
|
|
384
|
+
<span className="cv-skeleton-line cv-skeleton-bubble cv-skeleton-bubble--incoming" aria-hidden="true" />
|
|
385
|
+
<span className="cv-skeleton-line cv-skeleton-bubble cv-skeleton-bubble--outgoing" aria-hidden="true" />
|
|
386
|
+
<span className="cv-skeleton-line cv-skeleton-bubble cv-skeleton-bubble--incoming cv-skeleton-bubble--short" aria-hidden="true" />
|
|
387
|
+
</div>
|
|
388
|
+
) : hasMoreOlderMessages ? (
|
|
375
389
|
<div className="cv-workspace-load-older">
|
|
376
|
-
<button type="button" onClick={loadOlder.triggerLoadOlder}
|
|
377
|
-
{
|
|
390
|
+
<button type="button" onClick={loadOlder.triggerLoadOlder}>
|
|
391
|
+
{labels.loadOlderMessages}
|
|
378
392
|
</button>
|
|
379
393
|
</div>
|
|
380
394
|
) : null}
|
|
@@ -384,7 +398,7 @@ export function ConversationPane({
|
|
|
384
398
|
!previous || new Date(message.timestamp).toDateString() !== new Date(previous.timestamp).toDateString()
|
|
385
399
|
|
|
386
400
|
return (
|
|
387
|
-
<div key={message.id}>
|
|
401
|
+
<div key={message.id} data-message-id={message.id}>
|
|
388
402
|
{startsNewDay ? <DateDivider iso={message.timestamp} /> : null}
|
|
389
403
|
<MessageBubble
|
|
390
404
|
message={message}
|