hooks up the redux conversation state with conversation UI

pull/100/head
ajaythapliyal 2 years ago
parent 5b456cda16
commit e40eb53b7a

@ -1,3 +1,4 @@
import { useEffect, useRef } from 'react';
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
import Hero from '../Hero'; import Hero from '../Hero';
import ConversationBubble from './ConversationBubble'; import ConversationBubble from './ConversationBubble';
@ -6,13 +7,18 @@ import { selectConversation } from './conversationSlice';
export default function Conversation() { export default function Conversation() {
const messages = useSelector(selectConversation); const messages = useSelector(selectConversation);
const endMessageRef = useRef<HTMLDivElement>(null);
useEffect(() => endMessageRef?.current?.scrollIntoView());
return ( return (
<div className="flex justify-center p-6"> <div className="flex justify-center p-6">
<div className="w-10/12 transition-all md:w-1/2"> <div className="w-10/12 transition-all md:w-1/2">
{messages.map((message, index) => { {messages.map((message, index) => {
return ( return (
<ConversationBubble <ConversationBubble
className="mt-5" ref={index === messages.length - 1 ? endMessageRef : null}
className="mb-7"
key={index} key={index}
message={message.text} message={message.text}
type={message.type} type={message.type}

@ -1,18 +1,18 @@
import { forwardRef } from 'react';
import Avatar from '../Avatar'; import Avatar from '../Avatar';
import { User } from '../models/misc';
import { MESSAGE_TYPE } from './conversationModels'; import { MESSAGE_TYPE } from './conversationModels';
export default function ConversationBubble({ const ConversationBubble = forwardRef<
message, HTMLDivElement,
type, {
className,
}: {
message: string; message: string;
type: MESSAGE_TYPE; type: MESSAGE_TYPE;
className: string; className: string;
}) { }
>(function ConversationBubble({ message, type, className }, ref) {
return ( return (
<div <div
ref={ref}
className={`flex rounded-3xl ${ className={`flex rounded-3xl ${
type === 'QUESTION' ? '' : 'bg-gray-1000' type === 'QUESTION' ? '' : 'bg-gray-1000'
} py-7 px-5 ${className}`} } py-7 px-5 ${className}`}
@ -21,4 +21,6 @@ export default function ConversationBubble({
<p className="ml-5">{message}</p> <p className="ml-5">{message}</p>
</div> </div>
); );
} });
export default ConversationBubble;

@ -2,8 +2,33 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import store from '../store'; import store from '../store';
import { ConversationState, Message } from './conversationModels'; import { ConversationState, Message } from './conversationModels';
// harcoding the initial state just for demo
const initialState: ConversationState = { const initialState: ConversationState = {
conversation: [], conversation: [
{ text: 'what is ChatGPT', type: 'QUESTION' },
{ text: 'ChatGPT is large learning model', type: 'ANSWER' },
{ text: 'what is ChatGPT', type: 'QUESTION' },
{ text: 'ChatGPT is large learning model', type: 'ANSWER' },
{ text: 'what is ChatGPT', type: 'QUESTION' },
{ text: 'ChatGPT is large learning model', type: 'ANSWER' },
{ text: 'what is ChatGPT', type: 'QUESTION' },
{ text: 'ChatGPT is large learning model', type: 'ANSWER' },
{ text: 'what is ChatGPT', type: 'QUESTION' },
{ text: 'ChatGPT is large learning model', type: 'ANSWER' },
{ text: 'what is ChatGPT', type: 'QUESTION' },
{ text: 'ChatGPT is large learning model', type: 'ANSWER' },
{ text: 'what is ChatGPT', type: 'QUESTION' },
{ text: 'ChatGPT is large learning model', type: 'ANSWER' },
{ text: 'what is ChatGPT', type: 'QUESTION' },
{ text: 'ChatGPT is large learning model', type: 'ANSWER' },
{ text: 'what is ChatGPT', type: 'QUESTION' },
{ text: 'ChatGPT is large learning model', type: 'ANSWER' },
{ text: 'what is ChatGPT', type: 'QUESTION' },
{
text: 'ChatGPT is large learning model',
type: 'ANSWER',
},
],
}; };
export const conversationSlice = createSlice({ export const conversationSlice = createSlice({

Loading…
Cancel
Save