reachat 1.0.1 → 1.0.3

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
@@ -10,7 +10,6 @@ import { Prism } from "react-syntax-highlighter";
10
10
  import { findAndReplace } from "mdast-util-find-and-replace";
11
11
  import remarkYoutube from "remark-youtube";
12
12
  import { useHotkeys } from "reakeys";
13
- import useDimensions from "react-cool-dimensions";
14
13
  import { isToday, isYesterday, isThisWeek, isThisMonth, isThisYear, format } from "date-fns";
15
14
  const SvgSend = (props) => /* @__PURE__ */ React.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", width: 24, height: 24, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 1, strokeLinecap: "round", strokeLinejoin: "round", className: "lucide lucide-send-horizontal", ...props }, /* @__PURE__ */ React.createElement("path", { d: "m3 3 3 9-3 9 19-9Z" }), /* @__PURE__ */ React.createElement("path", { d: "M6 12h16" }));
16
15
  const SvgStop = (props) => /* @__PURE__ */ React.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", width: 24, height: 24, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 1, strokeLinecap: "round", strokeLinejoin: "round", className: "lucide lucide-octagon-x", ...props }, /* @__PURE__ */ React.createElement("path", { d: "m15 9-6 6" }), /* @__PURE__ */ React.createElement("path", { d: "M2.586 16.726A2 2 0 0 1 2 15.312V8.688a2 2 0 0 1 .586-1.414l4.688-4.688A2 2 0 0 1 8.688 2h6.624a2 2 0 0 1 1.414.586l4.688 4.688A2 2 0 0 1 22 8.688v6.624a2 2 0 0 1-.586 1.414l-4.688 4.688a2 2 0 0 1-1.414.586H8.688a2 2 0 0 1-1.414-.586z" }), /* @__PURE__ */ React.createElement("path", { d: "m9 9 6 6" }));
@@ -21,21 +20,18 @@ const ChatContext = createContext({
21
20
  });
