Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor(web): migrate chat sidebar collapse storage (#36963)

lmlm5cbbd78f38a2
diff --git a/eslint-suppressions.json b/eslint-suppressions.json
index 40255553fd6..180a51f7233 100644
--- a/eslint-suppressions.json
+++ b/eslint-suppressions.json
@@ -817,12 +817,6 @@
}
},
"web/app/components/base/chat/chat-with-history/hooks.tsx": {
- "no-restricted-globals": {
- "count": 2
- },
- "react/set-state-in-effect": {
- "count": 4
- },
"ts/no-explicit-any": {
"count": 18
}
diff --git a/web/app/components/base/chat/chat-with-history/hooks.tsx b/web/app/components/base/chat/chat-with-history/hooks.tsx
index ae5692a56ef..9f89b2e2317 100644
--- a/web/app/components/base/chat/chat-with-history/hooks.tsx
+++ b/web/app/components/base/chat/chat-with-history/hooks.tsx
@@ -21,6 +21,9 @@ import { addFileInfos, sortAgentSorts } from '../../../tools/utils'
import { CONVERSATION_ID_INFO } from '../constants'
import { buildChatItemTree, getProcessedSystemVariablesFromUrlParams, getRawInputsFromUrlParams, getRawUserVariablesFromUrlParams } from '../utils'
+const WEBAPP_SIDEBAR_COLLAPSE_STORAGE_KEY = 'webappSidebarCollapse'
+const rawStorageOptions = { raw: true } as const
+
function getFormattedChatList(messages: any[]) {
const newChatList: ChatItem[] = []
messages.forEach((item) => {
@@ -116,31 +119,16 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
}
setLocaleFromProps()
}, [appData])
- const [sidebarCollapseState, setSidebarCollapseState] = useState<boolean>(() => {
- if (typeof window !== 'undefined') {
- try {
- const localState = localStorage.getItem('webappSidebarCollapse')
- return localState === 'collapsed'
- }
- catch {
- // localStorage may be disabled in private browsing mode or by security settings
- // fallback to default value
- return false
- }
- }
- return false
- })
+ const [storedSidebarCollapseState, setStoredSidebarCollapseState] = useLocalStorage<string>(
+ WEBAPP_SIDEBAR_COLLAPSE_STORAGE_KEY,
+ undefined,
+ rawStorageOptions,
+ )
+ const sidebarCollapseState = storedSidebarCollapseState === 'collapsed'
const handleSidebarCollapse = useCallback((state: boolean) => {
- if (appId) {
- setSidebarCollapseState(state)
- try {
- localStorage.setItem('webappSidebarCollapse', state ? 'collapsed' : 'expanded')
- }
- catch {
- // localStorage may be disabled, continue without persisting state
- }
- }
- }, [appId, setSidebarCollapseState])
+ if (appId)
+ setStoredSidebarCollapseState(state ? 'collapsed' : 'expanded')
+ }, [appId, setStoredSidebarCollapseState])
const [conversationIdInfo, setConversationIdInfo] = useLocalStorage<Record<string, Record<string, string>>>(CONVERSATION_ID_INFO, {})
const currentConversationId = useMemo(() => conversationIdInfo?.[appId || '']?.[userId || 'DEFAULT'] || '', [appId, conversationIdInfo, userId])
const handleConversationIdInfoChange = useCallback((changeConversationId: string) => {
@@ -209,6 +197,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
const [initUserVariables, setInitUserVariables] = useState<Record<string, any>>({})
const handleNewConversationInputsChange = useCallback((newInputs: Record<string, any>) => {
newConversationInputsRef.current = newInputs
+ // eslint-disable-next-line react/set-state-in-effect -- This handler intentionally syncs derived input defaults when called from the reset effect below.
setNewConversationInputs(newInputs)
}, [])
const inputsForms = useMemo(() => {
@@ -305,6 +294,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
const [originConversationList, setOriginConversationList] = useState<ConversationItem[]>([])
useEffect(() => {
if (appConversationData?.data && !appConversationDataLoading)
+ // eslint-disable-next-line react/set-state-in-effect -- Conversation query results intentionally replace the local editable list.
setOriginConversationList(appConversationData?.data)
}, [appConversationData, appConversationDataLoading])
const conversationList = useMemo(() => {
@@ -321,6 +311,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
}, [originConversationList, showNewConversationItemInList, t])
useEffect(() => {
if (newConversation) {
+ // eslint-disable-next-line react/set-state-in-effect -- Newly resolved conversation names intentionally patch the local list cache.
setOriginConversationList(produce((draft) => {
const index = draft.findIndex(item => item.id === newConversation.id)
if (index > -1)
@@ -344,6 +335,7 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
const [currentConversationInputs, setCurrentConversationInputs] = useState<Record<string, any>>(currentConversationLatestInputs || {})
useEffect(() => {
if (currentConversationItem)
+ // eslint-disable-next-line react/set-state-in-effect -- Selected conversation changes intentionally resync the editable input snapshot.
setCurrentConversationInputs(currentConversationLatestInputs || {})
}, [currentConversationItem, currentConversationLatestInputs])
const checkInputsRequired = useCallback((silent?: boolean) => {