@dialtribe/chat-client 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -239,6 +239,12 @@ var InboxStore = class extends Store {
239
239
  this.readTimers = /* @__PURE__ */ new Map();
240
240
  /** Rooms currently on screen: activity there is read, not unread. */
241
241
  this.visible = /* @__PURE__ */ new Map();
242
+ /**
243
+ * Rooms the user removed (hide/ignore) and when. Filtered out of every list,
244
+ * so a refresh already in flight can't put them back, until a newer message
245
+ * arrives or the user messages/reopens/accepts them.
246
+ */
247
+ this.removed = /* @__PURE__ */ new Map();
242
248
  }
243
249
  acquire() {
244
250
  this.refs += 1;
@@ -269,11 +275,13 @@ var InboxStore = class extends Store {
269
275
  targetAvatarUrl: hint?.avatarUrl,
270
276
  showRequestId: hint?.showRequestId
271
277
  });
278
+ this.removed.delete(result.roomId);
272
279
  await this.refresh();
273
280
  return { status: result.status, roomId: result.roomId };
274
281
  }
275
282
  async acceptDM(requestId) {
276
283
  const { roomId } = await this.client.dm("chat-dm-accept", { requestId });
284
+ this.removed.delete(roomId);
277
285
  await this.refresh();
278
286
  return roomId;
279
287
  }
@@ -287,6 +295,7 @@ var InboxStore = class extends Store {
287
295
  }
288
296
  async reopenDM(roomId) {
289
297
  await this.client.dm("chat-dm-reopen", { roomId });
298
+ this.removed.delete(roomId);
290
299
  await this.refresh();
291
300
  }
292
301
  /** Add someone. Returns the room to show (a new group when added to a 1:1). */
@@ -300,6 +309,21 @@ var InboxStore = class extends Store {
300
309
  await this.refresh();
301
310
  return result.roomId;
302
311
  }
312
+ /**
313
+ * Remove a conversation from the user's own inbox (optimistic). Not deleted
314
+ * and nobody else is affected: it comes back when a newer message arrives or
315
+ * when the user messages/reopens it.
316
+ */
317
+ async hideDM(roomId) {
318
+ this.removeLocally(roomId);
319
+ try {
320
+ await this.client.dm("chat-dm-hide", { roomId });
321
+ } catch (err) {
322
+ this.removed.delete(roomId);
323
+ await this.refresh();
324
+ throw err;
325
+ }
326
+ }
303
327
  async setMuted(roomId, muted) {
304
328
  this.patchRoom(roomId, (room) => ({ ...room, isMuted: muted }));
305
329
  await this.client.dm("chat-dm-mute", { roomId, muted });
@@ -352,6 +376,7 @@ var InboxStore = class extends Store {
352
376
  }
353
377
  }
