@poolse/react-ui 0.1.2 → 0.2.0-alpha.2

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/dist/index.cjs CHANGED
@@ -246,31 +246,164 @@ function PoolseIcon({
246
246
  }
247
247
  );
248
248
  }
249
- function MessageBubble({ message, currentUserId, readState }) {
250
- const isSelf = currentUserId !== null && message.sender_id === currentUserId;
251
- const className = [
252
- "poolse-message",
253
- isSelf ? "poolse-message--self" : "poolse-message--other"
254
- ].join(" ");
255
- const time = message.inserted_at ? new Date(message.inserted_at).toLocaleTimeString(void 0, {
256
- hour: "2-digit",
257
- minute: "2-digit"
258
- }) : "";
259
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className, children: [
260
- message.deleted_at ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-message__body--deleted", children: "[deleted]" }) : /* @__PURE__ */ jsxRuntime.jsx("span", { children: message.body ?? "" }),
261
- /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "poolse-message__meta", children: [
262
- /* @__PURE__ */ jsxRuntime.jsx("span", { children: time }),
263
- message.edited_at ? /* @__PURE__ */ jsxRuntime.jsx("span", { children: " \xB7 edited" }) : null,
264
- isSelf && readState ? /* @__PURE__ */ jsxRuntime.jsx(
265
- PoolseIcon,
266
- {
267
- name: readState === "read" ? "check-double" : "check",
268
- size: 14,
269
- label: readState === "read" ? "Read" : "Sent"
249
+ function MentionInput({
250
+ conversationId,
251
+ onSend,
252
+ onTyping,
253
+ placeholder = "Type a message\u2026",
254
+ disabled = false,
255
+ labelFor
256
+ }) {
257
+ const { members } = react$1.useMembers(conversationId);
258
+ const [value, setValue] = react.useState("");
259
+ const [sending, setSending] = react.useState(false);
260
+ const [mention, setMention] = react.useState(null);
261
+ const [activeIdx, setActiveIdx] = react.useState(0);
262
+ const selectedMentions = react.useRef(/* @__PURE__ */ new Set());
263
+ const taRef = react.useRef(null);
264
+ const label = labelFor ?? ((id) => id.slice(0, 8));
265
+ const filtered = mention ? members.filter((m) => label(m.user_id).toLowerCase().includes(mention.query.toLowerCase())) : [];
266
+ react.useEffect(() => {
267
+ setActiveIdx(0);
268
+ }, [filtered.length, mention?.query]);
269
+ const submit = async () => {
270
+ const trimmed = value.trim();
271
+ if (!trimmed || sending) return;
272
+ setSending(true);
273
+ try {
274
+ const mentions = Array.from(selectedMentions.current);
275
+ await onSend({
276
+ body: trimmed,
277
+ ...mentions.length > 0 ? { mentions } : {}
278
+ });
279
+ setValue("");
280
+ selectedMentions.current = /* @__PURE__ */ new Set();
281
+ } finally {
282
+ setSending(false);
283
+ }
284
+ };
285
+ const insertMention = (m) => {
286
+ if (!mention) return;
287
+ const name = label(m.user_id);
288
+ const before = value.slice(0, mention.start);
289
+ const after = value.slice(mention.start + 1 + mention.query.length);
290
+ const inserted = `@${name} `;
291
+ const next = `${before}${inserted}${after}`;
292
+ selectedMentions.current.add(m.user_id);
293
+ setValue(next);
294
+ setMention(null);
295
+ requestAnimationFrame(() => {
296
+ const ta = taRef.current;
297
+ if (!ta) return;
298
+ const pos = before.length + inserted.length;
299
+ ta.focus();
300
+ ta.setSelectionRange(pos, pos);
301
+ });
302
+ };
303
+ const onChange = (e) => {
304
+ const next = e.target.value;
305
+ setValue(next);
306
+ onTyping?.();
307
+ const caret = e.target.selectionStart ?? next.length;
308
+ const head = next.slice(0, caret);
309
+ const at = head.lastIndexOf("@");
310
+ if (at === -1) {
311
+ setMention(null);
312
+ return;
313
+ }
314
+ const charBefore = at === 0 ? " " : head.charAt(at - 1);
315
+ const isAtBoundary = /\s/.test(charBefore);
316
+ const query = head.slice(at + 1);
317
+ if (isAtBoundary && !/\s/.test(query)) {
318
+ setMention({ start: at, query });
319
+ } else {
320
+ setMention(null);
321
+ }
322
+ };
323
+ const onKeyDown = (e) => {
324
+ if (mention && filtered.length > 0) {
325
+ if (e.key === "ArrowDown") {
326
+ e.preventDefault();
327
+ setActiveIdx((i) => (i + 1) % filtered.length);
328
+ return;
329
+ }
330
+ if (e.key === "ArrowUp") {
331
+ e.preventDefault();
332
+ setActiveIdx((i) => (i - 1 + filtered.length) % filtered.length);
333
+ return;
334
+ }
335
+ if (e.key === "Enter" || e.key === "Tab") {
336
+ const pick = filtered[activeIdx];
337
+ if (pick) {
338
+ e.preventDefault();
339
+ insertMention(pick);
340
+ return;
270
341
  }
271
- ) : null
272
- ] })
273
- ] });
342
+ }
343
+ if (e.key === "Escape") {
344
+ e.preventDefault();
345
+ setMention(null);
346
+ return;
347
+ }
348
+ }
349
+ if (e.key === "Enter" && !e.shiftKey) {
350
+ e.preventDefault();
351
+ void submit();
352
+ }
353
+ };
354
+ return /* @__PURE__ */ jsxRuntime.jsxs(
355
+ "form",
356
+ {
357
+ className: "poolse-composer",
358
+ onSubmit: (e) => {
359
+ e.preventDefault();
360
+ void submit();
361
+ },
362
+ style: { position: "relative" },
363
+ children: [
364
+ /* @__PURE__ */ jsxRuntime.jsx(
365
+ "textarea",
366
+ {
367
+ ref: taRef,
368
+ className: "poolse-composer__input",
369
+ rows: 1,
370
+ value,
371
+ onChange,
372
+ onKeyDown,
373
+ placeholder,
374
+ disabled: disabled || sending
375
+ }
376
+ ),
377
+ /* @__PURE__ */ jsxRuntime.jsx(
378
+ "button",
379
+ {
380
+ type: "submit",
381
+ className: "poolse-composer__send",
382
+ disabled: disabled || sending || value.trim() === "",
383
+ "aria-label": "Send message",
384
+ children: /* @__PURE__ */ jsxRuntime.jsx(PoolseIcon, { name: "send-fill", label: null })
385
+ }
386
+ ),
387
+ mention && filtered.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-mention-menu", role: "listbox", "aria-label": "Members", children: filtered.map((m, i) => /* @__PURE__ */ jsxRuntime.jsx(
388
+ "div",
389
+ {
390
+ role: "option",
391
+ "aria-selected": i === activeIdx,
392
+ className: `poolse-mention-menu__item${i === activeIdx ? " poolse-mention-menu__item--active" : ""}`,
393
+ onMouseDown: (e) => {
394
+ e.preventDefault();
395
+ insertMention(m);
396
+ },
397
+ children: /* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
398
+ "@",
399
+ label(m.user_id)
400
+ ] })
401
+ },
402
+ m.id
403
+ )) })
404
+ ]
405
+ }
406
+ );
274
407
  }
275
408
  function MessageComposer({
276
409
  onSend,
@@ -336,240 +469,11 @@ function MessageComposer({
336
469
  }
337
470
  );
338
471
  }
339
- function TypingIndicator({ typing, labelFor }) {
340
- const ids = Array.from(typing);
341
- if (ids.length === 0) {
342
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-typing", "aria-hidden": "true" });
343
- }
344
- const label = labelFor ?? ((id) => id.slice(0, 6));
345
- const names = ids.slice(0, 2).map(label);
346
- let text;
347
- if (ids.length === 1) text = `${names[0]} is typing`;
348
- else if (ids.length === 2) text = `${names[0]} and ${names[1]} are typing`;
349
- else text = `${ids.length} people are typing`;
350
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-typing", children: [
351
- /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "poolse-typing__dots", role: "status", "aria-live": "polite", children: [
352
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-typing__dot" }),
353
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-typing__dot" }),
354
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-typing__dot" })
355
- ] }),
356
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-typing__label", children: text })
357
- ] });
358
- }
359
- function ConversationView({
360
- conversationId,
361
- emptyState,
362
- renderMessage,
363
- labelFor,
364
- loadFonts = true
365
- }) {
366
- usePoolseFonts(loadFonts);
367
- const { me } = react$1.useMe();
368
- const { messages, loading, error, hasMore, loadMore, send } = react$1.useMessages(conversationId);
369
- const { typing, signalTyping } = react$1.useTyping(conversationId);
370
- const status = react$1.useRealtimeStatus();
371
- const listRef = react.useRef(null);
372
- const lastMessageIdRef = react.useRef(null);
373
- react.useEffect(() => {
374
- const tail = messages[messages.length - 1];
375
- if (!tail) return;
376
- if (tail.id === lastMessageIdRef.current) return;
377
- lastMessageIdRef.current = tail.id;
378
- const el = listRef.current;
379
- if (el) el.scrollTop = el.scrollHeight;
380
- }, [messages]);
381
- const renderOne = renderMessage ?? defaultRenderMessage;
382
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation", children: [
383
- status !== "connected" && status !== "idle" && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation__status", children: [
384
- status === "connecting" && "Connecting\u2026",
385
- status === "reconnecting" && "Reconnecting\u2026",
386
- status === "closed" && "Disconnected"
387
- ] }),
388
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation__messages", ref: listRef, children: [
389
- hasMore && !loading && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: "poolse-conversation__load-more", onClick: loadMore, children: "Load older messages" }),
390
- loading && messages.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-conversation__empty", children: "Loading\u2026" }) : messages.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-conversation__empty", children: emptyState ?? "No messages yet." }) : (
391
- // Fragment, not a wrapper <div>: MessageBubble's
392
- // `align-self: flex-end|flex-start` only works when the
393
- // bubble is a DIRECT flex child of `.poolse-conversation__
394
- // messages`. A wrapper div would block-default to 100%
395
- // width and pin every bubble to the left.
396
- messages.map((msg) => /* @__PURE__ */ jsxRuntime.jsx(react.Fragment, { children: renderOne(msg, me?.id ?? null) }, msg.id))
397
- ),
398
- error && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation__empty", children: [
399
- "Failed to load: ",
400
- error.message
401
- ] })
402
- ] }),
403
- /* @__PURE__ */ jsxRuntime.jsx(TypingIndicator, { typing, ...labelFor ? { labelFor } : {} }),
404
- /* @__PURE__ */ jsxRuntime.jsx(MessageComposer, { onSend: (body) => send({ body }), onTyping: signalTyping })
405
- ] });
406
- }
407
- function defaultRenderMessage(msg, currentUserId) {
408
- return /* @__PURE__ */ jsxRuntime.jsx(MessageBubble, { message: msg, currentUserId });
409
- }
410
- function Avatar({ src, name, online, size = "md", className }) {
411
- const initials = computeInitials(name);
412
- const classes = [
413
- "poolse-avatar",
414
- size === "sm" ? "poolse-avatar--sm" : null,
415
- size === "lg" ? "poolse-avatar--lg" : null,
416
- className
417
- ].filter(Boolean).join(" ");
418
- return /* @__PURE__ */ jsxRuntime.jsxs("span", { className: classes, "aria-label": name ?? "avatar", role: "img", children: [
419
- src ? /* @__PURE__ */ jsxRuntime.jsx("img", { src, alt: name ?? "" }) : /* @__PURE__ */ jsxRuntime.jsx("span", { "aria-hidden": "true", children: initials }),
420
- online ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-avatar__presence", "aria-hidden": "true" }) : null
421
- ] });
422
- }
423
- function computeInitials(name) {
424
- if (!name) return "?";
425
- const parts = name.trim().split(/\s+/).slice(0, 2);
426
- return parts.map((p) => p.charAt(0).toUpperCase()).join("").slice(0, 2) || "?";
427
- }
428
- function ConversationList({
429
- selectedId,
430
- onSelect,
431
- renderItem,
432
- emptyState,
433
- conversations: controlledConversations,
434
- loading: controlledLoading,
435
- error: controlledError
436
- }) {
437
- const auto = react$1.useConversations();
438
- const isControlled = controlledConversations !== void 0;
439
- const conversations = isControlled ? controlledConversations : auto.conversations;
440
- const loading = isControlled ? controlledLoading ?? false : auto.loading;
441
- const error = isControlled ? controlledError ?? null : auto.error;
442
- if (loading && conversations.length === 0) {
443
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: "Loading conversations\u2026" });
444
- }
445
- if (error) {
446
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder poolse-list--error", children: "Failed to load conversations." });
447
- }
448
- if (conversations.length === 0) {
449
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: emptyState ?? "No conversations yet." });
450
- }
451
- return /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "poolse-list", role: "listbox", "aria-label": "Conversations", children: conversations.map((conv) => {
452
- const selected = selectedId === conv.id;
453
- const content = renderItem ? renderItem(conv, selected) : /* @__PURE__ */ jsxRuntime.jsx(DefaultRow, { conv, selected });
454
- return /* @__PURE__ */ jsxRuntime.jsx(
455
- "li",
456
- {
457
- role: "option",
458
- "aria-selected": selected,
459
- className: `poolse-list__item${selected ? " poolse-list__item--selected" : ""}`,
460
- onClick: () => onSelect?.(conv),
461
- onKeyDown: (e) => {
462
- if (e.key === "Enter" || e.key === " ") {
463
- e.preventDefault();
464
- onSelect?.(conv);
465
- }
466
- },
467
- tabIndex: 0,
468
- children: content
469
- },
470
- conv.id
471
- );
472
- }) });
473
- }
474
- function DefaultRow({ conv, selected: _selected }) {
475
- const title = conv.name ?? "Untitled conversation";
476
- const time = conv.last_message_at ? formatRelative(conv.last_message_at) : "";
477
- return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
478
- /* @__PURE__ */ jsxRuntime.jsx(Avatar, { src: conv.avatar_url ?? null, name: conv.name, size: "md" }),
479
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-list__body", children: [
480
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-list__head", children: [
481
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-list__title", children: title }),
482
- time && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-list__time", children: time })
483
- ] }),
484
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list__preview", children: conv.last_sequence > 0 ? `${conv.last_sequence} message${conv.last_sequence === 1 ? "" : "s"}` : "No messages yet" })
485
- ] })
486
- ] });
487
- }
488
- function formatRelative(iso) {
489
- const then = new Date(iso).getTime();
490
- const now = Date.now();
491
- const diffSec = Math.max(0, Math.floor((now - then) / 1e3));
492
- if (diffSec < 60) return "now";
493
- if (diffSec < 3600) return `${Math.floor(diffSec / 60)}m`;
494
- if (diffSec < 86400) return `${Math.floor(diffSec / 3600)}h`;
495
- return `${Math.floor(diffSec / 86400)}d`;
496
- }
497
- function MemberList({
498
- conversationId,
499
- labelFor,
500
- avatarFor,
501
- onlineUserIds,
502
- canRemove,
503
- renderItem,
504
- emptyState
505
- }) {
506
- const { members, loading, error, removeMember } = react$1.useMembers(conversationId);
507
- if (loading && members.length === 0) {
508
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: "Loading members\u2026" });
509
- }
510
- if (error) {
511
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder poolse-list--error", children: "Failed to load members." });
512
- }
513
- if (members.length === 0) {
514
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: emptyState ?? "No members yet." });
515
- }
516
- const label = labelFor ?? ((id) => id.slice(0, 8));
517
- return /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "poolse-list", role: "list", "aria-label": "Members", children: members.map((m) => {
518
- const content = renderItem ? renderItem(m) : /* @__PURE__ */ jsxRuntime.jsx(
519
- DefaultMemberRow,
520
- {
521
- membership: m,
522
- name: label(m.user_id),
523
- avatarUrl: avatarFor?.(m.user_id) ?? null,
524
- online: onlineUserIds?.has(m.user_id) ?? false,
525
- removable: canRemove?.(m) ?? false,
526
- onRemove: () => {
527
- void removeMember(m.user_id);
528
- }
529
- }
530
- );
531
- return /* @__PURE__ */ jsxRuntime.jsx("li", { className: "poolse-list__item", children: content }, m.id);
532
- }) });
533
- }
534
- function DefaultMemberRow({
535
- membership,
536
- name,
537
- avatarUrl,
538
- online,
539
- removable,
540
- onRemove
541
- }) {
542
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-member-row", children: [
543
- /* @__PURE__ */ jsxRuntime.jsx(Avatar, { src: avatarUrl, name, online, size: "md" }),
544
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-member-row__body", children: [
545
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-member-row__name", children: name }),
546
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-member-row__sub", children: online ? "online" : "offline" })
547
- ] }),
548
- /* @__PURE__ */ jsxRuntime.jsx(RoleBadge, { role: membership.role }),
549
- removable && /* @__PURE__ */ jsxRuntime.jsx(
550
- "button",
551
- {
552
- type: "button",
553
- className: "poolse-icon-btn poolse-icon-btn--danger",
554
- "aria-label": `Remove ${name}`,
555
- onClick: (e) => {
556
- e.stopPropagation();
557
- onRemove();
558
- },
559
- children: /* @__PURE__ */ jsxRuntime.jsx(PoolseIcon, { name: "trash", size: 16, label: null })
560
- }
561
- )
562
- ] });
563
- }
564
- function RoleBadge({ role }) {
565
- const mod = role === "owner" ? "owner" : role === "admin" ? "admin" : null;
566
- return /* @__PURE__ */ jsxRuntime.jsx("span", { className: `poolse-badge${mod ? ` poolse-badge--${mod}` : ""}`, children: role });
567
- }
568
- function AttachmentPreview({ attachment, onClick }) {
569
- const isFullRow = "content_type" in attachment;
570
- const { url, loading, error } = react$1.useAttachmentUrl(attachment.id);
571
- if (loading) {
572
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-attachment poolse-list--placeholder", "aria-busy": "true", children: "Loading attachment\u2026" });
472
+ function AttachmentPreview({ attachment, onClick }) {
473
+ const isFullRow = "content_type" in attachment;
474
+ const { url, loading, error } = react$1.useAttachmentUrl(attachment.id);
475
+ if (loading) {
476
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-attachment poolse-list--placeholder", "aria-busy": "true", children: "Loading attachment\u2026" });
573
477
  }
574
478
  if (error || !url) {
575
479
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-attachment poolse-list--placeholder poolse-list--error", children: "Failed to load attachment." });
@@ -659,6 +563,115 @@ function formatBytes(n) {
659
563
  if (n < 1024 * 1024 * 1024) return `${(n / (1024 * 1024)).toFixed(1)} MB`;
660
564
  return `${(n / (1024 * 1024 * 1024)).toFixed(2)} GB`;
661
565
  }
566
+ function MessageBubble({ message, currentUserId, readState }) {
567
+ const isSelf = currentUserId !== null && message.sender_id === currentUserId;
568
+ const className = [
569
+ "poolse-message",
570
+ isSelf ? "poolse-message--self" : "poolse-message--other"
571
+ ].join(" ");
572
+ const time = message.inserted_at ? new Date(message.inserted_at).toLocaleTimeString(void 0, {
573
+ hour: "2-digit",
574
+ minute: "2-digit"
575
+ }) : "";
576
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className, children: [
577
+ message.deleted_at ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-message__body--deleted", children: "[deleted]" }) : /* @__PURE__ */ jsxRuntime.jsx("span", { children: message.body ?? "" }),
578
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "poolse-message__meta", children: [
579
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: time }),
580
+ message.edited_at ? /* @__PURE__ */ jsxRuntime.jsx("span", { children: " \xB7 edited" }) : null,
581
+ isSelf && readState ? /* @__PURE__ */ jsxRuntime.jsx(
582
+ PoolseIcon,
583
+ {
584
+ name: readState === "read" ? "check-double" : "check",
585
+ size: 14,
586
+ label: readState === "read" ? "Read" : "Sent"
587
+ }
588
+ ) : null
589
+ ] })
590
+ ] });
591
+ }
592
+ function EditableMessageBubble({
593
+ message,
594
+ currentUserId,
595
+ readState,
596
+ editing,
597
+ onSave,
598
+ onCancel
599
+ }) {
600
+ const [draft, setDraft] = react.useState(message.body ?? "");
601
+ const [saving, setSaving] = react.useState(false);
602
+ if (!editing) {
603
+ return /* @__PURE__ */ jsxRuntime.jsx(
604
+ MessageBubble,
605
+ {
606
+ message,
607
+ currentUserId,
608
+ ...readState ? { readState } : {}
609
+ }
610
+ );
611
+ }
612
+ const submit = async () => {
613
+ const trimmed = draft.trim();
614
+ if (!trimmed || saving) return;
615
+ setSaving(true);
616
+ try {
617
+ await onSave(trimmed);
618
+ } finally {
619
+ setSaving(false);
620
+ }
621
+ };
622
+ const onKeyDown = (e) => {
623
+ if (e.key === "Enter" && !e.shiftKey) {
624
+ e.preventDefault();
625
+ void submit();
626
+ } else if (e.key === "Escape") {
627
+ e.preventDefault();
628
+ onCancel();
629
+ }
630
+ };
631
+ const isSelf = currentUserId !== null && message.sender_id === currentUserId;
632
+ const className = [
633
+ "poolse-message",
634
+ "poolse-message--editing",
635
+ isSelf ? "poolse-message--self" : "poolse-message--other"
636
+ ].join(" ");
637
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className, children: [
638
+ /* @__PURE__ */ jsxRuntime.jsx(
639
+ "textarea",
640
+ {
641
+ className: "poolse-message__edit",
642
+ autoFocus: true,
643
+ value: draft,
644
+ onChange: (e) => setDraft(e.target.value),
645
+ onKeyDown,
646
+ rows: Math.max(1, draft.split("\n").length),
647
+ disabled: saving
648
+ },
649
+ message.id
650
+ ),
651
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-message__edit-actions", children: [
652
+ /* @__PURE__ */ jsxRuntime.jsx(
653
+ "button",
654
+ {
655
+ type: "button",
656
+ className: "poolse-message__edit-btn",
657
+ onClick: onCancel,
658
+ disabled: saving,
659
+ children: "Cancel"
660
+ }
661
+ ),
662
+ /* @__PURE__ */ jsxRuntime.jsx(
663
+ "button",
664
+ {
665
+ type: "button",
666
+ className: "poolse-message__edit-btn poolse-message__edit-btn--primary",
667
+ onClick: () => void submit(),
668
+ disabled: saving || draft.trim() === "",
669
+ children: saving ? "Saving\u2026" : "Save"
670
+ }
671
+ )
672
+ ] })
673
+ ] });
674
+ }
662
675
  var COMMON_EMOJIS = ["\u{1F44D}", "\u2764\uFE0F", "\u{1F602}", "\u{1F389}", "\u{1F622}", "\u{1F64F}"];
