@poolse/react-ui 0.1.0 → 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,226 +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." }) : messages.map((msg) => /* @__PURE__ */ jsxRuntime.jsx("div", { children: renderOne(msg, me?.id ?? null) }, msg.id)),
391
- error && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-conversation__empty", children: [
392
- "Failed to load: ",
393
- error.message
394
- ] })
395
- ] }),
396
- /* @__PURE__ */ jsxRuntime.jsx(TypingIndicator, { typing, ...labelFor ? { labelFor } : {} }),
397
- /* @__PURE__ */ jsxRuntime.jsx(MessageComposer, { onSend: (body) => send({ body }), onTyping: signalTyping })
398
- ] });
399
- }
400
- function defaultRenderMessage(msg, currentUserId) {
401
- return /* @__PURE__ */ jsxRuntime.jsx(MessageBubble, { message: msg, currentUserId });
402
- }
403
- function Avatar({ src, name, online, size = "md", className }) {
404
- const initials = computeInitials(name);
405
- const classes = [
406
- "poolse-avatar",
407
- size === "sm" ? "poolse-avatar--sm" : null,
408
- size === "lg" ? "poolse-avatar--lg" : null,
409
- className
410
- ].filter(Boolean).join(" ");
411
- return /* @__PURE__ */ jsxRuntime.jsxs("span", { className: classes, "aria-label": name ?? "avatar", role: "img", children: [
412
- src ? /* @__PURE__ */ jsxRuntime.jsx("img", { src, alt: name ?? "" }) : /* @__PURE__ */ jsxRuntime.jsx("span", { "aria-hidden": "true", children: initials }),
413
- online ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-avatar__presence", "aria-hidden": "true" }) : null
414
- ] });
415
- }
416
- function computeInitials(name) {
417
- if (!name) return "?";
418
- const parts = name.trim().split(/\s+/).slice(0, 2);
419
- return parts.map((p) => p.charAt(0).toUpperCase()).join("").slice(0, 2) || "?";
420
- }
421
- function ConversationList({
422
- selectedId,
423
- onSelect,
424
- renderItem,
425
- emptyState
426
- }) {
427
- const { conversations, loading, error } = react$1.useConversations();
428
- if (loading && conversations.length === 0) {
429
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: "Loading conversations\u2026" });
430
- }
431
- if (error) {
432
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder poolse-list--error", children: "Failed to load conversations." });
433
- }
434
- if (conversations.length === 0) {
435
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: emptyState ?? "No conversations yet." });
436
- }
437
- return /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "poolse-list", role: "listbox", "aria-label": "Conversations", children: conversations.map((conv) => {
438
- const selected = selectedId === conv.id;
439
- const content = renderItem ? renderItem(conv, selected) : /* @__PURE__ */ jsxRuntime.jsx(DefaultRow, { conv, selected });
440
- return /* @__PURE__ */ jsxRuntime.jsx(
441
- "li",
442
- {
443
- role: "option",
444
- "aria-selected": selected,
445
- className: `poolse-list__item${selected ? " poolse-list__item--selected" : ""}`,
446
- onClick: () => onSelect?.(conv),
447
- onKeyDown: (e) => {
448
- if (e.key === "Enter" || e.key === " ") {
449
- e.preventDefault();
450
- onSelect?.(conv);
451
- }
452
- },
453
- tabIndex: 0,
454
- children: content
455
- },
456
- conv.id
457
- );
458
- }) });
459
- }
460
- function DefaultRow({ conv, selected: _selected }) {
461
- const title = conv.name ?? "Untitled conversation";
462
- const time = conv.last_message_at ? formatRelative(conv.last_message_at) : "";
463
- return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
464
- /* @__PURE__ */ jsxRuntime.jsx(Avatar, { src: conv.avatar_url ?? null, name: conv.name, size: "md" }),
465
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-list__body", children: [
466
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-list__head", children: [
467
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-list__title", children: title }),
468
- time && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "poolse-list__time", children: time })
469
- ] }),
470
- /* @__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" })
471
- ] })
472
- ] });
473
- }
474
- function formatRelative(iso) {
475
- const then = new Date(iso).getTime();
476
- const now = Date.now();
477
- const diffSec = Math.max(0, Math.floor((now - then) / 1e3));
478
- if (diffSec < 60) return "now";
479
- if (diffSec < 3600) return `${Math.floor(diffSec / 60)}m`;
480
- if (diffSec < 86400) return `${Math.floor(diffSec / 3600)}h`;
481
- return `${Math.floor(diffSec / 86400)}d`;
482
- }
483
- function MemberList({
484
- conversationId,
485
- labelFor,
486
- avatarFor,
487
- onlineUserIds,
488
- canRemove,
489
- renderItem,
490
- emptyState
491
- }) {
492
- const { members, loading, error, removeMember } = react$1.useMembers(conversationId);
493
- if (loading && members.length === 0) {
494
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: "Loading members\u2026" });
495
- }
496
- if (error) {
497
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder poolse-list--error", children: "Failed to load members." });
498
- }
499
- if (members.length === 0) {
500
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-list poolse-list--placeholder", children: emptyState ?? "No members yet." });
501
- }
502
- const label = labelFor ?? ((id) => id.slice(0, 8));
503
- return /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "poolse-list", role: "list", "aria-label": "Members", children: members.map((m) => {
504
- const content = renderItem ? renderItem(m) : /* @__PURE__ */ jsxRuntime.jsx(
505
- DefaultMemberRow,
506
- {
507
- membership: m,
508
- name: label(m.user_id),
509
- avatarUrl: avatarFor?.(m.user_id) ?? null,
510
- online: onlineUserIds?.has(m.user_id) ?? false,
511
- removable: canRemove?.(m) ?? false,
512
- onRemove: () => {
513
- void removeMember(m.user_id);
514
- }
515
- }
516
- );
517
- return /* @__PURE__ */ jsxRuntime.jsx("li", { className: "poolse-list__item", children: content }, m.id);
518
- }) });
519
- }
520
- function DefaultMemberRow({
521
- membership,
522
- name,
523
- avatarUrl,
524
- online,
525
- removable,
526
- onRemove
527
- }) {
528
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-member-row", children: [
529
- /* @__PURE__ */ jsxRuntime.jsx(Avatar, { src: avatarUrl, name, online, size: "md" }),
530
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "poolse-member-row__body", children: [
531
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-member-row__name", children: name }),
532
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-member-row__sub", children: online ? "online" : "offline" })
533
- ] }),
534
- /* @__PURE__ */ jsxRuntime.jsx(RoleBadge, { role: membership.role }),
535
- removable && /* @__PURE__ */ jsxRuntime.jsx(
536
- "button",
537
- {
538
- type: "button",
539
- className: "poolse-icon-btn poolse-icon-btn--danger",
540
- "aria-label": `Remove ${name}`,
541
- onClick: (e) => {
542
- e.stopPropagation();
543
- onRemove();
544
- },
545
- children: /* @__PURE__ */ jsxRuntime.jsx(PoolseIcon, { name: "trash", size: 16, label: null })
546
- }
547
- )
548
- ] });
549
- }
550
- function RoleBadge({ role }) {
551
- const mod = role === "owner" ? "owner" : role === "admin" ? "admin" : null;
552
- return /* @__PURE__ */ jsxRuntime.jsx("span", { className: `poolse-badge${mod ? ` poolse-badge--${mod}` : ""}`, children: role });
553
- }
554
- function AttachmentPreview({ attachment, onClick }) {
555
- const isFullRow = "content_type" in attachment;
556
- const { url, loading, error } = react$1.useAttachmentUrl(attachment.id);
557
- if (loading) {
558
- 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" });
559
477
  }
560
478
  if (error || !url) {
561
479
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "poolse-attachment poolse-list--placeholder poolse-list--error", children: "Failed to load attachment." });
@@ -645,6 +563,115 @@ function formatBytes(n) {
645
563
  if (n < 1024 * 1024 * 1024) return `${(n / (1024 * 1024)).toFixed(1)} MB`;
646
564
  return `${(n / (1024 * 1024 * 1024)).toFixed(2)} GB`;
647
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
+ }
648
675
  var COMMON_EMOJIS = ["\u{1F44D}", "\u2764\uFE0F", "\u{1F602}", "\u{1F389}", "\u{1F622}", "\u{1F64F}"];
