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
type Props = { | |
items: Array<Video | LiveStream> | |
} | |
const VideoList = ({ items }) => { | |
return ( | |
<ul> | |
{items.map(item => { | |
if ('coverUrl' in item) { | |
// it's a video |
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
type LiveStream = { | |
name: string | |
previewUrl: string | |
} |
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
type Props = { | |
video: Video | |
} | |
const Thumbnail = ({ video }: Props) => { | |
return <img src={video.coverUrl} /> | |
} |
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
const knownTypes = {} | |
export const messageFactory = { | |
registerType: (type, Component) => knownTypes[type] = Component, | |
render: (message) => { | |
const Component = knownTypes[message.type] | |
if (!Component) { | |
throw new Error('Unknown message type') |
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
export const Chat = (messages, props) => { | |
// ... | |
return ( | |
<ul> | |
{messages.map(message => { | |
const MessageComponent = messageFactory.render(message) | |
return <MessageComponent key={message.id} /> | |
})} | |
</ul> |
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
const TextMessage = ({ message }) => { | |
// ... | |
return <div>{message.text}</div> | |
} | |
messageFactory.registerType('text', TextMessage) |
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
export const Chat = (messages, props) => { | |
// ... | |
return ( | |
<ul> | |
{messages.map(message => { | |
switch (message.type) { | |
case 'text': return <TextMessage message={message} /> | |
case 'image': return <ImageMessage message={message} /> | |
case 'audio': return <AudioMessage message={message} /> |
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
const Header = () => { | |
const { pathname } = useRouter() | |
return ( | |
<header> | |
<Logo /> | |
<Actions> | |
{pathname === '/dashboard' && <Link to="/events/new">Create event</Link>} | |
{pathname === '/' && <Link to="/dashboard">Go to dashboard</Link>} | |
</Actions> |
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
const Header = ({ children }) => ( | |
<header> | |
<Logo /> | |
<Actions> | |
{children} | |
</Actions> | |
</header> | |
) | |
const HomePage = () => ( |
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
const ActiveUsersList = () => { | |
const [users, setUsers] = useState([]) | |
useEffect(() => { | |
const loadUsers = async () => { | |
const response = await fetch('/some-api') | |
const data = await response.json() | |
setUsers(data) | |
} |