663
676
  function ReactionStrip({
664
677
  messageId,
@@ -752,164 +765,633 @@ function ReactionPicker({
752
765
  }
753
766
  );
754
767
  }
755
- function MentionInput({
756
- conversationId,
757
- onSend,
758
- onTyping,
759
- placeholder = "Type a message\u2026",
760
- disabled = false,
761
- labelFor
768
+ function MessageActions({
769
+ onReact,
770
+ onReply,
771
+ onEdit,
772
+ onDelete,
773
+ children
762
774
  }) {
763
- const { members } = react$1.useMembers(conversationId);
764
- const [value, setValue] = react.useState("");
765
- const [sending, setSending] = react.useState(false);
766
- const [mention, setMention] = react.useState(null);
767
- const [activeIdx, setActiveIdx] = react.useState(0);
768
- const selectedMentions = react.useRef(/* @__PURE__ */ new Set());
769
- const taRef = react.useRef(null);
770
- const label = labelFor ?? ((id) => id.slice(0, 8));
771
- const filtered = mention ? members.filter((m) => label(m.user_id).toLowerCase().includes(mention.query.toLowerCase())) : [];
772
- react.useEffect(() => {
773
- setActiveIdx(0);
774
- }, [filtered.length, mention?.query]);
775
- const submit = async () => {
776
- const trimmed = value.trim();
777
- if (!trimmed || sending) return;
778
- setSending(true);
779
- try {
780
- const mentions = Array.from(selectedMentions.current);
781
- await onSend({
782
- body: trimmed,
783
- ...mentions.length > 0 ? { mentions } : {}
784
- });
785
- setValue("");
786
- selectedMentions.current = /* @__PURE__ */ new Set();
787
- } finally {
788
- setSending(false);
789
- }
790
- };
791
- const insertMention = (m) => {
792
- if (!mention) return;
793
- const name = label(m.user_id);
794
- const before = value.slice(0, mention.start);
795
- const after = value.slice(mention.start + 1 + mention.query.length);
796
- const inserted = `@${name} `;
797
- const next = `${before}${inserted}${after}`;
798
- selectedMentions.current.add(m.user_id);
799
- setValue(next);
800
- setMention(null);
801
- requestAnimationFrame(() => {
802
- const ta = taRef.current;
803
- if (!ta) return;
804
- const pos = before.length + inserted.length;
805
- ta.focus();
806
- ta.setSelectionRange(pos, pos);
807
- });
808
- };
809
- const onChange = (e) => {
810
- const next = e.target.value;
811
- setValue(next);
812
- onTyping?.();
813
- const caret = e.target.selectionStart ?? next.length;
814
- const head = next.slice(0, caret);
815
- const at = head.lastIndexOf("@");
816
- if (at === -1) {
817
- setMention(null);
818
- return;
819
- }
820
- const charBefore = at === 0 ? " " : head.charAt(at - 1);
821
- const isAtBoundary = /\s/.test(charBefore);
822
- const query = head.slice(at + 1);
823
- if (isAtBoundary && !/\s/.test(query)) {
824
- setMention({ start: at, query });
825
- } else {
826
- setMention(null);
827
- }
828
- };
829
- const onKeyDown = (e) => {
830
- if (mention && filtered.length > 0) {
831
- if (e.key === "ArrowDown") {
832
- e.preventDefault();
833
- setActiveIdx((i) => (i + 1) % filtered.length);
834
- return;
775
+ const [pickerOpen, setPickerOpen] = react.useState(false);
776
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-msg-actions", role: "toolbar", "aria-label": "Message actions", children: [
777
+ onReact && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
778
+ /* @__PURE__ */ jsxRuntime.jsx(
779
+ ActionButton,
780
+ {
781
+ label: "React",
782
+ icon: "emoji",
783
+ onClick: () => setPickerOpen((o) => !o),
784
+ active: pickerOpen
785
+ }
786
+ ),
787
+ pickerOpen && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-msg-actions__picker", children: /* @__PURE__ */ jsxRuntime.jsx(
788
+ ReactionPicker,
789
+ {
790
+ onPick: (emoji) => {
791
+ setPickerOpen(false);
792
+ onReact(emoji);
793
+ },
794
+ onClose: () => setPickerOpen(false)
795
+ }
796
+ ) })
797
+ ] }),
798
+ onReply && /* @__PURE__ */ jsxRuntime.jsx(ActionButton, { label: "Reply in thread", icon: "reply", onClick: onReply }),
799
+ onEdit && /* @__PURE__ */ jsxRuntime.jsx(ActionButton, { label: "Edit", icon: "edit", onClick: onEdit }),
800
+ onDelete && /* @__PURE__ */ jsxRuntime.jsx(ActionButton, { label: "Delete", icon: "trash", danger: true, onClick: onDelete }),
801
+ children
802
+ ] });
803
+ }
804
+ function ActionButton({
805
+ label,
806
+ icon,
807
+ onClick,
808
+ active,
809
+ danger
810
+ }) {
811
+ const classes = [
812
+ "poolse-msg-actions__btn",
813
+ active ? "poolse-msg-actions__btn--active" : null,
814
+ danger ? "poolse-msg-actions__btn--danger" : null
815
+ ].filter(Boolean).join(" ");
816
+ return /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: classes, "aria-label": label, title: label, onClick, children: /* @__PURE__ */ jsxRuntime.jsx(PoolseIcon, { name: icon, size: 16, label: null }) });
817
+ }
818
+ function MessageRow({
819
+ msg,
820
+ meId,
821
+ reactions = true,
822
+ attachments = true,
823
+ actions = true,
824
+ threads = true,
825
+ readState,
826
+ editing = false,
827
+ onStartEdit,
828
+ onCancelEdit,
829
+ onSaveEdit,
830
+ onDelete,
831
+ onOpenThread
832
+ }) {
833
+ const isSelf = meId !== null && msg.sender_id === meId;
834
+ const reactionsHook = react$1.useReactions(reactions ? msg.id : "", {
835
+ conversationId: msg.conversation_id,
836
+ initialReactions: msg.reactions,
837
+ currentUserId: meId
838
+ });
839
+ const showReactions = reactions && !msg.deleted_at;
840
+ const showAttachments = attachments && Array.isArray(msg.attachments) && msg.attachments.length > 0;
841
+ const showActions = actions && !msg.deleted_at && !editing;
842
+ const handleSave = onSaveEdit ?? (async () => void 0);
843
+ const handleCancel = onCancelEdit ?? (() => void 0);
844
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: `poolse-message-row ${isSelf ? "poolse-message-row--right" : ""}`, children: [
845
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
846
+ EditableMessageBubble,
847
+ {
848
+ message: msg,
849
+ currentUserId: meId,
850
+ ...readState ? { readState } : {},
851
+ editing: true,
852
+ onSave: handleSave,
853
+ onCancel: handleCancel
835
854
  }
836
- if (e.key === "ArrowUp") {
837
- e.preventDefault();
838
- setActiveIdx((i) => (i - 1 + filtered.length) % filtered.length);
839
- return;
855
+ ) : /* @__PURE__ */ jsxRuntime.jsx(MessageBubble, { message: msg, currentUserId: meId, ...readState ? { readState } : {} }),
856
+ showAttachments && /* @__PURE__ */ jsxRuntime.jsx("div", { className: `poolse-message-row__attachments ${isSelf ? "is-self" : ""}`, children: msg.attachments.map((att) => /* @__PURE__ */ jsxRuntime.jsx(AttachmentPreview, { attachment: att }, att.id)) }),
857
+ showReactions && /* @__PURE__ */ jsxRuntime.jsx("div", { className: `poolse-message-row__reactions ${isSelf ? "is-self" : ""}`, children: /* @__PURE__ */ jsxRuntime.jsx(
858
+ InlineReactions,
859
+ {
860
+ reactions: reactionsHook.reactions,
861
+ currentUserId: meId,
862
+ onAdd: (e) => void reactionsHook.addReaction(e),
863
+ onRemove: (e) => void reactionsHook.removeReaction(e)
840
864
  }
841
- if (e.key === "Enter" || e.key === "Tab") {
842
- const pick = filtered[activeIdx];
843
- if (pick) {
844
- e.preventDefault();
845
- insertMention(pick);
846
- return;
847
- }
865
+ ) }),
866
+ showActions && /* @__PURE__ */ jsxRuntime.jsx(
867
+ "div",
868
+ {
869
+ className: `poolse-message-row__actions ${isSelf ? "poolse-message-row__actions--right" : "poolse-message-row__actions--left"}`,
870
+ children: /* @__PURE__ */ jsxRuntime.jsx(
871
+ MessageActions,
872
+ {
873
+ ...reactions ? {
874
+ onReact: (emoji) => {
875
+ void reactionsHook.addReaction(emoji);
876
+ }
877
+ } : {},
878
+ ...threads && onOpenThread ? { onReply: onOpenThread } : {},
879
+ ...isSelf && onStartEdit ? { onEdit: onStartEdit } : {},
880
+ ...isSelf && onDelete ? { onDelete } : {}
881
+ }
882
+ )
848
883
  }
849
- if (e.key === "Escape") {
850
- e.preventDefault();
851
- setMention(null);
852
- return;
884
+ )
885
+ ] });
886
+ }
887
+ function InlineReactions({
888
+ reactions,
889
+ currentUserId,
890
+ onAdd: _onAdd,
891
+ onRemove
892
+ }) {
893
+ const entries = Object.entries(reactions);
894
+ if (entries.length === 0) return null;
895
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-reactions", children: entries.map(([emoji, users]) => {
896
+ const mine = currentUserId !== null && currentUserId !== void 0 && users.includes(currentUserId);
897
+ return /* @__PURE__ */ jsxRuntime.jsxs(
898
+ "button",
899
+ {
900
+ type: "button",
901
+ className: `poolse-reaction-pill${mine ? " poolse-reaction-pill--mine" : ""}`,
902
+ onClick: () => {
903
+ if (mine) onRemove(emoji);
904
+ else _onAdd(emoji);
905
+ },
906
+ "aria-pressed": mine,
907
+ "aria-label": `${emoji} reacted by ${users.length} ${users.length === 1 ? "user" : "users"}`,
908
+ children: [
909
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: emoji }),
910
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-reaction-pill__count", children: users.length })
911
+ ]
912
+ },
913
+ emoji
914
+ );
915
+ }) });
916
+ }
917
+ function ThreadView({
918
+ conversationId,
919
+ rootMessage,
920
+ onClose,
921
+ renderMessage
922
+ }) {
923
+ const { me } = react$1.useMe();
924
+ const { replies, loading, error, hasMore, loadMore, sendReply } = react$1.useThread(
925
+ conversationId,
926
+ rootMessage.id
927
+ );
928
+ const listRef = react.useRef(null);
929
+ const lastReplyIdRef = react.useRef(null);
930
+ react.useEffect(() => {
931
+ const tail = replies[replies.length - 1];
932
+ if (!tail) return;
933
+ if (tail.id === lastReplyIdRef.current) return;
934
+ lastReplyIdRef.current = tail.id;
935
+ const el = listRef.current;
936
+ if (el) el.scrollTop = el.scrollHeight;
937
+ }, [replies]);
938
+ const renderOne = renderMessage ?? defaultRenderReply;
939
+ return /* @__PURE__ */ jsxRuntime.jsxs("aside", { className: "poolse-thread", "aria-label": "Thread", children: [
940
+ /* @__PURE__ */ jsxRuntime.jsxs("header", { className: "poolse-thread__header", children: [
941
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-thread__title", children: "Thread" }),
942
+ onClose && /* @__PURE__ */ jsxRuntime.jsx(
943
+ "button",
944
+ {
945
+ type: "button",
946
+ className: "poolse-icon-btn",
947
+ onClick: onClose,
948
+ "aria-label": "Close thread",
949
+ children: /* @__PURE__ */ jsxRuntime.jsx(PoolseIcon, { name: "close", size: 18, label: null })
950
+ }
951
+ )
952
+ ] }),
953
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-thread__root", children: /* @__PURE__ */ jsxRuntime.jsx(MessageBubble, { message: rootMessage, currentUserId: me?.id ?? null }) }),
954
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-thread__messages", ref: listRef, children: [
955
+ hasMore && !loading && replies.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: "poolse-conversation__load-more", onClick: loadMore, children: "Load older replies" }),
956
+ loading && replies.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-conversation__empty", children: "Loading replies\u2026" }) : replies.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-conversation__empty", children: "No replies yet \u2014 start the thread." }) : (
957
+ // Same Fragment trick as ConversationView so MessageBubble
958
+ // remains the direct flex child for align-self to work.
959
+ replies.map((msg) => /* @__PURE__ */ jsxRuntime.jsx(react.Fragment, { children: renderOne(msg, me?.id ?? null) }, msg.id))
960
+ ),
961
+ error && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation__empty", children: [
962
+ "Failed to load: ",
963
+ error.message
964
+ ] })
965
+ ] }),
966
+ /* @__PURE__ */ jsxRuntime.jsx(MessageComposer, { onSend: (body) => sendReply({ body }), placeholder: "Reply in thread\u2026" })
967
+ ] });
968
+ }
969
+ function defaultRenderReply(msg, currentUserId) {
970
+ return /* @__PURE__ */ jsxRuntime.jsx(MessageBubble, { message: msg, currentUserId });
971
+ }
972
+ function TypingIndicator({ typing, labelFor }) {
973
+ const ids = Array.from(typing);
974
+ if (ids.length === 0) {
975
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-typing", "aria-hidden": "true" });
976
+ }
977
+ const label = labelFor ?? ((id) => id.slice(0, 6));
978
+ const names = ids.slice(0, 2).map(label);
979
+ let text;
980
+ if (ids.length === 1) text = `${names[0]} is typing`;
981
+ else if (ids.length === 2) text = `${names[0]} and ${names[1]} are typing`;
982
+ else text = `${ids.length} people are typing`;
983
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-typing", children: [
984
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "poolse-typing__dots", role: "status", "aria-live": "polite", children: [
985
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-typing__dot" }),
986
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-typing__dot" }),
987
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-typing__dot" })
988
+ ] }),
989
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-typing__label", children: text })
990
+ ] });
991
+ }
992
+ var TRUE = true;
993
+ function ConversationView({
994
+ conversationId,
995
+ emptyState,
996
+ renderMessage,
997
+ labelFor,
998
+ loadFonts = TRUE,
999
+ reactions = TRUE,
1000
+ mentions = TRUE,
1001
+ attachments = TRUE,
1002
+ actions = TRUE,
1003
+ threads = TRUE,
1004
+ readReceipts = TRUE,
1005
+ onMarkedRead
1006
+ }) {
1007
+ usePoolseFonts(loadFonts);
1008
+ const { me } = react$1.useMe();
1009
+ const {
1010
+ messages,
1011
+ loading,
1012
+ error,
1013
+ hasMore,
1014
+ loadMore,
1015
+ send,
1016
+ edit,
1017
+ delete: deleteMsg,
1018
+ markReadUpTo
1019
+ } = react$1.useMessages(conversationId);
1020
+ const { typing, signalTyping } = react$1.useTyping(conversationId);
1021
+ const status = react$1.useRealtimeStatus();
1022
+ const membersOn = mentions || readReceipts;
1023
+ const { members } = react$1.useMembers(membersOn ? conversationId : "");
1024
+ const listRef = react.useRef(null);
1025
+ const lastMessageIdRef = react.useRef(null);
1026
+ const bottomSentinelRef = react.useRef(null);
1027
+ const [atBottom, setAtBottom] = react.useState(true);
1028
+ const [newCountWhileAway, setNewCountWhileAway] = react.useState(0);
1029
+ react.useEffect(() => {
1030
+ const el = bottomSentinelRef.current;
1031
+ if (!el) return;
1032
+ const root = listRef.current;
1033
+ if (!root) return;
1034
+ const io = new IntersectionObserver(
1035
+ ([entry]) => {
1036
+ if (!entry) return;
1037
+ setAtBottom(entry.isIntersecting);
1038
+ if (entry.isIntersecting) {
1039
+ setNewCountWhileAway(0);
1040
+ }
1041
+ },
1042
+ { root, threshold: 0.95 }
1043
+ );
1044
+ io.observe(el);
1045
+ return () => io.disconnect();
1046
+ }, []);
1047
+ react.useEffect(() => {
1048
+ const tail = messages[messages.length - 1];
1049
+ if (!tail) return;
1050
+ if (tail.id === lastMessageIdRef.current) return;
1051
+ const isFirstSet = lastMessageIdRef.current === null;
1052
+ lastMessageIdRef.current = tail.id;
1053
+ if (atBottom || isFirstSet) {
1054
+ const el = listRef.current;
1055
+ if (el) el.scrollTop = el.scrollHeight;
1056
+ } else if (me && tail.sender_id !== me.id) {
1057
+ setNewCountWhileAway((n) => n + 1);
1058
+ }
1059
+ }, [messages, atBottom, me]);
1060
+ const lastMarkedReadRef = react.useRef(null);
1061
+ react.useEffect(() => {
1062
+ if (!atBottom) return;
1063
+ if (messages.length === 0) return;
1064
+ const tail = messages[messages.length - 1];
1065
+ if (!tail) return;
1066
+ if (tail.sequence === Number.MAX_SAFE_INTEGER) return;
1067
+ if (lastMarkedReadRef.current === tail.id) return;
1068
+ lastMarkedReadRef.current = tail.id;
1069
+ void markReadUpTo(tail.id).then(() => {
1070
+ onMarkedRead?.(conversationId);
1071
+ }).catch(() => {
1072
+ lastMarkedReadRef.current = null;
1073
+ });
1074
+ }, [atBottom, messages, markReadUpTo, onMarkedRead, conversationId]);
1075
+ const scrollToBottom = () => {
1076
+ const el = listRef.current;
1077
+ if (el) el.scrollTop = el.scrollHeight;
1078
+ setNewCountWhileAway(0);
1079
+ };
1080
+ const maxReadByOthers = react.useMemo(() => {
1081
+ if (!readReceipts || !me) return null;
1082
+ let max = 0;
1083
+ let any = false;
1084
+ for (const m of members) {
1085
+ if (m.user_id === me.id) continue;
1086
+ const lr = m.last_read_message_id;
1087
+ if (!lr) continue;
1088
+ const idx = messages.findIndex((x) => x.id === lr);
1089
+ if (idx !== -1) {
1090
+ const seq = messages[idx]?.sequence ?? 0;
1091
+ if (seq > max) max = seq;
853
1092
  }
1093
+ any = true;
854
1094
  }
855
- if (e.key === "Enter" && !e.shiftKey) {
856
- e.preventDefault();
857
- void submit();
1095
+ return any ? max : null;
1096
+ }, [readReceipts, me, members, messages]);
1097
+ const [editingId, setEditingId] = react.useState(null);
1098
+ const [threadRootId, setThreadRootId] = react.useState(null);
1099
+ const threadRoot = react.useMemo(
1100
+ () => threadRootId ? messages.find((m) => m.id === threadRootId) ?? null : null,
1101
+ [threadRootId, messages]
1102
+ );
1103
+ const { upload, uploading } = react$1.useAttachmentUpload();
1104
+ const onPickFile = async (file) => {
1105
+ try {
1106
+ const att = await upload({
1107
+ body: file,
1108
+ contentType: file.type || "application/octet-stream",
1109
+ byteSize: file.size,
1110
+ filename: file.name
1111
+ });
1112
+ await send({ body: "", attachment_ids: [att.id] });
1113
+ } catch (err) {
1114
+ console.error("attachment upload failed:", err);
858
1115
  }
859
1116
  };
860
- return /* @__PURE__ */ jsxRuntime.jsxs(
861
- "form",
1117
+ const onSendBody = async (body) => {
1118
+ await send({ body });
1119
+ };
1120
+ const onSendWithMentions = async (req) => {
1121
+ await send(req);
1122
+ };
1123
+ const Composer = mentions && members.length > 0 ? /* @__PURE__ */ jsxRuntime.jsx(
1124
+ MentionInput,
862
1125
  {
863
- className: "poolse-composer",
864
- onSubmit: (e) => {
865
- e.preventDefault();
866
- void submit();
867
- },
868
- style: { position: "relative" },
869
- children: [
870
- /* @__PURE__ */ jsxRuntime.jsx(
871
- "textarea",
872
- {
873
- ref: taRef,
874
- className: "poolse-composer__input",
875
- rows: 1,
876
- value,
877
- onChange,
878
- onKeyDown,
879
- placeholder,
880
- disabled: disabled || sending
881
- }
882
- ),
883
- /* @__PURE__ */ jsxRuntime.jsx(
884
- "button",
885
- {
886
- type: "submit",
887
- className: "poolse-composer__send",
888
- disabled: disabled || sending || value.trim() === "",
889
- "aria-label": "Send message",
890
- children: /* @__PURE__ */ jsxRuntime.jsx(PoolseIcon, { name: "send-fill", label: null })
1126
+ conversationId,
1127
+ onSend: onSendWithMentions,
1128
+ onTyping: signalTyping
1129
+ }
1130
+ ) : /* @__PURE__ */ jsxRuntime.jsx(MessageComposer, { onSend: onSendBody, onTyping: signalTyping });
1131
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation-shell", children: [
1132
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation", children: [
1133
+ status !== "connected" && status !== "idle" && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation__status", children: [
1134
+ status === "connecting" && "Connecting\u2026",
1135
+ status === "reconnecting" && "Reconnecting\u2026",
1136
+ status === "closed" && "Disconnected"
1137
+ ] }),
1138
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation__messages", ref: listRef, children: [
1139
+ hasMore && !loading && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: "poolse-conversation__load-more", onClick: loadMore, children: "Load older messages" }),
1140
+ loading && messages.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-conversation__empty", children: "Loading\u2026" }) : messages.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-conversation__empty", children: emptyState ?? "No messages yet." }) : messages.map((msg) => {
1141
+ if (renderMessage) {
1142
+ return /* @__PURE__ */ jsxRuntime.jsx(react.Fragment, { children: renderMessage(msg, me?.id ?? null) }, msg.id);
891
1143
  }
892
- ),
893
- mention && filtered.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-mention-menu", role: "listbox", "aria-label": "Members", children: filtered.map((m, i) => /* @__PURE__ */ jsxRuntime.jsx(
894
- "div",
895
- {
896
- role: "option",
897
- "aria-selected": i === activeIdx,
898
- className: `poolse-mention-menu__item${i === activeIdx ? " poolse-mention-menu__item--active" : ""}`,
899
- onMouseDown: (e) => {
900
- e.preventDefault();
901
- insertMention(m);
1144
+ const isSelf = me !== null && msg.sender_id === me.id;
1145
+ const readState = readReceipts && isSelf && maxReadByOthers !== null ? maxReadByOthers >= msg.sequence ? "read" : "sent" : void 0;
1146
+ return /* @__PURE__ */ jsxRuntime.jsx(
1147
+ MessageRow,
1148
+ {
1149
+ msg,
1150
+ meId: me?.id ?? null,
1151
+ reactions,
1152
+ attachments,
1153
+ actions,
1154
+ threads,
1155
+ ...readState ? { readState } : {},
1156
+ editing: editingId === msg.id,
1157
+ onStartEdit: () => setEditingId(msg.id),
1158
+ onCancelEdit: () => setEditingId(null),
1159
+ onSaveEdit: async (body) => {
1160
+ await edit(msg.id, body);
1161
+ setEditingId(null);
1162
+ },
1163
+ onDelete: () => void deleteMsg(msg.id),
1164
+ onOpenThread: () => setThreadRootId(msg.id)
902
1165
  },
903
- children: /* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
904
- "@",
905
- label(m.user_id)
906
- ] })
907
- },
908
- m.id
909
- )) })
910
- ]
911
- }
912
- );
1166
+ msg.id
1167
+ );
1168
+ }),
1169
+ error && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation__empty", children: [
1170
+ "Failed to load: ",
1171
+ error.message
1172
+ ] }),
1173
+ /* @__PURE__ */ jsxRuntime.jsx("div", { ref: bottomSentinelRef, "aria-hidden": "true", style: { height: 1 } })
1174
+ ] }),
1175
+ !atBottom && newCountWhileAway > 0 && /* @__PURE__ */ jsxRuntime.jsxs(
1176
+ "button",
1177
+ {
1178
+ type: "button",
1179
+ className: "poolse-conversation__scroll-to-new",
1180
+ onClick: scrollToBottom,
1181
+ children: [
1182
+ "\u2193 ",
1183
+ newCountWhileAway,
1184
+ " new ",
1185
+ newCountWhileAway === 1 ? "message" : "messages"
1186
+ ]
1187
+ }
1188
+ ),
1189
+ /* @__PURE__ */ jsxRuntime.jsx(TypingIndicator, { typing, ...labelFor ? { labelFor } : {} }),
1190
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation__composer-row", children: [
1191
+ attachments && /* @__PURE__ */ jsxRuntime.jsx(AttachmentPickerButton, { onPick: onPickFile, disabled: uploading }),
1192
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-conversation__composer-flex", children: Composer })
1193
+ ] })
1194
+ ] }),
1195
+ threads && threadRoot && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-conversation-shell__thread", children: /* @__PURE__ */ jsxRuntime.jsx(
1196
+ ThreadView,
1197
+ {
1198
+ conversationId,
1199
+ rootMessage: threadRoot,
1200
+ onClose: () => setThreadRootId(null)
1201
+ }
1202
+ ) })
1203
+ ] });
1204
+ }
1205
+ function AttachmentPickerButton({
1206
+ onPick,
1207
+ disabled
1208
+ }) {
1209
+ const inputRef = react.useRef(null);
1210
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1211
+ /* @__PURE__ */ jsxRuntime.jsx(
1212
+ "input",
1213
+ {
1214
+ ref: inputRef,
1215
+ type: "file",
1216
+ className: "poolse-attach-input",
1217
+ onChange: (e) => {
1218
+ const f = e.target.files?.[0];
1219
+ if (f) onPick(f);
1220
+ e.target.value = "";
1221
+ }
1222
+ }
1223
+ ),
1224
+ /* @__PURE__ */ jsxRuntime.jsx(
1225
+ "button",
1226
+ {
1227
+ type: "button",
1228
+ className: "poolse-attach-btn",
1229
+ onClick: () => inputRef.current?.click(),
1230
+ disabled,
1231
+ "aria-label": "Attach file",
1232
+ title: "Attach file",
1233
+ children: /* @__PURE__ */ jsxRuntime.jsx(PoolseIcon, { name: "attachment", size: 20, label: null })
1234
+ }
1235
+ )
1236
+ ] });
1237
+ }
1238
+ function Avatar({ src, name, online, size = "md", className }) {
1239
+ const initials = computeInitials(name);
1240
+ const classes = [
1241
+ "poolse-avatar",
1242
+ size === "sm" ? "poolse-avatar--sm" : null,
1243
+ size === "lg" ? "poolse-avatar--lg" : null,
1244
+ className
1245
+ ].filter(Boolean).join(" ");
1246
+ return /* @__PURE__ */ jsxRuntime.jsxs("span", { className: classes, "aria-label": name ?? "avatar", role: "img", children: [
1247
+ src ? /* @__PURE__ */ jsxRuntime.jsx("img", { src, alt: name ?? "" }) : /* @__PURE__ */ jsxRuntime.jsx("span", { "aria-hidden": "true", children: initials }),
1248
+ online ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-avatar__presence", "aria-hidden": "true" }) : null
1249
+ ] });
1250
+ }
1251
+ function computeInitials(name) {
1252
+ if (!name) return "?";
1253
+ const parts = name.trim().split(/\s+/).slice(0, 2);
1254
+ return parts.map((p) => p.charAt(0).toUpperCase()).join("").slice(0, 2) || "?";
1255
+ }
1256
+ function ConversationList({
1257
+ selectedId,
1258
+ onSelect,
1259
+ renderItem,
1260
+ emptyState,
1261
+ conversations: controlledConversations,
1262
+ loading: controlledLoading,
1263
+ error: controlledError
1264
+ }) {
1265
+ const auto = react$1.useConversations();
1266
+ const isControlled = controlledConversations !== void 0;
1267
+ const conversations = isControlled ? controlledConversations : auto.conversations;
1268
+ const loading = isControlled ? controlledLoading ?? false : auto.loading;
1269
+ const error = isControlled ? controlledError ?? null : auto.error;
1270
+ if (loading && conversations.length === 0) {
1271
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: "Loading conversations\u2026" });
1272
+ }
1273
+ if (error) {
1274
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder poolse-list--error", children: "Failed to load conversations." });
1275
+ }
1276
+ if (conversations.length === 0) {
1277
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: emptyState ?? "No conversations yet." });
1278
+ }
1279
+ return /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "poolse-list", role: "listbox", "aria-label": "Conversations", children: conversations.map((conv) => {
1280
+ const selected = selectedId === conv.id;
1281
+ const content = renderItem ? renderItem(conv, selected) : /* @__PURE__ */ jsxRuntime.jsx(DefaultRow, { conv, selected });
1282
+ return /* @__PURE__ */ jsxRuntime.jsx(
1283
+ "li",
1284
+ {
1285
+ role: "option",
1286
+ "aria-selected": selected,
1287
+ className: `poolse-list__item${selected ? " poolse-list__item--selected" : ""}`,
1288
+ onClick: () => onSelect?.(conv),
1289
+ onKeyDown: (e) => {
1290
+ if (e.key === "Enter" || e.key === " ") {
1291
+ e.preventDefault();
1292
+ onSelect?.(conv);
1293
+ }
1294
+ },
1295
+ tabIndex: 0,
1296
+ children: content
1297
+ },
1298
+ conv.id
1299
+ );
1300
+ }) });
1301
+ }
1302
+ function DefaultRow({ conv, selected: _selected }) {
1303
+ const title = conv.name ?? "Untitled conversation";
1304
+ const time = conv.last_message_at ? formatRelative(conv.last_message_at) : "";
1305
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1306
+ /* @__PURE__ */ jsxRuntime.jsx(Avatar, { src: conv.avatar_url ?? null, name: conv.name, size: "md" }),
1307
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-list__body", children: [
1308
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-list__head", children: [
1309
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-list__title", children: title }),
1310
+ time && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-list__time", children: time })
1311
+ ] }),
1312
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list__preview", children: conv.last_sequence > 0 ? `${conv.last_sequence} message${conv.last_sequence === 1 ? "" : "s"}` : "No messages yet" })
1313
+ ] })
1314
+ ] });
1315
+ }
1316
+ function formatRelative(iso) {
1317
+ const then = new Date(iso).getTime();
1318
+ const now = Date.now();
1319
+ const diffSec = Math.max(0, Math.floor((now - then) / 1e3));
1320
+ if (diffSec < 60) return "now";
1321
+ if (diffSec < 3600) return `${Math.floor(diffSec / 60)}m`;
1322
+ if (diffSec < 86400) return `${Math.floor(diffSec / 3600)}h`;
1323
+ return `${Math.floor(diffSec / 86400)}d`;
1324
+ }
1325
+ function MemberList({
1326
+ conversationId,
1327
+ labelFor,
1328
+ avatarFor,
1329
+ onlineUserIds,
1330
+ canRemove,
1331
+ renderItem,
1332
+ emptyState
1333
+ }) {
1334
+ const { members, loading, error, removeMember } = react$1.useMembers(conversationId);
1335
+ if (loading && members.length === 0) {
1336
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: "Loading members\u2026" });
1337
+ }
1338
+ if (error) {
1339
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder poolse-list--error", children: "Failed to load members." });
1340
+ }
1341
+ if (members.length === 0) {
1342
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: emptyState ?? "No members yet." });
1343
+ }
1344
+ const label = labelFor ?? ((id) => id.slice(0, 8));
1345
+ return /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "poolse-list", role: "list", "aria-label": "Members", children: members.map((m) => {
1346
+ const content = renderItem ? renderItem(m) : /* @__PURE__ */ jsxRuntime.jsx(
1347
+ DefaultMemberRow,
1348
+ {
1349
+ membership: m,
1350
+ name: label(m.user_id),
1351
+ avatarUrl: avatarFor?.(m.user_id) ?? null,
1352
+ online: onlineUserIds?.has(m.user_id) ?? false,
1353
+ removable: canRemove?.(m) ?? false,
1354
+ onRemove: () => {
1355
+ void removeMember(m.user_id);
1356
+ }
1357
+ }
1358
+ );
1359
+ return /* @__PURE__ */ jsxRuntime.jsx("li", { className: "poolse-list__item", children: content }, m.id);
1360
+ }) });
1361
+ }
1362
+ function DefaultMemberRow({
1363
+ membership,
1364
+ name,
1365
+ avatarUrl,
1366
+ online,
1367
+ removable,
1368
+ onRemove
1369
+ }) {
1370
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-member-row", children: [
1371
+ /* @__PURE__ */ jsxRuntime.jsx(Avatar, { src: avatarUrl, name, online, size: "md" }),
1372
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-member-row__body", children: [
1373
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-member-row__name", children: name }),
1374
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-member-row__sub", children: online ? "online" : "offline" })
1375
+ ] }),
1376
+ /* @__PURE__ */ jsxRuntime.jsx(RoleBadge, { role: membership.role }),
1377
+ removable && /* @__PURE__ */ jsxRuntime.jsx(
1378
+ "button",
1379
+ {
1380
+ type: "button",
1381
+ className: "poolse-icon-btn poolse-icon-btn--danger",
1382
+ "aria-label": `Remove ${name}`,
1383
+ onClick: (e) => {
1384
+ e.stopPropagation();
1385
+ onRemove();
1386
+ },
1387
+ children: /* @__PURE__ */ jsxRuntime.jsx(PoolseIcon, { name: "trash", size: 16, label: null })
1388
+ }
1389
+ )
1390
+ ] });
1391
+ }
1392
+ function RoleBadge({ role }) {
1393
+ const mod = role === "owner" ? "owner" : role === "admin" ? "admin" : null;
1394
+ return /* @__PURE__ */ jsxRuntime.jsx("span", { className: `poolse-badge${mod ? ` poolse-badge--${mod}` : ""}`, children: role });
913
1395
  }