649
676
  function ReactionStrip({
650
677
  messageId,
@@ -738,164 +765,633 @@ function ReactionPicker({
738
765
  }
739
766
  );
740
767
  }
741
- function MentionInput({
768
+ function MessageActions({
769
+ onReact,
770
+ onReply,
771
+ onEdit,
772
+ onDelete,
773
+ children
774
+ }) {
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
854
+ }
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)
864
+ }
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
+ )
883
+ }
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({
742
918
  conversationId,
743
- onSend,
744
- onTyping,
745
- placeholder = "Type a message\u2026",
746
- disabled = false,
747
- labelFor
919
+ rootMessage,
920
+ onClose,
921
+ renderMessage
748
922
  }) {
749
- const { members } = react$1.useMembers(conversationId);
750
- const [value, setValue] = react.useState("");
751
- const [sending, setSending] = react.useState(false);
752
- const [mention, setMention] = react.useState(null);
753
- const [activeIdx, setActiveIdx] = react.useState(0);
754
- const selectedMentions = react.useRef(/* @__PURE__ */ new Set());
755
- const taRef = react.useRef(null);
756
- const label = labelFor ?? ((id) => id.slice(0, 8));
757
- const filtered = mention ? members.filter((m) => label(m.user_id).toLowerCase().includes(mention.query.toLowerCase())) : [];
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);
758
930
  react.useEffect(() => {
759
- setActiveIdx(0);
760
- }, [filtered.length, mention?.query]);
761
- const submit = async () => {
762
- const trimmed = value.trim();
763
- if (!trimmed || sending) return;
764
- setSending(true);
765
- try {
766
- const mentions = Array.from(selectedMentions.current);
767
- await onSend({
768
- body: trimmed,
769
- ...mentions.length > 0 ? { mentions } : {}
770
- });
771
- setValue("");
772
- selectedMentions.current = /* @__PURE__ */ new Set();
773
- } finally {
774
- setSending(false);
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);
775
1058
  }
