Skip to content

Commit 9b774a4

Browse files
committed
scrolling bug fixes
1 parent 62f1e59 commit 9b774a4

File tree

1 file changed

+55
-33
lines changed

1 file changed

+55
-33
lines changed

src/ChatBot.jsx

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ class ChatBot extends Component {
1212
wrapTitle: props.wrapTitle,
1313
widgetReady: false,
1414
currentPath: "",
15-
profile: appProfile
15+
profile: appProfile,
16+
lastUserText: ""
1617
};
1718
this.onPageChange = this.onPageChange.bind(this);
1819
this.onWrapperClick = this.onWrapperClick.bind(this);
1920
}
2021

2122
componentDidMount() {
22-
this.interval = setInterval(() => {
23+
const interval = setInterval(() => {
2324
if (
2425
this.props.username.status === "available" &&
2526
this.props.userid.status === "available" &&
@@ -28,7 +29,7 @@ class ChatBot extends Component {
2829
this.props.enablechatbot.status === "available"
2930
) {
3031
if (!this.isBotEnabled()) {
31-
clearInterval(this.interval);
32+
clearInterval(interval);
3233
return;
3334
}
3435
var pageContext = this.translateContext(window.mx.ui.getContentForm().path);
@@ -50,7 +51,7 @@ class ChatBot extends Component {
5051
.querySelector(".mx-groupbox-header")
5152
.addEventListener("click", this.onWrapperClick);
5253
}
53-
clearInterval(this.interval);
54+
clearInterval(interval);
5455
}
5556
}, 50);
5657
}
@@ -88,40 +89,49 @@ class ChatBot extends Component {
8889
};
8990

9091
scrollIntoLastMessage() {
92+
//if your message is the last one, wait else - scroll
9193
var message;
92-
var messages = document.querySelector("ul[role='list']").children;
93-
for (var i = messages.length - 2; i >= 0; i--) {
94-
if (messages[i].children[0].querySelector(".from-user") !== null && messages[i + 1] !== undefined) {
95-
message = messages[i + 1];
96-
break;
97-
}
98-
}
99-
if (message === undefined) {
100-
message = document.querySelector("ul[role='list']").lastChild;
94+
var messages;
95+
if (this.state.lastUserText === "") {
96+
document.querySelector("ul[role='list']").lastChild.scrollIntoView({ behavior: "smooth", block: "start" });
97+
return;
10198
}
102-
message.scrollIntoView({ behavior: "smooth", block: "start" });
99+
const scrollInterval = setInterval(() => {
100+
messages = document.querySelector("ul[role='list']").children;
101+
if (messages[messages.length - 1].children[0].querySelector(".from-user") === null) {
102+
for (var i = messages.length - 2; i >= 0; i--) {
103+
if (
104+
messages[i].lastChild.children[0].children[0].children[1].children[0].innerText ===
105+
this.state.lastUserText
106+
) {
107+
message = messages[i + 1];
108+
break;
109+
}
110+
}
111+
}
112+
if (message !== undefined) {
113+
message.scrollIntoView({ behavior: "smooth", block: "start" });
114+
clearInterval(scrollInterval);
115+
}
116+
}, 50);
103117
}
104118

105119
onWrapperClick() {
106-
var exec = false;
107120
var maxAttempts = 10;
108121
var attempts = 0;
109-
this.interval = setInterval(() => {
110-
// try executing scroll action every 50 ms for 500 ms
111-
if (exec === false) {
112-
try {
113-
this.scrollIntoLastMessage();
114-
exec = true;
115-
} catch (error) {
116-
if (attempts === maxAttempts) {
117-
console.error(
118-
"Reached maximum number of attempts to execute on click action. Task has been terminated."
119-
);
120-
console.error(error);
121-
exec = true;
122-
} else {
123-
attempts++;
124-
}
122+
const wrapperInterval = setInterval(() => {
123+
try {
124+
this.scrollIntoLastMessage();
125+
clearInterval(wrapperInterval);
126+
} catch (error) {
127+
if (attempts === maxAttempts) {
128+
console.error(
129+
"Reached maximum number of attempts to execute on click action. Task has been terminated."
130+
);
131+
console.error(error);
132+
clearInterval(wrapperInterval);
133+
} else {
134+
attempts++;
125135
}
126136
}
127137
}, 50);
@@ -159,9 +169,21 @@ class ChatBot extends Component {
159169
}
160170
}
161171
});
162-
} else if (action.type === "DIRECT_LINE/POST_ACTIVITY_FULFILLED") {
172+
} else if (
173+
action.type === "DIRECT_LINE/INCOMING_ACTIVITY" &&
174+
action.payload.activity.from.role === "user" &&
175+
action.payload.activity.type === "message"
176+
) {
177+
this.setState({
178+
lastUserText: action.payload.activity.text
179+
});
180+
} else if (
181+
action.type === "DIRECT_LINE/INCOMING_ACTIVITY" &&
182+
action.payload.activity.from.role === "bot" &&
183+
action.payload.activity.type === "message"
184+
) {
163185
try {
164-
setTimeout(this.scrollIntoLastMessage(), 100);
186+
this.scrollIntoLastMessage();
165187
} catch (error) {
166188
//Could not scroll into message - conversation box hidden.
167189
}

0 commit comments

Comments
 (0)