mirror of
https://github.com/siteboon/claudecodeui.git
synced 2025-12-14 09:09:32 +00:00
refactor: Create useLocalStorage hook to reduce code duplication
This commit is contained in:
41
src/App.jsx
41
src/App.jsx
@@ -33,6 +33,7 @@ import { TasksSettingsProvider } from './contexts/TasksSettingsContext';
|
|||||||
import { WebSocketProvider, useWebSocketContext } from './contexts/WebSocketContext';
|
import { WebSocketProvider, useWebSocketContext } from './contexts/WebSocketContext';
|
||||||
import ProtectedRoute from './components/ProtectedRoute';
|
import ProtectedRoute from './components/ProtectedRoute';
|
||||||
import { useVersionCheck } from './hooks/useVersionCheck';
|
import { useVersionCheck } from './hooks/useVersionCheck';
|
||||||
|
import useLocalStorage from './hooks/useLocalStorage';
|
||||||
import { api, authenticatedFetch } from './utils/api';
|
import { api, authenticatedFetch } from './utils/api';
|
||||||
|
|
||||||
|
|
||||||
@@ -54,22 +55,10 @@ function AppContent() {
|
|||||||
const [isInputFocused, setIsInputFocused] = useState(false);
|
const [isInputFocused, setIsInputFocused] = useState(false);
|
||||||
const [showSettings, setShowSettings] = useState(false);
|
const [showSettings, setShowSettings] = useState(false);
|
||||||
const [showQuickSettings, setShowQuickSettings] = useState(false);
|
const [showQuickSettings, setShowQuickSettings] = useState(false);
|
||||||
const [autoExpandTools, setAutoExpandTools] = useState(() => {
|
const [autoExpandTools, setAutoExpandTools] = useLocalStorage('autoExpandTools', false);
|
||||||
const saved = localStorage.getItem('autoExpandTools');
|
const [showRawParameters, setShowRawParameters] = useLocalStorage('showRawParameters', false);
|
||||||
return saved !== null ? JSON.parse(saved) : false;
|
const [autoScrollToBottom, setAutoScrollToBottom] = useLocalStorage('autoScrollToBottom', true);
|
||||||
});
|
const [sendByCtrlEnter, setSendByCtrlEnter] = useLocalStorage('sendByCtrlEnter', false);
|
||||||
const [showRawParameters, setShowRawParameters] = useState(() => {
|
|
||||||
const saved = localStorage.getItem('showRawParameters');
|
|
||||||
return saved !== null ? JSON.parse(saved) : false;
|
|
||||||
});
|
|
||||||
const [autoScrollToBottom, setAutoScrollToBottom] = useState(() => {
|
|
||||||
const saved = localStorage.getItem('autoScrollToBottom');
|
|
||||||
return saved !== null ? JSON.parse(saved) : true;
|
|
||||||
});
|
|
||||||
const [sendByCtrlEnter, setSendByCtrlEnter] = useState(() => {
|
|
||||||
const saved = localStorage.getItem('sendByCtrlEnter');
|
|
||||||
return saved !== null ? JSON.parse(saved) : false;
|
|
||||||
});
|
|
||||||
// Session Protection System: Track sessions with active conversations to prevent
|
// Session Protection System: Track sessions with active conversations to prevent
|
||||||
// automatic project updates from interrupting ongoing chats. When a user sends
|
// automatic project updates from interrupting ongoing chats. When a user sends
|
||||||
// a message, the session is marked as "active" and project updates are paused
|
// a message, the session is marked as "active" and project updates are paused
|
||||||
@@ -690,25 +679,13 @@ function AppContent() {
|
|||||||
isOpen={showQuickSettings}
|
isOpen={showQuickSettings}
|
||||||
onToggle={setShowQuickSettings}
|
onToggle={setShowQuickSettings}
|
||||||
autoExpandTools={autoExpandTools}
|
autoExpandTools={autoExpandTools}
|
||||||
onAutoExpandChange={(value) => {
|
onAutoExpandChange={setAutoExpandTools}
|
||||||
setAutoExpandTools(value);
|
|
||||||
localStorage.setItem('autoExpandTools', JSON.stringify(value));
|
|
||||||
}}
|
|
||||||
showRawParameters={showRawParameters}
|
showRawParameters={showRawParameters}
|
||||||
onShowRawParametersChange={(value) => {
|
onShowRawParametersChange={setShowRawParameters}
|
||||||
setShowRawParameters(value);
|
|
||||||
localStorage.setItem('showRawParameters', JSON.stringify(value));
|
|
||||||
}}
|
|
||||||
autoScrollToBottom={autoScrollToBottom}
|
autoScrollToBottom={autoScrollToBottom}
|
||||||
onAutoScrollChange={(value) => {
|
onAutoScrollChange={setAutoScrollToBottom}
|
||||||
setAutoScrollToBottom(value);
|
|
||||||
localStorage.setItem('autoScrollToBottom', JSON.stringify(value));
|
|
||||||
}}
|
|
||||||
sendByCtrlEnter={sendByCtrlEnter}
|
sendByCtrlEnter={sendByCtrlEnter}
|
||||||
onSendByCtrlEnterChange={(value) => {
|
onSendByCtrlEnterChange={setSendByCtrlEnter}
|
||||||
setSendByCtrlEnter(value);
|
|
||||||
localStorage.setItem('sendByCtrlEnter', JSON.stringify(value));
|
|
||||||
}}
|
|
||||||
isMobile={isMobile}
|
isMobile={isMobile}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
28
src/hooks/useLocalStorage.jsx
Normal file
28
src/hooks/useLocalStorage.jsx
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
function useLocalStorage(key, initialValue) {
|
||||||
|
const [storedValue, setStoredValue] = useState(() => {
|
||||||
|
try {
|
||||||
|
const item = window.localStorage.getItem(key);
|
||||||
|
return item ? JSON.parse(item) : initialValue;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return initialValue;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const setValue = (value) => {
|
||||||
|
try {
|
||||||
|
const valueToStore =
|
||||||
|
value instanceof Function ? value(storedValue) : value;
|
||||||
|
setStoredValue(valueToStore);
|
||||||
|
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return [storedValue, setValue];
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useLocalStorage;
|
||||||
Reference in New Issue
Block a user