776
- };
777
- const insertMention = (m) => {
778
- if (!mention) return;
779
- const name = label(m.user_id);
780
- const before = value.slice(0, mention.start);
781
- const after = value.slice(mention.start + 1 + mention.query.length);
782
- const inserted = `@${name} `;
783
- const next = `${before}${inserted}${after}`;
784
- selectedMentions.current.add(m.user_id);
785
- setValue(next);
786
- setMention(null);
787
- requestAnimationFrame(() => {
788
- const ta = taRef.current;
789
- if (!ta) return;
790
- const pos = before.length + inserted.length;
791
- ta.focus();
792
- ta.setSelectionRange(pos, pos);
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;
793
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);
794
1079
  };
795
- const onChange = (e) => {
796
- const next = e.target.value;
797
- setValue(next);
798
- onTyping?.();
799
- const caret = e.target.selectionStart ?? next.length;
800
- const head = next.slice(0, caret);
801
- const at = head.lastIndexOf("@");
802
- if (at === -1) {
803
- setMention(null);
804
- return;
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;
1092
+ }
1093
+ any = true;
805
1094
  }
806
- const charBefore = at === 0 ? " " : head.charAt(at - 1);
807
- const isAtBoundary = /\s/.test(charBefore);
808
- const query = head.slice(at + 1);
809
- if (isAtBoundary && !/\s/.test(query)) {
810
- setMention({ start: at, query });
811
- } else {
812
- setMention(null);
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);
813
1115
  }
814
1116
  };
