Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developer #218

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ yarn-error.log
# OSX
.DS_Store

# Build files
lib

# Test files
coverage
Binary file added assets/mic-red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions assets/mic-red.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/mic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions assets/mic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dev/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class App extends Component {
render() {
return (
<div>
<button style={{position: 'absolute', right: 40, bottom: 150}}>test</button>
{/* <button style={{position: 'absolute', right: 40, bottom: 150}}>test</button> */}
<Widget
title="Bienvenido"
subtitle="Asistente virtual"
Expand Down
20 changes: 20 additions & 0 deletions lib/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/styles.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "react-chat-widget",
"version": "3.0.5",
"name": "react-chat-widget-with-mic",
"version": "0.0.2",
"description": "Chat web widget for React apps",
"main": "lib/index.js",
"repository": "[email protected]:Wolox/react-chat-widget.git",
"author": "Martín Callegari <[email protected]>",
"repository": "[email protected]:Vexole/react-chat-widget.git",
"author": "",
"license": "MIT",
"types": "./lib/index.d.ts",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useSelector } from 'react-redux';
import { GlobalState } from 'src/store/types';

const send = require('../../../../../../../assets/send_button.svg') as string;
const micBlack = require('../../../../../../../assets/mic.svg') as string;
const micRed = require('../../../../../../../assets/mic-red.svg') as string;

import './style.scss';

Expand All @@ -14,16 +16,33 @@ type Props = {
sendMessage: (event: any) => void;
buttonAlt: string;
onTextInputChange?: (event: any) => void;
isRecording: boolean;
handleStream: () => void;
inputMessage: string;
}

function Sender({ sendMessage, placeholder, disabledInput, autofocus, onTextInputChange, buttonAlt }: Props) {
function Sender({ isRecording, handleStream, sendMessage, placeholder, disabledInput, autofocus, inputMessage, onTextInputChange, buttonAlt }: Props) {
const showChat = useSelector((state: GlobalState) => state.behavior.showChat);
const inputRef = useRef(null);
let mic = isRecording ? micRed : micBlack;
// @ts-ignore
useEffect(() => { if (showChat) inputRef.current?.focus(); }, [showChat]);
const [message, setMessage] = useState(inputMessage);
useEffect(() => {
mic = isRecording ? micRed : micBlack;
}, [isRecording])

useEffect(() => {
setMessage(inputMessage)
}, [inputMessage])

const handleSubmission = e => {
sendMessage(e)
setMessage('')
}

return (
<form className="rcw-sender" onSubmit={sendMessage}>
<form className="rcw-sender" onSubmit={handleSubmission}>
<input
type="text"
className="rcw-new-message"
Expand All @@ -33,8 +52,12 @@ function Sender({ sendMessage, placeholder, disabledInput, autofocus, onTextInpu
disabled={disabledInput}
autoFocus={autofocus}
autoComplete="off"
onChange={onTextInputChange}
defaultValue={message}
onChange={e => setMessage(e.target.value)}
/>
<button type="button" className="rcw-send" onClick={handleStream}>
<img src={mic} className="rcw-send-icon" alt={buttonAlt} />
</button>
<button type="submit" className="rcw-send">
<img src={send} className="rcw-send-icon" alt={buttonAlt} />
</button>
Expand Down
11 changes: 10 additions & 1 deletion src/components/Widget/components/Conversation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Props = {
onTextInputChange?: (event: any) => void;
sendButtonAlt: string;
showTimeStamp: boolean;
isRecording: boolean;
handleStream: AnyFunction;
inputMessage: string;
};

function Conversation({
Expand All @@ -43,7 +46,10 @@ function Conversation({
onQuickButtonClicked,
onTextInputChange,
sendButtonAlt,
showTimeStamp
showTimeStamp,
isRecording,
handleStream,
inputMessage
}: Props) {
return (
<div className={cn('rcw-conversation-container', className)} aria-live="polite">
Expand All @@ -57,12 +63,15 @@ function Conversation({
<Messages profileAvatar={profileAvatar} showTimeStamp={showTimeStamp} />
<QuickButtons onQuickButtonClicked={onQuickButtonClicked} />
<Sender
handleStream={handleStream}
isRecording={isRecording}
sendMessage={sendMessage}
placeholder={senderPlaceHolder}
disabledInput={disabledInput}
autofocus={autofocus}
onTextInputChange={onTextInputChange}
buttonAlt={sendButtonAlt}
inputMessage={inputMessage}
/>
</div>
);
Expand Down
17 changes: 13 additions & 4 deletions src/components/Widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Props = {
imagePreview?: boolean;
zoomStep?: number;
handleSubmit?: AnyFunction;
handleStream: AnyFunction;
isRecording: boolean;
inputMessage: string;
}

function Widget({
Expand All @@ -49,7 +52,10 @@ function Widget({
showTimeStamp,
imagePreview,
zoomStep,
handleSubmit
handleSubmit,
handleStream,
isRecording,
inputMessage
}: Props) {
const dispatch = useDispatch();

Expand All @@ -60,9 +66,9 @@ function Widget({
const handleMessageSubmit = (event) => {
event.preventDefault();
const userInput = event.target.message.value;
if (!userInput.trim()) {
return;

if (!userInput.trim()) {
return;
}

handleSubmit?.(userInput);
Expand Down Expand Up @@ -98,6 +104,9 @@ function Widget({
showTimeStamp={showTimeStamp}
imagePreview={imagePreview}
zoomStep={zoomStep}
handleStream={handleStream}
isRecording={isRecording}
inputMessage={inputMessage}
/>
);
}
Expand Down
21 changes: 15 additions & 6 deletions src/components/Widget/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React,{ useEffect, useRef } from 'react';
import React, { useEffect, useRef } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import cn from 'classnames';

Expand Down Expand Up @@ -33,6 +33,9 @@ type Props = {
showTimeStamp: boolean;
imagePreview?: boolean;
zoomStep?: number;
handleStream: AnyFunction;
isRecording: boolean;
inputMessage: string;
}

function WidgetLayout({
Expand All @@ -55,7 +58,10 @@ function WidgetLayout({
sendButtonAlt,
showTimeStamp,
imagePreview,
handleStream,
isRecording,
zoomStep,
inputMessage,
}: Props) {
const dispatch = useDispatch();
const { dissableInput, showChat, visible } = useSelector((state: GlobalState) => ({
Expand All @@ -67,16 +73,16 @@ function WidgetLayout({
const messageRef = useRef<HTMLDivElement | null>(null);

useEffect(() => {
if(showChat) {
if (showChat) {
messageRef.current = document.getElementById('messages') as HTMLDivElement;
}
return () => {
messageRef.current = null;
}
}, [showChat])

const eventHandle = evt => {
if(evt.target && evt.target.className === 'rcw-message-img') {
if (evt.target && evt.target.className === 'rcw-message-img') {
const { src, alt, naturalWidth, naturalHeight } = (evt.target as HTMLImageElement);
const obj = {
src: src,
Expand All @@ -93,7 +99,7 @@ function WidgetLayout({
*/
useEffect(() => {
const target = messageRef?.current;
if(imagePreview && showChat) {
if (imagePreview && showChat) {
target?.addEventListener('click', eventHandle, false);
}

Expand All @@ -111,11 +117,12 @@ function WidgetLayout({
className={cn('rcw-widget-container', {
'rcw-full-screen': fullScreenMode,
'rcw-previewer': imagePreview
})
})
}
>
{showChat &&
<Conversation
inputMessage={inputMessage}
title={title}
subtitle={subtitle}
sendMessage={onSendMessage}
Expand All @@ -131,6 +138,8 @@ function WidgetLayout({
onTextInputChange={onTextInputChange}
sendButtonAlt={sendButtonAlt}
showTimeStamp={showTimeStamp}
handleStream={handleStream}
isRecording={isRecording}
/>
}
{customLauncher ?
Expand Down
13 changes: 11 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Provider } from 'react-redux';

import Widget from './components/Widget';

import store from './store';
import store from './store';

import { AnyFunction } from './utils/types';

Expand All @@ -28,6 +28,9 @@ type Props = {
imagePreview?: boolean;
zoomStep?: number;
handleSubmit?: AnyFunction;
handleStream: AnyFunction;
isRecording: boolean;
inputMessage: string;
} & typeof defaultProps;

function ConnectedWidget({
Expand All @@ -50,7 +53,10 @@ function ConnectedWidget({
showTimeStamp,
imagePreview,
zoomStep,
handleSubmit
handleSubmit,
handleStream,
isRecording,
inputMessage
}: Props) {
return (
<Provider store={store}>
Expand All @@ -75,6 +81,9 @@ function ConnectedWidget({
imagePreview={imagePreview}
zoomStep={zoomStep}
handleSubmit={handleSubmit}
handleStream={handleStream}
isRecording={isRecording}
inputMessage={inputMessage}
/>
</Provider>
);
Expand Down