-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchat-messages-view.tsx
More file actions
158 lines (142 loc) · 4.51 KB
/
chat-messages-view.tsx
File metadata and controls
158 lines (142 loc) · 4.51 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use client';
import { useAnonymousAuth } from '@/components/anonymous-auth';
import { PENDING_VOICE_MESSAGE_KEY } from '@/components/home/home-input';
import { useChatStore } from '@/components/providers/chat-store-provider';
import { useTenant } from '@/components/providers/tenant-provider';
import type {
ChatSession,
ProposedQuestion,
} from '@/lib/firebase/firebase.types';
import type { PartyDetails } from '@/lib/party-details';
import type { GroupedMessage } from '@/lib/stores/chat-store.types';
import { useEffect, useMemo, useRef } from 'react';
import ChatEmptyView from './chat-empty-view';
import ChatGroupedMessages from './chat-grouped-messages';
import ChatMessagesScrollView from './chat-messages-scroll-view';
import { INITIAL_MESSAGE_ID } from './chat-single-user-message';
import CurrentStreamingMessages from './current-streaming-messages';
type Props = {
sessionId?: string;
chatSession?: ChatSession;
messages?: GroupedMessage[];
parties?: PartyDetails[];
allParties?: PartyDetails[];
proposedQuestions?: ProposedQuestion[];
initialQuestion?: string;
hasPendingVoiceMessage?: boolean;
};
function ChatMessagesView({
sessionId,
chatSession,
messages,
parties,
allParties,
proposedQuestions,
initialQuestion,
hasPendingVoiceMessage,
}: Props) {
const hasFetched = useRef(false);
const hasProcessedVoiceMessage = useRef(false);
const storeMessages = useChatStore((state) => state.messages);
const hydrateChatSession = useChatStore((state) => state.hydrateChatSession);
const sendVoiceMessage = useChatStore((state) => state.sendVoiceMessage);
const { user } = useAnonymousAuth();
const tenant = useTenant();
const hasCurrentStreamingMessages = useChatStore(
(state) => state.currentStreamingMessages !== undefined,
);
const isSocketConnected = useChatStore((state) => state.socket.connected);
useEffect(() => {
if (!user?.uid) return;
hydrateChatSession({
chatSession,
chatSessionId: sessionId,
messages,
preSelectedPartyIds: parties?.map((party) => party.party_id),
initialQuestion,
userId: user.uid,
tenant,
});
hasFetched.current = true;
}, [
sessionId,
user?.uid,
chatSession,
hydrateChatSession,
messages,
parties,
initialQuestion,
tenant,
]);
// Handle pending voice message from home page
useEffect(() => {
if (
!hasPendingVoiceMessage ||
hasProcessedVoiceMessage.current ||
!isSocketConnected
)
return;
const pendingAudioBase64 = sessionStorage.getItem(
PENDING_VOICE_MESSAGE_KEY,
);
if (pendingAudioBase64) {
sessionStorage.removeItem(PENDING_VOICE_MESSAGE_KEY);
hasProcessedVoiceMessage.current = true;
// Convert base64 back to Uint8Array
const binaryString = atob(pendingAudioBase64);
const audioBytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
audioBytes[i] = binaryString.charCodeAt(i);
}
sendVoiceMessage(audioBytes);
}
}, [hasPendingVoiceMessage, sendVoiceMessage, isSocketConnected]);
const normalizedMessages = useMemo(() => {
if (messages && !storeMessages.length) {
return messages;
}
if (!storeMessages.length && initialQuestion) {
return [
{
id: INITIAL_MESSAGE_ID,
messages: [
{
role: 'user',
content: initialQuestion,
id: INITIAL_MESSAGE_ID,
sources: [],
},
],
role: 'user',
} satisfies GroupedMessage,
];
}
return storeMessages;
}, [messages, storeMessages, initialQuestion]);
return (
<ChatMessagesScrollView>
<div className="flex flex-col gap-6 px-3 py-4 md:px-[35px]">
{normalizedMessages.length === 0 && (
<div className="mt-12 flex h-full grow justify-center md:mt-24">
<ChatEmptyView
parties={parties}
proposedQuestions={proposedQuestions}
/>
</div>
)}
{normalizedMessages.map((m, index) => (
<ChatGroupedMessages
key={m.id}
message={m}
isLastMessage={index === normalizedMessages.length - 1}
parties={allParties?.filter((p) =>
m.messages.some((m) => m.party_id === p.party_id),
)}
/>
))}
{hasCurrentStreamingMessages && <CurrentStreamingMessages />}
</div>
</ChatMessagesScrollView>
);
}
export default ChatMessagesView;