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