From dfebd261cec20210af782eb72e3c858fa5b67cb8 Mon Sep 17 00:00:00 2001 From: "p.chikhaliya" Date: Wed, 18 Dec 2024 10:47:17 +0100 Subject: [PATCH] Fixed usememo issue by using null checks --- front/src/Redux/reducers/conversationsSlice.jsx | 4 ++-- front/src/components/Layout/Header.jsx | 6 +++--- front/src/components/Layout/Layout.jsx | 4 ++-- front/src/components/Layout/Sidebar.jsx | 2 +- front/src/components/Prompt.jsx | 14 +++++++------- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/front/src/Redux/reducers/conversationsSlice.jsx b/front/src/Redux/reducers/conversationsSlice.jsx index f539737..02786e6 100644 --- a/front/src/Redux/reducers/conversationsSlice.jsx +++ b/front/src/Redux/reducers/conversationsSlice.jsx @@ -63,7 +63,7 @@ const conversationsSlice = createSlice({ }, updateConversation: (state, action) => { const { id, updates } = action.payload; - const conversation = state.conversations.find((conv) => conv.id === id); + const conversation = state.conversations?.find((conv) => conv.id === id); if (conversation) { // Ensure system message remains at the start when updating conversation array if (updates.conversation) { @@ -134,7 +134,7 @@ export const selectConversations = (state) => state.conversations.conversations; export const selectCurrentConversationId = (state) => state.conversations.currentConversationId; export const selectCurrentConversation = (state) => - state.conversations.conversations.find( + state?.conversations?.conversations?.find( (conv) => conv.id === state.conversations.currentConversationId ); diff --git a/front/src/components/Layout/Header.jsx b/front/src/components/Layout/Header.jsx index ee5efd4..eb8a43d 100644 --- a/front/src/components/Layout/Header.jsx +++ b/front/src/components/Layout/Header.jsx @@ -51,11 +51,11 @@ function Header({ const dropdownRef = useRef(null); // Computed properties using useMemo + const currentModel = useMemo( - () => modelList.find((m) => m.name === modelSettings.model), - [modelList, modelSettings.model] + () => modelList?.find((m) => m.name === modelSettings?.model), + [modelList, modelSettings?.model] ); - const isImageSupported = useMemo( () => currentModel?.input.includes("image") || false, [currentModel] diff --git a/front/src/components/Layout/Layout.jsx b/front/src/components/Layout/Layout.jsx index 2e027e2..afee5d4 100644 --- a/front/src/components/Layout/Layout.jsx +++ b/front/src/components/Layout/Layout.jsx @@ -98,7 +98,7 @@ function Layout() { }, [currentConversation]); useEffect(() => { - const currentModel = modelList.find( + const currentModel = modelList?.find( (modelX) => modelX.name === modelSettings.model ); if (currentModel?.status === "offline") { @@ -346,7 +346,7 @@ function Layout() { showModel={setShowRenameModal} conversationId={renamingConversationId} currentTitle={ - conversations.find((conv) => conv.id === renamingConversationId) + conversations?.find((conv) => conv.id === renamingConversationId) ?.title || "" } onClose={() => { diff --git a/front/src/components/Layout/Sidebar.jsx b/front/src/components/Layout/Sidebar.jsx index 25e1bbc..a249330 100644 --- a/front/src/components/Layout/Sidebar.jsx +++ b/front/src/components/Layout/Sidebar.jsx @@ -71,7 +71,7 @@ function Sidebar({
{conversationIds.map((id) => { - const conv = conversations.find((c) => c.id === id); + const conv = conversations?.find((c) => c.id === id); if (!conv) return null; return ( diff --git a/front/src/components/Prompt.jsx b/front/src/components/Prompt.jsx index b8dc5a6..3809cf2 100644 --- a/front/src/components/Prompt.jsx +++ b/front/src/components/Prompt.jsx @@ -110,7 +110,7 @@ function Prompt({ modelSettings, modelList, onModelChange }) { (state) => state.conversations.currentConversationId ); const currentConversation = useSelector((state) => - state.conversations.conversations.find((conv) => conv.id === conversationId) + state.conversations?.conversations?.find((conv) => conv.id === conversationId) ); const [localState, setLocalState] = useState({ @@ -229,11 +229,11 @@ function Prompt({ modelSettings, modelList, onModelChange }) { const [shareSettingsModel, setShareSettingsModel] = useState(false); const [systemPromptError, setSystemPromptError] = useState(""); // Computed properties using useMemo + const currentModel = useMemo( - () => modelList.find((m) => m.name === modelSettings.model), - [modelList, modelSettings.model] + () => modelList?.find((m) => m.name === modelSettings?.model), + [modelList, modelSettings?.model] ); - const isImageSupported = useMemo( () => currentModel?.input.includes("image") || false, [currentModel] @@ -1920,7 +1920,7 @@ function Prompt({ modelSettings, modelList, onModelChange }) { } } - const systemMessage = parsedData.messages.find( + const systemMessage = parsedData?.messages?.find( (message) => message.role === "system" ); @@ -1967,7 +1967,7 @@ function Prompt({ modelSettings, modelList, onModelChange }) { } } - const systemMessage = parsedData.find( + const systemMessage = parsedData?.find( (message) => message.role === "system" ); @@ -2051,7 +2051,7 @@ function Prompt({ modelSettings, modelList, onModelChange }) { top_p: 0.05, }, })); - const currentConversation = conversations.find( + const currentConversation = conversations?.find( (conv) => conv.id === currentConversationId ); dispatch(