HonzaH commited on
Commit
1e2cefc
·
verified ·
1 Parent(s): 24b7e75

Upload 192 files

Browse files
Files changed (2) hide show
  1. src/components/chat/Chat.tsx +238 -116
  2. src/lib/persistence.ts +1 -1
src/components/chat/Chat.tsx CHANGED
@@ -1,117 +1,239 @@
1
- 'use client'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- import { useState, useRef, useEffect } from 'react'
4
- import type { MessageData } from 'lib/messages'
5
- import { formatTime } from 'lib/utils'
6
-
7
- interface ChatProps {
8
- messages: MessageData[]
9
- currentCodeId?: string
10
- roomName?: string
11
- loading?: boolean
12
- error?: string | null
13
- onSend?: (content: string) => Promise<void> | void
14
- }
15
-
16
- export function Chat({ messages, currentCodeId, roomName = 'Chat', loading, error, onSend }: ChatProps) {
17
- const [inputValue, setInputValue] = useState('')
18
- const messagesEndRef = useRef<HTMLDivElement | null>(null)
19
-
20
- useEffect(() => {
21
- messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' })
22
- }, [messages])
23
-
24
- const handleSubmit = async (e?: React.FormEvent) => {
25
- e?.preventDefault()
26
- const val = inputValue.trim()
27
- if (!val) return
28
- try {
29
- await onSend?.(val)
30
- setInputValue('')
31
- } catch (err) {
32
- console.error('Chat onSend failed', err)
33
- }
34
- }
35
-
36
- if (loading) {
37
- return (
38
- <div className="h-full flex items-center justify-center">
39
- <div className="text-center">
40
- <div className="w-8 h-8 border-4 border-brand-200 border-t-brand-600 rounded-full animate-spin mx-auto mb-4"></div>
41
- <p className="text-gray-600">Načítání...</p>
42
- </div>
43
- </div>
44
- )
45
- }
46
-
47
- if (error) {
48
- return (
49
- <div className="h-full flex items-center justify-center text-red-600">
50
- <div className="text-center">
51
- <p className="text-lg font-medium">Chyba</p>
52
- <p className="text-sm mt-1">{error}</p>
53
- </div>
54
- </div>
55
- )
56
- }
57
-
58
- return (
59
- <div className="h-full flex flex-col bg-gray-50">
60
- <div className="box flex-1 flex flex-col max-w-4xl mx-auto w-full shadow-sm">
61
- <div className="title p-4 border-b bg-white flex items-center justify-between">
62
- <div className="flex items-center gap-3">
63
- <div className="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center text-purple-700 font-semibold">{roomName?.slice(0,1)}</div>
64
- <div>
65
- <div className="text-lg font-semibold">{roomName}</div>
66
- <div className="text-xs text-gray-400">{messages.length} zpráv • {formatTime(new Date())}</div>
67
- </div>
68
- </div>
69
- </div>
70
-
71
- <div className="messages p-4 overflow-auto flex-1 bg-gradient-to-b from-white to-gray-50">
72
- <div className="bubbles space-y-4">
73
- {messages.map((msg) => {
74
- const isOwn = currentCodeId && msg.code_id === currentCodeId
75
- return (
76
- <div key={msg.id} className={`flex items-end ${isOwn ? 'justify-end' : 'justify-start'}`}>
77
- {!isOwn && (
78
- <div className="flex-shrink-0 mr-3">
79
- <div className="w-10 h-10 rounded-full bg-gray-200 flex items-center justify-center text-sm text-gray-600">{(msg.sender?.nickname || msg.sender?.fname || '').slice(0,2).toUpperCase() || 'U'}</div>
80
- </div>
81
- )}
82
-
83
- <div className={`max-w-[75%] p-3 rounded-2xl ${isOwn ? 'bg-purple-600 text-white' : 'bg-white text-gray-900'} shadow-md`}>
84
- <div className="text-xs text-gray-400 mb-1">{!isOwn && (msg.sender?.nickname || `${msg.sender?.fname || ''} ${msg.sender?.sname || ''}`)} {formatTime(new Date(msg.created_at))}</div>
85
- <div className="whitespace-pre-wrap break-words">{msg.content}</div>
86
- </div>
87
-
88
- {isOwn && (
89
- <div className="flex-shrink-0 ml-3">
90
- <div className="w-10 h-10 rounded-full bg-purple-200 flex items-center justify-center text-sm text-purple-700">TY</div>
91
- </div>
92
- )}
93
- </div>
94
- )
95
- })}
96
- <div ref={messagesEndRef} />
97
- </div>
98
- </div>
99
-
100
- <div className="tool p-4 bg-white border-t">
101
- <form onSubmit={handleSubmit} className="flex items-center gap-3 max-w-4xl mx-auto">
102
- <input
103
- name="message"
104
- type="text"
105
- value={inputValue}
106
- onChange={(e) => setInputValue(e.target.value)}
107
- maxLength={2048}
108
- placeholder="Napište zprávu..."
109
- className="flex-1 px-4 py-3 border rounded-full bg-gray-50 focus:outline-none focus:ring-2 focus:ring-purple-300"
110
- />
111
- <button type="submit" className="px-5 py-2 bg-purple-600 text-white rounded-full shadow">Odeslat</button>
112
- </form>
113
- </div>
114
- </div>
115
- </div>
116
- )
117
- }
 
