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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use client';
2
- import { useMe, useMessages, useTyping, useRealtimeStatus, useConversations, useMembers, useAttachmentUrl, useReactions } from '@poolse/react';
3
- import { useEffect, useState, useRef, Fragment } from 'react';
4
- import { jsx, jsxs, Fragment as Fragment$1 } from 'react/jsx-runtime';
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
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
5
 
6
6
  // src/ConversationView.tsx
7
7
  var FONT_HREF = "https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:opsz,wght@12..96,400;12..96,600;12..96,800&family=Hanken+Grotesk:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap";
@@ -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,240 +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." }) : (
389
- // Fragment, not a wrapper <div>: MessageBubble's
390
- // `align-self: flex-end|flex-start` only works when the
391
- // bubble is a DIRECT flex child of `.poolse-conversation__
392
- // messages`. A wrapper div would block-default to 100%
393
- // width and pin every bubble to the left.
394
- messages.map((msg) => /* @__PURE__ */ jsx(Fragment, { children: renderOne(msg, me?.id ?? null) }, msg.id))
395
- ),
396
- error && /* @__PURE__ */ jsxs("div", { className: "poolse-conversation__empty", children: [
397
- "Failed to load: ",
398
- error.message
399
- ] })
400
- ] }),
401
- /* @__PURE__ */ jsx(TypingIndicator, { typing, ...labelFor ? { labelFor } : {} }),
402
- /* @__PURE__ */ jsx(MessageComposer, { onSend: (body) => send({ body }), onTyping: signalTyping })
403
- ] });
404
- }
405
- function defaultRenderMessage(msg, currentUserId) {
406
- return /* @__PURE__ */ jsx(MessageBubble, { message: msg, currentUserId });
407
- }
408
- function Avatar({ src, name, online, size = "md", className }) {
409
- const initials = computeInitials(name);
410
- const classes = [
411
- "poolse-avatar",
412
- size === "sm" ? "poolse-avatar--sm" : null,
413
- size === "lg" ? "poolse-avatar--lg" : null,
414
- className
415
- ].filter(Boolean).join(" ");
416
- return /* @__PURE__ */ jsxs("span", { className: classes, "aria-label": name ?? "avatar", role: "img", children: [
417
- src ? /* @__PURE__ */ jsx("img", { src, alt: name ?? "" }) : /* @__PURE__ */ jsx("span", { "aria-hidden": "true", children: initials }),
418
- online ? /* @__PURE__ */ jsx("span", { className: "poolse-avatar__presence", "aria-hidden": "true" }) : null
419
- ] });
420
- }
421
- function computeInitials(name) {
422
- if (!name) return "?";
423
- const parts = name.trim().split(/\s+/).slice(0, 2);
424
- return parts.map((p) => p.charAt(0).toUpperCase()).join("").slice(0, 2) || "?";
425
- }
426
- function ConversationList({
427
- selectedId,
428
- onSelect,
429
- renderItem,
430
- emptyState,
431
- conversations: controlledConversations,
432
- loading: controlledLoading,
433
- error: controlledError
434
- }) {
435
- const auto = useConversations();
436
- const isControlled = controlledConversations !== void 0;
437
- const conversations = isControlled ? controlledConversations : auto.conversations;
438
- const loading = isControlled ? controlledLoading ?? false : auto.loading;
439
- const error = isControlled ? controlledError ?? null : auto.error;
440
- if (loading && conversations.length === 0) {
441
- return /* @__PURE__ */ jsx("div", { className: "poolse-list poolse-list--placeholder", children: "Loading conversations\u2026" });
442
- }
443
- if (error) {
444
- return /* @__PURE__ */ jsx("div", { className: "poolse-list poolse-list--placeholder poolse-list--error", children: "Failed to load conversations." });
445
- }
446
- if (conversations.length === 0) {
447
- return /* @__PURE__ */ jsx("div", { className: "poolse-list poolse-list--placeholder", children: emptyState ?? "No conversations yet." });
448
- }
449
- return /* @__PURE__ */ jsx("ul", { className: "poolse-list", role: "listbox", "aria-label": "Conversations", children: conversations.map((conv) => {
450
- const selected = selectedId === conv.id;
451
- const content = renderItem ? renderItem(conv, selected) : /* @__PURE__ */ jsx(DefaultRow, { conv, selected });
452
- return /* @__PURE__ */ jsx(
453
- "li",
454
- {
455
- role: "option",
456
- "aria-selected": selected,
457
- className: `poolse-list__item${selected ? " poolse-list__item--selected" : ""}`,
458
- onClick: () => onSelect?.(conv),
459
- onKeyDown: (e) => {
460
- if (e.key === "Enter" || e.key === " ") {
461
- e.preventDefault();
462
- onSelect?.(conv);
463
- }
464
- },
465
- tabIndex: 0,
466
- children: content
467
- },
468
- conv.id
469
- );
470
- }) });
471
- }
472
- function DefaultRow({ conv, selected: _selected }) {
473
- const title = conv.name ?? "Untitled conversation";
474
- const time = conv.last_message_at ? formatRelative(conv.last_message_at) : "";
475
- return /* @__PURE__ */ jsxs(Fragment$1, { children: [
476
- /* @__PURE__ */ jsx(Avatar, { src: conv.avatar_url ?? null, name: conv.name, size: "md" }),
477
- /* @__PURE__ */ jsxs("div", { className: "poolse-list__body", children: [
478
- /* @__PURE__ */ jsxs("div", { className: "poolse-list__head", children: [
479
- /* @__PURE__ */ jsx("span", { className: "poolse-list__title", children: title }),
480
- time && /* @__PURE__ */ jsx("span", { className: "poolse-list__time", children: time })
481
- ] }),
482
- /* @__PURE__ */ jsx("div", { className: "poolse-list__preview", children: conv.last_sequence > 0 ? `${conv.last_sequence} message${conv.last_sequence === 1 ? "" : "s"}` : "No messages yet" })
483
- ] })
484
- ] });
485
- }
486
- function formatRelative(iso) {
487
- const then = new Date(iso).getTime();
488
- const now = Date.now();
489
- const diffSec = Math.max(0, Math.floor((now - then) / 1e3));
490
- if (diffSec < 60) return "now";
491
- if (diffSec < 3600) return `${Math.floor(diffSec / 60)}m`;
492
- if (diffSec < 86400) return `${Math.floor(diffSec / 3600)}h`;
493
- return `${Math.floor(diffSec / 86400)}d`;
494
- }
495
- function MemberList({
496
- conversationId,
497
- labelFor,
498
- avatarFor,
499
- onlineUserIds,
500
- canRemove,
501
- renderItem,
502
- emptyState
503
- }) {
504
- const { members, loading, error, removeMember } = useMembers(conversationId);
505
- if (loading && members.length === 0) {
506
- return /* @__PURE__ */ jsx("div", { className: "poolse-list poolse-list--placeholder", children: "Loading members\u2026" });
507
- }
508
- if (error) {
509
- return /* @__PURE__ */ jsx("div", { className: "poolse-list poolse-list--placeholder poolse-list--error", children: "Failed to load members." });
510
- }
511
- if (members.length === 0) {
512
- return /* @__PURE__ */ jsx("div", { className: "poolse-list poolse-list--placeholder", children: emptyState ?? "No members yet." });
513
- }
514
- const label = labelFor ?? ((id) => id.slice(0, 8));
515
- return /* @__PURE__ */ jsx("ul", { className: "poolse-list", role: "list", "aria-label": "Members", children: members.map((m) => {
516
- const content = renderItem ? renderItem(m) : /* @__PURE__ */ jsx(
517
- DefaultMemberRow,
518
- {
519
- membership: m,
520
- name: label(m.user_id),
521
- avatarUrl: avatarFor?.(m.user_id) ?? null,
522
- online: onlineUserIds?.has(m.user_id) ?? false,
523
- removable: canRemove?.(m) ?? false,
524
- onRemove: () => {
525
- void removeMember(m.user_id);
526
- }
527
- }
528
- );
529
- return /* @__PURE__ */ jsx("li", { className: "poolse-list__item", children: content }, m.id);
530
- }) });
531
- }
532
- function DefaultMemberRow({
533
- membership,
534
- name,
535
- avatarUrl,
536
- online,
537
- removable,
538
- onRemove
539
- }) {
540
- return /* @__PURE__ */ jsxs("div", { className: "poolse-member-row", children: [
541
- /* @__PURE__ */ jsx(Avatar, { src: avatarUrl, name, online, size: "md" }),
542
- /* @__PURE__ */ jsxs("div", { className: "poolse-member-row__body", children: [
543
- /* @__PURE__ */ jsx("div", { className: "poolse-member-row__name", children: name }),
544
- /* @__PURE__ */ jsx("div", { className: "poolse-member-row__sub", children: online ? "online" : "offline" })
545
- ] }),
546
- /* @__PURE__ */ jsx(RoleBadge, { role: membership.role }),
547
- removable && /* @__PURE__ */ jsx(
548
- "button",
549
- {
550
- type: "button",
551
- className: "poolse-icon-btn poolse-icon-btn--danger",
552
- "aria-label": `Remove ${name}`,
553
- onClick: (e) => {
554
- e.stopPropagation();
555
- onRemove();
556
- },
557
- children: /* @__PURE__ */ jsx(PoolseIcon, { name: "trash", size: 16, label: null })
558
- }
559
- )
560
- ] });
561
- }
562
- function RoleBadge({ role }) {
563
- const mod = role === "owner" ? "owner" : role === "admin" ? "admin" : null;
564
- return /* @__PURE__ */ jsx("span", { className: `poolse-badge${mod ? ` poolse-badge--${mod}` : ""}`, children: role });
565
- }
566
- function AttachmentPreview({ attachment, onClick }) {
567
- const isFullRow = "content_type" in attachment;
568
- const { url, loading, error } = useAttachmentUrl(attachment.id);
569
- if (loading) {
570
- 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" });
571
475
  }
572
476
  if (error || !url) {
573
477
  return /* @__PURE__ */ jsx("div", { className: "poolse-attachment poolse-list--placeholder poolse-list--error", children: "Failed to load attachment." });
@@ -657,6 +561,115 @@ function formatBytes(n) {
657
561
  if (n < 1024 * 1024 * 1024) return `${(n / (1024 * 1024)).toFixed(1)} MB`;
658
562
  return `${(n / (1024 * 1024 * 1024)).toFixed(2)} GB`;
659
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
+ }
660
673
  var COMMON_EMOJIS = ["\u{1F44D}", "\u2764\uFE0F", "\u{1F602}", "\u{1F389}", "\u{1F622}", "\u{1F64F}"];
661
674
  function ReactionStrip({
662
675
  messageId,
@@ -695,7 +708,7 @@ function ReactionStrip({
695
708
  emoji
696
709
  );
697
710
  }),
698
- picker && /* @__PURE__ */ jsxs(Fragment$1, { children: [
711
+ picker && /* @__PURE__ */ jsxs(Fragment, { children: [
699
712
  /* @__PURE__ */ jsxs(
700
713
  "button",
701
714
  {
@@ -750,166 +763,635 @@ function ReactionPicker({
750
763
  }
751
764
  );
752
765
  }
753
- function MentionInput({
754
- conversationId,
755
- onSend,
756
- onTyping,
757
- placeholder = "Type a message\u2026",
758
- disabled = false,
759
- labelFor
766
+ function MessageActions({
767
+ onReact,
768
+ onReply,
769
+ onEdit,
770
+ onDelete,
771
+ children
760
772
  }) {
761
- const { members } = useMembers(conversationId);
762
- const [value, setValue] = useState("");
763
- const [sending, setSending] = useState(false);
764
- const [mention, setMention] = useState(null);
765
- const [activeIdx, setActiveIdx] = useState(0);
766
- const selectedMentions = useRef(/* @__PURE__ */ new Set());
767
- const taRef = useRef(null);
768
- const label = labelFor ?? ((id) => id.slice(0, 8));
769
- const filtered = mention ? members.filter((m) => label(m.user_id).toLowerCase().includes(mention.query.toLowerCase())) : [];
770
- useEffect(() => {
771
- setActiveIdx(0);
772
- }, [filtered.length, mention?.query]);
773
- const submit = async () => {
774
- const trimmed = value.trim();
775
- if (!trimmed || sending) return;
776
- setSending(true);
777
- try {
778
- const mentions = Array.from(selectedMentions.current);
779
- await onSend({
780
- body: trimmed,
781
- ...mentions.length > 0 ? { mentions } : {}
782
- });
783
- setValue("");
784
- selectedMentions.current = /* @__PURE__ */ new Set();
785
- } finally {
786
- setSending(false);
787
- }
788
- };
789
- const insertMention = (m) => {
790
- if (!mention) return;
791
- const name = label(m.user_id);
792
- const before = value.slice(0, mention.start);
793
- const after = value.slice(mention.start + 1 + mention.query.length);
794
- const inserted = `@${name} `;
795
- const next = `${before}${inserted}${after}`;
796
- selectedMentions.current.add(m.user_id);
797
- setValue(next);
798
- setMention(null);
799
- requestAnimationFrame(() => {
800
- const ta = taRef.current;
801
- if (!ta) return;
802
- const pos = before.length + inserted.length;
803
- ta.focus();
804
- ta.setSelectionRange(pos, pos);
805
- });
806
- };
807
- const onChange = (e) => {
808
- const next = e.target.value;
809
- setValue(next);
810
- onTyping?.();
811
- const caret = e.target.selectionStart ?? next.length;
812
- const head = next.slice(0, caret);
813
- const at = head.lastIndexOf("@");
814
- if (at === -1) {
815
- setMention(null);
816
- return;
817
- }
818
- const charBefore = at === 0 ? " " : head.charAt(at - 1);
819
- const isAtBoundary = /\s/.test(charBefore);
820
- const query = head.slice(at + 1);
821
- if (isAtBoundary && !/\s/.test(query)) {
822
- setMention({ start: at, query });
823
- } else {
824
- setMention(null);
825
- }
826
- };
827
- const onKeyDown = (e) => {
828
- if (mention && filtered.length > 0) {
829
- if (e.key === "ArrowDown") {
830
- e.preventDefault();
831
- setActiveIdx((i) => (i + 1) % filtered.length);
832
- return;
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
833
852
  }
834
- if (e.key === "ArrowUp") {
835
- e.preventDefault();
836
- setActiveIdx((i) => (i - 1 + filtered.length) % filtered.length);
837
- return;
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)
838
862
  }
839
- if (e.key === "Enter" || e.key === "Tab") {
840
- const pick = filtered[activeIdx];
841
- if (pick) {
842
- e.preventDefault();
843
- insertMention(pick);
844
- return;
845
- }
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
+ )
846
881
  }
847
- if (e.key === "Escape") {
848
- e.preventDefault();
849
- setMention(null);
850
- return;
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({
916
+ conversationId,
917
+ rootMessage,
918
+ onClose,
919
+ renderMessage
920
+ }) {
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);
928
+ useEffect(() => {
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);
1056
+ }
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;
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);
1077
+ };
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;
851
1090
  }
1091
+ any = true;
852
1092
  }
853
- if (e.key === "Enter" && !e.shiftKey) {
854
- e.preventDefault();
855
- void submit();
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);
856
1113
  }
857
1114
  };
858
- return /* @__PURE__ */ jsxs(
859
- "form",
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,
860
1123
  {
861
- className: "poolse-composer",
862
- onSubmit: (e) => {
863
- e.preventDefault();
864
- void submit();
865
- },
866
- style: { position: "relative" },
867
- children: [
868
- /* @__PURE__ */ jsx(
869
- "textarea",
870
- {
871
- ref: taRef,
872
- className: "poolse-composer__input",
873
- rows: 1,
874
- value,
875
- onChange,
876
- onKeyDown,
877
- placeholder,
878
- disabled: disabled || sending
879
- }
880
- ),
881
- /* @__PURE__ */ jsx(
882
- "button",
883
- {
884
- type: "submit",
885
- className: "poolse-composer__send",
886
- disabled: disabled || sending || value.trim() === "",
887
- "aria-label": "Send message",
888
- children: /* @__PURE__ */ jsx(PoolseIcon, { name: "send-fill", label: null })
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);
889
1141
  }
890
- ),
891
- mention && filtered.length > 0 && /* @__PURE__ */ jsx("div", { className: "poolse-mention-menu", role: "listbox", "aria-label": "Members", children: filtered.map((m, i) => /* @__PURE__ */ jsx(
892
- "div",
893
- {
894
- role: "option",
895
- "aria-selected": i === activeIdx,
896
- className: `poolse-mention-menu__item${i === activeIdx ? " poolse-mention-menu__item--active" : ""}`,
897
- onMouseDown: (e) => {
898
- e.preventDefault();
899
- insertMention(m);
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)
900
1163
  },
901
- children: /* @__PURE__ */ jsxs("span", { children: [
902
- "@",
903
- label(m.user_id)
904
- ] })
905
- },
906
- m.id
907
- )) })
908
- ]
909
- }
910
- );
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)
1199
+ }
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 = "";
1219
+ }
1220
+ }
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 })
1232
+ }
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);
1291
+ }
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 });
911
1393
  }
