diff --git a/src/components/Collapsible/index.tsx b/src/components/Collapsible/index.tsx new file mode 100644 index 0000000..9ef0311 --- /dev/null +++ b/src/components/Collapsible/index.tsx @@ -0,0 +1,74 @@ +import { Button } from '@/components/ui/button' +import { useEffect, useRef, useState } from 'react' +import { useTranslation } from 'react-i18next' + +export default function Collapsible({ + alwaysExpand = false, + children, + threshold = 1000, + collapsedHeight = 600, + ...props +}: { + alwaysExpand?: boolean + threshold?: number + collapsedHeight?: number +} & React.HTMLProps) { + const { t } = useTranslation() + const containerRef = useRef(null) + const [expanded, setExpanded] = useState(false) + const [shouldCollapse, setShouldCollapse] = useState(false) + + useEffect(() => { + if (alwaysExpand || shouldCollapse) return + + const contentEl = containerRef.current + if (!contentEl) return + + const checkHeight = () => { + const fullHeight = contentEl.scrollHeight + if (fullHeight > threshold) { + setShouldCollapse(true) + } + } + + checkHeight() + + const observer = new ResizeObserver(() => { + checkHeight() + }) + + observer.observe(contentEl) + + return () => { + observer.disconnect() + } + }, [alwaysExpand, shouldCollapse]) + + return ( +
+
+ {children} + {shouldCollapse && !expanded && ( +
+
+ +
+
+ )} +
+
+ ) +} diff --git a/src/components/NoteCard/MainNoteCard.tsx b/src/components/NoteCard/MainNoteCard.tsx index bad1463..6730393 100644 --- a/src/components/NoteCard/MainNoteCard.tsx +++ b/src/components/NoteCard/MainNoteCard.tsx @@ -1,10 +1,8 @@ -import { Button } from '@/components/ui/button' import { Separator } from '@/components/ui/separator' import { toNote } from '@/lib/link' import { useSecondaryPage } from '@/PageManager' import { Event } from 'nostr-tools' -import { useEffect, useRef, useState } from 'react' -import { useTranslation } from 'react-i18next' +import Collapsible from '../Collapsible' import Note from '../Note' import NoteStats from '../NoteStats' import RepostDescription from './RepostDescription' @@ -20,41 +18,10 @@ export default function MainNoteCard({ reposter?: string embedded?: boolean }) { - const { t } = useTranslation() const { push } = useSecondaryPage() - const containerRef = useRef(null) - const [expanded, setExpanded] = useState(false) - const [shouldCollapse, setShouldCollapse] = useState(false) - - useEffect(() => { - if (embedded || shouldCollapse) return - - const contentEl = containerRef.current - if (!contentEl) return - - const checkHeight = () => { - const fullHeight = contentEl.scrollHeight - if (fullHeight > 1000) { - setShouldCollapse(true) - } - } - - checkHeight() - - const observer = new ResizeObserver(() => { - checkHeight() - }) - - observer.observe(contentEl) - - return () => { - observer.disconnect() - } - }, [embedded, shouldCollapse]) return (
{ e.stopPropagation() @@ -62,34 +29,14 @@ export default function MainNoteCard({ }} >
-
+ - {shouldCollapse && !expanded && ( -
-
- -
-
- )} -
+ {!embedded && }
{!embedded && }