1
+
2
+ 'use client'
3
+
4
+
5
+
6
+ import { useState, useRef, useEffect } from 'react'
7
+
8
+ import type { MessageData } from 'lib/messages'
9
+
10
+ import { formatTime } from 'lib/utils'
11
+
12
+
13
+
14
+ interface ChatProps {
15
+
16
+ messages: MessageData[]
17
+
18
+ currentCodeId?: string
19
+
20
+ roomName?: string
21
+
22
+ loading?: boolean
23
+
24
+ error?: string | null
25
+
26
+ onSend?: (content: string) => Promise<void> | void
27
+
28
+ }
29
+
30
+
31
+
32
+ export function Chat({ messages, currentCodeId, roomName = 'Chat', loading, error, onSend }: ChatProps) {
33
+
34
+ const [inputValue, setInputValue] = useState('')
35
+
36
+ const messagesEndRef = useRef<HTMLDivElement | null>(null)
37
+
38
+
39
+
40
+ useEffect(() => {
41
+
42
+ messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' })
43
+
44
+ }, [messages])
45
+
46
+
47
+
48
+ const handleSubmit = async (e?: React.FormEvent) => {
49
+
50
+ e?.preventDefault()
51
+
52
+ const val = inputValue.trim()
53
+
54
+ if (!val) return
55
+
56
+ try {
57
+
58
+ await onSend?.(val)
59
+
60
+ setInputValue('')
61
+
62
+ } catch (err) {
63
+
64
+ console.error('Chat onSend failed', err)
65
+
66
+ }
67
+
68
+ }
69
+
70
+
71
+
72
+ if (loading) {
73
+
74
+ return (
75
+
76
+ <div className="h-full flex items-center justify-center">
77
+
78
+ <div className="text-center">
79
+
80
+ <div className="w-8 h-8 border-4 border-brand-200 border-t-brand-600 rounded-full animate-spin mx-auto mb-4"></div>
81
+
82
+ <p className="text-gray-600">Načítání...</p>
83
+
84
+ </div>
85
+
86
+ </div>
87
+
88
+ )
89
+
90
+ }
91
+
92
+
93
+
94
+ if (error) {
95
+
96
+ return (
97
+
98
+ <div className="h-full flex items-center justify-center text-red-600">
99
+
100
+ <div className="text-center">
101
+
102
+ <p className="text-lg font-medium">Chyba</p>
103
+
104
+ <p className="text-sm mt-1">{error}</p>
105
+
106
+ </div>
107
+
108
+ </div>
109
+
110
+ )
111
+
112
+ }
113
+
114
+
115
+
116
+ return (
117
+
118
+ <div className="h-full flex flex-col bg-gray-50">
119
+
120
+ <div className="box flex-1 flex flex-col max-w-4xl mx-auto w-full shadow-sm">
121
+
122
+ <div className="title p-4 border-b bg-gradient-to-r from-purple-600 to-purple-500 text-white flex items-center justify-between">
123
+
124
+ <div className="flex items-center gap-3">
125
+
126
+ <div className="w-10 h-10 rounded-full bg-white/10 flex items-center justify-center text-white font-semibold">{(roomName||'')[0]}</div>
127
+
128
+ <div>
129
+
130
+ <div className="text-lg font-semibold">{roomName}</div>
131
+
132
+ <div className="text-xs text-purple-100">{messages.length} zpráv • {formatTime(new Date())}</div>
133
+
134
+ </div>
135
+
136
+ </div>
137
+
138
+ </div>
139
+
140
+
141
+
142
+ <div className="messages p-4 overflow-auto flex-1 bg-gradient-to-b from-white to-gray-50">
143
+
144
+ <div className="bubbles space-y-4">
145
+
146
+ {messages.map((msg) => {
147
+
148
+ const isOwn = currentCodeId && msg.code_id === currentCodeId
149
+
150
+ return (
151
+
152
+ <div key={msg.id} className={`flex items-end ${isOwn ? 'justify-end' : 'justify-start'}`}>
153
+
154
+ {!isOwn && (
155
+
156
+ <div className="flex-shrink-0 mr-3">
157
+
158
+ <div className="w-10 h-10 rounded-full bg-gray-200 flex items-center justify-center text-sm text-gray-600">{(msg.sender?.nickname || msg.sender?.fname || '').slice(0,2).toUpperCase() || 'U'}</div>
159
+
160
+ </div>
161
+
162
+ )}
163
+
164
+
165
+
166
+ <div className={`max-w-[75%] p-3 rounded-2xl ${isOwn ? 'bg-purple-600' : 'bg-white'} shadow-md`}>
167
+
168
+ <div className={isOwn ? 'text-xs text-purple-200 mb-1' : 'text-xs text-gray-500 mb-1'}>
169
+
170
+ {!isOwn && (msg.sender?.nickname || `${msg.sender?.fname || ''} ${msg.sender?.sname || ''}`)} {formatTime(new Date(msg.created_at))}
171
+
172
+ </div>
173
+
174
+ <div className={isOwn ? 'whitespace-pre-wrap break-words text-white' : 'whitespace-pre-wrap break-words text-gray-900'}>{msg.content}</div>
175
+
176
+ </div>
177
+
178
+
179
+
180
+ {isOwn && (
181
+
182
+ <div className="flex-shrink-0 ml-3">
183
+
184
+ <div className="w-10 h-10 rounded-full bg-purple-200 flex items-center justify-center text-sm text-purple-700">TY</div>
185
+
186
+ </div>
187
+
188
+ )}
189
+
190
+ </div>
191
+
192
+ )
193
+
194
+ })}
195
+
196
+ <div ref={messagesEndRef} />
197
+
198
+ </div>
199
+
200
+ </div>
201
+
202
+
203
+
204
+ <div className="tool p-4 bg-white border-t">
205
+
206
+ <form onSubmit={handleSubmit} className="flex items-center gap-3 max-w-4xl mx-auto">
207
+
208
+ <input
209
+
210
+ name="message"
211
+
212
+ type="text"
213
+
214
+ value={inputValue}
215
+
216
+ onChange={(e) => setInputValue(e.target.value)}
217
+
218
+ maxLength={2048}
219
+
220
+ placeholder="Napište zprávu..."
221
+
222
+ className="flex-1 px-4 py-3 border rounded-full bg-gray-50 focus:outline-none focus:ring-2 focus:ring-purple-300"
223
+
224
+ />
225
+
226
+ <button type="submit" className="px-5 py-2 bg-purple-600 text-white rounded-full shadow">Odeslat</button>
227
+
228
+ </form>
229
+
230
+ </div>
231
+
232
+ </div>
233
+
234
+ </div>
235
+
236
+ )
237
+
238
+ }
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/lib/persistence.ts CHANGED
@@ -412,7 +412,7 @@ export async function updateRoom(roomId: string, payload: Partial<Database['publ
412
  return res.rows[0]
413
  }
414
  try {
415
- const supabase = await getSupabase()
416
  const { data, error } = await supabase.from('rooms').update(payload).eq('id', roomId).select().single()
417
  if (error) {
418
  console.error('[persistence.updateRoom] supabase error:', error)
 
412
  return res.rows[0]
413
  }
414
  try {
415
+ const supabase = await getSupabaseWrite()
416
  const { data, error } = await supabase.from('rooms').update(payload).eq('id', roomId).select().single()
417
  if (error) {
418
  console.error('[persistence.updateRoom] supabase error:', error)