914
1396
  var MARK_PATHS = /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
915
1397
  /* @__PURE__ */ jsxRuntime.jsx(
@@ -1023,8 +1505,10 @@ exports.AttachmentPreview = AttachmentPreview;
1023
1505
  exports.Avatar = Avatar;
1024
1506
  exports.ConversationList = ConversationList;
1025
1507
  exports.ConversationView = ConversationView;
1508
+ exports.EditableMessageBubble = EditableMessageBubble;
1026
1509
  exports.MemberList = MemberList;
1027
1510
  exports.MentionInput = MentionInput;
1511
+ exports.MessageActions = MessageActions;
1028
1512
  exports.MessageBubble = MessageBubble;
1029
1513
  exports.MessageComposer = MessageComposer;
1030
1514
  exports.PoolseFonts = PoolseFonts;
@@ -1032,6 +1516,7 @@ exports.PoolseIcon = PoolseIcon;
1032
1516
  exports.PoolseLogo = PoolseLogo;
1033
1517
  exports.ReactionPicker = ReactionPicker;
1034
1518
  exports.ReactionStrip = ReactionStrip;
1519
+ exports.ThreadView = ThreadView;
1035
1520
  exports.TypingIndicator = TypingIndicator;
1036
1521
  exports.usePoolseFonts = usePoolseFonts;
1037
1522
  //# sourceMappingURL=index.cjs.map