mirror of
https://github.com/He4eT/elseifplayer.git
synced 2026-05-05 01:17:22 +00:00
Rearrange components
This commit is contained in:
parent
e44831db08
commit
90f630f277
13 changed files with 10 additions and 8 deletions
|
|
@ -1,76 +0,0 @@
|
|||
import { h } from 'preact'
|
||||
import { useEffect, useRef, useState } from 'preact/hooks'
|
||||
|
||||
import TextMessage from './TextMessage'
|
||||
|
||||
const trimImputPrompt = messages =>
|
||||
messages.length < 1
|
||||
? messages
|
||||
: messages.slice(-1)[0].text === '>'
|
||||
? messages.slice(0, messages.length - 1)
|
||||
: messages
|
||||
|
||||
const parseInbox = (inbox, currentWindow) => {
|
||||
const currentInbox =
|
||||
inbox.find(({ id }) =>
|
||||
id === currentWindow.id)
|
||||
|
||||
if (!currentInbox) {
|
||||
return {
|
||||
clear: false,
|
||||
incoming: []
|
||||
}
|
||||
}
|
||||
|
||||
const { clear, text: inboxMessagesRaw } =
|
||||
currentInbox
|
||||
|
||||
const eol = { style: 'endOfLine' }
|
||||
|
||||
const incoming =
|
||||
inboxMessagesRaw
|
||||
/* Normalize. */
|
||||
.map(({ content }) =>
|
||||
content
|
||||
? [...trimImputPrompt(content), eol]
|
||||
: [eol])
|
||||
/* Flatten. */
|
||||
.reduce((acc, x) =>
|
||||
acc.concat(x), [])
|
||||
|
||||
return { clear, incoming }
|
||||
}
|
||||
|
||||
export default function ({ inbox, currentWindow }) {
|
||||
const [messages, setMessages] = useState([])
|
||||
const textBufferEl = useRef(null)
|
||||
|
||||
useEffect(() => {
|
||||
const { incoming, clear } =
|
||||
parseInbox(inbox, currentWindow)
|
||||
|
||||
setMessages(clear
|
||||
? incoming
|
||||
: messages.concat(incoming))
|
||||
|
||||
setTimeout(() => {
|
||||
const inputs =
|
||||
textBufferEl.current.querySelectorAll('.message.input')
|
||||
const lastInput =
|
||||
inputs[inputs.length - 1]
|
||||
|
||||
textBufferEl.current.scrollTop =
|
||||
lastInput
|
||||
? lastInput.offsetTop
|
||||
: textBufferEl.current.scrollHeight * 2
|
||||
}, 0)
|
||||
}, [inbox])
|
||||
|
||||
return (
|
||||
<section
|
||||
ref={textBufferEl}
|
||||
className='textBuffer'>
|
||||
{messages.map(TextMessage)}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue