import { h, render, Component } from 'preact'; import setupPusher from './pusher'; class UnopenedChannelNotice extends Component { constructor(props) { super(props); const unopenedChannels = this.props.unopenedChannels; const visible = unopenedChannels.length > 0 ? true : false; this.state = { visible: visible, unopenedChannels } } componentDidMount() { setupPusher(this.props.pusherKey, { channelId: `private-message-notifications-${window.currentUser.id}`, messageCreated: this.receiveNewMessage, }); } receiveNewMessage = e => { if (location.pathname.startsWith("/💌") || location.pathname.startsWith("/%F0%9F%92%8C")) { return } let channels = this.state.unopenedChannels; const newObj = {adjusted_slug: e.chat_channel_adjusted_slug} if(channels.filter(obj => obj.adjusted_slug === newObj.adjusted_slug).length === 0 && newObj.adjusted_slug != `@${window.currentUser.username}`) { channels.push(newObj); } this.setState({visible: channels.length > 0, unopenedChannels: channels}) } handleClick = e => { this.setState({visible: false}) } render() { if (this.state.visible) { const channels = this.state.unopenedChannels.map(channel => { return {channel.adjusted_slug} }); return (
💌 New Message from {channels} (beta testing)
); } } } export default function getUnopenedChannels(user, successCb) { if (location.pathname.startsWith("/💌") || location.pathname.startsWith("/%F0%9F%92%8C")) { return } fetch('/chat_channels?state=unopened', { method: 'GET', credentials: 'same-origin', }) .then(response => response.json()) .then(json => { render(, document.getElementById('message-notice')); }) .catch(error => { console.log(error); }); }