22
21
  const ChatInput = ({
23
22
  allowedFiles,
24
- onSendMessage,
25
23
  placeholder,
26
- onStopMessage,
27
- onFileUpload,
28
24
  defaultValue,
29
25
  sendIcon = /* @__PURE__ */ jsx(SvgSend, {}),
30
26
  stopIcon = /* @__PURE__ */ jsx(SvgStop, {}),
31
27
  attachIcon = /* @__PURE__ */ jsx(SvgPaperclip, {})
32
28
  }) => {
33
- const { theme, isLoading, disabled } = useContext(ChatContext);
29
+ const { theme, isLoading, disabled, sendMessage, stopMessage, fileUpload } = useContext(ChatContext);
34
30
  const [message, setMessage] = useState("");
35
31
  const fileInputRef = useRef(null);
36
32
  const handleSendMessage = () => {
37
33
  if (message.trim()) {
38
- onSendMessage == null ? void 0 : onSendMessage(message);
34
+ sendMessage == null ? void 0 : sendMessage(message);
39
35
  setMessage("");
40
36
  }
41
37
  };
@@ -48,8 +44,8 @@ const ChatInput = ({
48
44
  const handleFileUpload = (event) => {
49
45
  var _a;
50
46
  const file = (_a = event.target.files) == null ? void 0 : _a[0];
51
- if (file && onFileUpload) {
52
- onFileUpload(file);
47
+ if (file && fileUpload) {
48
+ fileUpload(file);
53
49
  }
54
50
  };
55
51
  return /* @__PURE__ */ jsxs("div", { className: cn(theme.input.base), children: [
@@ -97,7 +93,7 @@ const ChatInput = ({
97
93
  {
98
94
  title: "Stop",
99
95
  className: cn(theme.input.stop),
100
- onClick: onStopMessage,
96
+ onClick: stopMessage,
101
97
  disabled,
102
98
  children: stopIcon
103
99
  }
@@ -1316,6 +1312,7 @@ const MessageFile = ({
1316
1312
  fileIcon = /* @__PURE__ */ jsx(SvgFile, {})
1317
1313
  }) => {
1318
1314
  const { theme } = useContext(ChatContext);
1315
+ const isImage = type == null ? void 0 : type.startsWith("image/");
1319
1316
  return /* @__PURE__ */ jsxs(
1320
1317
  "figure",
1321
1318
  {
@@ -1324,8 +1321,8 @@ const MessageFile = ({
1324
1321
  window.open(url, "_blank");
1325
1322
  },
1326
1323
  children: [
1327
- fileIcon,
1328
- (name || type) && /* @__PURE__ */ jsx("figcaption", { children: name && /* @__PURE__ */ jsx("span", { className: cn(theme.messages.message.files.file.name), children: /* @__PURE__ */ jsx(Ellipsis, { value: name, limit }) }) })
1324
+ isImage ? /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx("img", { src: url, className: "h-10 w-10" }) }) : /* @__PURE__ */ jsx(Fragment, { children: fileIcon }),
1325
+ name && /* @__PURE__ */ jsx("figcaption", { children: name && /* @__PURE__ */ jsx("span", { className: cn(theme.messages.message.files.file.name), children: /* @__PURE__ */ jsx(Ellipsis, { value: name, limit }) }) })
1329
1326
  ]
1330
1327
  }
1331
1328
  );
@@ -1632,12 +1629,35 @@ const chatTheme = {
1632
1629
  stop: "px-4 py-2 bg-red-500 text-white rounded hover:bg-red-700"
1633
1630
  }
1634
1631
  };
1632
+ const useDimensions = () => {
1633
+ const [ref, setRef] = useState(null);
1634
+ const [width, setWidth] = useState(void 0);
1635
+ const observe = useCallback((element) => {
1636
+ if (element) setRef(element);
1637
+ }, []);
1638
+ useEffect(() => {
1639
+ if (!ref) return;
1640
+ const resizeObserver = new ResizeObserver((entries) => {
1641
+ for (let entry of entries) {
1642
+ setWidth(entry.contentRect.width);
1643
+ }
1644
+ });
1645
+ resizeObserver.observe(ref);
1646
+ return () => {
1647
+ resizeObserver.disconnect();
1648
+ };
1649
+ }, [ref]);
1650
+ return { width, observe };
1651
+ };
1635
1652
  const Chat = ({
1636
1653
  children,
1637
1654
  viewType = "console",
1638
1655
  sessions,
1639
1656
  onSelectSession,
1640
1657
  onDeleteSession,
1658
+ onSendMessage,
1659
+ onStopMessage,
1660
+ onFileUpload,
1641
1661
  isLoading,
1642
1662
  activeSessionId,
1643
1663
  theme: customTheme = chatTheme,
@@ -1699,7 +1719,10 @@ const Chat = ({
1699
1719
  activeSessionId: internalActiveSessionID,
1700
1720
  selectSession: handleSelectSession,
1701
1721
  deleteSession: handleDeleteSession,
1702
- createSession: handleCreateNewSession
1722
+ createSession: handleCreateNewSession,
1723
+ sendMessage: onSendMessage,
1724
+ stopMessage: onStopMessage,
1725
+ fileUpload: onFileUpload
1703
1726
  }),
1704
1727
  [
1705
1728
  isLoading,
@@ -1712,7 +1735,10 @@ const Chat = ({
1712
1735
  internalActiveSessionID,
1713
1736
  handleSelectSession,
1714
1737
  handleDeleteSession,
1715
- handleCreateNewSession
1738
+ handleCreateNewSession,
1739
+ onSendMessage,
1740
+ onStopMessage,
1741
+ onFileUpload
1716
1742
  ]
1717
1743
  );
1718
1744
  return /* @__PURE__ */ jsx(ChatContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(AnimatePresence, { initial: false, children: /* @__PURE__ */ jsx(