import React, { useState } from 'react' import styled from 'styled-components' import { authToken } from '../../helpers/authentication' import MessageProgress from './MessageProgress' import MessagePlain from './Message' import SubscriptionsHook, { Message } from './SubscriptionsHook' import { NotificationType } from '../../__generated__/globalTypes' const Container = styled.div` position: fixed; bottom: 20px; right: 20px; /* Centered within the 380px sidebar: 20px inset on both left and right */ width: 340px; /* Keep notifications above the sidebar (z-40) and modals (z-40) */ z-index: 50; @media (max-width: 1000px) { display: none; } ` type MessageStateType = { set: React.Dispatch> get: Message[] add(message: Message): void removeKey(key: string): void } export const MessageState: MessageStateType = { set: fn => { console.warn('set function is not defined yet, called with', fn) }, get: [], add: message => { MessageState.set(messages => { const newMessages = messages.filter(msg => msg.key != message.key) newMessages.push(message) return newMessages }) }, removeKey: key => { MessageState.set(messages => { const newMessages = messages.filter(msg => msg.key != key) return newMessages }) }, } const Messages = () => { const [messages, setMessages] = useState([]) MessageState.set = setMessages MessageState.get = messages const getMessageElement = (message: Message): React.FunctionComponent => { const dismissMessage = (message: Message) => { message.onDismiss && message.onDismiss() setMessages(messages => messages.filter(msg => msg.key != message.key)) } switch (message.type) { case NotificationType.Message: return props => ( { dismissMessage(message) }} {...message.props} {...props} /> ) case NotificationType.Progress: return props => ( { dismissMessage(message) }} {...message.props} {...props} /> ) default: throw new Error(`Invalid message type: ${message.type}`) } } // const transitions = useTransition(messages.slice().reverse(), x => x.key, { // from: { // opacity: 0, // height: '0px', // }, // enter: { // opacity: 1, // height: `100px`, // }, // leave: { opacity: 0, height: '0px' }, // }) const messageElems = messages.map(msg => { const Elem = getMessageElement(msg) return (
) }) return ( {messageElems} {authToken() && ( )} ) } export default Messages