import { Button, Box, Card, Text, Grid, Avatar, Flex } from 'theme-ui'
import { formatDate } from '../../lib/dates'
import { Fragment, memo } from 'react'
import { last, filter } from 'lodash'
import Masonry from 'react-masonry-css'
import Image from 'next/image'
import Mention from './mention'
import Emoji from './emoji'
const dataDetector = /(<.+?\|?\S+>)|(@\S+)|(`{3}[\S\s]+`{3})|(`[^`]+`)|(_[^_]+_)|(\*[^\*]+\*)|(:[^ .,;`\u2013~!@#$%^&*(){}=\\:"<>?|A-Z]+:)/
export const formatText = text =>
text.split(dataDetector).map((chunk, i) => {
if (chunk?.startsWith(':') && chunk?.endsWith(':')) {
return
}
if (chunk?.startsWith('@') || chunk?.startsWith('<@')) {
const punct = /([,!:.'"’”]|’s|'s|\))+$/g
const username = chunk.replace(/[@<>]/g, '').replace(punct, '')
return (
{chunk.match(punct)}
)
}
if (chunk?.startsWith('<')) {
const parts = chunk.match(/<(([^\|]+)\|)?([^>]+?)>/)
const url = parts?.[2] || last(parts)
const children = last(parts)
?.replace(/https?:\/\//, '')
.replace(/\/$/, '')
return (
{children}
)
}
if (chunk?.startsWith('```')) {
return
{chunk.replace(/```/g, '')}
}
if (chunk?.startsWith('`')) {
return {chunk.replace(/`/g, '')}
}
if (chunk?.startsWith('*')) {
return {chunk.replace(/\*/g, '')}
}
if (chunk?.startsWith('_')) {
return {chunk.replace(/_/g, '')}
}
return {chunk?.replace(/&/g, '&')}
})
const Post = ({
id = new Date().toISOString(),
profile = false,
user = {
username: 'abc',
avatar: '',
streakDisplay: false,
streakCount: 0
},
text,
attachments = [],
postedAt
}) => (
@{user.username}
{formatDate(postedAt)}
div': { width: 18, height: 18 }
}}
>
{formatText(text)}
{attachments.length > 0 && (
<>
{filter(attachments, a => ['jpg', 'jpeg', 'png'].includes(a.split('.')[a.split('.').length - 1])).map(img => (
))}
>
)}
)
const Posts = ({ data = [] }) => (
{data.map(post => (
))}
These are just a few posts…
)
export default Posts