Skip to content

Commit

Permalink
Merge pull request #51 from odracirdev/feat/chat-loader
Browse files Browse the repository at this point in the history
Feat/chat loader
  • Loading branch information
Facundodandrea authored Jul 31, 2024
2 parents 9eff5e5 + 6a17c11 commit 34680b7
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 91 deletions.
78 changes: 0 additions & 78 deletions frontend/src/components/Chat/Chat.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions frontend/src/components/Chat/index.css

This file was deleted.

3 changes: 2 additions & 1 deletion frontend/src/components/ChatBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import SenderButton from './SenderButton'
type ChatMode = 'text' | 'audio'

const ChatBox = () => {
const [loading, setLoading] = useState(false)
const [inputText, setInputText] = useState('')
const initialMode = useMemo<ChatMode>(
() => (inputText ? 'text' : 'audio'),
[inputText]
)
const sendMessage = useChat((state) => state.appendTextMessage)
const setLoading = useChat((state) => state.setLoading)
const loading = useChat((state) => state.loading)

const recognitionText = useSpeechRecognition((state) => state.text)
const hasRecognitionSupport = useSpeechRecognition(
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/ChatBubble/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const ChatBubble: FC<ChatBubbleProps> = ({ alignment = 'left', text }) => {
/>
<div
className={cn([
'space-y-2',
'space-y-2 bg-stone-800/30 p-4 rounded-[6px] w-fit',
{
'bg-zinc-800 text-white p-4 rounded-[6px] w-fit ml-auto':
'bg-zinc-800 text-white w-fit ml-auto':
alignment === 'right'
}
])}
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/components/Loader/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'
import './style.css'

const Loader: React.FC = () => {
return (
<section id="loader" className="dots-container">
<div className="dot"></div>
<div className="dot"></div>
<div className="dot"></div>
<div className="dot"></div>
<div className="dot"></div>
</section>
)
}

export default Loader
52 changes: 52 additions & 0 deletions frontend/src/components/Loader/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.dots-container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
}

.dot {
height: 20px;
width: 20px;
margin-right: 10px;
border-radius: 10px;
background-color: #b3d4fc;
animation: pulse 1.5s infinite ease-in-out;
}

.dot:last-child {
margin-right: 0;
}

.dot:nth-child(1) {
animation-delay: -0.3s;
}

.dot:nth-child(2) {
animation-delay: -0.1s;
}

.dot:nth-child(3) {
animation-delay: 0.1s;
}

@keyframes pulse {
0% {
transform: scale(0.8);
background-color: #b3d4fc;
box-shadow: 0 0 0 0 rgba(178, 212, 252, 0.7);
}

50% {
transform: scale(1.2);
background-color: #6793fb;
box-shadow: 0 0 0 10px rgba(178, 212, 252, 0);
}

100% {
transform: scale(0.8);
background-color: #b3d4fc;
box-shadow: 0 0 0 0 rgba(178, 212, 252, 0.7);
}
}
22 changes: 19 additions & 3 deletions frontend/src/contexts/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import { devtools } from 'zustand/middleware'
import { immer } from 'zustand/middleware/immer'
import type { Message, RoleMessage } from '../contracts/messages.contract'

type ChatState = { messages: Message[] }
type ChatState = {
messages: Message[]
loading: boolean
}

type ChatActions = { appendTextMessage(msg: string, role: RoleMessage): void }
type ChatActions = {
appendTextMessage(msg: string, role: RoleMessage): void
setLoading(loading: boolean): void
}

type ChatStore = ChatState & ChatActions

Expand All @@ -15,7 +21,8 @@ const INITIAL_STATE: ChatState = {
role: 'assistant',
content: { type: 'text', text: 'Hola, ¿en qué puedo ayudarte hoy?' }
}
]
],
loading: false
}

const useChat = create<ChatStore>()(
Expand All @@ -26,9 +33,18 @@ const useChat = create<ChatStore>()(
set(
(state) => {
state.messages.push({ role, content: { type: 'text', text: msg } })
// state.loading = false
},
false,
'appendTextMessage'
),
setLoading: (loading) =>
set(
(state) => {
state.loading = loading
},
false,
'setLoading'
)
}))
)
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/pages/Chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import ChatBubble from '../../components/ChatBubble'
import useChat from '../../contexts/chat'
import useSpeechRecognition from '../../contexts/speech-recognition'
import './index.css'
import Loader from '@/components/Loader'
import CVView from '@/components/CVView'

export default function Chat() {
const initSpeechRecognition = useSpeechRecognition((state) => state.init)
const messages = useChat((state) => state.messages)
const loading = useChat((state) => state.loading)

const chatRef = useRef<HTMLDivElement>(null)

Expand Down Expand Up @@ -67,6 +69,9 @@ export default function Chat() {
alignment={role === 'assistant' ? 'left' : 'right'}
/>
))}
{
loading && <Loader />
}
</div>
</div>
</div>
Expand Down

0 comments on commit 34680b7

Please sign in to comment.