import { Box, VStack, HStack, SpanBox } from '../../elements/LayoutPrimitives' import { useCallback, useMemo, useState } from 'react' import { CaretDown, CaretUp } from '@phosphor-icons/react' import { MetaStyle, timeAgo, TitleStyle } from './LibraryCardStyles' import { styled } from '@stitches/react' import { UserBasicData } from '../../../lib/networking/queries/useGetViewerQuery' import { LibraryItemNode } from '../../../lib/networking/library_items/useLibraryItems' import { Button } from '../../elements/Button' import { theme } from '../../tokens/stitches.config' import { Highlight } from '../../../lib/networking/fragments/highlightFragment' import { HighlightView } from '../HighlightView' import { useRouter } from 'next/router' import { showErrorToast } from '../../../lib/toastHelpers' import { sortHighlights } from '../../../lib/highlights/sortHighlights' 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 highlightCount = props.item.highlights?.length ?? 0 const router = useRouter() const viewInReader = useCallback( (highlightId: string) => { if (!router || !router.isReady || !props.viewer) { showErrorToast('Error navigating to highlight') return } router.push( { pathname: '/[username]/[slug]', query: { username: props.viewer.profile.username, slug: props.item.slug, }, hash: highlightId, }, `${props.viewer.profile.username}/${props.item.slug}#${highlightId}`, { scroll: false, } ) }, [router, props] ) const sortedHighlights = useMemo(() => { return sortHighlights(props.item.highlights ?? []) }, [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('TODO: set labels') }} setShowConfirmDeleteHighlightId={() => { console.log('TODO: confirm delete') }} updateHighlight={(highlight) => { console.log('updated highlight: ', highlight) }} /> ))} )} {!expanded ? ( ) : ( )} ) }