added floatingMode

This commit is contained in:
SrGooglo 2022-12-21 19:15:35 +00:00
parent f6baa8ff11
commit b996266d5d
2 changed files with 128 additions and 6 deletions

View File

@ -2,6 +2,8 @@ import React from "react"
import * as antd from "antd" import * as antd from "antd"
import classnames from "classnames" import classnames from "classnames"
import { io } from "socket.io-client" import { io } from "socket.io-client"
import { TransitionGroup, CSSTransition } from "react-transition-group"
import config from "config" import config from "config"
import SessionModel from "models/session" import SessionModel from "models/session"
@ -26,9 +28,13 @@ export default class LiveChat extends React.Component {
connectionEnd: false, connectionEnd: false,
roomInfo: null, roomInfo: null,
timeline: [], timeline: [],
temporalTimeline: [],
writtedMessage: "", writtedMessage: "",
maxTemporalLines: this.props.maxTemporalLines ?? 10,
} }
debouncedIntervalTimelinePurge = null
timelineRef = React.createRef() timelineRef = React.createRef()
joinSocketRoom = async () => { joinSocketRoom = async () => {
@ -75,7 +81,6 @@ export default class LiveChat extends React.Component {
socket.on("message", (message) => { socket.on("message", (message) => {
this.pushToTimeline(message) this.pushToTimeline(message)
this.scrollTimelineToBottom()
}) })
socket.connect() socket.connect()
@ -90,8 +95,6 @@ export default class LiveChat extends React.Component {
message message
}) })
this.scrollTimelineToBottom()
// remove writted message // remove writted message
this.setState({ this.setState({
writtedMessage: "" writtedMessage: ""
@ -101,9 +104,63 @@ export default class LiveChat extends React.Component {
pushToTimeline = (message) => { pushToTimeline = (message) => {
const { timeline } = this.state const { timeline } = this.state
if (typeof message.key === "undefined") {
message.key = this.state.timeline.length
}
this.setState({ this.setState({
timeline: [...timeline, message] timeline: [...timeline, message]
}) })
if (this.props.floatingMode) {
if (this.state.temporalTimeline.length >= this.state.maxTemporalLines) {
this.setState({
temporalTimeline: this.state.temporalTimeline.slice(1)
})
}
// calculate duration based on message length (Minimum 3 second, maximum 10 seconds)
const calculatedDuration = Math.min(Math.max(message.content.length * 0.1, 3), 10) * 1000
const temporalLine = {
expireTime: Date.now() + calculatedDuration,
duration: calculatedDuration,
messageKey: message.key
}
this.setState({
temporalTimeline: [...this.state.temporalTimeline, temporalLine]
})
if (this.debouncedIntervalTimelinePurge) {
clearInterval(this.debouncedIntervalTimelinePurge)
}
this.debouncedIntervalTimelinePurge = setInterval(this.purgeLastTemporalLine, 3000)
}
this.scrollTimelineToBottom()
}
purgeLastTemporalLine = () => {
if (!this.props.floatingMode) {
return false
}
const { temporalTimeline } = this.state
if (temporalTimeline.length === 0) {
clearInterval(this.debouncedIntervalTimelinePurge)
return false
}
const lastTemporalLine = temporalTimeline[0]
if (lastTemporalLine.expireTime < Date.now()) {
this.setState({
temporalTimeline: temporalTimeline.slice(1)
})
}
} }
handleInputChange = (e) => { handleInputChange = (e) => {
@ -128,9 +185,13 @@ export default class LiveChat extends React.Component {
} }
scrollTimelineToBottom = () => { scrollTimelineToBottom = () => {
if (this.timelineRef.current) { const scrollingElement = document.getElementById("liveChat_timeline")
this.timelineRef.current.scrollTo({
top: this.timelineRef.current.scrollHeight, console.log(`Scrolling to bottom`, scrollingElement)
if (scrollingElement) {
scrollingElement.scrollTo({
top: scrollingElement.scrollHeight,
behavior: "smooth" behavior: "smooth"
}) })
} }
@ -138,10 +199,18 @@ export default class LiveChat extends React.Component {
componentDidMount = async () => { componentDidMount = async () => {
await this.joinSocketRoom() await this.joinSocketRoom()
app.ctx = {
submit: this.submitMessage
}
} }
componentWillUnmount() { componentWillUnmount() {
this.state.socket.close() this.state.socket.close()
if (this.debouncedIntervalTimelinePurge) {
clearInterval(this.debouncedIntervalTimelinePurge)
}
} }
render() { render() {
@ -161,6 +230,31 @@ export default class LiveChat extends React.Component {
</div> </div>
} }
if (this.props.floatingMode) {
return <div className="liveChat floating">
<TransitionGroup
ref={this.timelineRef}
className="liveChat_timeline"
id="liveChat_timeline"
>
{
this.state.temporalTimeline.map((line, index) => {
return <CSSTransition
key={index}
timeout={300}
classNames={{
enterActive: "transverse-enter",
exitActive: "transverse-out"
}}
>
<Line {...this.state.timeline[line.messageKey]} />
</CSSTransition>
})
}
</TransitionGroup>
</div>
}
return <div return <div
className={classnames( className={classnames(
"liveChat", "liveChat",
@ -173,6 +267,7 @@ export default class LiveChat extends React.Component {
<div <div
className="liveChat_timeline" className="liveChat_timeline"
ref={this.timelineRef} ref={this.timelineRef}
id="liveChat_timeline"
> >
{ {
this.state.timeline.map((line, index) => { this.state.timeline.map((line, index) => {

View File

@ -14,8 +14,35 @@
align-items: center; align-items: center;
} }
&.floating {
.textRoom_line {
display: relative;
background-color: rgba(var(--layoutBackgroundColor), 0.7);
backdrop-filter: blur(20px);
}
.liveChat_timeline {
display: flex;
flex-direction: column;
padding-top: 0;
overflow-x: hidden;
overflow-y: hidden;
//justify-content: flex-end;
margin-bottom: 0;
transition: all 250ms ease-in-out;
}
}
.liveChat_timeline { .liveChat_timeline {
position: relative;
height: 100%; height: 100%;
overflow-y: scroll; overflow-y: scroll;
margin-bottom: 70px; margin-bottom: 70px;