@dialtribe/chat-client 0.6.0 → 0.7.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
@@ -298,6 +298,17 @@ var InboxStore = class extends Store {
298
298
  this.removed.delete(roomId);
299
299
  await this.refresh();
300
300
  }
301
+ /**
302
+ * Start a group with several people in one step. Each person's DM settings
303
+ * apply: those whose DMs are closed come back in `refused`. If only one
304
+ * person is left, `roomId` is your 1:1 with them.
305
+ */
306
+ async createGroup(targetUserIds) {
307
+ const result = await this.client.dm("chat-dm-create-group", { targetUserIds });
308
+ this.removed.delete(result.roomId);
309
+ await this.refresh();
310
+ return { roomId: result.roomId, added: result.added ?? [], refused: result.refused ?? [] };
311
+ }
301
312
  /** Add someone. Returns the room to show (a new group when added to a 1:1). */
302
313
  async addParticipant(roomId, targetUserId, hint) {
303
314
  const result = await this.client.dm("chat-dm-add-participant", {
@@ -832,6 +843,19 @@ var ChatClient = class {
832
843
  }
833
844
  return lookup;
834
845
  }
846
+ /**
847
+ * People whose handle starts with `query` (2+ characters), among those you can
848
+ * reach through this app's organization. Empty for apps not on Dialtribe identity.
849
+ */
850
+ async searchPeople(query) {
851
+ try {
852
+ const { people } = await this.api(`/chat/people?q=${encodeURIComponent(query)}`);
853
+ return people;
854
+ } catch (err) {
855
+ if (err instanceof ChatRequestError && err.code === "not-found") return [];
856
+ throw err;
857
+ }
858
+ }
835
859
  /** A DM action on POST /dm (see DMS.md). */
836
860
  dm(action, fields = {}) {
837
861
  return this.request("/dm", { method: "POST", body: { action, ...fields } });
@@ -909,16 +933,23 @@ var MessagesUiStore = class extends Store {
909
933
  openItem(itemId) {
910
934
  this.update({ open: true, view: { kind: "item", itemId } });
911
935
  }
936
+ /** The view to return to from a profile or the composer (never another overlay). */
937
+ returnTo() {
938
+ const current = this.getSnapshot().view;
939
+ return current.kind === "profile" || current.kind === "compose" ? current.from : current;
940
+ }
912
941
  /** Show a person's mini-profile, returning to the current view on back. */
913
942
  openProfile(userId, hint) {
914
- const current = this.getSnapshot().view;
915
- const from = current.kind === "profile" ? current.from : current;
916
- this.update({ open: true, view: { kind: "profile", userId, from, hint } });
943
+ this.update({ open: true, view: { kind: "profile", userId, from: this.returnTo(), hint } });
944
+ }
945
+ /** Show the "Start new" composer, returning to the current view on back. */
946
+ openCompose() {
947
+ this.update({ open: true, view: { kind: "compose", from: this.returnTo() } });
917
948
  }
918
- /** From a mini-profile, return to where it was opened. */
949
+ /** From a mini-profile or the composer, return to where it was opened. */
919
950
  back() {
920
951
  const { view } = this.getSnapshot();
921
- this.update({ view: view.kind === "profile" ? view.from : { kind: "inbox" } });
952
+ this.update({ view: view.kind === "profile" || view.kind === "compose" ? view.from : { kind: "inbox" } });
922
953
  }
923
954
  };
924
955
  var ClientContext = createContext(null);
@@ -1036,6 +1067,17 @@ function useDialtribeMessages() {
1036
1067
  openNotifications: () => ui.openNotifications(),
1037
1068
  openProfile: (userId, hint) => ui.openProfile(userId, hint),
1038
1069
  back: () => ui.back(),
1070
+ openCompose: () => ui.openCompose(),
1071
+ startConversation: async (userIds) => {
1072
+ if (userIds.length === 1) {
1073
+ const { roomId } = await client.inbox.requestDM(userIds[0]);
1074
+ ui.openRoom(roomId);
1075
+ return { roomId, added: userIds, refused: [] };
1076
+ }
1077
+ const result = await client.inbox.createGroup(userIds);
1078
+ ui.openRoom(result.roomId);
1079
+ return result;
1080
+ },
1039
1081
  messageUser: async (userId, hint) => {
1040
1082
  const result = await client.inbox.requestDM(userId, hint);
1041
1083
  ui.openRoom(result.roomId);
@@ -1414,7 +1456,16 @@ var DEFAULT_LABELS = {
1414
1456
  alsoOn: "Also on",
1415
1457
  message: "Message",
1416
1458
  viewProfileOf: (name) => `View profile of ${name}`,
1417
- members: "Members"
1459
+ members: "Members",
1460
+ newMessage: "New message",
1461
+ searchPeople: "Search by handle",
1462
+ recent: "Recent",
1463
+ noPeopleFound: "No one found",
1464
+ start: "Start",
1465
+ cantMessage: (names) => `${names} can't be messaged right now`,
1466
+ openConversation: "Open conversation",
1467
+ removePerson: (name) => `Remove ${name}`,
1468
+ startFailed: "Couldn't start the conversation. Try again."
1418
1469
  };
1419
1470
  function badgeText(count) {
1420
1471
  return count > 99 ? "99+" : String(count);
@@ -1892,6 +1943,144 @@ function CloseIcon2() {
1892
1943
  function ArrowIcon() {
1893
1944
  return /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", width: "14", height: "14", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M7 17L17 7M9 7h8v8" }) });
1894
1945
  }
1946
+ var SEARCH_MIN = 2;
1947
+ var SEARCH_DEBOUNCE_MS = 250;
1948
+ function ComposeView({ labels, onBack, onClose, onOpenRoom }) {
1949
+ const client = useChatClient();
1950
+ const inbox = useChatRooms();
1951
+ const [query, setQuery] = useState("");
1952
+ const [results, setResults] = useState(null);
1953
+ const [picked, setPicked] = useState([]);
1954
+ const [busy, setBusy] = useState(false);
1955
+ const [notice, setNotice] = useState(null);
1956
+ const inputRef = useRef(null);
1957
+ const selfId = client.self?.userId;
1958
+ useEffect(() => inputRef.current?.focus(), []);
1959
+ const recent = useMemo(() => {
1960
+ const seen = /* @__PURE__ */ new Set();
1961
+ const people = [];
1962
+ for (const room of inbox.rooms) {
1963
+ for (const p of room.participants) {
1964
+ if (p.userId === selfId || seen.has(p.userId)) continue;
1965
+ seen.add(p.userId);
1966
+ people.push({ userId: p.userId, name: p.name, handle: null, avatarUrl: p.avatarUrl });
1967
+ }
1968
+ }
1969
+ return people;
1970
+ }, [inbox.rooms, selfId]);
1971
+ const q = query.trim().replace(/^@/, "");
1972
+ useEffect(() => {
1973
+ if (q.length < SEARCH_MIN) {
1974
+ setResults(null);
1975
+ return;
1976
+ }
1977
+ let live = true;
1978
+ const timer = setTimeout(() => {
1979
+ client.searchPeople(q).then(
1980
+ (people) => live && setResults(people),
1981
+ () => live && setResults([])
1982
+ );
1983
+ }, SEARCH_DEBOUNCE_MS);
1984
+ return () => {
1985
+ live = false;
1986
+ clearTimeout(timer);
1987
+ };
1988
+ }, [client, q]);
1989
+ const searching = q.length >= SEARCH_MIN;
1990
+ const list = searching ? results ?? [] : recent;
1991
+ const pickedIds = new Set(picked.map((p) => p.userId));
1992
+ const toggle = (person) => {
1993
+ setNotice(null);
1994
+ setPicked(
1995
+ (current) => current.some((p) => p.userId === person.userId) ? current.filter((p) => p.userId !== person.userId) : [...current, person]
1996
+ );
1997
+ };
1998
+ const namesOf = (ids) => picked.filter((p) => ids.includes(p.userId)).map((p) => p.name).join(", ");
1999
+ const start = async () => {
2000
+ if (!picked.length || busy) return;
2001
+ setBusy(true);
2002
+ setNotice(null);
2003
+ try {
2004
+ if (picked.length === 1) {
2005
+ const { roomId } = await client.inbox.requestDM(picked[0].userId, {
2006
+ name: picked[0].name,
2007
+ avatarUrl: picked[0].avatarUrl ?? void 0
2008
+ });
2009
+ onOpenRoom(roomId);
2010
+ return;
2011
+ }
2012
+ const result = await client.inbox.createGroup(picked.map((p) => p.userId));
2013
+ if (result.refused.length) setNotice({ text: labels.cantMessage(namesOf(result.refused)), roomId: result.roomId });
2014
+ else onOpenRoom(result.roomId);
2015
+ } catch (err) {
2016
+ const refused = err instanceof ChatRequestError && err.code === "dm-not-allowed";
2017
+ setNotice({ text: refused ? labels.cantMessage(namesOf(picked.map((p) => p.userId))) : labels.startFailed, roomId: null });
2018
+ } finally {
2019
+ setBusy(false);
2020
+ }
2021
+ };
2022
+ return /* @__PURE__ */ jsxs("div", { className: "dtc-conversation", children: [
2023
+ /* @__PURE__ */ jsxs("div", { className: "dtc-conversation-header", children: [
2024
+ /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: onBack, "aria-label": labels.back, title: labels.back, children: /* @__PURE__ */ jsx(ChevronLeft3, {}) }),
2025
+ /* @__PURE__ */ jsx("span", { className: "dtc-conversation-title", children: labels.newMessage }),
2026
+ /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: onClose, "aria-label": labels.close, title: labels.close, children: /* @__PURE__ */ jsx(CloseIcon3, {}) })
2027
+ ] }),
2028
+ /* @__PURE__ */ jsxs("div", { className: "dtc-compose", children: [
2029
+ /* @__PURE__ */ jsxs("div", { className: "dtc-compose-field", children: [
2030
+ picked.map((p) => /* @__PURE__ */ jsxs("span", { className: "dtc-compose-chip", children: [
2031
+ p.name,
2032
+ /* @__PURE__ */ jsx("button", { type: "button", onClick: () => toggle(p), "aria-label": labels.removePerson(p.name), children: /* @__PURE__ */ jsx(CloseIcon3, { size: 12 }) })
2033
+ ] }, p.userId)),
2034
+ /* @__PURE__ */ jsx(
2035
+ "input",
2036
+ {
2037
+ ref: inputRef,
2038
+ className: "dtc-compose-input",
2039
+ value: query,
2040
+ onChange: (e) => setQuery(e.target.value.slice(0, 50)),
2041
+ onKeyDown: (e) => {
2042
+ if (e.key === "Backspace" && !query && picked.length) setPicked(picked.slice(0, -1));
2043
+ },
2044
+ placeholder: picked.length ? "" : labels.searchPeople,
2045
+ "aria-label": labels.searchPeople
2046
+ }
2047
+ )
2048
+ ] }),
2049
+ !searching && recent.length > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-profile-sites-title", children: labels.recent }),
2050
+ /* @__PURE__ */ jsx("ul", { className: "dtc-compose-list", children: list.map((person) => {
2051
+ const on = pickedIds.has(person.userId);
2052
+ return /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsxs("button", { type: "button", className: "dtc-member", "aria-pressed": on, onClick: () => toggle(person), children: [
2053
+ /* @__PURE__ */ jsx(Avatar, { name: person.name, url: person.avatarUrl, size: 32 }),
2054
+ /* @__PURE__ */ jsxs("span", { className: "dtc-compose-names", children: [
2055
+ /* @__PURE__ */ jsx("span", { className: "dtc-member-name", children: person.name }),
2056
+ person.handle && person.handle.toLowerCase() !== person.name.toLowerCase() && /* @__PURE__ */ jsxs("span", { className: "dtc-profile-handle", children: [
2057
+ "@",
2058
+ person.handle
2059
+ ] })
2060
+ ] }),
2061
+ /* @__PURE__ */ jsx("span", { className: `dtc-compose-check ${on ? "dtc-compose-check-on" : ""}`, "aria-hidden": "true", children: on && /* @__PURE__ */ jsx(CheckIcon, {}) })
2062
+ ] }) }, person.userId);
2063
+ }) }),
2064
+ searching && results !== null && results.length === 0 && /* @__PURE__ */ jsx("div", { className: "dtc-empty", children: labels.noPeopleFound })
2065
+ ] }),
2066
+ /* @__PURE__ */ jsxs("div", { className: "dtc-compose-footer", children: [
2067
+ notice && /* @__PURE__ */ jsxs("div", { className: "dtc-conversation-note", children: [
2068
+ notice.text,
2069
+ notice.roomId && /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-text-btn", onClick: () => onOpenRoom(notice.roomId), children: labels.openConversation })
2070
+ ] }),
2071
+ /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-btn dtc-btn-primary dtc-compose-start", disabled: !picked.length || busy, onClick: () => void start(), children: labels.start })
2072
+ ] })
2073
+ ] });
2074
+ }
2075
+ function ChevronLeft3() {
2076
+ 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" }) });
2077
+ }
2078
+ function CloseIcon3({ size = 18 }) {
2079
+ return /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", width: size, height: size, fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M18 6L6 18M6 6l12 12" }) });
2080
+ }
2081
+ function CheckIcon() {
2082
+ return /* @__PURE__ */ jsx("svg", { viewBox: "0 0 24 24", width: "14", height: "14", fill: "none", stroke: "currentColor", strokeWidth: 3, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M5 12l5 5L20 7" }) });
2083
+ }
1895
2084
  var RAIL_WIDTH = 64;
1896
2085
  var EDGE_SWIPE_ZONE = 24;
1897
2086
  var SWIPE_DISTANCE = 60;
@@ -1999,7 +2188,7 @@ function DialtribeMessages({
1999
2188
  rollup > 0 && /* @__PURE__ */ jsx("span", { className: "dtc-badge-count dtc-badge-float", children: badgeText(rollup) })
2000
2189
  ] }),
2001
2190
  isOpen && /* @__PURE__ */ jsxs("div", { className: "dtc-messages-panel", role: "dialog", "aria-label": activeItem?.label ?? labels.messages, children: [
2002
- view.kind === "profile" ? /* @__PURE__ */ jsx(
2191
+ view.kind === "compose" ? /* @__PURE__ */ jsx(ComposeView, { labels, onBack: ui.back, onClose: ui.close, onOpenRoom: ui.openRoom }) : view.kind === "profile" ? /* @__PURE__ */ jsx(
2003
2192
  ProfileView,
2004
2193
  {
2005
2194
  userId: view.userId,
@@ -2030,7 +2219,8 @@ function DialtribeMessages({
2030
2219
  ) : 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: [
2031
2220
  /* @__PURE__ */ jsxs("div", { className: "dtc-panel-header", children: [
2032
2221
  /* @__PURE__ */ jsx("span", { className: "dtc-panel-title", children: labels.messages }),
2033
- /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: ui.close, "aria-label": labels.close, title: labels.close, children: /* @__PURE__ */ jsx(CloseIcon3, {}) })
2222
+ /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: ui.openCompose, "aria-label": labels.newMessage, title: labels.newMessage, children: /* @__PURE__ */ jsx(ComposeIcon, {}) }),
2223
+ /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: ui.close, "aria-label": labels.close, title: labels.close, children: /* @__PURE__ */ jsx(CloseIcon4, {}) })
2034
2224
  ] }),
2035
2225
  /* @__PURE__ */ jsx(
2036
2226
  MessagesInbox,
@@ -2047,7 +2237,7 @@ function DialtribeMessages({
2047
2237
  /* @__PURE__ */ jsxs("div", { className: "dtc-conversation-header", children: [
2048
2238
  /* @__PURE__ */ jsx(Avatar, { name: activeContext.label, url: activeContext.avatarUrl, size: 28 }),
2049
2239
  /* @__PURE__ */ jsx("span", { className: "dtc-conversation-title", children: activeContext.label }),
2050
- /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: ui.close, "aria-label": labels.close, title: labels.close, children: /* @__PURE__ */ jsx(CloseIcon3, {}) })
2240
+ /* @__PURE__ */ jsx("button", { type: "button", className: "dtc-icon-btn", onClick: ui.close, "aria-label": labels.close, title: labels.close, children: /* @__PURE__ */ jsx(CloseIcon4, {}) })
2051
2241
  ] }),
2052
2242
  /* @__PURE__ */ jsx(ChatRoomProvider, { roomId: activeContext.roomId, children: /* @__PURE__ */ jsx(
2053
2243
  ChatPanel,
@@ -2290,10 +2480,16 @@ function useNotificationsBadge(enabled) {
2290
2480
  const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
2291
2481
  return enabled ? snapshot.unreadCount : 0;
2292
2482
  }
2293
- function CloseIcon3() {
2483
+ function ComposeIcon() {
2484
+ return /* @__PURE__ */ jsxs("svg", { viewBox: "0 0 24 24", width: "18", height: "18", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: [
2485
+ /* @__PURE__ */ jsx("path", { d: "M12 20h9" }),
2486
+ /* @__PURE__ */ jsx("path", { d: "M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z" })
2487
+ ] });
2488
+ }
2489
+ function CloseIcon4() {
2294
2490
  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" }) });
2295
2491
  }
2296
2492
 
2297
- export { Avatar, ChatClient, ChatInput, ChatMessageItem, ChatPanel, ChatRequestError, ChatRoomProvider, DEFAULT_LABELS, DialtribeChatProvider, DialtribeMessages, NotificationsPanel, ProfileView, parseReactions, useChatClient, useChatConnection, useChatRoomContext, useChatRooms, useDialtribeChat, useDialtribeMessages, useDialtribeNotifications };
2493
+ export { Avatar, ChatClient, ChatInput, ChatMessageItem, ChatPanel, ChatRequestError, ChatRoomProvider, ComposeView, DEFAULT_LABELS, DialtribeChatProvider, DialtribeMessages, NotificationsPanel, ProfileView, parseReactions, useChatClient, useChatConnection, useChatRoomContext, useChatRooms, useDialtribeChat, useDialtribeMessages, useDialtribeNotifications };
2298
2494
  //# sourceMappingURL=index.mjs.map
2299
2495
  //# sourceMappingURL=index.mjs.map