From db945f0dcc274b841e6f6eb76790ec99d82bed80 Mon Sep 17 00:00:00 2001 From: a7m-1st Date: Tue, 7 Oct 2025 15:53:41 +0300 Subject: [PATCH] feat: adapter for backward compatiblity --- src/hooks/useChatStoreAdapter.tsx | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/hooks/useChatStoreAdapter.tsx diff --git a/src/hooks/useChatStoreAdapter.tsx b/src/hooks/useChatStoreAdapter.tsx new file mode 100644 index 000000000..b3a4ef279 --- /dev/null +++ b/src/hooks/useChatStoreAdapter.tsx @@ -0,0 +1,57 @@ +import { useProjectStore } from '@/store/projectStore'; +import React, { useEffect, useMemo, useState } from 'react' + +const useChatStoreAdapter = () => { + const projectStore = useProjectStore(); + + // Get the active chat store from project store + // This creates a hook-like interface for the vanilla store + const activeChatStore = projectStore.getActiveChatStore(); + + // Create a state subscription to make the component reactive + const [chatState, setChatState] = useState(() => + activeChatStore ? activeChatStore.getState() : null + ); + + useEffect(() => { + if (!activeChatStore) { + setChatState(null); + return; + } + + // Subscribe to store changes + const unsubscribe = activeChatStore.subscribe((state) => { + setChatState(state); + }); + // Set initial state + setChatState(activeChatStore.getState()); + return unsubscribe; + }, [activeChatStore]); + + // Create a chatStore-like object that mimics the original interface + const chatStore = useMemo(() => { + if (!activeChatStore || !chatState) return null; + + // Get the store methods (actions) from the vanilla store + const storeMethods = activeChatStore.getState(); + + return { + ...chatState, + // Bind store methods to maintain proper context + ...Object.keys(storeMethods).reduce((acc, key) => { + const value = (storeMethods as any)[key]; + if (typeof value === 'function') { + (acc as any)[key] = value.bind(storeMethods); + } + return acc; + }, {} as any) + }; + }, [activeChatStore, chatState]); + + return { + projectStore, + chatStore + } +} + +export default useChatStoreAdapter