From af3b8ab3deb0440deac7369b5149471e7fee1326 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 7 Oct 2024 19:23:15 +0200 Subject: [PATCH] Memoize Settings --- frontend/src/components/ContentView.js | 2 -- frontend/src/components/FileTree.js | 26 ++++++++------------ frontend/src/contexts/SettingsContext.js | 30 +++++++++++++++++------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/frontend/src/components/ContentView.js b/frontend/src/components/ContentView.js index c0eb55d..833c74b 100644 --- a/frontend/src/components/ContentView.js +++ b/frontend/src/components/ContentView.js @@ -44,8 +44,6 @@ const ContentView = ({ ); } - console.log('ContentView content', content); - return activeTab === 'source' ? ( { +const FileTree = ({ files, handleFileSelect }) => { if (files.length === 0) { return
No files to display
; } - const renderLabel = (node) => { - const path = node.extra; - return ( - - {node.name} - - ); - }; - - const renderIcon = ({ type, name }) => { - if (type === 'directory') return ; - return isImageFile(name) ? : ; - }; + const renderIcon = useMemo( + () => + ({ type, name }) => { + if (type === 'directory') return ; + return isImageFile(name) ? : ; + }, + [] + ); return ( handleFileSelect(filePath)} renderIcon={renderIcon} - renderLabel={renderLabel} /> ); }; diff --git a/frontend/src/contexts/SettingsContext.js b/frontend/src/contexts/SettingsContext.js index 255c755..3267649 100644 --- a/frontend/src/contexts/SettingsContext.js +++ b/frontend/src/contexts/SettingsContext.js @@ -1,12 +1,16 @@ -import React, { createContext, useState, useContext, useEffect } from 'react'; +import React, { + createContext, + useState, + useContext, + useEffect, + useMemo, +} from 'react'; import { fetchUserSettings, saveUserSettings } from '../services/api'; import { DEFAULT_SETTINGS } from '../utils/constants'; const SettingsContext = createContext(); -export const useSettings = () => { - return useContext(SettingsContext); -}; +export const useSettings = () => useContext(SettingsContext); export const SettingsProvider = ({ children }) => { const [settings, setSettings] = useState(DEFAULT_SETTINGS); @@ -15,7 +19,7 @@ export const SettingsProvider = ({ children }) => { useEffect(() => { const loadSettings = async () => { try { - const userSettings = await fetchUserSettings(1); // Assuming user ID 1 for now + const userSettings = await fetchUserSettings(1); setSettings(userSettings.settings); } catch (error) { console.error('Failed to load user settings:', error); @@ -30,7 +34,7 @@ export const SettingsProvider = ({ children }) => { const updateSettings = async (newSettings) => { try { await saveUserSettings({ - userId: 1, // Assuming user ID 1 for now + userId: 1, settings: newSettings, }); setSettings(newSettings); @@ -47,10 +51,18 @@ export const SettingsProvider = ({ children }) => { })); }; + const contextValue = useMemo( + () => ({ + settings, + updateSettings, + updateTheme, + loading, + }), + [settings, loading] + ); + return ( - + {children} );