Last active
May 5, 2020 10:00
-
-
Save apaleslimghost/d5c11a89c06727122e07900ee60062d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OrderedMap } from 'immutable' | |
import React, { useState, useEffect } from 'react' | |
import { LiveBlogStream } from './stream' // imaginary | |
import { LiveBlogPost } from '@financial-times/x-live-blog-post' | |
// e.g. <LiveBlogList initialPosts={dataFromCatchupRequest} eventUrl='https://next-live-event.ft.com/?eventid=the-world/liveblogs/2020-05-05-2/' /> | |
export function LiveBlogList({ serverConnection = false, initialPosts = [] }) { | |
const [posts, setPosts] = useState( | |
// using an OrderedMap means that new posts will be iterated | |
// last and later-edited posts will stay in place | |
OrderedMap( | |
initialPosts.map(post => [post.id, post]) | |
) | |
) | |
useEffect(() => { | |
if(eventUrl) { | |
// an imaginary connection to a server stream of live blog events | |
const channel = new LiveBlogStream(eventUrl) | |
channel.on('post', post => { | |
setPosts(posts => posts.set(post.id, post)) | |
}) | |
return () => { | |
channel.close() | |
} | |
} | |
}, [serverConnection]) | |
return posts.map(post => <LiveBlogPost post={post} key={post.id} />) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment