Page MenuHomeSealhub

notifications.jsx
No OneTemporary

notifications.jsx

import { render, Component } from "preact";
import { io } from "socket.io-client";
function rand_num() {
return Math.floor(Math.random() * Number.MAX_VALUE);
}
class Notifications extends Component {
constructor() {
super();
this.state = { notifications: [] };
}
remove_notification(id) {
const newNotifications = this.state.notifications.filter(
(notification) => notification.id !== id
);
this.setState({ notifications: newNotifications });
}
componentDidMount() {
// This should also be dynamic
io().on("notification", (data) => {
let new_id = rand_num();
this.setState({
notifications: [
{ id: new_id, notification: data },
...this.state.notifications,
],
});
// a 10 sec timeout
setTimeout(() => {
this.remove_notification(new_id);
}, 10000);
});
}
render() {
console.log("Render", this.state.notifications);
return this.state.notifications.map(({ id, notification }) => (
<div
onClick={() => this.remove_notification(id)}
style={`
background-color: ${notification.is_ok ? "#66ff99" : "#ff5c33"};
border-radius: 5px;
border-width: 2px;
border-style: solid;
border-color: ${notification.is_ok ? "#369648" : "#a23915"};
padding: 5px;
margin-top: 2px;
margin-bottom: 2px;
`}
>
<div>
<b>{notification.context}</b>
</div>
<div>
{notification.message.split("\n").map((line) => (
<p>{line}</p>
))}
</div>
</div>
));
}
}
render(<Notifications />, document.getElementById("notifications"));

File Metadata

Mime Type
text/x-java
Expires
Sat, Nov 8, 12:32 (11 h, 55 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1034578
Default Alt Text
notifications.jsx (1 KB)

Event Timeline