import {ChevronDownIcon, ChevronUpIcon} from '@heroicons/react/24/solid' import {JSONContent} from '@tiptap/react' import clsx from 'clsx' import {MouseEventHandler, useEffect, useRef, useState} from 'react' import {usePersistentLocalState} from 'web/hooks/use-persistent-local-state' import {useSafeLayoutEffect} from 'web/hooks/use-safe-layout-effect' import {Row} from '../layout/row' import {Content} from './editor' export const COLLAPSIBLE_HEIGHT = 26 * 3 // line height is 26px export const SHOW_COLLAPSE_TRESHOLD = 180 export function ShowMoreLessButton(props: { onClick?: MouseEventHandler | undefined isCollapsed: boolean className?: string howManyMore?: number moreWhat?: string }) { const {onClick, isCollapsed, className, howManyMore, moreWhat = ''} = props const howManyMoreText = howManyMore ? howManyMore + ' ' : '' return ( ) } export function CollapsibleContent(props: { content: JSONContent | string stateKey: string defaultCollapse?: boolean hideCollapse?: boolean }) { const {content, stateKey, defaultCollapse, hideCollapse} = props const [shouldAllowCollapseOfContent, setShouldAllowCollapseOfContent] = useState(false) const contentRef = useRef(null) useSafeLayoutEffect(() => { if (contentRef.current) { if (contentRef.current.offsetHeight > SHOW_COLLAPSE_TRESHOLD) { setShouldAllowCollapseOfContent(true) } } }, [contentRef.current?.offsetHeight]) if (shouldAllowCollapseOfContent && !hideCollapse) { return ( ) } return (
) } // Moved to its own component to reduce unnecessary isCollapsed states in local storage function ActuallyCollapsibleContent(props: { content: JSONContent | string stateKey: string defaultCollapse?: boolean }) { const {content, stateKey, defaultCollapse} = props const [isCollapsed, setCollapsed] = usePersistentLocalState( defaultCollapse ?? false, stateKey, ) useEffect(() => { if (defaultCollapse !== undefined) setCollapsed(defaultCollapse) }, []) return (
setCollapsed(!isCollapsed)} isCollapsed={isCollapsed} /> {isCollapsed && (
)}
) }