listpage-next 0.0.90 → 0.0.91

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.
@@ -3,18 +3,22 @@ import { useState } from "react";
3
3
  import { Button, Divider, Input } from "antd";
4
4
  import { ChatInputBottom, ChatInputContent, ChatInputPanel } from "./styles.js";
5
5
  import { ArrowUpOutlined, PauseCircleTwoTone } from "@ant-design/icons";
6
- import { useMessageContext } from "../../context/index.js";
6
+ import { useChatClientContext, useMessageContext } from "../../context/index.js";
7
7
  const ChatSender = (props)=>{
8
8
  const { placeholder, extra, style, className } = props;
9
9
  const { isRequesting, sendMessage } = useMessageContext();
10
10
  const [inputValue, setInputValue] = useState("");
11
+ const { bubbleListRef } = useChatClientContext();
11
12
  const handleClick = async ()=>{
12
13
  if (isRequesting) return;
13
14
  sendMessage({
14
15
  role: 'user',
15
16
  content: inputValue.trim()
17
+ }, ()=>{
18
+ bubbleListRef.current?.scrollToBottom('instant');
16
19
  });
17
20
  setInputValue("");
21
+ bubbleListRef.current?.scrollToBottom('instant');
18
22
  };
19
23
  const handleKeyDown = (e)=>{
20
24
  if ('Enter' === e.key && !e.shiftKey) {
@@ -2,23 +2,26 @@ import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useImperativeHandle, useRef } from "react";
3
3
  import { styled } from "styled-components";
4
4
  import { useChatClientContext, useMessageContext } from "../../context/index.js";
5
- import { useMount } from "ahooks";
6
5
  const BubbleList = (props)=>{
7
6
  const { styles } = props;
8
7
  const containerRef = useRef(null);
9
8
  const { render, bubbleListRef } = useChatClientContext();
10
9
  const { messages } = useMessageContext();
11
- const scrollToBottom = ()=>{
10
+ const scrollToBottom = (behavior = "smooth")=>{
12
11
  containerRef.current?.scrollTo({
13
12
  top: containerRef.current.scrollHeight,
14
- behavior: "smooth"
13
+ behavior
15
14
  });
16
15
  };
17
- useMount(()=>{
18
- scrollToBottom();
19
- });
16
+ const scrollToBottomWhenAtBottom = (behavior = "instant")=>{
17
+ const ele = containerRef.current;
18
+ if (ele) {
19
+ if (Math.abs(ele.scrollHeight - ele.scrollTop - ele.clientHeight) < 10) scrollToBottom(behavior);
20
+ }
21
+ };
20
22
  useImperativeHandle(bubbleListRef, ()=>({
21
- scrollToBottom
23
+ scrollToBottom,
24
+ scrollToBottomWhenAtBottom
22
25
  }), [
23
26
  scrollToBottom
24
27
  ]);
@@ -6,6 +6,7 @@ import { BubbleList } from "./BubbleList.js";
6
6
  import { MessageProvider, useChatClientContext } from "../../context/index.js";
7
7
  import { PageLoading } from "../../../../components/Page/components/Loading/index.js";
8
8
  import { Container } from "./styles.js";
9
+ import { useEffect } from "react";
9
10
  const ClientContentComponent = (props)=>{
10
11
  const { style, className, headerProps, footerProps, bubbleProps } = props;
11
12
  return /*#__PURE__*/ jsxs(Container, {
@@ -25,8 +26,15 @@ const ClientContentComponent = (props)=>{
25
26
  });
26
27
  };
27
28
  const ClientContent = (props)=>{
28
- const { request } = useChatClientContext();
29
+ const { request, bubbleListRef } = useChatClientContext();
29
30
  const { loading, data } = useRequest(request.getMessages);
31
+ useEffect(()=>{
32
+ if (false === loading) setTimeout(()=>{
33
+ bubbleListRef.current?.scrollToBottom();
34
+ }, 200);
35
+ }, [
36
+ loading
37
+ ]);
30
38
  if (loading) return /*#__PURE__*/ jsx(Container, {
31
39
  style: props.style,
32
40
  className: props.className,
@@ -1,6 +1,7 @@
1
1
  import { ReactNode } from "react";
2
2
  import { InfiniteListProps, InfiniteListRef, RenderActions } from "../../../components/InfiniteList";
3
3
  import { BaseMessageInfo, Message, SendMessageRequest } from "./message";
4
+ import { BubbleListRef } from "./useBubbleListRef";
4
5
  export type ChatRequest<SessionData = any, MessageInfo extends BaseMessageInfo = any> = {
5
6
  getConversations: InfiniteListProps<SessionData>['request'];
6
7
  getMessages: () => Promise<MessageInfo[]>;
@@ -17,7 +18,7 @@ export interface ChatClientContextProps<SessionData = any, MessageInfo extends B
17
18
  showUpdateTitleModal: (title: string, onSubmit?: (title: string) => any) => void;
18
19
  conversationListRef: InfiniteListRef<SessionData>;
19
20
  conversationAction: Partial<RenderActions<SessionData>>;
20
- bubbleListRef: any;
21
+ bubbleListRef: BubbleListRef;
21
22
  }
22
23
  export interface ChatClientProviderProps<SessionData = any, MessageInfo extends BaseMessageInfo = any> {
23
24
  children: ReactNode;
@@ -14,7 +14,7 @@ export type Message<MessageInfo extends BaseMessageInfo = any> = {
14
14
  };
15
15
  export type MessageContextValue<MessageInfo extends BaseMessageInfo = any> = {
16
16
  messages: Message<MessageInfo>[];
17
- sendMessage: (info: MessageInfo) => void;
17
+ sendMessage: (info: MessageInfo, callback?: (info: MessageInfo) => void) => void;
18
18
  isRequesting: boolean;
19
19
  };
20
20
  export type SendMessageRequest<MessageInfo extends BaseMessageInfo = any> = (info: MessageInfo, callbacks: {
@@ -67,7 +67,7 @@ const MessageProvider = ({ initialMessages, children, request })=>{
67
67
  message.id = info.id;
68
68
  }
69
69
  }, []);
70
- const sendMessage = useCallback((info)=>{
70
+ const sendMessage = useCallback((info, onUpdate)=>{
71
71
  setIsRequesting(true);
72
72
  createUserMessage(info);
73
73
  setMessages([
@@ -89,6 +89,7 @@ const MessageProvider = ({ initialMessages, children, request })=>{
89
89
  setMessages([
90
90
  ...messagesRef.current
91
91
  ]);
92
+ onUpdate?.(info);
92
93
  },
93
94
  onSuccess: (id, info)=>{
94
95
  completeAssistantMessage(id, info);
@@ -1,9 +1,11 @@
1
- import { Ref } from "react";
1
+ import { RefObject } from "react";
2
2
  export type BubbleListAction = {
3
- scrollToBottom: () => void;
3
+ scrollToBottom: (behavior?: ScrollBehavior) => void;
4
+ scrollToBottomWhenAtBottom: (behavior?: ScrollBehavior) => void;
4
5
  };
5
- export type BubbleListRef = Ref<BubbleListAction>;
6
+ export type BubbleListRef = RefObject<BubbleListAction>;
6
7
  export declare function useBubbleListRef(): {
7
- scrollToBottom: () => void;
8
- bubbleListRef: import("react").RefObject<BubbleListAction>;
8
+ scrollToBottom: (behavior?: ScrollBehavior) => void;
9
+ scrollToBottomWhenAtBottom: (behavior?: ScrollBehavior) => void;
10
+ bubbleListRef: RefObject<BubbleListAction>;
9
11
  };
@@ -1,7 +1,8 @@
1
1
  import { useRef } from "react";
2
2
  function useBubbleListRef() {
3
3
  const bubbleListRef = useRef({
4
- scrollToBottom: ()=>{}
4
+ scrollToBottom: ()=>{},
5
+ scrollToBottomWhenAtBottom: ()=>{}
5
6
  });
6
7
  return {
7
8
  bubbleListRef,
@@ -1,7 +1,7 @@
1
1
  export declare function useChatContext(): {
2
2
  messages: import("./message").Message<any>[];
3
- sendMessage: (info: any) => void;
3
+ sendMessage: (info: any, callback?: ((info: any) => void) | undefined) => void;
4
4
  deleteConversation: ((key: string) => void) | undefined;
5
5
  updateConversation: ((key: string, item: any) => void) | undefined;
6
- scrollToBottom: () => any;
6
+ scrollToBottom: () => void;
7
7
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "listpage-next",
3
- "version": "0.0.90",
3
+ "version": "0.0.91",
4
4
  "description": "A React component library for creating filter forms with Ant Design",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",