CoastalCommitsPastes/client/components/post-list/list-item.tsx

66 lines
2.6 KiB
TypeScript
Raw Normal View History

import NextLink from "next/link"
import { useEffect, useMemo, useState } from "react"
2022-03-12 23:40:28 -05:00
import timeAgo from "@lib/time-ago"
2022-03-09 20:11:37 -05:00
import VisibilityBadge from "../visibility-badge"
import getPostPath from "@lib/get-post-path"
import { Link, Text, Card, Tooltip, Divider, Badge } from "@geist-ui/core"
import { File, Post } from "@lib/types"
import FadeIn from "@components/fade-in"
2022-03-09 20:11:37 -05:00
// TODO: isOwner should default to false so this can be used generically
const ListItem = ({ post, isOwner = true }: { post: Post, isOwner?: boolean }) => {
const createdDate = useMemo(() => new Date(post.createdAt), [post.createdAt])
const [time, setTimeAgo] = useState(timeAgo(createdDate))
useEffect(() => {
const interval = setInterval(() => {
setTimeAgo(timeAgo(createdDate))
}, 10000)
return () => clearInterval(interval)
}, [createdDate])
const formattedTime = `${createdDate.toLocaleDateString()} ${createdDate.toLocaleTimeString()}`
return (<FadeIn><li key={post.id}>
2022-03-09 20:11:37 -05:00
<Card style={{ overflowY: 'scroll' }}>
2022-03-24 22:25:02 -04:00
<Card.Body>
<Text h3>
<NextLink passHref={true} href={getPostPath(post.visibility, post.id)}>
<Link color marginRight={'var(--gap)'}>
{post.title}
</Link>
</NextLink>
<div style={{ display: 'inline-flex' }}>
<span>
<VisibilityBadge visibility={post.visibility} />
</span>
<span style={{ marginLeft: 'var(--gap)' }}>
<Badge type="secondary"><Tooltip text={formattedTime}>{time}</Tooltip></Badge>
</span>
<span style={{ marginLeft: 'var(--gap)' }}>
<Badge type="secondary">{post.files.length === 1 ? "1 file" : `${post.files.length} files`}</Badge>
</span>
</div>
{isOwner && <span style={{ float: 'right' }}>
</span>}
2022-03-24 22:25:02 -04:00
</Text>
2022-03-09 20:11:37 -05:00
2022-03-24 22:25:02 -04:00
</Card.Body>
<Divider h="1px" my={0} />
2022-03-23 19:28:39 -04:00
<Card.Content>
{post.files.map((file: File) => {
return <div key={file.id}>
<Link color href={`${getPostPath(post.visibility, post.id)}#${file.title}`}>
{file.title || 'Untitled file'}
</Link></div>
2022-03-09 20:11:37 -05:00
})}
</Card.Content>
</Card>
</li> </FadeIn>)
2022-03-09 20:11:37 -05:00
}
export default ListItem