项目作者: ConversationalComponents

项目描述 :
Basic chat window functionality
高级语言: TypeScript
项目地址: git://github.com/ConversationalComponents/chat-window.git
创建时间: 2020-01-10T12:22:36Z
项目社区:https://github.com/ConversationalComponents/chat-window

开源协议:

下载


chat-window

Basic chat window functionality

install with npm install @conversationalcomponents/chat-window

example:

  1. import React from "react";
  2. import {ChatWindow} from "@conversationalcomponents/chat-window";
  3. export const Example = () => (
  4. <ChatWindow
  5. content={[
  6. {
  7. isUser: false,
  8. message: "Hi, this is an example",
  9. avatar: "https://img.icons8.com/color/search/96",
  10. id: "message_0",
  11. isLoading: false
  12. }
  13. ]}
  14. ></ChatWindow>
  15. );

An example with fake conversation:

  1. import React, {useState, useEffect} from "react";
  2. import {ChatWindow} from "@conversationalcomponents/chat-window";
  3. const makeMessage = (isUser: boolean, text: string, id: number) => ({
  4. isUser,
  5. message: text,
  6. avatar: isUser ? "https://img.icons8.com/color/search/0" : "https://img.icons8.com/color/search/96",
  7. id: `message_${id}`,
  8. isLoading: false
  9. });
  10. const botReplies = ["Wow!", "Fascinating, please do go on", "Amazing!", "Really?", "If you say so..."];
  11. export const Example = () => {
  12. const [content, setContent] = useState([makeMessage(false, "Hello and welcome to the example!", 0)]);
  13. useEffect(() => {
  14. const lastEntry = content.length && content[content.length - 1];
  15. lastEntry &&
  16. lastEntry.isUser &&
  17. setContent([...content, makeMessage(false, botReplies[Math.floor(Math.random() * botReplies.length)], 0)]);
  18. }, [content]);
  19. return (
  20. <ChatWindow
  21. title="Example"
  22. content={content}
  23. onSubmit={(text: string) => setContent([...content, makeMessage(true, text, content.length)])}
  24. />
  25. );
  26. };

An example with custom header, typing animations (Typescript):

  1. import React, {useState, useEffect} from "react";
  2. import {useUserTyping, useBotTyping, ChatWindow} from "@conversationalcomponents/chat-window";
  3. import {ChatEntry} from "@conversationalcomponents/chat-window/types";
  4. const botReplies = ["Wow!", "Fascinating, please do go on", "Amazing!", "Really?", "If you say so..."];
  5. const getBotReply = () => botReplies[Math.floor(Math.random() * botReplies.length)];
  6. const userAvatar = "https://img.icons8.com/color/search/0";
  7. const botAvatar = "https://img.icons8.com/color/search/1";
  8. export const Example = () => {
  9. const [content, setContent] = useState<ChatEntry[]>([]);
  10. const [lastInputValue, setLastInputValue] = useState("");
  11. const [lastUnsubmittedInput, setLastUnsubmittedInput] = useState("");
  12. useEffect(() => {
  13. const lastEntry = content.length && content[content.length - 1];
  14. if (!lastEntry || lastEntry.isUser) return;
  15. setLastInputValue("");
  16. }, [content]);
  17. useEffect(() => {
  18. lastInputValue && setLastUnsubmittedInput("");
  19. }, [lastInputValue]);
  20. useUserTyping(content, setContent, lastUnsubmittedInput, lastInputValue, userAvatar);
  21. const isBotDoneTyping = useBotTyping(content, setContent, lastInputValue, botAvatar);
  22. useEffect(() => {
  23. if (!isBotDoneTyping) return;
  24. const lastEntry = content.length && content[content.length - 1];
  25. if (!lastEntry || lastEntry.isUser) return;
  26. lastEntry.message = getBotReply();
  27. lastEntry.isLoading = false;
  28. }, [isBotDoneTyping]);
  29. return (
  30. <ChatWindow
  31. headerAdditionalContent={<div style={{flex: 1, display: "flex", justifyContent: "center"}}>HEADER</div>}
  32. content={content}
  33. onChange={(text: string) => setLastUnsubmittedInput(text)}
  34. onSubmit={(text: string) => setLastInputValue(text)}
  35. />
  36. );
  37. };

An example with typing animations that echoes user’s input back:

  1. import React, {useState, useEffect} from "react";
  2. import {ChatEntry} from "@conversationalcomponents/chat-window/types";
  3. import {useUserTyping, useBotTyping, ChatWindow} from "@conversationalcomponents/chat-window";
  4. const userAvatar = "https://img.icons8.com/color/search/0";
  5. const botAvatar = "https://img.icons8.com/color/search/1";
  6. export const Example = () => {
  7. const [content, setContent] = useState<ChatEntry[]>([]);
  8. const [lastInputValue, setLastInputValue] = useState("");
  9. const [lastUnsubmittedInput, setLastUnsubmittedInput] = useState("");
  10. const [nextBotReply, setNextBotReply] = useState("");
  11. useEffect(() => {
  12. const lastEntry = content.length && content[content.length - 1];
  13. if (!lastEntry || lastEntry.isUser) return;
  14. setNextBotReply(lastInputValue);
  15. setLastInputValue("");
  16. }, [content]);
  17. useEffect(() => {
  18. lastInputValue && setLastUnsubmittedInput("");
  19. }, [lastInputValue]);
  20. useUserTyping(content, setContent, lastUnsubmittedInput, lastInputValue, userAvatar);
  21. const isBotDoneTyping = useBotTyping(content, setContent, lastInputValue, botAvatar);
  22. useEffect(() => {
  23. if (!isBotDoneTyping || !nextBotReply) return;
  24. const lastEntry = content.length && content[content.length - 1];
  25. if (!lastEntry || lastEntry.isUser) return;
  26. lastEntry.message = nextBotReply;
  27. lastEntry.isLoading = false;
  28. setNextBotReply("");
  29. }, [isBotDoneTyping]);
  30. return (
  31. <ChatWindow
  32. headerAdditionalContent={<div style={{flex: 1, display: "flex", justifyContent: "center"}}>HEADER</div>}
  33. content={content}
  34. onChange={(text: string) => setLastUnsubmittedInput(text)}
  35. onSubmit={(text: string) => setLastInputValue(text)}
  36. />
  37. );
  38. };