import { Box, VStack, HStack, SpanBox } from '../../elements/LayoutPrimitives' import { useCallback, useMemo, useState } from 'react' import { CaretDown, CaretUp } from 'phosphor-react' import { MetaStyle, timeAgo, TitleStyle } from './LibraryCardStyles' import { styled } from '@stitches/react' import { UserBasicData } from '../../../lib/networking/queries/useGetViewerQuery' import { LibraryItemNode } from '../../../lib/networking/queries/useGetLibraryItemsQuery' import { Button } from '../../elements/Button' import { theme } from '../../tokens/stitches.config' import { getHighlightLocation } from '../../templates/article/NotebookModal' import { Highlight } from '../../../lib/networking/fragments/highlightFragment' import { HighlightView } from '../HighlightView' export const GridSeparator = styled(Box, { height: '1px', marginTop: '15px', backgroundColor: '$thBorderColor', }) type LibraryHighlightGridCardProps = { viewer: UserBasicData item: LibraryItemNode deleteHighlight: (item: LibraryItemNode, highlight: Highlight) => void } export function LibraryHighlightGridCard( props: LibraryHighlightGridCardProps ): JSX.Element { const [expanded, setExpanded] = useState(false) const higlightCount = props.item.highlights?.length ?? 0 const sortedHighlights = useMemo(() => { const sorted = (a: number, b: number) => { if (a < b) { return -1 } if (a > b) { return 1 } return 0 } if (!props.item.highlights) { return [] } return props.item.highlights .filter((h) => h.type === 'HIGHLIGHT') .sort((a: Highlight, b: Highlight) => { if (a.highlightPositionPercent && b.highlightPositionPercent) { return sorted(a.highlightPositionPercent, b.highlightPositionPercent) } // We do this in a try/catch because it might be an invalid diff // With PDF it will definitely be an invalid diff. try { const aPos = getHighlightLocation(a.patch) const bPos = getHighlightLocation(b.patch) if (aPos && bPos) { return sorted(aPos, bPos) } } catch {} return a.createdAt.localeCompare(b.createdAt) }) }, [props.item.highlights]) return ( {!expanded && ( {timeAgo(props.item.savedAt)} {` `} {props.item.wordsCount ?? 0 > 0 ? ` • ${Math.max( 1, Math.round((props.item.wordsCount ?? 0) / 235) )} min read` : null} {props.item.highlights?.length ?? 0 > 0 ? ` • ${props.item.highlights?.length} highlights` : null} )} {props.item.title} {expanded && ( <> {sortedHighlights.map((highlight) => ( { console.log('updated highlight: ', highlight) }} /> ))} )} {!expanded ? ( ) : ( )} ) }