@dialtribe/chat-client 0.4.0 → 0.5.1
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/README.md +40 -1
- package/dist/index.d.mts +148 -3
- package/dist/index.d.ts +148 -3
- package/dist/index.js +345 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +344 -10
- package/dist/index.mjs.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -466,6 +466,156 @@ var InboxStore = class extends Store {
|
|
|
466
466
|
}
|
|
467
467
|
};
|
|
468
468
|
|
|
469
|
+
// src/client/NotificationsStore.ts
|
|
470
|
+
var NotificationsStore = class extends Store {
|
|
471
|
+
constructor(client) {
|
|
472
|
+
super({ status: "idle", items: [], unreadCount: 0, hasMore: false, loadingMore: false, error: null });
|
|
473
|
+
this.client = client;
|
|
474
|
+
this.refs = 0;
|
|
475
|
+
this.unlisten = [];
|
|
476
|
+
this.refreshTimer = null;
|
|
477
|
+
this.nextCursor = null;
|
|
478
|
+
}
|
|
479
|
+
acquire() {
|
|
480
|
+
this.refs += 1;
|
|
481
|
+
if (this.refs > 1) return;
|
|
482
|
+
this.unlisten = [
|
|
483
|
+
this.client.on("connected", () => this.scheduleRefresh(0)),
|
|
484
|
+
this.client.on("user", (event) => this.onUserEvent(event))
|
|
485
|
+
];
|
|
486
|
+
void this.client.start();
|
|
487
|
+
this.scheduleRefresh(0);
|
|
488
|
+
}
|
|
489
|
+
release() {
|
|
490
|
+
this.refs = Math.max(0, this.refs - 1);
|
|
491
|
+
if (this.refs > 0) return;
|
|
492
|
+
for (const off of this.unlisten) off();
|
|
493
|
+
this.unlisten = [];
|
|
494
|
+
if (this.refreshTimer) clearTimeout(this.refreshTimer);
|
|
495
|
+
}
|
|
496
|
+
// ===========================================================================
|
|
497
|
+
// Actions
|
|
498
|
+
// ===========================================================================
|
|
499
|
+
/** Reload the first page and the unread count. */
|
|
500
|
+
async refresh() {
|
|
501
|
+
if (this.getSnapshot().status === "idle") this.update({ status: "loading" });
|
|
502
|
+
try {
|
|
503
|
+
const page = await this.client.api("/notifications/inbox?limit=30");
|
|
504
|
+
this.nextCursor = page.nextCursor;
|
|
505
|
+
this.update({
|
|
506
|
+
status: "ready",
|
|
507
|
+
items: page.items,
|
|
508
|
+
unreadCount: page.unreadCount,
|
|
509
|
+
hasMore: page.nextCursor !== null,
|
|
510
|
+
error: null
|
|
511
|
+
});
|
|
512
|
+
} catch (err) {
|
|
513
|
+
this.update({
|
|
514
|
+
status: "error",
|
|
515
|
+
error: err instanceof ChatRequestError ? err : new ChatRequestError("network", "Could not load notifications")
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/** Append the next (older) page. */
|
|
520
|
+
async loadMore() {
|
|
521
|
+
const snap = this.getSnapshot();
|
|
522
|
+
if (!this.nextCursor || snap.loadingMore) return;
|
|
523
|
+
this.update({ loadingMore: true });
|
|
524
|
+
try {
|
|
525
|
+
const page = await this.client.api(
|
|
526
|
+
`/notifications/inbox?limit=30&cursor=${encodeURIComponent(this.nextCursor)}`
|
|
527
|
+
);
|
|
528
|
+
this.nextCursor = page.nextCursor;
|
|
529
|
+
const have = new Set(this.getSnapshot().items.map((i) => i.id));
|
|
530
|
+
this.update({
|
|
531
|
+
items: [...this.getSnapshot().items, ...page.items.filter((i) => !have.has(i.id))],
|
|
532
|
+
hasMore: page.nextCursor !== null,
|
|
533
|
+
unreadCount: page.unreadCount
|
|
534
|
+
});
|
|
535
|
+
} finally {
|
|
536
|
+
this.update({ loadingMore: false });
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
/** Mark items read (optimistic). */
|
|
540
|
+
async markRead(ids) {
|
|
541
|
+
const unread = ids.filter((id) => this.getSnapshot().items.some((i) => i.id === id && !i.readAt));
|
|
542
|
+
if (unread.length === 0) return;
|
|
543
|
+
this.applyRead(unread, Math.max(0, this.getSnapshot().unreadCount - unread.length));
|
|
544
|
+
const { unreadCount } = await this.client.api("/notifications/inbox", {
|
|
545
|
+
method: "POST",
|
|
546
|
+
body: { action: "read", ids: unread }
|
|
547
|
+
});
|
|
548
|
+
this.update({ unreadCount });
|
|
549
|
+
}
|
|
550
|
+
/** Mark everything read (optimistic). */
|
|
551
|
+
async markAllRead() {
|
|
552
|
+
this.applyRead(null, 0);
|
|
553
|
+
const { unreadCount } = await this.client.api("/notifications/inbox", {
|
|
554
|
+
method: "POST",
|
|
555
|
+
body: { action: "read-all" }
|
|
556
|
+
});
|
|
557
|
+
this.update({ unreadCount });
|
|
558
|
+
}
|
|
559
|
+
/** Record that the list was shown (for stats; doesn't change unread). */
|
|
560
|
+
async markSeen() {
|
|
561
|
+
const unseen = this.getSnapshot().items.filter((i) => !i.seenAt).map((i) => i.id);
|
|
562
|
+
if (unseen.length === 0) return;
|
|
563
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
564
|
+
const set = new Set(unseen);
|
|
565
|
+
this.update({ items: this.getSnapshot().items.map((i) => set.has(i.id) ? { ...i, seenAt: now } : i) });
|
|
566
|
+
await this.client.api("/notifications/inbox", { method: "POST", body: { action: "seen", ids: unseen } });
|
|
567
|
+
}
|
|
568
|
+
/** Record a click (also marks read). Pass every id in a grouped row. */
|
|
569
|
+
async click(ids) {
|
|
570
|
+
if (ids.length === 0) return;
|
|
571
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
572
|
+
const set = new Set(ids);
|
|
573
|
+
const newlyRead = this.getSnapshot().items.filter((i) => set.has(i.id) && !i.readAt).length;
|
|
574
|
+
this.update({
|
|
575
|
+
items: this.getSnapshot().items.map(
|
|
576
|
+
(i) => set.has(i.id) ? { ...i, clickedAt: i.clickedAt ?? now, readAt: i.readAt ?? now } : i
|
|
577
|
+
),
|
|
578
|
+
unreadCount: Math.max(0, this.getSnapshot().unreadCount - newlyRead)
|
|
579
|
+
});
|
|
580
|
+
const { unreadCount } = await this.client.api("/notifications/inbox", {
|
|
581
|
+
method: "POST",
|
|
582
|
+
body: { action: "click", ids }
|
|
583
|
+
});
|
|
584
|
+
this.update({ unreadCount });
|
|
585
|
+
}
|
|
586
|
+
// ===========================================================================
|
|
587
|
+
// Live updates
|
|
588
|
+
// ===========================================================================
|
|
589
|
+
onUserEvent(event) {
|
|
590
|
+
if (event.type === "notification.created") {
|
|
591
|
+
const items = this.getSnapshot().items;
|
|
592
|
+
if (items.some((i) => i.id === event.notification.id)) return;
|
|
593
|
+
this.update({
|
|
594
|
+
items: [event.notification, ...items],
|
|
595
|
+
unreadCount: typeof event.unreadCount === "number" ? event.unreadCount : this.getSnapshot().unreadCount + (event.notification.readAt ? 0 : 1)
|
|
596
|
+
});
|
|
597
|
+
} else if (event.type === "notification.read") {
|
|
598
|
+
this.applyRead(event.ids, event.unreadCount);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
applyRead(ids, unreadCount) {
|
|
602
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
603
|
+
const set = ids ? new Set(ids) : null;
|
|
604
|
+
this.update({
|
|
605
|
+
items: this.getSnapshot().items.map((i) => !i.readAt && (!set || set.has(i.id)) ? { ...i, readAt: now } : i),
|
|
606
|
+
unreadCount
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
scheduleRefresh(delayMs) {
|
|
610
|
+
if (this.refs === 0) return;
|
|
611
|
+
if (this.refreshTimer) clearTimeout(this.refreshTimer);
|
|
612
|
+
this.refreshTimer = setTimeout(() => {
|
|
613
|
+
this.refreshTimer = null;
|
|
614
|
+
void this.refresh();
|
|
615
|
+
}, delayMs);
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
|
|
469
619
|
// src/client/ChatClient.ts
|
|
470
620
|
var ConnectionStore = class extends Store {
|
|
471
621
|
set(patch) {
|
|
@@ -485,7 +635,9 @@ var ChatClient = class {
|
|
|
485
635
|
this.generation = 0;
|
|
486
636
|
this.rooms = /* @__PURE__ */ new Map();
|
|
487
637
|
this.inboxStore = null;
|
|
638
|
+
this.notificationsStore = null;
|
|
488
639
|
this.serverUrl = options.serverUrl.replace(/\/$/, "");
|
|
640
|
+
this.apiUrl = (options.apiUrl ?? "https://dialtribe.com").replace(/\/$/, "");
|
|
489
641
|
}
|
|
490
642
|
// ===========================================================================
|
|
491
643
|
// Lifecycle
|
|
@@ -527,6 +679,11 @@ var ChatClient = class {
|
|
|
527
679
|
this.inboxStore ?? (this.inboxStore = new InboxStore(this));
|
|
528
680
|
return this.inboxStore;
|
|
529
681
|
}
|
|
682
|
+
/** The signed-in user's notifications (created on first use). */
|
|
683
|
+
get notifications() {
|
|
684
|
+
this.notificationsStore ?? (this.notificationsStore = new NotificationsStore(this));
|
|
685
|
+
return this.notificationsStore;
|
|
686
|
+
}
|
|
530
687
|
get self() {
|
|
531
688
|
return this.connection.getSnapshot().self;
|
|
532
689
|
}
|
|
@@ -646,6 +803,13 @@ var ChatClient = class {
|
|
|
646
803
|
users(roomId) {
|
|
647
804
|
return this.request(`/rooms/${encodeURIComponent(roomId)}/users`);
|
|
648
805
|
}
|
|
806
|
+
/**
|
|
807
|
+
* Authenticated JSON request to the Dialtribe API (apiUrl), with the same
|
|
808
|
+
* session token and 401 retry as chat-ws requests. `path` is under /api/public/v1.
|
|
809
|
+
*/
|
|
810
|
+
api(path, init = {}) {
|
|
811
|
+
return this.request(path, init, `${this.apiUrl}/api/public/v1`);
|
|
812
|
+
}
|
|
649
813
|
/** A DM action on POST /dm (see DMS.md). */
|
|
650
814
|
dm(action, fields = {}) {
|
|
651
815
|
return this.request("/dm", { method: "POST", body: { action, ...fields } });
|
|
@@ -655,8 +819,8 @@ var ChatClient = class {
|
|
|
655
819
|
* token on 401. Throws ChatRequestError for transport failures and for
|
|
656
820
|
* `{ ok: false, error }` bodies.
|
|
657
821
|
*/
|
|
658
|
-
async request(path, init = {}) {
|
|
659
|
-
const attempt = async (token) => fetch(
|
|
822
|
+
async request(path, init = {}, base = this.serverUrl) {
|
|
823
|
+
const attempt = async (token) => fetch(base + path, {
|
|
660
824
|
method: init.method ?? "GET",
|
|
661
825
|
headers: {
|
|
662
826
|
Authorization: `Bearer ${token}`,
|
|
@@ -717,17 +881,20 @@ var MessagesUiStore = class extends Store {
|
|
|
717
881
|
openRoom(roomId) {
|
|
718
882
|
this.update({ open: true, view: { kind: "room", roomId } });
|
|
719
883
|
}
|
|
884
|
+
openNotifications() {
|
|
885
|
+
this.update({ open: true, view: { kind: "notifications" } });
|
|
886
|
+
}
|
|
720
887
|
openItem(itemId) {
|
|
721
888
|
this.update({ open: true, view: { kind: "item", itemId } });
|
|
722
889
|
}
|
|
723
890
|
};
|
|
724
891
|
var ClientContext = createContext(null);
|
|
725
892
|
var MessagesUiContext = createContext(null);
|
|
726
|
-
function DialtribeChatProvider({ children, getSessionToken, serverUrl, websocketUrl, websocket }) {
|
|
893
|
+
function DialtribeChatProvider({ children, getSessionToken, serverUrl, apiUrl, websocketUrl, websocket }) {
|
|
727
894
|
const tokenRef = useRef(getSessionToken);
|
|
728
895
|
tokenRef.current = getSessionToken;
|
|
729
896
|
const [client] = useState(
|
|
730
|
-
() => new ChatClient({ serverUrl, websocketUrl, websocket, getSessionToken: () => tokenRef.current() })
|
|
897
|
+
() => new ChatClient({ serverUrl, apiUrl, websocketUrl, websocket, getSessionToken: () => tokenRef.current() })
|
|
731
898
|
);
|
|
732
899
|
const [messagesUi] = useState(() => new MessagesUiStore());
|
|
733
900
|
useEffect(() => {
|
|
@@ -833,6 +1000,7 @@ function useDialtribeMessages() {
|
|
|
833
1000
|
openInbox: () => ui.openInbox(),
|
|
834
1001
|
openRoom: (roomId) => ui.openRoom(roomId),
|
|
835
1002
|
openItem: (itemId) => ui.openItem(itemId),
|
|
1003
|
+
openNotifications: () => ui.openNotifications(),
|
|
836
1004
|
messageUser: async (userId, hint) => {
|
|
837
1005
|
const result = await client.inbox.requestDM(userId, hint);
|
|
838
1006
|
ui.openRoom(result.roomId);
|
|
@@ -843,6 +1011,26 @@ function useDialtribeMessages() {
|
|
|
843
1011
|
);
|
|
844
1012
|
return useMemo(() => ({ isOpen: snapshot.open, view: snapshot.view, ...actions }), [snapshot, actions]);
|
|
845
1013
|
}
|
|
1014
|
+
function useDialtribeNotifications() {
|
|
1015
|
+
const store = useChatClient().notifications;
|
|
1016
|
+
useEffect(() => {
|
|
1017
|
+
store.acquire();
|
|
1018
|
+
return () => store.release();
|
|
1019
|
+
}, [store]);
|
|
1020
|
+
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
|
|
1021
|
+
const actions = useMemo(
|
|
1022
|
+
() => ({
|
|
1023
|
+
refresh: store.refresh.bind(store),
|
|
1024
|
+
loadMore: store.loadMore.bind(store),
|
|
1025
|
+
markRead: store.markRead.bind(store),
|
|
1026
|
+
markAllRead: store.markAllRead.bind(store),
|
|
1027
|
+
markSeen: store.markSeen.bind(store),
|
|
1028
|
+
click: store.click.bind(store)
|
|
1029
|
+
}),
|
|
1030
|
+
[store]
|
|
1031
|
+
);
|
|
1032
|
+
return useMemo(() => ({ ...snapshot, ...actions }), [snapshot, actions]);
|
|
1033
|
+
}
|
|
846
1034
|
|
|
847
1035
|
// src/types/index.ts
|
|
848
1036
|
function parseReactions(reactionsJson) {
|
|
@@ -1158,7 +1346,20 @@ var DEFAULT_LABELS = {
|
|
|
1158
1346
|
archiveConfirm: "Archive",
|
|
1159
1347
|
cancel: "Cancel",
|
|
1160
1348
|
messagePlaceholder: "Message...",
|
|
1161
|
-
openMessages: "Open messages"
|
|
1349
|
+
openMessages: "Open messages",
|
|
1350
|
+
notifications: "Notifications",
|
|
1351
|
+
noNotifications: "No notifications yet",
|
|
1352
|
+
markAllRead: "Mark all read",
|
|
1353
|
+
loadMore: "Load more",
|
|
1354
|
+
groupMore: (count) => `and ${count} more`,
|
|
1355
|
+
timeAgo: (date) => {
|
|
1356
|
+
const s = Math.max(0, Math.floor((Date.now() - date.getTime()) / 1e3));
|
|
1357
|
+
if (s < 60) return "now";
|
|
1358
|
+
if (s < 3600) return `${Math.floor(s / 60)}m`;
|
|
1359
|
+
if (s < 86400) return `${Math.floor(s / 3600)}h`;
|
|
1360
|
+
if (s < 604800) return `${Math.floor(s / 86400)}d`;
|
|
1361
|
+
return date.toLocaleDateString(void 0, { month: "short", day: "numeric" });
|
|
1362
|
+
}
|
|
1162
1363
|
};
|
|
1163
1364
|
function badgeText(count) {
|
|
1164
1365
|
return count > 99 ? "99+" : String(count);
|
|
@@ -1405,6 +1606,99 @@ function CloseIcon() {
|
|
|
1405
1606
|
function ChevronLeft() {
|
|
1406
1607
|
return /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", width: "18", height: "18", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M15 18l-6-6 6-6" }) });
|
|
1407
1608
|
}
|
|
1609
|
+
function groupRows(items) {
|
|
1610
|
+
const rows = [];
|
|
1611
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
1612
|
+
for (const item of items) {
|
|
1613
|
+
const existing = item.groupKey ? byKey.get(item.groupKey) : void 0;
|
|
1614
|
+
if (existing) {
|
|
1615
|
+
existing.ids.push(item.id);
|
|
1616
|
+
existing.more += 1;
|
|
1617
|
+
existing.unread || (existing.unread = !item.readAt);
|
|
1618
|
+
continue;
|
|
1619
|
+
}
|
|
1620
|
+
const row = { lead: item, ids: [item.id], more: 0, unread: !item.readAt };
|
|
1621
|
+
rows.push(row);
|
|
1622
|
+
if (item.groupKey) byKey.set(item.groupKey, row);
|
|
1623
|
+
}
|
|
1624
|
+
return rows;
|
|
1625
|
+
}
|
|
1626
|
+
function NotificationsPanel({ labels, onClose, onOpen, onOpened }) {
|
|
1627
|
+
const n = useDialtribeNotifications();
|
|
1628
|
+
const rows = useMemo(() => groupRows(n.items), [n.items]);
|
|
1629
|
+
const { status, markSeen } = n;
|
|
1630
|
+
useEffect(() => {
|
|
1631
|
+
if (status === "ready") void markSeen().catch(() => {
|
|
1632
|
+
});
|
|
1633
|
+
}, [status, markSeen]);
|
|
1634
|
+
const open = (row) => {
|
|
1635
|
+
if (!row.lead.url) {
|
|
1636
|
+
if (row.unread) void n.markRead(row.ids).catch(() => {
|
|
1637
|
+
});
|
|
1638
|
+
return;
|
|
1639
|
+
}
|
|
1640
|
+
void n.click(row.ids).catch(() => {
|
|
1641
|
+
});
|
|
1642
|
+
const handled = onOpen?.(row.lead);
|
|
1643
|
+
onOpened?.(row.lead);
|
|
1644
|
+
if (!handled) window.location.assign(row.lead.url);
|
|
1645
|
+
};
|
|
1646
|
+
return /* @__PURE__ */ jsxs("div", { className: "dtc-notif", children: [
|
|
1647
|
+
/* @__PURE__ */ jsxs("div", { className: "dtc-panel-header", children: [
|
|
1648
|
+
/* @__PURE__ */ jsxs("span", { className: "dtc-panel-title", children: [
|
|
1649
|
+
labels.notifications,
|
|
1650
|
+
n.unreadCount > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-badge-count dtc-notif-title-badge", children: badgeText(n.unreadCount) })
|
|
1651
|
+
] }),
|
|
1652
|
+
n.unreadCount > 0 && /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-notif-markall", onClick: () => void n.markAllRead().catch(() => {
|
|
1653
|
+
}), children: labels.markAllRead }),
|
|
1654
|
+
onClose && /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: onClose, "aria-label": labels.close, title: labels.close, children: /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", width: "18", height: "18", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M18 6L6 18M6 6l12 12" }) }) })
|
|
1655
|
+
] }),
|
|
1656
|
+
n.status !== "ready" && rows.length === 0 ? /* @__PURE__ */ jsx("div", { className: "dtc-empty", children: n.status === "error" ? n.error?.message ?? "" : "" }) : rows.length === 0 ? /* @__PURE__ */ jsx("div", { className: "dtc-empty", children: labels.noNotifications }) : /* @__PURE__ */ jsxs("div", { className: "dtc-notif-list", children: [
|
|
1657
|
+
rows.map((row) => /* @__PURE__ */ jsxs(
|
|
1658
|
+
"button",
|
|
1659
|
+
{
|
|
1660
|
+
type: "button",
|
|
1661
|
+
className: `dtc-notif-row ${row.unread ? "dtc-notif-row-unread" : ""} ${row.lead.url ? "" : "dtc-notif-row-static"}`,
|
|
1662
|
+
onClick: () => open(row),
|
|
1663
|
+
children: [
|
|
1664
|
+
/* @__PURE__ */ jsx(
|
|
1665
|
+
Avatar,
|
|
1666
|
+
{
|
|
1667
|
+
name: row.lead.actor?.name ?? row.lead.title,
|
|
1668
|
+
url: row.lead.actor?.avatarUrl ?? row.lead.imageUrl,
|
|
1669
|
+
size: 40
|
|
1670
|
+
}
|
|
1671
|
+
),
|
|
1672
|
+
/* @__PURE__ */ jsxs("span", { className: "dtc-notif-text", children: [
|
|
1673
|
+
/* @__PURE__ */ jsx("span", { className: "dtc-notif-title", children: row.lead.title }),
|
|
1674
|
+
row.lead.body && /* @__PURE__ */ jsx("span", { className: "dtc-notif-body", children: row.lead.body }),
|
|
1675
|
+
/* @__PURE__ */ jsxs("span", { className: "dtc-notif-meta", children: [
|
|
1676
|
+
labels.timeAgo(new Date(row.lead.createdAt)),
|
|
1677
|
+
row.more > 0 && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1678
|
+
" \xB7 ",
|
|
1679
|
+
labels.groupMore(row.more)
|
|
1680
|
+
] })
|
|
1681
|
+
] })
|
|
1682
|
+
] }),
|
|
1683
|
+
row.unread && /* @__PURE__ */ jsx("span", { className: "dtc-notif-dot", "aria-hidden": "true" })
|
|
1684
|
+
]
|
|
1685
|
+
},
|
|
1686
|
+
row.lead.id
|
|
1687
|
+
)),
|
|
1688
|
+
n.hasMore && /* @__PURE__ */ jsx(
|
|
1689
|
+
"button",
|
|
1690
|
+
{
|
|
1691
|
+
type: "button",
|
|
1692
|
+
className: "dtc-btn dtc-notif-more",
|
|
1693
|
+
disabled: n.loadingMore,
|
|
1694
|
+
onClick: () => void n.loadMore().catch(() => {
|
|
1695
|
+
}),
|
|
1696
|
+
children: labels.loadMore
|
|
1697
|
+
}
|
|
1698
|
+
)
|
|
1699
|
+
] })
|
|
1700
|
+
] });
|
|
1701
|
+
}
|
|
1408
1702
|
var RAIL_WIDTH = 64;
|
|
1409
1703
|
var EDGE_SWIPE_ZONE = 24;
|
|
1410
1704
|
var SWIPE_DISTANCE = 60;
|
|
@@ -1420,7 +1714,8 @@ function DialtribeMessages({
|
|
|
1420
1714
|
panelWidth = 380,
|
|
1421
1715
|
railDmLimit = 5,
|
|
1422
1716
|
mobileBreakpoint = 768,
|
|
1423
|
-
confirm: hostConfirm
|
|
1717
|
+
confirm: hostConfirm,
|
|
1718
|
+
notifications = false
|
|
1424
1719
|
}) {
|
|
1425
1720
|
const labels = useMemo(() => ({ ...DEFAULT_LABELS, ...labelOverrides }), [labelOverrides]);
|
|
1426
1721
|
const inbox = useChatRooms();
|
|
@@ -1428,7 +1723,9 @@ function DialtribeMessages({
|
|
|
1428
1723
|
const isMobile = useMediaQuery(`(max-width: ${mobileBreakpoint - 1}px)`);
|
|
1429
1724
|
const { isOpen, view } = ui;
|
|
1430
1725
|
const pendingCount = inbox.requests.length;
|
|
1431
|
-
const
|
|
1726
|
+
const notifUnread = useNotificationsBadge(notifications !== false);
|
|
1727
|
+
const rollup = inbox.totalUnread + pendingCount + notifUnread;
|
|
1728
|
+
const notifOnOpen = typeof notifications === "object" ? notifications.onOpen : void 0;
|
|
1432
1729
|
const recentDms = inbox.rooms.filter((r) => r.myStatus === "accepted" && !r.isArchived).slice(0, railDmLimit);
|
|
1433
1730
|
const [builtInConfirm, setBuiltInConfirm] = useState(null);
|
|
1434
1731
|
const confirmOpen = useRef(false);
|
|
@@ -1464,10 +1761,11 @@ function DialtribeMessages({
|
|
|
1464
1761
|
return () => window.removeEventListener("keydown", onKey);
|
|
1465
1762
|
}, [isOpen, ui]);
|
|
1466
1763
|
const select = (target) => {
|
|
1467
|
-
const showing = isOpen && (target.kind === "inbox" ? view.kind === "inbox" : target.kind === "room" ? activeRoomId === target.roomId : activeItemId === target.itemId);
|
|
1764
|
+
const showing = isOpen && (target.kind === "inbox" ? view.kind === "inbox" : target.kind === "room" ? activeRoomId === target.roomId : target.kind === "notifications" ? view.kind === "notifications" : activeItemId === target.itemId);
|
|
1468
1765
|
if (showing) ui.close();
|
|
1469
1766
|
else if (target.kind === "inbox") ui.openInbox();
|
|
1470
1767
|
else if (target.kind === "room") ui.openRoom(target.roomId);
|
|
1768
|
+
else if (target.kind === "notifications") ui.openNotifications();
|
|
1471
1769
|
else ui.openItem(target.itemId);
|
|
1472
1770
|
};
|
|
1473
1771
|
const renderItemButton = (item) => {
|
|
@@ -1498,7 +1796,17 @@ function DialtribeMessages({
|
|
|
1498
1796
|
rollup > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-badge-count dtc-badge-float", children: badgeText(rollup) })
|
|
1499
1797
|
] }),
|
|
1500
1798
|
isOpen && /* @__PURE__ */ jsxs("div", { className: "dtc-messages-panel", role: "dialog", "aria-label": activeItem?.label ?? labels.messages, children: [
|
|
1501
|
-
|
|
1799
|
+
view.kind === "notifications" && notifications !== false ? /* @__PURE__ */ jsx(
|
|
1800
|
+
NotificationsPanel,
|
|
1801
|
+
{
|
|
1802
|
+
labels,
|
|
1803
|
+
onClose: ui.close,
|
|
1804
|
+
onOpen: notifOnOpen,
|
|
1805
|
+
onOpened: () => {
|
|
1806
|
+
if (isMobile) ui.close();
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
) : activeItem ? /* @__PURE__ */ jsx("div", { className: "dtc-item-panel", children: typeof activeItem.panel === "function" ? activeItem.panel({ close: ui.close }) : activeItem.panel }) : view.kind !== "room" ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1502
1810
|
/* @__PURE__ */ jsxs("div", { className: "dtc-panel-header", children: [
|
|
1503
1811
|
/* @__PURE__ */ jsx("span", { className: "dtc-panel-title", children: labels.messages }),
|
|
1504
1812
|
/* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: ui.close, "aria-label": labels.close, title: labels.close, children: /* @__PURE__ */ jsx(CloseIcon2, {}) })
|
|
@@ -1544,6 +1852,16 @@ function DialtribeMessages({
|
|
|
1544
1852
|
/* @__PURE__ */ jsxs("div", { className: "dtc-rail-scroll", children: [
|
|
1545
1853
|
topItems.map(renderItemButton),
|
|
1546
1854
|
topItems.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-rail-divider" }),
|
|
1855
|
+
notifications !== false && /* @__PURE__ */ jsx(
|
|
1856
|
+
RailButton,
|
|
1857
|
+
{
|
|
1858
|
+
label: labels.notifications,
|
|
1859
|
+
active: isOpen && view.kind === "notifications",
|
|
1860
|
+
badge: notifUnread,
|
|
1861
|
+
onClick: () => select({ kind: "notifications" }),
|
|
1862
|
+
children: /* @__PURE__ */ jsx(BellIcon, {})
|
|
1863
|
+
}
|
|
1864
|
+
),
|
|
1547
1865
|
/* @__PURE__ */ jsx(
|
|
1548
1866
|
RailButton,
|
|
1549
1867
|
{
|
|
@@ -1725,10 +2043,26 @@ function themeVars(theme) {
|
|
|
1725
2043
|
function ChatIcon() {
|
|
1726
2044
|
return /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", width: "20", height: "20", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" }) });
|
|
1727
2045
|
}
|
|
2046
|
+
function BellIcon() {
|
|
2047
|
+
return /* @__PURE__ */ jsxs("svg", { viewBox: "0 0 24 24", width: "20", height: "20", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: [
|
|
2048
|
+
/* @__PURE__ */ jsx("path", { d: "M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9" }),
|
|
2049
|
+
/* @__PURE__ */ jsx("path", { d: "M13.73 21a2 2 0 0 1-3.46 0" })
|
|
2050
|
+
] });
|
|
2051
|
+
}
|
|
2052
|
+
function useNotificationsBadge(enabled) {
|
|
2053
|
+
const store = useChatClient().notifications;
|
|
2054
|
+
useEffect(() => {
|
|
2055
|
+
if (!enabled) return;
|
|
2056
|
+
store.acquire();
|
|
2057
|
+
return () => store.release();
|
|
2058
|
+
}, [enabled, store]);
|
|
2059
|
+
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
|
|
2060
|
+
return enabled ? snapshot.unreadCount : 0;
|
|
2061
|
+
}
|
|
1728
2062
|
function CloseIcon2() {
|
|
1729
2063
|
return /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", width: "18", height: "18", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M18 6L6 18M6 6l12 12" }) });
|
|
1730
2064
|
}
|
|
1731
2065
|
|
|
1732
|
-
export { Avatar, ChatClient, ChatInput, ChatMessageItem, ChatPanel, ChatRequestError, ChatRoomProvider, DEFAULT_LABELS, DialtribeChatProvider, DialtribeMessages, parseReactions, useChatClient, useChatConnection, useChatRoomContext, useChatRooms, useDialtribeChat, useDialtribeMessages };
|
|
2066
|
+
export { Avatar, ChatClient, ChatInput, ChatMessageItem, ChatPanel, ChatRequestError, ChatRoomProvider, DEFAULT_LABELS, DialtribeChatProvider, DialtribeMessages, NotificationsPanel, parseReactions, useChatClient, useChatConnection, useChatRoomContext, useChatRooms, useDialtribeChat, useDialtribeMessages, useDialtribeNotifications };
|
|
1733
2067
|
//# sourceMappingURL=index.mjs.map
|
|
1734
2068
|
//# sourceMappingURL=index.mjs.map
|