354
378
  onActivity(roomId, message) {
379
+ this.removed.delete(roomId);
355
380
  const known = this.getSnapshot().rooms.some((r) => r.roomId === roomId);
356
381
  if (!known) {
357
382
  this.scheduleRefresh(REFRESH_DEBOUNCE_MS);
@@ -409,9 +434,14 @@ var InboxStore = class extends Store {
409
434
  this.setRooms(moveToTop ? [next, ...rest] : rooms.map((r, i) => i === index ? next : r));
410
435
  }
411
436
  removeLocally(roomId) {
412
- this.setRooms(this.getSnapshot().rooms.filter((r) => r.roomId !== roomId));
437
+ this.removed.set(roomId, Date.now());
438
+ this.setRooms(this.getSnapshot().rooms);
413
439
  }
414
- setRooms(rooms) {
440
+ setRooms(all) {
441
+ const rooms = all.filter((r) => {
442
+ const removedAt = this.removed.get(r.roomId);
443
+ return removedAt === void 0 || r.lastMessageAt !== null && Date.parse(r.lastMessageAt) > removedAt;
444
+ });
415
445
  this.update({
416
446
  rooms,
417
447
  requests: rooms.filter((r) => r.myStatus === "pending"),
@@ -762,6 +792,7 @@ function useChatRooms() {
762
792
  ignoreDM: inbox.ignoreDM.bind(inbox),
763
793
  closeDM: inbox.closeDM.bind(inbox),
764
794
  reopenDM: inbox.reopenDM.bind(inbox),
795
+ hideDM: inbox.hideDM.bind(inbox),
765
796
  addParticipant: inbox.addParticipant.bind(inbox),
766
797
  setMuted: inbox.setMuted.bind(inbox),
767
798
  markRead: inbox.markRead.bind(inbox),
@@ -1098,6 +1129,7 @@ var DEFAULT_LABELS = {
1098
1129
  conversationClosed: "This conversation is closed",
1099
1130
  reopen: "Reopen",
1100
1131
  closeConversation: "Close conversation",
1132
+ removeConversation: "Remove from inbox",
1101
1133
  mute: "Mute",
1102
1134
  unmute: "Unmute",
1103
1135
  messagePlaceholder: "Message...",
@@ -1106,7 +1138,7 @@ var DEFAULT_LABELS = {
1106
1138
  function badgeText(count) {
1107
1139
  return count > 99 ? "99+" : String(count);
1108
1140
  }
1109
- function MessagesInbox({ rooms, requests, labels, onOpenRoom }) {
1141
+ function MessagesInbox({ rooms, requests, labels, onOpenRoom, onRemoveRoom }) {
1110
1142
  const conversations = rooms.filter((r) => r.myStatus === "accepted");
1111
1143
  if (rooms.length === 0) return /* @__PURE__ */ jsx("div", { className: "dtc-empty", children: labels.noMessages });
1112
1144
  return /* @__PURE__ */ jsxs("div", { className: "dtc-inbox", children: [
@@ -1114,19 +1146,46 @@ function MessagesInbox({ rooms, requests, labels, onOpenRoom }) {
1114
1146
  /* @__PURE__ */ jsx("h3", { className: "dtc-inbox-heading", children: labels.requests }),
1115
1147
  requests.map((room) => /* @__PURE__ */ jsx(InboxRow, { room, onOpen: onOpenRoom }, room.roomId))
1116
1148
  ] }),
1117
- conversations.length > 0 && /* @__PURE__ */ jsx("section", { className: "dtc-inbox-section", children: conversations.map((room) => /* @__PURE__ */ jsx(InboxRow, { room, onOpen: onOpenRoom }, room.roomId)) })
1149
+ conversations.length > 0 && /* @__PURE__ */ jsx("section", { className: "dtc-inbox-section", children: conversations.map((room) => /* @__PURE__ */ jsx(
1150
+ InboxRow,
1151
+ {
1152
+ room,
1153
+ onOpen: onOpenRoom,
1154
+ onRemove: onRemoveRoom,
1155
+ removeLabel: labels.removeConversation
1156
+ },
1157
+ room.roomId
1158
+ )) })
1118
1159
  ] });
1119
1160
  }
1120
- function InboxRow({ room, onOpen }) {
1161
+ function InboxRow({
1162
+ room,
1163
+ onOpen,
1164
+ onRemove,
1165
+ removeLabel
1166
+ }) {
1121
1167
  const unread = room.unreadCount > 0 && !room.isMuted;
1122
1168
  const lead = room.participants[0];
1123
- return /* @__PURE__ */ jsxs("button", { type: "button", className: `dtc-inbox-row ${unread ? "dtc-inbox-row-unread" : ""}`, onClick: () => onOpen(room.roomId), children: [
1124
- /* @__PURE__ */ jsx(Avatar, { name: room.name, url: room.kind === "dm" ? lead?.avatarUrl : null, size: 40 }),
1125
- /* @__PURE__ */ jsxs("span", { className: "dtc-inbox-text", children: [
1126
- /* @__PURE__ */ jsx("span", { className: "dtc-inbox-name", children: room.name }),
1127
- room.lastMessage && /* @__PURE__ */ jsx("span", { className: "dtc-inbox-preview", children: room.lastMessage.text })
1169
+ return /* @__PURE__ */ jsxs("div", { className: `dtc-inbox-item ${unread ? "dtc-inbox-row-unread" : ""}`, children: [
1170
+ /* @__PURE__ */ jsxs("button", { type: "button", className: "dtc-inbox-row", onClick: () => onOpen(room.roomId), children: [
1171
+ /* @__PURE__ */ jsx(Avatar, { name: room.name, url: room.kind === "dm" ? lead?.avatarUrl : null, size: 40 }),
1172
+ /* @__PURE__ */ jsxs("span", { className: "dtc-inbox-text", children: [
1173
+ /* @__PURE__ */ jsx("span", { className: "dtc-inbox-name", children: room.name }),
1174
+ room.lastMessage && /* @__PURE__ */ jsx("span", { className: "dtc-inbox-preview", children: room.lastMessage.text })
1175
+ ] }),
1176
+ unread && /* @__PURE__ */ jsx("span", { className: "dtc-badge-count", children: badgeText(room.unreadCount) })
1128
1177
  ] }),
1129
- unread && /* @__PURE__ */ jsx("span", { className: "dtc-badge-count", children: badgeText(room.unreadCount) })
1178
+ onRemove && /* @__PURE__ */ jsx(
1179
+ "button",
1180
+ {
1181
+ type: "button",
1182
+ className: "dtc-inbox-remove",
1183
+ onClick: () => onRemove(room.roomId),
1184
+ "aria-label": `${removeLabel ?? "Remove"}: ${room.name}`,
1185
+ title: removeLabel,
1186
+ children: /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", width: "14", height: "14", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M18 6L6 18M6 6l12 12" }) })
1187
+ }
1188
+ )
1130
1189
  ] });
1131
1190
  }
1132
1191
  function ConversationView({ roomId, labels, onBack }) {
@@ -1192,9 +1251,11 @@ var RAIL_WIDTH = 64;
1192
1251
  var EDGE_SWIPE_ZONE = 24;
1193
1252
  var SWIPE_DISTANCE = 60;
1194
1253
  function DialtribeMessages({
1254
+ side = "right",
1195
1255
  placement = "push",
1196
1256
  openPlacement = "push",
1197
1257
  items = [],
1258
+ inboxRowClose = true,
1198
1259
  contexts = [],
1199
1260
  theme,
1200
1261
  labels: labelOverrides,
@@ -1216,10 +1277,11 @@ function DialtribeMessages({
1216
1277
  const activeItem = items.find((i) => i.id === activeItemId);
1217
1278
  const topItems = items.filter((i) => (i.placement ?? "top") === "top");
1218
1279
  const bottomItems = items.filter((i) => i.placement === "bottom");
1280
+ const footerItems = items.filter((i) => i.placement === "footer");
1219
1281
  const panelInset = isOpen && openPlacement === "push" ? panelWidth : 0;
1220
- usePushLayout(placement === "push" && !isMobile, RAIL_WIDTH + panelInset);
1282
+ usePushLayout(placement === "push" && !isMobile, RAIL_WIDTH + panelInset, side);
1221
1283
  useScrollLock(isMobile && isOpen);
1222
- useEdgeSwipe(isMobile, isOpen, ui.open, ui.close);
1284
+ useEdgeSwipe(isMobile, isOpen, ui.open, ui.close, side);
1223
1285
  useEffect(() => {
1224
1286
  if (!isOpen) return;
1225
1287
  const onKey = (e) => e.key === "Escape" && ui.close();
@@ -1253,6 +1315,7 @@ function DialtribeMessages({
1253
1315
  "div",
1254
1316
  {
1255
1317
  className: `dtc-rail-root ${isMobile ? "dtc-rail-root-mobile" : ""} ${isOpen ? "dtc-rail-root-open" : ""}`,
1318
+ "data-side": side,
1256
1319
  style,
1257
1320
  children: [
1258
1321
  isMobile && !isOpen && /* @__PURE__ */ jsxs("button", { type: "button", className: "dtc-messages-fab", onClick: ui.openInbox, "aria-label": labels.openMessages, children: [
@@ -1264,7 +1327,17 @@ function DialtribeMessages({
1264
1327
  /* @__PURE__ */ jsx("span", { className: "dtc-panel-title", children: labels.messages }),
1265
1328
  /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: ui.close, "aria-label": labels.close, title: labels.close, children: /* @__PURE__ */ jsx(CloseIcon, {}) })
1266
1329
  ] }),
1267
- /* @__PURE__ */ jsx(MessagesInbox, { rooms: inbox.rooms, requests: inbox.requests, labels, onOpenRoom: ui.openRoom })
1330
+ /* @__PURE__ */ jsx(
1331
+ MessagesInbox,
1332
+ {
1333
+ rooms: inbox.rooms,
1334
+ requests: inbox.requests,
1335
+ labels,
1336
+ onOpenRoom: ui.openRoom,
1337
+ onRemoveRoom: inboxRowClose ? (roomId) => void inbox.hideDM(roomId).catch(() => {
1338
+ }) : void 0
1339
+ }
1340
+ )
1268
1341
  ] }) : activeContext ? /* @__PURE__ */ jsxs("div", { className: "dtc-conversation", children: [
1269
1342
  /* @__PURE__ */ jsxs("div", { className: "dtc-conversation-header", children: [
1270
1343
  /* @__PURE__ */ jsx(Avatar, { name: activeContext.label, url: activeContext.avatarUrl, size: 28 }),
@@ -1274,44 +1347,47 @@ function DialtribeMessages({
1274
1347
  /* @__PURE__ */ jsx(ChatRoomProvider, { roomId: activeContext.roomId, children: /* @__PURE__ */ jsx(ChatPanel, { showHeader: false, emptyLabel: labels.noMessages, placeholder: labels.messagePlaceholder }) })
1275
1348
  ] }) : /* @__PURE__ */ jsx(ConversationView, { roomId: view.roomId, labels, onBack: ui.openInbox }, view.roomId) }),
1276
1349
  showRail && /* @__PURE__ */ jsxs("nav", { className: "dtc-rail", "aria-label": labels.messages, children: [
1277
- topItems.map(renderItemButton),
1278
- topItems.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-rail-divider" }),
1279
- /* @__PURE__ */ jsx(
1280
- RailButton,
1281
- {
1282
- label: labels.messages,
1283
- active: isOpen && view.kind === "inbox",
1284
- badge: rollup,
1285
- onClick: () => select({ kind: "inbox" }),
1286
- children: /* @__PURE__ */ jsx(ChatIcon, {})
1287
- }
1288
- ),
1289
- contexts.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-rail-divider" }),
1290
- contexts.map((c) => /* @__PURE__ */ jsx(
1291
- RailButton,
1292
- {
1293
- label: c.label,
1294
- active: activeRoomId === c.roomId,
1295
- badge: c.badge ?? 0,
1296
- onClick: () => select({ kind: "room", roomId: c.roomId }),
1297
- children: /* @__PURE__ */ jsx(Avatar, { name: c.label, url: c.avatarUrl, size: 36 })
1298
- },
1299
- c.roomId
1300
- )),
1301
- recentDms.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-rail-divider" }),
1302
- recentDms.map((room) => /* @__PURE__ */ jsx(
1303
- RailButton,
1304
- {
1305
- label: room.name,
1306
- active: activeRoomId === room.roomId,
1307
- badge: room.isMuted ? 0 : room.unreadCount,
1308
- onClick: () => select({ kind: "room", roomId: room.roomId }),
1309
- children: /* @__PURE__ */ jsx(Avatar, { name: room.name, url: room.kind === "dm" ? room.participants[0]?.avatarUrl : null, size: 36 })
1310
- },
1311
- room.roomId
1312
- )),
1313
- bottomItems.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-rail-divider" }),
1314
- bottomItems.map(renderItemButton)
1350
+ /* @__PURE__ */ jsxs("div", { className: "dtc-rail-scroll", children: [
1351
+ topItems.map(renderItemButton),
1352
+ topItems.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-rail-divider" }),
1353
+ /* @__PURE__ */ jsx(
1354
+ RailButton,
1355
+ {
1356
+ label: labels.messages,
1357
+ active: isOpen && view.kind === "inbox",
1358
+ badge: rollup,
1359
+ onClick: () => select({ kind: "inbox" }),
1360
+ children: /* @__PURE__ */ jsx(ChatIcon, {})
1361
+ }
1362
+ ),
1363
+ contexts.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-rail-divider" }),
1364
+ contexts.map((c) => /* @__PURE__ */ jsx(
1365
+ RailButton,
1366
+ {
1367
+ label: c.label,
1368
+ active: activeRoomId === c.roomId,
1369
+ badge: c.badge ?? 0,
1370
+ onClick: () => select({ kind: "room", roomId: c.roomId }),
1371
+ children: /* @__PURE__ */ jsx(Avatar, { name: c.label, url: c.avatarUrl, size: 36 })
1372
+ },
1373
+ c.roomId
1374
+ )),
1375
+ recentDms.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-rail-divider" }),
1376
+ recentDms.map((room) => /* @__PURE__ */ jsx(
1377
+ RailButton,
1378
+ {
1379
+ label: room.name,
1380
+ active: activeRoomId === room.roomId,
1381
+ badge: room.isMuted ? 0 : room.unreadCount,
1382
+ onClick: () => select({ kind: "room", roomId: room.roomId }),
1383
+ children: /* @__PURE__ */ jsx(Avatar, { name: room.name, url: room.kind === "dm" ? room.participants[0]?.avatarUrl : null, size: 36 })
1384
+ },
1385
+ room.roomId
1386
+ )),
1387
+ bottomItems.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-rail-divider" }),
1388
+ bottomItems.map(renderItemButton)
1389
+ ] }),
1390
+ footerItems.length > 0 && /* @__PURE__ */ jsx("div", { className: "dtc-rail-footer", children: footerItems.map(renderItemButton) })
1315
1391
  ] })
1316
1392
  ]
1317
1393
  }
@@ -1351,37 +1427,25 @@ function useMediaQuery(query) {
1351
1427
  }, [query]);
1352
1428
  return matches;
1353
1429
  }
1354
- function usePushLayout(enabled, inset) {
1355
- const original = useRef(null);
1430
+ function usePushLayout(enabled, inset, side) {
1356
1431
  useEffect(() => {
1357
- const root = document.documentElement;
1358
1432
  if (!enabled) return;
1359
- original.current ?? (original.current = { padding: root.style.paddingRight, transition: root.style.transition });
1360
- root.style.transition = "padding-right 250ms ease-out";
1361
- root.style.paddingRight = `${inset}px`;
1362
- root.style.setProperty("--dtc-messages-inset", `${inset}px`);
1363
- }, [enabled, inset]);
1364
- useEffect(() => {
1365
- if (enabled) return;
1366
1433
  const root = document.documentElement;
1367
- if (original.current) {
1368
- root.style.paddingRight = original.current.padding;
1369
- root.style.transition = original.current.transition;
1370
- original.current = null;
1371
- }
1372
- root.style.removeProperty("--dtc-messages-inset");
1373
- }, [enabled]);
1374
- useEffect(
1375
- () => () => {
1376
- const root = document.documentElement;
1377
- if (original.current) {
1378
- root.style.paddingRight = original.current.padding;
1379
- root.style.transition = original.current.transition;
1380
- }
1434
+ const prop = side === "left" ? "paddingLeft" : "paddingRight";
1435
+ const original = { padding: root.style[prop], transition: root.style.transition };
1436
+ root.style.transition = `${side === "left" ? "padding-left" : "padding-right"} 250ms ease-out`;
1437
+ root.style[prop] = `${inset}px`;
1438
+ root.style.setProperty("--dtc-messages-inset", `${inset}px`);
1439
+ root.style.setProperty("--dtc-messages-side", side);
1440
+ root.dataset.dtcMessagesSide = side;
1441
+ return () => {
1442
+ root.style[prop] = original.padding;
1443
+ root.style.transition = original.transition;
1381
1444
  root.style.removeProperty("--dtc-messages-inset");
1382
- },
1383
- []
1384
- );
1445
+ root.style.removeProperty("--dtc-messages-side");
1446
+ delete root.dataset.dtcMessagesSide;
1447
+ };
1448
+ }, [enabled, inset, side]);
1385
1449
  }
1386
1450
  function useScrollLock(locked) {
1387
1451
  useEffect(() => {
@@ -1393,14 +1457,15 @@ function useScrollLock(locked) {
1393
1457
  };
1394
1458
  }, [locked]);
1395
1459
  }
1396
- function useEdgeSwipe(enabled, isOpen, open, close) {
1460
+ function useEdgeSwipe(enabled, isOpen, open, close, side) {
1397
1461
  useEffect(() => {
1398
1462
  if (!enabled) return;
1399
1463
  let start = null;
1400
1464
  const onStart = (e) => {
1401
1465
  const t = e.touches[0];
1402
1466
  if (!t) return;
1403
- if (isOpen || t.clientX >= window.innerWidth - EDGE_SWIPE_ZONE) start = { x: t.clientX, y: t.clientY };
1467
+ const atEdge = side === "left" ? t.clientX <= EDGE_SWIPE_ZONE : t.clientX >= window.innerWidth - EDGE_SWIPE_ZONE;
1468
+ if (isOpen || atEdge) start = { x: t.clientX, y: t.clientY };
1404
1469
  };
1405
1470
  const onEnd = (e) => {
1406
1471
  const t = e.changedTouches[0];
@@ -1409,8 +1474,9 @@ function useEdgeSwipe(enabled, isOpen, open, close) {
1409
1474
  const dy = Math.abs(t.clientY - start.y);
1410
1475
  start = null;
1411
1476
  if (Math.abs(dx) < SWIPE_DISTANCE || dy > Math.abs(dx)) return;
1412
- if (!isOpen && dx < 0) open();
1413
- else if (isOpen && dx > 0) close();
1477
+ const inward = side === "left" ? dx > 0 : dx < 0;
1478
+ if (!isOpen && inward) open();
1479
+ else if (isOpen && !inward) close();
1414
1480
  };
1415
1481
  window.addEventListener("touchstart", onStart, { passive: true });
1416
1482
  window.addEventListener("touchend", onEnd, { passive: true });
@@ -1418,7 +1484,7 @@ function useEdgeSwipe(enabled, isOpen, open, close) {
1418
1484
  window.removeEventListener("touchstart", onStart);
1419
1485
  window.removeEventListener("touchend", onEnd);
1420
1486
  };
1421
- }, [enabled, isOpen, open, close]);
1487
+ }, [enabled, isOpen, open, close, side]);
1422
1488
  }
1423
1489
  function themeVars(theme) {
1424
1490
  if (!theme) return {};