@mordn/chat-widget 0.4.1 → 0.4.2

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
@@ -1699,31 +1699,28 @@ function ChatInterface({ id, initialMessages, config, onClose, headerActions } =
1699
1699
  (0, import_react9.useEffect)(() => {
1700
1700
  if (hasInitialized.current) return;
1701
1701
  const loadInitialTabs = () => {
1702
- try {
1703
- const savedTabs = localStorage.getItem(storageKey("tabs"));
1704
- const savedActiveTabId = localStorage.getItem(storageKey("active-tab-id"));
1705
- if (savedTabs && savedTabs !== "[]") {
1706
- const parsedTabs = JSON.parse(savedTabs);
1707
- setTabs(parsedTabs);
1708
- const activeId = savedActiveTabId || parsedTabs[0]?.id;
1709
- setActiveTabId(activeId);
1710
- setInitialTabCreated(true);
1711
- } else if (!initialTabCreated && tabs.length === 0) {
1712
- const initialTabId = `chat-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
1713
- const currentTab = {
1714
- id: initialTabId,
1715
- title: "New Chat",
1716
- isActive: true
1717
- };
1718
- setTabs([currentTab]);
1719
- setActiveTabId(initialTabId);
1720
- setInitialTabCreated(true);
1721
- }
1722
- } finally {
1702
+ const savedTabs = localStorage.getItem(storageKey("tabs"));
1703
+ const savedActiveTabId = localStorage.getItem(storageKey("active-tab-id"));
1704
+ if (savedTabs && savedTabs !== "[]") {
1705
+ const parsedTabs = JSON.parse(savedTabs);
1706
+ setTabs(parsedTabs);
1707
+ const activeId = savedActiveTabId || parsedTabs[0]?.id;
1708
+ setActiveTabId(activeId);
1709
+ setInitialTabCreated(true);
1710
+ } else if (!initialTabCreated && tabs.length === 0) {
1711
+ const initialTabId = `chat-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
1712
+ setTabs([{ id: initialTabId, title: "New Chat", isActive: true }]);
1713
+ setActiveTabId(initialTabId);
1714
+ setInitialTabCreated(true);
1723
1715
  setIsInitializing(false);
1724
1716
  }
1725
1717
  };
1726
- loadInitialTabs();
1718
+ try {
1719
+ loadInitialTabs();
1720
+ } catch (err) {
1721
+ console.error("[chat-widget] init failed, falling back to clean start:", err);
1722
+ setIsInitializing(false);
1723
+ }
1727
1724
  hasInitialized.current = true;
1728
1725
  }, []);
1729
1726
  const hasLoadedInitialMessages = (0, import_react9.useRef)(false);
@@ -1731,10 +1728,15 @@ function ChatInterface({ id, initialMessages, config, onClose, headerActions } =
1731
1728
  if (hasLoadedInitialMessages.current) return;
1732
1729
  if (!config?.userId) return;
1733
1730
  if (!activeTabId) return;
1734
- if (isInitializing) return;
1735
- loadConversation(activeTabId);
1736
- hasLoadedInitialMessages.current = true;
1737
- }, [config?.userId, activeTabId, isInitializing]);
1731
+ (async () => {
1732
+ try {
1733
+ await loadConversation(activeTabId);
1734
+ } finally {
1735
+ hasLoadedInitialMessages.current = true;
1736
+ setIsInitializing(false);
1737
+ }
1738
+ })();
1739
+ }, [config?.userId, activeTabId]);
1738
1740
  (0, import_react9.useEffect)(() => {
1739
1741
  if (isInitializing) return;
1740
1742
  if (activeTabId && tabs.length > 0 && activeTabId !== lastSyncedTabId.current) {