const Follows = datalog.intoTable([
{ username: "marco", follows: "daiyi" },
{ username: "marco", follows: "noah" },
])
const Posts = datalog.intoTable([
{ username: "daiyi", content: "sourdough is a great buttermilk substitute! here's some sourdough cheddar cornbread", id: 0 },
{ username: "noah", content: "the earliest glitch art is Renaissance humanists thinking that colorless paint statues were cool", id: 1 },
])
const UnreadPosts = datalog.intoTable([
{ messageID: 0 },
])
const TimelineQueryFn = (username) => ({content, follows, messageID}) => {
Follows({username, follows})
Posts({username: follows, content, id: messageID})
}
const Timeline = ({username, posts}) => {
return <>
<h3> {username}'s Timeline: </h3>
{posts}
</>
}
const Home = ({username}) => {
const TimelineQuery = TimelineQueryFn(username)
const posts = useQuery(TimelineQuery).map(({follows, content, messageID}) => {
return <p key={messageID}> {follows}: {content}</p>
})
const unreadCount = useQuery(({messageID}) => {
TimelineQuery({messageID})
UnreadPosts({messageID})
}).length
const pluralized = unreadCount === 1 ? 'post' : 'posts'
return <>
{unreadCount > 0 ? <h4>{unreadCount} new {pluralized}</h4> : null}
<Timeline username={username} posts={posts} />
</>
}
render(<Home username="marco" />)