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