@ray-js/t-agent-ui-ray 0.1.0-beta-9 → 0.1.0-beta-11

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.
@@ -1,3 +1,4 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
1
2
  import "core-js/modules/es.array.reverse.js";
2
3
  import "core-js/modules/esnext.iterator.constructor.js";
3
4
  import "core-js/modules/esnext.iterator.map.js";
@@ -6,8 +7,12 @@ import './index.less';
6
7
  import { View } from '@ray-js/components';
7
8
  import React, { useMemo, useRef, useState } from 'react';
8
9
  import { ScrollView } from '@ray-js/ray';
10
+ import { generateId } from '@ray-js/t-agent';
11
+ import { isVersionMatch } from '@ray-js/t-agent-plugin-assistant';
9
12
  import MessageRender from '../MessageRender';
10
- import { useAgentMessage, useOnEvent, useSleep } from '../hooks';
13
+ import { useAgentMessage, useOnEvent } from '../hooks';
14
+ import { useDebouncedFn } from '../hooks/useDebouncedFn';
15
+ import { useSleep } from '../hooks/useSleep';
11
16
  export default function MessageList(props) {
12
17
  const {
13
18
  className,
@@ -16,11 +21,30 @@ export default function MessageList(props) {
16
21
  const {
17
22
  messages
18
23
  } = useAgentMessage();
24
+ const [id] = useState(() => "ScrollView-".concat(generateId()));
19
25
  const [scrollAnimation, setScrollAnimation] = useState(false);
20
26
  // 最大整数位数,用于强制滚动到底部,收到连续 scrollToBottom 时,每 10 毫秒更新一次
21
27
  const [scrollTop, setScrollTop] = useState(() => 1000000000000000 + Math.floor(Date.now() / 10));
22
28
  const followNewMessageRef = useRef(true);
23
29
  const sleep = useSleep();
30
+ const canIUse = useMemo(() => {
31
+ // @ts-ignore
32
+ const {
33
+ containerVersion
34
+ } = ty.getSystemInfoSync();
35
+ return {
36
+ // 在 2.27.2 版本之前,pageScrollTo 在某些场景不生效
37
+ scroll: containerVersion && isVersionMatch('>=2.27.2', containerVersion)
38
+ };
39
+ }, []);
40
+ const scroll = useDebouncedFn(animation => {
41
+ // @ts-ignore
42
+ ty.pageScrollTo({
43
+ scrollTop: 0,
44
+ duration: animation ? 100 : 1,
45
+ selector: "#".concat(id)
46
+ });
47
+ }, 100);
24
48
 
25
49
  // 强制滚动到底部
26
50
  useOnEvent('scrollToBottom', _ref => {
@@ -32,24 +56,32 @@ export default function MessageList(props) {
32
56
  if (follow && !followNewMessageRef.current) {
33
57
  return;
34
58
  }
35
- sleep(100).then(() => {
36
- setScrollAnimation(!!animation);
37
- const top = 1000000000000000 + Math.floor(Date.now() / 10);
38
- if (top !== scrollTop) {
39
- setScrollTop(top);
40
- }
41
- });
59
+ if (canIUse.scroll) {
60
+ scroll(animation);
61
+ } else {
62
+ sleep(100).then(() => {
63
+ setScrollAnimation(!!animation);
64
+ const top = 1000000000000000 + Math.floor(Date.now() / 10);
65
+ if (top !== scrollTop) {
66
+ setScrollTop(top);
67
+ }
68
+ });
69
+ }
42
70
  });
43
71
 
44
72
  // 使用翻转列表,让滚动和新消息出现在第一条,这样可以避免滚动到底部时的闪烁
45
73
  const reversed = useMemo(() => [...messages].reverse(), [messages]);
74
+ const scrollProps = canIUse.scroll ? {} : {
75
+ scrollWithAnimation: scrollAnimation,
76
+ scrollTop
77
+ };
46
78
  return /*#__PURE__*/React.createElement(View, {
47
79
  className: "t-agent-message-list",
48
80
  "data-testid": "t-agent-message-list"
49
- }, /*#__PURE__*/React.createElement(ScrollView, {
81
+ }, /*#__PURE__*/React.createElement(ScrollView, _extends({
50
82
  className: "".concat(className || '', " t-agent-message-list-scroll"),
51
- scrollWithAnimation: scrollAnimation,
52
- scrollTop: scrollTop,
83
+ id: id
84
+ }, scrollProps, {
53
85
  refresherTriggered: false,
54
86
  enableFlex: true,
55
87
  scrollY: true,
@@ -57,7 +89,7 @@ export default function MessageList(props) {
57
89
  // 使用了 flex-direction: column-reverse,所以滚动到顶部时,scrollTop = 0
58
90
  followNewMessageRef.current = event.detail.scrollTop > -10;
59
91
  }
60
- }, /*#__PURE__*/React.createElement(View, {
92
+ }), /*#__PURE__*/React.createElement(View, {
61
93
  className: "t-agent-message-list-padding"
62
94
  }), reversed.map((msg, index) => {
63
95
  let side = roleSide === null || roleSide === void 0 ? void 0 : roleSide[msg.role];
@@ -1,7 +1,6 @@
1
1
  export * from './context';
2
2
  export * from './useAttachmentInput';
3
3
  export * from './useAsrInput';
4
- export * from './useSleep';
5
4
  export * from './useIsUnmounted';
6
5
  export * from './useLongPress';
7
6
  export * from './useTranslate';
@@ -1,7 +1,6 @@
1
1
  export * from './context';
2
2
  export * from './useAttachmentInput';
3
3
  export * from './useAsrInput';
4
- export * from './useSleep';
5
4
  export * from './useIsUnmounted';
6
5
  export * from './useLongPress';
7
6
  export * from './useTranslate';
@@ -0,0 +1 @@
1
+ export declare function useDebouncedFn<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void;
@@ -0,0 +1,32 @@
1
+ import "core-js/modules/web.dom-collections.iterator.js";
2
+ import { useRef, useEffect, useCallback } from 'react';
3
+ export function useDebouncedFn(fn, delay) {
4
+ const timer = useRef();
5
+ const fnRef = useRef(fn);
6
+
7
+ // 保证引用是最新的
8
+ useEffect(() => {
9
+ fnRef.current = fn;
10
+ });
11
+ const debounced = useCallback(function () {
12
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
13
+ args[_key] = arguments[_key];
14
+ }
15
+ if (timer.current) {
16
+ clearTimeout(timer.current);
17
+ }
18
+ timer.current = setTimeout(() => {
19
+ fnRef.current(...args);
20
+ }, delay);
21
+ }, [delay]);
22
+
23
+ // 卸载时清理
24
+ useEffect(() => {
25
+ return () => {
26
+ if (timer.current) {
27
+ clearTimeout(timer.current);
28
+ }
29
+ };
30
+ }, []);
31
+ return debounced;
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent-ui-ray",
3
- "version": "0.1.0-beta-9",
3
+ "version": "0.1.0-beta-11",
4
4
  "author": "Tuya.inc",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -41,5 +41,5 @@
41
41
  "@types/echarts": "^4.9.22",
42
42
  "@types/markdown-it": "^14.1.1"
43
43
  },
44
- "gitHead": "1cebc0a6223565deb09fc16d17c5ff6c59e3e008"
44
+ "gitHead": "aaa1b649b8773de00e74693b1fcbd584431323e4"
45
45
  }