815
- const onKeyDown = (e) => {
816
- if (mention && filtered.length > 0) {
817
- if (e.key === "ArrowDown") {
818
- e.preventDefault();
819
- setActiveIdx((i) => (i + 1) % filtered.length);
820
- return;
821
- }
822
- if (e.key === "ArrowUp") {
823
- e.preventDefault();
824
- setActiveIdx((i) => (i - 1 + filtered.length) % filtered.length);
825
- return;
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,
1125
+ {
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);
1143
+ }
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)
1165
+ },
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)
826
1201
  }
827
- if (e.key === "Enter" || e.key === "Tab") {
828
- const pick = filtered[activeIdx];
829
- if (pick) {
830
- e.preventDefault();
831
- insertMention(pick);
832
- return;
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 = "";
833
1221
  }
834
1222
  }
835
- if (e.key === "Escape") {
836
- e.preventDefault();
837
- setMention(null);
838
- return;
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 })
839
1234
  }
840
- }
841
- if (e.key === "Enter" && !e.shiftKey) {
842
- e.preventDefault();
843
- void submit();
844
- }
845
- };
846
- return /* @__PURE__ */ jsxRuntime.jsxs(
847
- "form",
848
- {
849
- className: "poolse-composer",
850
- onSubmit: (e) => {
851
- e.preventDefault();
852
- void submit();
853
- },
854
- style: { position: "relative" },
855
- children: [
856
- /* @__PURE__ */ jsxRuntime.jsx(
857
- "textarea",
858
- {
859
- ref: taRef,
860
- className: "poolse-composer__input",
861
- rows: 1,
862
- value,
863
- onChange,
864
- onKeyDown,
865
- placeholder,
866
- disabled: disabled || sending
867
- }
868
- ),
869
- /* @__PURE__ */ jsxRuntime.jsx(
870
- "button",
871
- {
872
- type: "submit",
873
- className: "poolse-composer__send",
874
- disabled: disabled || sending || value.trim() === "",
875
- "aria-label": "Send message",
876
- children: /* @__PURE__ */ jsxRuntime.jsx(PoolseIcon, { name: "send-fill", label: null })
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);
877
1293
  }
878
- ),
879
- 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(
880
- "div",
881
- {
882
- role: "option",
883
- "aria-selected": i === activeIdx,
884
- className: `poolse-mention-menu__item${i === activeIdx ? " poolse-mention-menu__item--active" : ""}`,
885
- onMouseDown: (e) => {
886
- e.preventDefault();
887
- insertMention(m);
888
- },
889
- children: /* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
890
- "@",
891
- label(m.user_id)
892
- ] })
893
- },
894
- m.id
895
- )) })
896
- ]
897
- }
898
- );
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 });
899
1395
  }
900
1396
  var MARK_PATHS = /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
901
1397
  /* @__PURE__ */ jsxRuntime.jsx(
@@ -1009,8 +1505,10 @@ exports.AttachmentPreview = AttachmentPreview;
1009
1505
  exports.Avatar = Avatar;
1010
1506
  exports.ConversationList = ConversationList;
1011
1507
  exports.ConversationView = ConversationView;
1508
+ exports.EditableMessageBubble = EditableMessageBubble;
1012
1509
  exports.MemberList = MemberList;
1013
1510
  exports.MentionInput = MentionInput;
1511
+ exports.MessageActions = MessageActions;
1014
1512
  exports.MessageBubble = MessageBubble;
1015
1513
  exports.MessageComposer = MessageComposer;
1016
1514
  exports.PoolseFonts = PoolseFonts;
@@ -1018,6 +1516,7 @@ exports.PoolseIcon = PoolseIcon;
1018
1516
  exports.PoolseLogo = PoolseLogo;
1019
1517
  exports.ReactionPicker = ReactionPicker;
1020
1518
  exports.ReactionStrip = ReactionStrip;
1519
+ exports.ThreadView = ThreadView;
1021
1520
  exports.TypingIndicator = TypingIndicator;
1022
1521
  exports.usePoolseFonts = usePoolseFonts;
1023
1522
  //# sourceMappingURL=index.cjs.map