@joewinke/jatui 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +46 -0
- package/src/lib/components/AudioWaveform.svelte +694 -0
- package/src/lib/components/AvailabilityModal.svelte +173 -0
- package/src/lib/components/Badge.svelte +38 -0
- package/src/lib/components/BookingForm.svelte +276 -0
- package/src/lib/components/Button.svelte +72 -0
- package/src/lib/components/CalendarPicker.svelte +284 -0
- package/src/lib/components/Card.svelte +67 -0
- package/src/lib/components/CharacterCounter.svelte +82 -0
- package/src/lib/components/ChipInput.svelte +596 -0
- package/src/lib/components/ColorSelector.svelte +163 -0
- package/src/lib/components/ConfirmModal.svelte +75 -0
- package/src/lib/components/CountdownTimer.svelte +94 -0
- package/src/lib/components/DateRangePicker.svelte +192 -0
- package/src/lib/components/Drawer.svelte +110 -0
- package/src/lib/components/FilterDropdown.svelte +202 -0
- package/src/lib/components/ImageUpload.svelte +97 -0
- package/src/lib/components/InlineEdit.svelte +283 -0
- package/src/lib/components/LazyImage.svelte +122 -0
- package/src/lib/components/LoadingSpinner.svelte +102 -0
- package/src/lib/components/Modal.svelte +208 -0
- package/src/lib/components/PhoneInput.svelte +92 -0
- package/src/lib/components/ResizableDivider.svelte +305 -0
- package/src/lib/components/ResizablePanel.svelte +302 -0
- package/src/lib/components/SearchDropdown.svelte +341 -0
- package/src/lib/components/SelectInput.svelte +215 -0
- package/src/lib/components/SignaturePad.svelte +171 -0
- package/src/lib/components/SortDropdown.svelte +148 -0
- package/src/lib/components/Sparkline.svelte +107 -0
- package/src/lib/components/SpeechForm.svelte +114 -0
- package/src/lib/components/StatusBadge.svelte +155 -0
- package/src/lib/components/TextArea.svelte +143 -0
- package/src/lib/components/TextInput.svelte +108 -0
- package/src/lib/components/ThemeSelector.svelte +195 -0
- package/src/lib/components/TimeSlotPicker.svelte +162 -0
- package/src/lib/components/VoicePlayer.svelte +420 -0
- package/src/lib/components/messaging/Avatar.svelte +81 -0
- package/src/lib/components/messaging/ChannelInfoModal.svelte +163 -0
- package/src/lib/components/messaging/ChannelList.svelte +107 -0
- package/src/lib/components/messaging/ChannelMemberAvatarStack.svelte +69 -0
- package/src/lib/components/messaging/ChannelMembersModal.svelte +182 -0
- package/src/lib/components/messaging/CreateChannelModal.svelte +190 -0
- package/src/lib/components/messaging/DirectMessageList.svelte +145 -0
- package/src/lib/components/messaging/EmojiSelector.svelte +260 -0
- package/src/lib/components/messaging/MentionAutocomplete.svelte +193 -0
- package/src/lib/components/messaging/MessageAttachment.svelte +270 -0
- package/src/lib/components/messaging/MessageAttachmentUpload.svelte +243 -0
- package/src/lib/components/messaging/MessageInput.svelte +451 -0
- package/src/lib/components/messaging/MessageItem.svelte +338 -0
- package/src/lib/components/messaging/MessageThread.svelte +306 -0
- package/src/lib/components/messaging/NotificationSettingsModal.svelte +234 -0
- package/src/lib/components/messaging/QuotedMessageDisplay.svelte +118 -0
- package/src/lib/components/messaging/StartDMModal.svelte +100 -0
- package/src/lib/components/messaging/ThreadPanel.svelte +153 -0
- package/src/lib/index.ts +185 -0
- package/src/lib/types/booking.ts +143 -0
- package/src/lib/types/messaging.ts +459 -0
- package/src/lib/utils/currency.ts +20 -0
- package/src/lib/utils/daisyuiColors.ts +243 -0
- package/src/lib/utils/dateFormatters.ts +153 -0
- package/src/lib/utils/mentionParser.ts +188 -0
- package/src/lib/utils/phoneFormat.ts +74 -0
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
MessageItem Component
|
|
3
|
+
|
|
4
|
+
Displays an individual message with sender info, timestamps, reactions, and actions.
|
|
5
|
+
All data operations are via callbacks — no direct API/store access.
|
|
6
|
+
-->
|
|
7
|
+
|
|
8
|
+
<script lang="ts">
|
|
9
|
+
import type {
|
|
10
|
+
Message,
|
|
11
|
+
ReactionSummary,
|
|
12
|
+
EmojiSelection,
|
|
13
|
+
StandardEmoji,
|
|
14
|
+
CustomEmoji,
|
|
15
|
+
ReactionCallbacks,
|
|
16
|
+
AttachmentCallbacks,
|
|
17
|
+
} from '../../types/messaging'
|
|
18
|
+
import { renderMentionsAsHTML, extractPlainText } from '../../utils/mentionParser'
|
|
19
|
+
import Avatar from './Avatar.svelte'
|
|
20
|
+
import QuotedMessageDisplay from './QuotedMessageDisplay.svelte'
|
|
21
|
+
import MessageAttachment from './MessageAttachment.svelte'
|
|
22
|
+
import EmojiSelector from './EmojiSelector.svelte'
|
|
23
|
+
|
|
24
|
+
interface Props {
|
|
25
|
+
message: Message
|
|
26
|
+
isOwn?: boolean
|
|
27
|
+
isConsecutive?: boolean
|
|
28
|
+
currentUserId?: string
|
|
29
|
+
currentUserRole?: 'admin' | 'moderator' | 'member'
|
|
30
|
+
channelId?: string
|
|
31
|
+
reactions?: ReactionSummary[]
|
|
32
|
+
quickReactions?: StandardEmoji[]
|
|
33
|
+
reactionCallbacks?: ReactionCallbacks
|
|
34
|
+
attachmentCallbacks?: AttachmentCallbacks
|
|
35
|
+
emojiSelectorProps?: {
|
|
36
|
+
emojis?: StandardEmoji[]
|
|
37
|
+
customEmojis?: CustomEmoji[]
|
|
38
|
+
}
|
|
39
|
+
onreply?: (data: { messageId: string }) => void
|
|
40
|
+
onedit?: (data: { messageId: string; content: string }) => void
|
|
41
|
+
ondelete?: (data: { messageId: string }) => void
|
|
42
|
+
onpin?: (data: { messageId: string; pinned: boolean }) => void
|
|
43
|
+
onopenthread?: (data: { messageId: string }) => void
|
|
44
|
+
onquotereply?: (data: { messageId: string }) => void
|
|
45
|
+
onreactionchange?: () => void
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let {
|
|
49
|
+
message,
|
|
50
|
+
isOwn = false,
|
|
51
|
+
isConsecutive = false,
|
|
52
|
+
currentUserId,
|
|
53
|
+
currentUserRole = 'member',
|
|
54
|
+
channelId,
|
|
55
|
+
reactions = [],
|
|
56
|
+
quickReactions = [],
|
|
57
|
+
reactionCallbacks,
|
|
58
|
+
attachmentCallbacks,
|
|
59
|
+
emojiSelectorProps,
|
|
60
|
+
onreply,
|
|
61
|
+
onedit,
|
|
62
|
+
ondelete,
|
|
63
|
+
onpin,
|
|
64
|
+
onopenthread,
|
|
65
|
+
onquotereply,
|
|
66
|
+
onreactionchange,
|
|
67
|
+
}: Props = $props()
|
|
68
|
+
|
|
69
|
+
let isEditing = $state(false)
|
|
70
|
+
let editContent = $state('')
|
|
71
|
+
let editTextarea = $state<HTMLTextAreaElement | undefined>(undefined)
|
|
72
|
+
let showEmojiSelector = $state(false)
|
|
73
|
+
let emojiSelectorPosition = $state({ x: 0, y: 0 })
|
|
74
|
+
|
|
75
|
+
function formatTime(dateString: string): string {
|
|
76
|
+
const date = new Date(dateString)
|
|
77
|
+
const now = new Date()
|
|
78
|
+
if (date.toDateString() === now.toDateString()) {
|
|
79
|
+
return date.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true })
|
|
80
|
+
}
|
|
81
|
+
const yesterday = new Date(now)
|
|
82
|
+
yesterday.setDate(yesterday.getDate() - 1)
|
|
83
|
+
if (date.toDateString() === yesterday.toDateString()) {
|
|
84
|
+
return 'Yesterday ' + date.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true })
|
|
85
|
+
}
|
|
86
|
+
return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit', hour12: true })
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function handleCopyMessage() {
|
|
90
|
+
const plainTextContent = extractPlainText(message.content)
|
|
91
|
+
try { await navigator.clipboard.writeText(plainTextContent) } catch {}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function handlePinToggle() {
|
|
95
|
+
onpin?.({ messageId: message.id, pinned: !message.isPinned })
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const canPin = $derived(currentUserRole === 'admin' || currentUserRole === 'moderator')
|
|
99
|
+
|
|
100
|
+
function handleReply() { onreply?.({ messageId: message.id }) }
|
|
101
|
+
function handleQuoteReply() { onquotereply?.({ messageId: message.id }) }
|
|
102
|
+
|
|
103
|
+
function handleEdit() {
|
|
104
|
+
isEditing = true
|
|
105
|
+
editContent = message.content
|
|
106
|
+
setTimeout(() => {
|
|
107
|
+
if (editTextarea) {
|
|
108
|
+
editTextarea.focus()
|
|
109
|
+
editTextarea.setSelectionRange(editContent.length, editContent.length)
|
|
110
|
+
}
|
|
111
|
+
}, 50)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function cancelEdit() { isEditing = false; editContent = '' }
|
|
115
|
+
|
|
116
|
+
function saveEdit() {
|
|
117
|
+
const trimmedContent = editContent.trim()
|
|
118
|
+
if (!trimmedContent || trimmedContent === message.content) { cancelEdit(); return }
|
|
119
|
+
onedit?.({ messageId: message.id, content: trimmedContent })
|
|
120
|
+
isEditing = false
|
|
121
|
+
editContent = ''
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function handleEditKeydown(event: KeyboardEvent) {
|
|
125
|
+
if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault(); saveEdit() }
|
|
126
|
+
else if (event.key === 'Escape') { event.preventDefault(); cancelEdit() }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function handleDelete() { ondelete?.({ messageId: message.id }) }
|
|
130
|
+
|
|
131
|
+
function handleMessageClick(event: MouseEvent) {
|
|
132
|
+
const target = event.target as HTMLElement
|
|
133
|
+
if (target.tagName === 'A' || target.tagName === 'BUTTON' || target.closest('a') || target.closest('button') || target.closest('details') || target.closest('.dropdown')) return
|
|
134
|
+
handleReply()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const renderedContent = $derived(renderMentionsAsHTML(message.content))
|
|
138
|
+
|
|
139
|
+
async function handleEmojiSelect(selection: EmojiSelection) {
|
|
140
|
+
if (!reactionCallbacks || !currentUserId) return
|
|
141
|
+
try {
|
|
142
|
+
await reactionCallbacks.addReaction(message.id, { unicode: selection.unicode, customEmojiId: selection.customEmojiId, skinTone: selection.skinTone })
|
|
143
|
+
onreactionchange?.()
|
|
144
|
+
} catch {}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async function toggleReaction(reaction: ReactionSummary) {
|
|
148
|
+
if (!reactionCallbacks || !currentUserId) return
|
|
149
|
+
try {
|
|
150
|
+
if (reaction.userReacted) {
|
|
151
|
+
await reactionCallbacks.removeReaction(message.id, { unicode: reaction.emojiUnicode, customEmojiId: reaction.customEmojiId })
|
|
152
|
+
} else {
|
|
153
|
+
await reactionCallbacks.addReaction(message.id, { unicode: reaction.emojiUnicode, customEmojiId: reaction.customEmojiId })
|
|
154
|
+
}
|
|
155
|
+
onreactionchange?.()
|
|
156
|
+
} catch {}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function addQuickReaction(emoji: string, event: MouseEvent) {
|
|
160
|
+
event.stopPropagation()
|
|
161
|
+
if (!reactionCallbacks || !currentUserId) return
|
|
162
|
+
try {
|
|
163
|
+
await reactionCallbacks.addReaction(message.id, { unicode: emoji })
|
|
164
|
+
onreactionchange?.()
|
|
165
|
+
} catch {}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function showEmojiPicker(event: MouseEvent) {
|
|
169
|
+
const rect = (event.target as HTMLElement).getBoundingClientRect()
|
|
170
|
+
const messageContainer = (event.target as HTMLElement).closest('.group')
|
|
171
|
+
const messageRect = messageContainer?.getBoundingClientRect() || rect
|
|
172
|
+
const selectorWidth = 352
|
|
173
|
+
const selectorHeight = 320
|
|
174
|
+
const margin = 10
|
|
175
|
+
let x = messageRect.right - selectorWidth
|
|
176
|
+
let y = rect.bottom + margin
|
|
177
|
+
if (x + selectorWidth > window.innerWidth - margin) x = window.innerWidth - selectorWidth - margin
|
|
178
|
+
if (x < margin) x = margin
|
|
179
|
+
if (y + selectorHeight > window.innerHeight - margin) y = rect.top - selectorHeight - margin
|
|
180
|
+
emojiSelectorPosition = { x, y }
|
|
181
|
+
setTimeout(() => { showEmojiSelector = true }, 0)
|
|
182
|
+
}
|
|
183
|
+
</script>
|
|
184
|
+
|
|
185
|
+
<div
|
|
186
|
+
class="group flex gap-3 px-2 py-1 rounded relative transition-colors w-full text-left cursor-pointer {message.isPinned ? 'bg-warning/5 hover:bg-warning/10 border-l-4 border-warning pl-1' : 'hover:bg-base-200'}"
|
|
187
|
+
class:pt-2={!isConsecutive}
|
|
188
|
+
data-message-id={message.id}
|
|
189
|
+
onclick={handleMessageClick}
|
|
190
|
+
role="button"
|
|
191
|
+
tabindex="0"
|
|
192
|
+
onkeydown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); handleMessageClick(e as any) } }}
|
|
193
|
+
>
|
|
194
|
+
<!-- Avatar -->
|
|
195
|
+
<div class="w-8 flex-shrink-0">
|
|
196
|
+
{#if !isConsecutive}
|
|
197
|
+
<Avatar name={message.senderName} avatarUrl={message.senderAvatar} size="small" />
|
|
198
|
+
{:else}
|
|
199
|
+
<div class="text-xs text-base-content/40 opacity-0 group-hover:opacity-100 transition-opacity text-center pt-0.5">
|
|
200
|
+
{formatTime(message.createdAt).split(' ')[1] || ''}
|
|
201
|
+
</div>
|
|
202
|
+
{/if}
|
|
203
|
+
</div>
|
|
204
|
+
|
|
205
|
+
<!-- Content -->
|
|
206
|
+
<div class="flex-1 min-w-0 relative">
|
|
207
|
+
<!-- Floating action buttons -->
|
|
208
|
+
<div class="absolute right-0 {!isConsecutive ? 'top-0' : 'top-1'} opacity-0 group-hover:opacity-100 transition-opacity bg-base-100/90 backdrop-blur-sm rounded-lg shadow-lg border border-base-300 p-1 z-10">
|
|
209
|
+
<div class="flex items-center">
|
|
210
|
+
{#if reactionCallbacks && currentUserId}
|
|
211
|
+
<div class="flex items-center gap-1 mr-1">
|
|
212
|
+
{#each quickReactions as emoji}
|
|
213
|
+
<button class="btn btn-ghost btn-xs btn-square hover:bg-base-300" onclick={(e) => addQuickReaction(emoji.unicode, e)} title="Add {emoji.unicode} reaction">{emoji.unicode}</button>
|
|
214
|
+
{/each}
|
|
215
|
+
<button class="btn btn-ghost btn-xs btn-square hover:bg-base-300" onclick={showEmojiPicker} title="Add reaction...">😊</button>
|
|
216
|
+
</div>
|
|
217
|
+
{/if}
|
|
218
|
+
<div class="flex items-center gap-1">
|
|
219
|
+
<button class="btn btn-ghost btn-xs btn-square hover:bg-base-300" onclick={handleQuoteReply} title="Quote reply">💬</button>
|
|
220
|
+
<button class="btn btn-ghost btn-xs btn-square hover:bg-base-300" onclick={handleReply} title="Reply in thread">↩</button>
|
|
221
|
+
{#if canPin}
|
|
222
|
+
<button class="btn btn-ghost btn-xs btn-square hover:bg-base-300" onclick={handlePinToggle} title={message.isPinned ? 'Unpin' : 'Pin'}>{message.isPinned ? '📌' : '📍'}</button>
|
|
223
|
+
{/if}
|
|
224
|
+
{#if isOwn}
|
|
225
|
+
<button class="btn btn-ghost btn-xs btn-square hover:bg-base-300" onclick={handleEdit} title="Edit">✏</button>
|
|
226
|
+
{/if}
|
|
227
|
+
<button class="btn btn-ghost btn-xs btn-square hover:bg-base-300" onclick={handleCopyMessage} title="Copy">📋</button>
|
|
228
|
+
{#if isOwn}
|
|
229
|
+
<button class="btn btn-ghost btn-xs btn-square hover:bg-base-300 text-error hover:text-error" onclick={handleDelete} title="Delete">🗑</button>
|
|
230
|
+
{/if}
|
|
231
|
+
</div>
|
|
232
|
+
</div>
|
|
233
|
+
</div>
|
|
234
|
+
|
|
235
|
+
<!-- Sender + timestamp -->
|
|
236
|
+
{#if !isConsecutive}
|
|
237
|
+
<div class="flex items-center gap-2 mb-1">
|
|
238
|
+
<span class="font-medium text-sm" class:text-primary={isOwn}>{message.senderName}</span>
|
|
239
|
+
<span class="text-xs text-base-content/50">{formatTime(message.createdAt)}</span>
|
|
240
|
+
{#if message.editedAt}<span class="text-xs text-base-content/40">(edited)</span>{/if}
|
|
241
|
+
{#if isOwn && message.readByCount !== undefined}
|
|
242
|
+
{#if message.readByCount > 0}
|
|
243
|
+
<span class="text-[11px] text-success/60" title="Read by {message.readByCount}">✓✓</span>
|
|
244
|
+
{:else}
|
|
245
|
+
<span class="text-[11px] text-base-content/30" title="Delivered">✓</span>
|
|
246
|
+
{/if}
|
|
247
|
+
{/if}
|
|
248
|
+
</div>
|
|
249
|
+
{/if}
|
|
250
|
+
|
|
251
|
+
<!-- Pinned indicator -->
|
|
252
|
+
{#if message.isPinned}
|
|
253
|
+
<div class="flex items-center gap-1 mb-2">
|
|
254
|
+
<span class="text-warning text-xs">📌</span>
|
|
255
|
+
<span class="text-xs text-warning font-medium">Pinned Message</span>
|
|
256
|
+
{#if message.pinnedAt}<span class="text-xs text-base-content/50">• {formatTime(message.pinnedAt)}</span>{/if}
|
|
257
|
+
</div>
|
|
258
|
+
{/if}
|
|
259
|
+
|
|
260
|
+
<!-- Edit form or content -->
|
|
261
|
+
{#if isEditing}
|
|
262
|
+
<div class="space-y-2">
|
|
263
|
+
<textarea bind:this={editTextarea} bind:value={editContent} class="textarea textarea-bordered w-full text-sm min-h-[60px] resize-none" placeholder="Edit your message..." onkeydown={handleEditKeydown}></textarea>
|
|
264
|
+
<div class="flex gap-2">
|
|
265
|
+
<button class="btn btn-primary btn-xs" onclick={saveEdit}>Save</button>
|
|
266
|
+
<button class="btn btn-ghost btn-xs" onclick={cancelEdit}>Cancel</button>
|
|
267
|
+
<span class="text-xs text-base-content/60 self-center">Enter to save • Escape to cancel</span>
|
|
268
|
+
</div>
|
|
269
|
+
</div>
|
|
270
|
+
{:else}
|
|
271
|
+
{#if message.quotedMessage}
|
|
272
|
+
<QuotedMessageDisplay quotedMessage={message.quotedMessage} mode="inline" class="mb-2" />
|
|
273
|
+
{/if}
|
|
274
|
+
|
|
275
|
+
{#if message.content}
|
|
276
|
+
<div class="text-sm whitespace-pre-wrap break-words message-content">
|
|
277
|
+
{@html renderedContent}
|
|
278
|
+
</div>
|
|
279
|
+
{/if}
|
|
280
|
+
|
|
281
|
+
<!-- Attachments -->
|
|
282
|
+
{#if message.attachments && message.attachments.length > 0 && attachmentCallbacks}
|
|
283
|
+
<div class="flex flex-wrap gap-2 mt-2">
|
|
284
|
+
{#each message.attachments as att (att.id)}
|
|
285
|
+
<MessageAttachment attachment={att} callbacks={attachmentCallbacks} {currentUserId} showDelete={isOwn} compact={message.attachments.length > 2} />
|
|
286
|
+
{/each}
|
|
287
|
+
</div>
|
|
288
|
+
{/if}
|
|
289
|
+
|
|
290
|
+
<!-- Thread indicator -->
|
|
291
|
+
{#if message.thread_reply_count && message.thread_reply_count > 0}
|
|
292
|
+
<button type="button" class="mt-2 p-2 bg-base-200/50 rounded-lg border-l-2 border-primary/30 hover:bg-base-300/50 transition-colors w-full text-left" onclick={(e) => { e.stopPropagation(); handleReply() }}>
|
|
293
|
+
<div class="flex items-center gap-2 text-xs">
|
|
294
|
+
{#if message.thread_last_sender}
|
|
295
|
+
<Avatar name={message.thread_last_sender.full_name} avatarUrl={message.thread_last_sender.avatar_url} size="xs" />
|
|
296
|
+
{/if}
|
|
297
|
+
<div class="flex items-center gap-1 text-primary">
|
|
298
|
+
<span class="font-medium">{message.thread_reply_count} {message.thread_reply_count === 1 ? 'reply' : 'replies'}</span>
|
|
299
|
+
{#if message.thread_last_reply_at}<span class="text-base-content/60">• Last reply {formatTime(message.thread_last_reply_at)}</span>{/if}
|
|
300
|
+
</div>
|
|
301
|
+
</div>
|
|
302
|
+
</button>
|
|
303
|
+
{/if}
|
|
304
|
+
|
|
305
|
+
<!-- Reactions -->
|
|
306
|
+
{#if reactions.length > 0}
|
|
307
|
+
<div class="flex flex-wrap gap-1 mt-2 items-center">
|
|
308
|
+
{#each reactions as reaction}
|
|
309
|
+
<button
|
|
310
|
+
class="reaction-pill inline-flex items-center gap-1 px-2 py-1 rounded-full text-xs border transition-colors {reaction.userReacted ? 'bg-primary/10 border-primary text-primary' : 'bg-base-200 border-base-300 hover:bg-base-300'}"
|
|
311
|
+
onclick={() => toggleReaction(reaction)}
|
|
312
|
+
title="Toggle reaction"
|
|
313
|
+
>
|
|
314
|
+
<span class="text-sm">{reaction.emojiUnicode || '🎨'}</span>
|
|
315
|
+
<span class="font-medium">{reaction.reactionCount}</span>
|
|
316
|
+
</button>
|
|
317
|
+
{/each}
|
|
318
|
+
</div>
|
|
319
|
+
{/if}
|
|
320
|
+
|
|
321
|
+
{#if message.editedAt && isConsecutive}
|
|
322
|
+
<span class="text-xs text-base-content/40">(edited)</span>
|
|
323
|
+
{/if}
|
|
324
|
+
{/if}
|
|
325
|
+
</div>
|
|
326
|
+
</div>
|
|
327
|
+
|
|
328
|
+
<!-- Emoji Selector -->
|
|
329
|
+
{#if showEmojiSelector && currentUserId}
|
|
330
|
+
<EmojiSelector
|
|
331
|
+
visible={showEmojiSelector}
|
|
332
|
+
position={emojiSelectorPosition}
|
|
333
|
+
emojis={emojiSelectorProps?.emojis}
|
|
334
|
+
customEmojis={emojiSelectorProps?.customEmojis}
|
|
335
|
+
onselect={(selection) => { handleEmojiSelect(selection); showEmojiSelector = false }}
|
|
336
|
+
onclose={() => (showEmojiSelector = false)}
|
|
337
|
+
/>
|
|
338
|
+
{/if}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
MessageThread Component
|
|
3
|
+
|
|
4
|
+
Displays a conversation thread (channel or DM).
|
|
5
|
+
All data operations are via callbacks — no Supabase/realtime dependencies.
|
|
6
|
+
The consuming app is responsible for:
|
|
7
|
+
- Loading messages (initial + pagination)
|
|
8
|
+
- Sending messages
|
|
9
|
+
- Subscribing to realtime updates and calling onNewMessage
|
|
10
|
+
- Presence tracking
|
|
11
|
+
-->
|
|
12
|
+
|
|
13
|
+
<script lang="ts">
|
|
14
|
+
import { onMount } from 'svelte'
|
|
15
|
+
import type {
|
|
16
|
+
Message,
|
|
17
|
+
QuotedMessage,
|
|
18
|
+
ChannelMember,
|
|
19
|
+
ReactionSummary,
|
|
20
|
+
StandardEmoji,
|
|
21
|
+
PendingAttachment,
|
|
22
|
+
PresenceState,
|
|
23
|
+
MentionItem,
|
|
24
|
+
MessageCallbacks,
|
|
25
|
+
ReactionCallbacks,
|
|
26
|
+
AttachmentCallbacks,
|
|
27
|
+
} from '../../types/messaging'
|
|
28
|
+
import MessageItem from './MessageItem.svelte'
|
|
29
|
+
import MessageInput from './MessageInput.svelte'
|
|
30
|
+
import ChannelMemberAvatarStack from './ChannelMemberAvatarStack.svelte'
|
|
31
|
+
import Avatar from './Avatar.svelte'
|
|
32
|
+
|
|
33
|
+
interface Props {
|
|
34
|
+
type: 'channel' | 'dm'
|
|
35
|
+
channelId?: string
|
|
36
|
+
channelName?: string
|
|
37
|
+
channelEmoji?: string
|
|
38
|
+
dmUserId?: string
|
|
39
|
+
dmUserName?: string
|
|
40
|
+
dmUserAvatar?: string
|
|
41
|
+
currentUser: {
|
|
42
|
+
id: string
|
|
43
|
+
full_name: string
|
|
44
|
+
avatar_url?: string
|
|
45
|
+
}
|
|
46
|
+
messages?: Message[]
|
|
47
|
+
members?: ChannelMember[]
|
|
48
|
+
presenceState?: PresenceState
|
|
49
|
+
quickReactions?: StandardEmoji[]
|
|
50
|
+
messageCallbacks: MessageCallbacks
|
|
51
|
+
reactionCallbacks?: ReactionCallbacks
|
|
52
|
+
attachmentCallbacks?: AttachmentCallbacks
|
|
53
|
+
getReactions?: (messageId: string) => ReactionSummary[]
|
|
54
|
+
onsearchmentions?: (query: string) => Promise<MentionItem[]>
|
|
55
|
+
onmessagesent?: (message: Message) => void
|
|
56
|
+
onopenthread?: (messageId: string) => void
|
|
57
|
+
onopensidebar?: () => void
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let {
|
|
61
|
+
type,
|
|
62
|
+
channelId,
|
|
63
|
+
channelName,
|
|
64
|
+
channelEmoji,
|
|
65
|
+
dmUserId,
|
|
66
|
+
dmUserName,
|
|
67
|
+
dmUserAvatar,
|
|
68
|
+
currentUser,
|
|
69
|
+
messages = [],
|
|
70
|
+
members = [],
|
|
71
|
+
presenceState = {},
|
|
72
|
+
quickReactions = [],
|
|
73
|
+
messageCallbacks,
|
|
74
|
+
reactionCallbacks,
|
|
75
|
+
attachmentCallbacks,
|
|
76
|
+
getReactions,
|
|
77
|
+
onsearchmentions,
|
|
78
|
+
onmessagesent,
|
|
79
|
+
onopenthread,
|
|
80
|
+
onopensidebar,
|
|
81
|
+
}: Props = $props()
|
|
82
|
+
|
|
83
|
+
let messagesContainer: HTMLElement
|
|
84
|
+
let loading = $state(false)
|
|
85
|
+
let sending = $state(false)
|
|
86
|
+
let quotedMessage = $state<QuotedMessage | undefined>(undefined)
|
|
87
|
+
let hasMoreMessages = $state(true)
|
|
88
|
+
let showSidebar = $state(false)
|
|
89
|
+
|
|
90
|
+
function isConsecutive(msg: Message, index: number): boolean {
|
|
91
|
+
if (index === 0) return false
|
|
92
|
+
const prev = messages[index - 1]
|
|
93
|
+
if (!prev || prev.senderId !== msg.senderId) return false
|
|
94
|
+
const diff = new Date(msg.createdAt).getTime() - new Date(prev.createdAt).getTime()
|
|
95
|
+
return diff < 5 * 60 * 1000 // 5 minutes
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function handleSend(data: {
|
|
99
|
+
content: string
|
|
100
|
+
priority?: string
|
|
101
|
+
quotedMessageId?: string
|
|
102
|
+
attachments?: PendingAttachment[]
|
|
103
|
+
}) {
|
|
104
|
+
if (sending) return
|
|
105
|
+
sending = true
|
|
106
|
+
try {
|
|
107
|
+
const newMessage = await messageCallbacks.sendMessage({
|
|
108
|
+
content: data.content,
|
|
109
|
+
channelId: type === 'channel' ? channelId : undefined,
|
|
110
|
+
dmUserId: type === 'dm' ? dmUserId : undefined,
|
|
111
|
+
quotedMessageId: data.quotedMessageId,
|
|
112
|
+
attachments: data.attachments,
|
|
113
|
+
priority: data.priority,
|
|
114
|
+
})
|
|
115
|
+
onmessagesent?.(newMessage)
|
|
116
|
+
quotedMessage = undefined
|
|
117
|
+
scrollToBottom()
|
|
118
|
+
} catch (err) {
|
|
119
|
+
console.error('Failed to send message:', err)
|
|
120
|
+
} finally {
|
|
121
|
+
sending = false
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function handleEdit(data: { messageId: string; content: string }) {
|
|
126
|
+
try {
|
|
127
|
+
await messageCallbacks.editMessage(data.messageId, data.content)
|
|
128
|
+
} catch (err) {
|
|
129
|
+
console.error('Failed to edit message:', err)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function handleDelete(data: { messageId: string }) {
|
|
134
|
+
if (!confirm('Delete this message?')) return
|
|
135
|
+
try {
|
|
136
|
+
await messageCallbacks.deleteMessage(data.messageId)
|
|
137
|
+
} catch (err) {
|
|
138
|
+
console.error('Failed to delete message:', err)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function handlePin(data: { messageId: string; pinned: boolean }) {
|
|
143
|
+
try {
|
|
144
|
+
if (data.pinned) await messageCallbacks.pinMessage(data.messageId, channelId || '')
|
|
145
|
+
else await messageCallbacks.unpinMessage(data.messageId, channelId || '')
|
|
146
|
+
} catch (err) {
|
|
147
|
+
console.error('Failed to toggle pin:', err)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function handleQuoteReply(data: { messageId: string }) {
|
|
152
|
+
const msg = messages.find((m) => m.id === data.messageId)
|
|
153
|
+
if (msg) {
|
|
154
|
+
quotedMessage = {
|
|
155
|
+
id: msg.id,
|
|
156
|
+
content: msg.content,
|
|
157
|
+
senderId: msg.senderId,
|
|
158
|
+
senderName: msg.senderName,
|
|
159
|
+
senderAvatar: msg.senderAvatar,
|
|
160
|
+
createdAt: msg.createdAt,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function handleOpenThread(data: { messageId: string }) {
|
|
166
|
+
onopenthread?.(data.messageId)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function loadOlderMessages() {
|
|
170
|
+
if (loading || !hasMoreMessages || messages.length === 0) return
|
|
171
|
+
loading = true
|
|
172
|
+
try {
|
|
173
|
+
const olderMessages = await messageCallbacks.loadMessages({
|
|
174
|
+
channelId: type === 'channel' ? channelId : undefined,
|
|
175
|
+
dmUserId: type === 'dm' ? dmUserId : undefined,
|
|
176
|
+
before: messages[0]?.createdAt,
|
|
177
|
+
limit: 50,
|
|
178
|
+
})
|
|
179
|
+
if (olderMessages.length === 0) hasMoreMessages = false
|
|
180
|
+
} catch (err) {
|
|
181
|
+
console.error('Failed to load older messages:', err)
|
|
182
|
+
} finally {
|
|
183
|
+
loading = false
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function scrollToBottom() {
|
|
188
|
+
setTimeout(() => {
|
|
189
|
+
if (messagesContainer) {
|
|
190
|
+
messagesContainer.scrollTop = messagesContainer.scrollHeight
|
|
191
|
+
}
|
|
192
|
+
}, 50)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function handleScroll() {
|
|
196
|
+
if (messagesContainer && messagesContainer.scrollTop < 100) {
|
|
197
|
+
loadOlderMessages()
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Scroll to bottom on mount and when new messages arrive
|
|
202
|
+
onMount(scrollToBottom)
|
|
203
|
+
$effect(() => {
|
|
204
|
+
if (messages.length > 0) {
|
|
205
|
+
const lastMsg = messages[messages.length - 1]
|
|
206
|
+
if (lastMsg.senderId === currentUser.id) scrollToBottom()
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
</script>
|
|
210
|
+
|
|
211
|
+
<div class="flex flex-col h-full">
|
|
212
|
+
<!-- Header -->
|
|
213
|
+
<div class="flex items-center justify-between px-4 py-3 border-b border-base-300">
|
|
214
|
+
<div class="flex items-center gap-3">
|
|
215
|
+
{#if type === 'channel'}
|
|
216
|
+
<span class="text-lg">{channelEmoji || '#'}</span>
|
|
217
|
+
<div>
|
|
218
|
+
<h2 class="font-semibold text-sm">{channelName}</h2>
|
|
219
|
+
<span class="text-xs text-base-content/50">{members.length} members</span>
|
|
220
|
+
</div>
|
|
221
|
+
{:else}
|
|
222
|
+
<Avatar name={dmUserName || ''} avatarUrl={dmUserAvatar} size="medium" />
|
|
223
|
+
<div>
|
|
224
|
+
<h2 class="font-semibold text-sm">{dmUserName}</h2>
|
|
225
|
+
<span class="text-xs text-base-content/50">
|
|
226
|
+
{presenceState[dmUserId || '']?.status === 'online' ? 'Online' : 'Offline'}
|
|
227
|
+
</span>
|
|
228
|
+
</div>
|
|
229
|
+
{/if}
|
|
230
|
+
</div>
|
|
231
|
+
|
|
232
|
+
<div class="flex items-center gap-2">
|
|
233
|
+
{#if type === 'channel' && members.length > 0}
|
|
234
|
+
<ChannelMemberAvatarStack {members} {presenceState} maxVisible={3} size="xs" />
|
|
235
|
+
{/if}
|
|
236
|
+
{#if onopensidebar}
|
|
237
|
+
<button class="btn btn-ghost btn-sm btn-square" onclick={onopensidebar} title="Toggle sidebar">
|
|
238
|
+
☰
|
|
239
|
+
</button>
|
|
240
|
+
{/if}
|
|
241
|
+
</div>
|
|
242
|
+
</div>
|
|
243
|
+
|
|
244
|
+
<!-- Messages -->
|
|
245
|
+
<div
|
|
246
|
+
bind:this={messagesContainer}
|
|
247
|
+
class="flex-1 overflow-y-auto px-4 py-2"
|
|
248
|
+
onscroll={handleScroll}
|
|
249
|
+
>
|
|
250
|
+
{#if loading && messages.length === 0}
|
|
251
|
+
<div class="flex items-center justify-center h-full">
|
|
252
|
+
<span class="loading loading-spinner loading-md"></span>
|
|
253
|
+
</div>
|
|
254
|
+
{:else if messages.length === 0}
|
|
255
|
+
<div class="flex flex-col items-center justify-center h-full text-base-content/50">
|
|
256
|
+
<div class="text-4xl mb-4">{type === 'channel' ? '#' : '💬'}</div>
|
|
257
|
+
<h3 class="text-lg font-medium">
|
|
258
|
+
{#if type === 'channel'}
|
|
259
|
+
Welcome to #{channelName}
|
|
260
|
+
{:else}
|
|
261
|
+
Start a conversation with {dmUserName}
|
|
262
|
+
{/if}
|
|
263
|
+
</h3>
|
|
264
|
+
<p class="text-sm">Send a message to get started.</p>
|
|
265
|
+
</div>
|
|
266
|
+
{:else}
|
|
267
|
+
{#if loading}
|
|
268
|
+
<div class="flex justify-center py-4">
|
|
269
|
+
<span class="loading loading-spinner loading-sm"></span>
|
|
270
|
+
</div>
|
|
271
|
+
{/if}
|
|
272
|
+
|
|
273
|
+
{#each messages as msg, index (msg.id)}
|
|
274
|
+
<MessageItem
|
|
275
|
+
message={msg}
|
|
276
|
+
isOwn={msg.senderId === currentUser.id}
|
|
277
|
+
isConsecutive={isConsecutive(msg, index)}
|
|
278
|
+
currentUserId={currentUser.id}
|
|
279
|
+
{channelId}
|
|
280
|
+
reactions={getReactions?.(msg.id) || []}
|
|
281
|
+
{quickReactions}
|
|
282
|
+
{reactionCallbacks}
|
|
283
|
+
{attachmentCallbacks}
|
|
284
|
+
onreply={(d) => handleOpenThread(d)}
|
|
285
|
+
onedit={handleEdit}
|
|
286
|
+
ondelete={handleDelete}
|
|
287
|
+
onpin={handlePin}
|
|
288
|
+
onopenthread={(d) => handleOpenThread(d)}
|
|
289
|
+
onquotereply={handleQuoteReply}
|
|
290
|
+
/>
|
|
291
|
+
{/each}
|
|
292
|
+
{/if}
|
|
293
|
+
</div>
|
|
294
|
+
|
|
295
|
+
<!-- Input -->
|
|
296
|
+
<div class="px-4 py-3 border-t border-base-300">
|
|
297
|
+
<MessageInput
|
|
298
|
+
{quotedMessage}
|
|
299
|
+
disabled={sending}
|
|
300
|
+
onsend={handleSend}
|
|
301
|
+
onquoteremove={() => (quotedMessage = undefined)}
|
|
302
|
+
{onsearchmentions}
|
|
303
|
+
{attachmentCallbacks}
|
|
304
|
+
/>
|
|
305
|
+
</div>
|
|
306
|
+
</div>
|