912
- var MARK_PATHS = /* @__PURE__ */ jsxs(Fragment$1, { children: [
1394
+ var MARK_PATHS = /* @__PURE__ */ jsxs(Fragment, { children: [
913
1395
  /* @__PURE__ */ jsx(
914
1396
  "path",
915
1397
  {
@@ -977,7 +1459,7 @@ var VARIANTS = {
977
1459
  lockup: {
978
1460
  vb: "0 0 454 100",
979
1461
  aspect: 4.54,
980
- render: () => /* @__PURE__ */ jsxs(Fragment$1, { children: [
1462
+ render: () => /* @__PURE__ */ jsxs(Fragment, { children: [
981
1463
  MARK_PATHS,
982
1464
  WORDMARK_PATHS(126)
983
1465
  ] })
@@ -987,7 +1469,7 @@ var VARIANTS = {
987
1469
  mono: {
988
1470
  vb: "0 0 454 100",
989
1471
  aspect: 4.54,
990
- render: () => /* @__PURE__ */ jsxs(Fragment$1, { children: [
1472
+ render: () => /* @__PURE__ */ jsxs(Fragment, { children: [
991
1473
  MARK_PATHS,
992
1474
  WORDMARK_PATHS(126)
993
1475
  ] })
@@ -1017,6 +1499,6 @@ function PoolseLogo({
1017
1499
  );
1018
1500
  }
1019
1501
 
1020
- 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 };
1021
1503
  //# sourceMappingURL=index.js.map
1022
1504
  //# sourceMappingURL=index.js.map