-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
82 lines (67 loc) · 2.01 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { useState, useEffect, useCallback } from 'react'
import Chat, { Bubble, useMessages, MessageProps } from '@chatui/core'
import memoriApiClient from '@memori.ai/memori-api-client'
import '@chatui/core/dist/index.css'
import './App.css'
const client = memoriApiClient()
function App() {
const { messages, appendMsg, setTyping } = useMessages([])
const [sessionId, setSessionId] = useState<string>()
const fetchSession = useCallback(async () => {
if (sessionId) return
const { sessionID, currentState, ...resp } = await client.initSession({
memoriID: '1afe57c6-1b69-4a61-96ea-52bf7b8d158e'
})
if (sessionID && currentState && resp.resultCode === 0) {
setSessionId(sessionID)
console.log('fetched session', sessionID, currentState)
if (currentState.emission) {
appendMsg({
type: 'text',
content: { text: currentState.emission }
})
}
}
}, [appendMsg, sessionId])
useEffect(() => {
if (!sessionId) fetchSession()
}, [sessionId, fetchSession])
const handleSend = async (type: string, val: string) => {
if (!sessionId) {
console.error('No session ID')
return
}
if (type === 'text' && val.trim()) {
appendMsg({
type: 'text',
content: { text: val },
position: 'right'
})
setTyping(true)
const { currentState, ...resp } = await client.postTextEnteredEvent({
sessionId,
text: val
})
if (currentState.emission && resp.resultCode === 0)
appendMsg({
type: 'text',
content: { text: currentState.emission }
})
}
}
function renderMessageContent(msg: MessageProps) {
const { content } = msg
return <Bubble content={content.text} />
}
return (
<Chat
locale="en-US"
navbar={{ title: 'Parla con Nunzio' }}
messages={messages}
renderMessageContent={renderMessageContent}
onSend={handleSend}
placeholder="Chiedimi qualcosa..."
/>
)
}
export default App