mirror of
https://github.com/He4eT/elseifplayer.git
synced 2026-05-05 01:17:22 +00:00
Add TextBuffer component
This commit is contained in:
parent
a5c6205bc0
commit
95c8ae8780
3 changed files with 91 additions and 20 deletions
55
src/components/player/TextBuffer.js
Normal file
55
src/components/player/TextBuffer.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import { h } from 'preact'
|
||||
import { useState, useEffect } from 'preact/hooks'
|
||||
|
||||
const parseInbox = (inbox, currentWindow) => {
|
||||
const currentInbox =
|
||||
inbox.find(({id}) =>
|
||||
id === currentWindow.id)
|
||||
|
||||
if (!currentInbox) return {
|
||||
clear: false,
|
||||
incoming: []}
|
||||
|
||||
const {clear, text: inboxMessagesRaw} =
|
||||
currentInbox
|
||||
|
||||
const incoming =
|
||||
inboxMessagesRaw
|
||||
/* Normalize. */
|
||||
.map(({content}) =>
|
||||
content || [{ style: 'emptyLine' }])
|
||||
/* Flatten. */
|
||||
.reduce((acc, x) =>
|
||||
acc.concat(x), [])
|
||||
/* Collapse empty lines. */
|
||||
.reduce((acc, x, i, xs) => {
|
||||
if (x.style !== 'emptyLine') return [...acc, x]
|
||||
|
||||
const prev = xs[i - 1] || {}
|
||||
return prev.style === 'emptyLine'
|
||||
? acc
|
||||
: [...acc, x]
|
||||
}, [])
|
||||
|
||||
return {clear, incoming}
|
||||
}
|
||||
|
||||
export default function ({ inbox, currentWindow }) {
|
||||
const [messages, setMessages] = useState([])
|
||||
|
||||
useEffect(() => {
|
||||
const {incoming, clear} =
|
||||
parseInbox(inbox, currentWindow)
|
||||
|
||||
setMessages(clear
|
||||
? incoming
|
||||
: messages.concat(incoming))
|
||||
}, [inbox])
|
||||
|
||||
return (
|
||||
<div>
|
||||
{messages?.map(({text}) =>
|
||||
(<div>{text}</div>))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue