@diffsome/react 1.2.10 → 1.2.11

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
@@ -2823,49 +2823,80 @@ var ChatBubble = ({
2823
2823
  }) => {
2824
2824
  const [isOpen, setIsOpen] = (0, import_react24.useState)(false);
2825
2825
  const [messages, setMessages] = (0, import_react24.useState)([]);
2826
- const [conversationId, setConversationId] = (0, import_react24.useState)(null);
2826
+ const [conversationId, setConversationId] = (0, import_react24.useState)(() => {
2827
+ if (typeof localStorage !== "undefined") {
2828
+ const saved = localStorage.getItem("diffsome_conversation_id");
2829
+ return saved ? parseInt(saved, 10) : null;
2830
+ }
2831
+ return null;
2832
+ });
2827
2833
  const [connection, setConnection] = (0, import_react24.useState)(null);
2828
2834
  const [loading, setLoading] = (0, import_react24.useState)(false);
2829
2835
  const [typing, setTyping] = (0, import_react24.useState)(false);
2830
2836
  const [unreadCount, setUnreadCount] = (0, import_react24.useState)(0);
2831
2837
  const seenMessageIds = (0, import_react24.useRef)(/* @__PURE__ */ new Set());
2838
+ const isRestoringRef = (0, import_react24.useRef)(false);
2839
+ const connectToConversation = (0, import_react24.useCallback)((convId) => {
2840
+ const conn = client.chat.connect(convId);
2841
+ setConnection(conn);
2842
+ conn.onMessage((msg) => {
2843
+ if (seenMessageIds.current.has(msg.id)) {
2844
+ return;
2845
+ }
2846
+ seenMessageIds.current.add(msg.id);
2847
+ setMessages((prev) => [...prev, msg]);
2848
+ if (!isOpen && msg.sender_type !== "visitor") {
2849
+ setUnreadCount((c) => c + 1);
2850
+ }
2851
+ });
2852
+ conn.onTyping((senderType) => {
2853
+ if (senderType === "agent") {
2854
+ setTyping(true);
2855
+ setTimeout(() => setTyping(false), 3e3);
2856
+ }
2857
+ });
2858
+ return conn;
2859
+ }, [client.chat, isOpen]);
2832
2860
  const initChat = (0, import_react24.useCallback)(async () => {
2833
- if (conversationId) return;
2861
+ if (connection) return;
2834
2862
  setLoading(true);
2835
2863
  try {
2864
+ let convId = conversationId;
2865
+ if (convId && !isRestoringRef.current) {
2866
+ isRestoringRef.current = true;
2867
+ try {
2868
+ const conn2 = connectToConversation(convId);
2869
+ const existingMessages2 = await conn2.getMessages();
2870
+ existingMessages2.forEach((m) => seenMessageIds.current.add(m.id));
2871
+ setMessages(existingMessages2);
2872
+ return;
2873
+ } catch (error) {
2874
+ console.log("Could not restore conversation, starting new one");
2875
+ convId = null;
2876
+ setConversationId(null);
2877
+ localStorage.removeItem("diffsome_conversation_id");
2878
+ }
2879
+ }
2836
2880
  const result = await client.chat.start({
2837
2881
  visitor_name: visitorName,
2838
2882
  visitor_email: visitorEmail,
2839
2883
  initial_message: greeting
2840
2884
  });
2841
- setConversationId(result.conversation_id);
2842
- const conn = client.chat.connect(result.conversation_id);
2843
- setConnection(conn);
2885
+ convId = result.conversation_id;
2886
+ setConversationId(convId);
2887
+ if (typeof localStorage !== "undefined") {
2888
+ localStorage.setItem("diffsome_conversation_id", convId.toString());
2889
+ }
2890
+ const conn = connectToConversation(convId);
2844
2891
  const existingMessages = await conn.getMessages();
2845
2892
  existingMessages.forEach((m) => seenMessageIds.current.add(m.id));
2846
2893
  setMessages(existingMessages);
2847
- conn.onMessage((msg) => {
2848
- if (seenMessageIds.current.has(msg.id)) {
2849
- return;
2850
- }
2851
- seenMessageIds.current.add(msg.id);
2852
- setMessages((prev) => [...prev, msg]);
2853
- if (!isOpen && msg.sender_type !== "visitor") {
2854
- setUnreadCount((c) => c + 1);
2855
- }
2856
- });
2857
- conn.onTyping((senderType) => {
2858
- if (senderType === "agent") {
2859
- setTyping(true);
2860
- setTimeout(() => setTyping(false), 3e3);
2861
- }
2862
- });
2863
2894
  } catch (error) {
2864
2895
  console.error("Failed to initialize chat:", error);
2865
2896
  } finally {
2866
2897
  setLoading(false);
2867
2898
  }
2868
- }, [client.chat, conversationId, visitorName, visitorEmail, greeting, isOpen]);
2899
+ }, [client.chat, connection, conversationId, connectToConversation, visitorName, visitorEmail, greeting]);
2869
2900
  const handleOpen = () => {
2870
2901
  setIsOpen(true);
2871
2902
  setUnreadCount(0);
package/dist/index.mjs CHANGED
@@ -2731,49 +2731,80 @@ var ChatBubble = ({
2731
2731
  }) => {
2732
2732
  const [isOpen, setIsOpen] = useState22(false);
2733
2733
  const [messages, setMessages] = useState22([]);
2734
- const [conversationId, setConversationId] = useState22(null);
2734
+ const [conversationId, setConversationId] = useState22(() => {
2735
+ if (typeof localStorage !== "undefined") {
2736
+ const saved = localStorage.getItem("diffsome_conversation_id");
2737
+ return saved ? parseInt(saved, 10) : null;
2738
+ }
2739
+ return null;
2740
+ });
2735
2741
  const [connection, setConnection] = useState22(null);
2736
2742
  const [loading, setLoading] = useState22(false);
2737
2743
  const [typing, setTyping] = useState22(false);
2738
2744
  const [unreadCount, setUnreadCount] = useState22(0);
2739
2745
  const seenMessageIds = useRef3(/* @__PURE__ */ new Set());
2746
+ const isRestoringRef = useRef3(false);
2747
+ const connectToConversation = useCallback22((convId) => {
2748
+ const conn = client.chat.connect(convId);
2749
+ setConnection(conn);
2750
+ conn.onMessage((msg) => {
2751
+ if (seenMessageIds.current.has(msg.id)) {
2752
+ return;
2753
+ }
2754
+ seenMessageIds.current.add(msg.id);
2755
+ setMessages((prev) => [...prev, msg]);
2756
+ if (!isOpen && msg.sender_type !== "visitor") {
2757
+ setUnreadCount((c) => c + 1);
2758
+ }
2759
+ });
2760
+ conn.onTyping((senderType) => {
2761
+ if (senderType === "agent") {
2762
+ setTyping(true);
2763
+ setTimeout(() => setTyping(false), 3e3);
2764
+ }
2765
+ });
2766
+ return conn;
2767
+ }, [client.chat, isOpen]);
2740
2768
  const initChat = useCallback22(async () => {
2741
- if (conversationId) return;
2769
+ if (connection) return;
2742
2770
  setLoading(true);
2743
2771
  try {
2772
+ let convId = conversationId;
2773
+ if (convId && !isRestoringRef.current) {
2774
+ isRestoringRef.current = true;
2775
+ try {
2776
+ const conn2 = connectToConversation(convId);
2777
+ const existingMessages2 = await conn2.getMessages();
2778
+ existingMessages2.forEach((m) => seenMessageIds.current.add(m.id));
2779
+ setMessages(existingMessages2);
2780
+ return;
2781
+ } catch (error) {
2782
+ console.log("Could not restore conversation, starting new one");
2783
+ convId = null;
2784
+ setConversationId(null);
2785
+ localStorage.removeItem("diffsome_conversation_id");
2786
+ }
2787
+ }
2744
2788
  const result = await client.chat.start({
2745
2789
  visitor_name: visitorName,
2746
2790
  visitor_email: visitorEmail,
2747
2791
  initial_message: greeting
2748
2792
  });
2749
- setConversationId(result.conversation_id);
2750
- const conn = client.chat.connect(result.conversation_id);
2751
- setConnection(conn);
2793
+ convId = result.conversation_id;
2794
+ setConversationId(convId);
2795
+ if (typeof localStorage !== "undefined") {
2796
+ localStorage.setItem("diffsome_conversation_id", convId.toString());
2797
+ }
2798
+ const conn = connectToConversation(convId);
2752
2799
  const existingMessages = await conn.getMessages();
2753
2800
  existingMessages.forEach((m) => seenMessageIds.current.add(m.id));
2754
2801
  setMessages(existingMessages);
2755
- conn.onMessage((msg) => {
2756
- if (seenMessageIds.current.has(msg.id)) {
2757
- return;
2758
- }
2759
- seenMessageIds.current.add(msg.id);
2760
- setMessages((prev) => [...prev, msg]);
2761
- if (!isOpen && msg.sender_type !== "visitor") {
2762
- setUnreadCount((c) => c + 1);
2763
- }
2764
- });
2765
- conn.onTyping((senderType) => {
2766
- if (senderType === "agent") {
2767
- setTyping(true);
2768
- setTimeout(() => setTyping(false), 3e3);
2769
- }
2770
- });
2771
2802
  } catch (error) {
2772
2803
  console.error("Failed to initialize chat:", error);
2773
2804
  } finally {
2774
2805
  setLoading(false);
2775
2806
  }
2776
- }, [client.chat, conversationId, visitorName, visitorEmail, greeting, isOpen]);
2807
+ }, [client.chat, connection, conversationId, connectToConversation, visitorName, visitorEmail, greeting]);
2777
2808
  const handleOpen = () => {
2778
2809
  setIsOpen(true);
2779
2810
  setUnreadCount(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diffsome/react",
3
- "version": "1.2.10",
3
+ "version": "1.2.11",
4
4
  "description": "React hooks and providers for Diffsome SDK - Headless e-commerce & CMS",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",