diff --git a/apps/meteor/client/components/message/MessageCollapsible.tsx b/apps/meteor/client/components/message/MessageCollapsible.tsx index a949be34e379e..3d8ab2215a857 100644 --- a/apps/meteor/client/components/message/MessageCollapsible.tsx +++ b/apps/meteor/client/components/message/MessageCollapsible.tsx @@ -12,10 +12,11 @@ type MessageCollapsibleProps = { link?: string; size?: number; isCollapsed?: boolean; + attachmentId? : string ; // }; -const MessageCollapsible = ({ children, title, hasDownload, link, size, isCollapsed }: MessageCollapsibleProps): ReactElement => { - const [collapsed, collapse] = useCollapse(isCollapsed); +const MessageCollapsible = ({ children, title, hasDownload, link, size, isCollapsed, attachmentId }: MessageCollapsibleProps): ReactElement => { + const [collapsed, collapse] = useCollapse(attachmentId,isCollapsed); // return ( <> diff --git a/apps/meteor/client/components/message/content/attachments/file/AudioAttachment.tsx b/apps/meteor/client/components/message/content/attachments/file/AudioAttachment.tsx index 9fa94126127e8..cd6bcb043b457 100644 --- a/apps/meteor/client/components/message/content/attachments/file/AudioAttachment.tsx +++ b/apps/meteor/client/components/message/content/attachments/file/AudioAttachment.tsx @@ -7,6 +7,7 @@ import { useReloadOnError } from './hooks/useReloadOnError'; import MessageCollapsible from '../../../MessageCollapsible'; const AudioAttachment = ({ + id, title, audio_url: url, audio_type: type, @@ -21,7 +22,7 @@ const AudioAttachment = ({ return ( <> - + diff --git a/apps/meteor/client/components/message/content/attachments/file/GenericFileAttachment.tsx b/apps/meteor/client/components/message/content/attachments/file/GenericFileAttachment.tsx index 2e0fb7107d983..99d86bda74339 100644 --- a/apps/meteor/client/components/message/content/attachments/file/GenericFileAttachment.tsx +++ b/apps/meteor/client/components/message/content/attachments/file/GenericFileAttachment.tsx @@ -21,6 +21,7 @@ const openDocumentViewer = window.RocketChatDesktop?.openDocumentViewer; type GenericFileAttachmentProps = MessageAttachmentBase; const GenericFileAttachment = ({ + id, title, title_link: link, title_link_download: hasDownload, @@ -68,7 +69,7 @@ const GenericFileAttachment = ({ return ( <> - + } diff --git a/apps/meteor/client/components/message/content/attachments/file/ImageAttachment.tsx b/apps/meteor/client/components/message/content/attachments/file/ImageAttachment.tsx index b1410b6bbf895..08ba8c32f9b43 100644 --- a/apps/meteor/client/components/message/content/attachments/file/ImageAttachment.tsx +++ b/apps/meteor/client/components/message/content/attachments/file/ImageAttachment.tsx @@ -25,7 +25,7 @@ const ImageAttachment = ({ return ( <> - + - + diff --git a/apps/meteor/client/components/message/content/urlPreviews/OEmbedCollapsible.tsx b/apps/meteor/client/components/message/content/urlPreviews/OEmbedCollapsible.tsx index 12977c0545c78..07aca983a9804 100644 --- a/apps/meteor/client/components/message/content/urlPreviews/OEmbedCollapsible.tsx +++ b/apps/meteor/client/components/message/content/urlPreviews/OEmbedCollapsible.tsx @@ -12,7 +12,7 @@ const OEmbedCollapsible = ({ children, ...props }: OEmbedCollapsibleProps): Reac const { t } = useTranslation(); return ( - + {children} diff --git a/apps/meteor/client/components/message/hooks/useCollapse.tsx b/apps/meteor/client/components/message/hooks/useCollapse.tsx index 25598d551e6cb..30541db2376b3 100644 --- a/apps/meteor/client/components/message/hooks/useCollapse.tsx +++ b/apps/meteor/client/components/message/hooks/useCollapse.tsx @@ -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') + const collapseByDefault = useAttachmentIsCollapsedByDefault() + const initialCollapsed = collapseByDefault || attachmentCollapsed + const [collapsed, toogleCollapsed] = usePersistedCollapse(attachmentId,initialCollapsed) ||useToggle(initialCollapsed); return [collapsed, ]; };