-
Notifications
You must be signed in to change notification settings - Fork 13.6k
fix: persist attachment collapsed state across channel switches #40365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,11 +1,57 @@ | ||||
| import { useToggle } from '@rocket.chat/fuselage-hooks'; | ||||
| import { useAttachmentIsCollapsedByDefault } from '@rocket.chat/ui-contexts'; | ||||
| import type { ReactNode } from 'react'; | ||||
| import {useState,useEffect} from 'react' | ||||
|
|
||||
| import CollapsibleContent from '../content/collapsible/CollapsibleContent'; | ||||
|
|
||||
| export const useCollapse = (attachmentCollapsed?: boolean): [collapsed: boolean, node: ReactNode] => { | ||||
| const collpaseByDefault = useAttachmentIsCollapsedByDefault(); | ||||
| const [collapsed, toogleCollapsed] = useToggle(collpaseByDefault || attachmentCollapsed); | ||||
| const usePersistedCollapse= (attachmentId : string | undefined , initialCollapsed: boolean)=>{ | ||||
| const [collapsed,setCollapsed] = useState(()=>{ | ||||
| if(attachmentId){ | ||||
| const stored= sessionStorage.getItem(`persistant-attachment-${attachmentId}`) | ||||
| if(stored != null){ | ||||
| return stored == 'true' | ||||
|
|
||||
| } | ||||
| } | ||||
|
|
||||
| return initialCollapsed | ||||
|
|
||||
| }) | ||||
|
|
||||
| const toogleCollapsed= ()=>{ | ||||
| setCollapsed((prev)=>{ | ||||
| const value = !prev | ||||
| if(attachmentId){ | ||||
| sessionStorage.setItem(`persistant-attachment-${attachmentId}`,value ? 'true' : 'false') | ||||
| } | ||||
| return value | ||||
| }) | ||||
|
|
||||
| } | ||||
|
|
||||
| // Sync with state | ||||
|
|
||||
| useEffect(() => { | ||||
| if (attachmentId) { | ||||
| const stored = sessionStorage.getItem(`persistant-attachment-${attachmentId}`); | ||||
| if (stored !== null) { | ||||
| setCollapsed(stored === 'true'); | ||||
| } | ||||
| } | ||||
| }, [attachmentId]); | ||||
|
|
||||
| return [collapsed,toogleCollapsed] as const | ||||
|
|
||||
| } | ||||
|
|
||||
|
|
||||
|
|
||||
| export const useCollapse = (attachmentId : string | undefined,attachmentCollapsed?: boolean): [collapsed: boolean, node: ReactNode] => { | ||||
|
|
||||
| console.log('reset state') | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the render-time debug log.
🧹 Proposed fix- console.log('reset state')📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||
| const collapseByDefault = useAttachmentIsCollapsedByDefault() | ||||
| const initialCollapsed = collapseByDefault || attachmentCollapsed | ||||
| const [collapsed, toogleCollapsed] = usePersistedCollapse(attachmentId,initialCollapsed) ||useToggle(initialCollapsed); | ||||
| return [collapsed, <CollapsibleContent collapsed={collapsed} onClick={toogleCollapsed as any} key='collapsible-content-action' />]; | ||||
| }; | ||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset to the new attachment's default when storage is empty.
When
attachmentIdchanges, this effect only updates state ifsessionStoragealready has a value. If the new attachment has no saved entry yet, the hook keeps the previous attachment’s collapsed state instead of reinitializing frominitialCollapsed.🔧 Proposed fix
useEffect(() => { if (attachmentId) { const stored = sessionStorage.getItem(`persistant-attachment-${attachmentId}`); - if (stored !== null) { - setCollapsed(stored === 'true'); - } + setCollapsed(stored === null ? initialCollapsed : stored === 'true'); } -}, [attachmentId]); +}, [attachmentId, initialCollapsed]);📝 Committable suggestion
🤖 Prompt for AI Agents