Skip to content

Commit 5760b65

Browse files
committed
add use-Auto-Scroll.tsx in common/hooks and implement in student.component.tsx and session.component.tsx
1 parent ec27ee4 commit 5760b65

3 files changed

Lines changed: 24 additions & 20 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
3+
export const useAutoScroll = () => {
4+
const [isAutoScrollEnabled, setIsAutoScrollEnabled] = React.useState(true);
5+
const textAreaRef = React.useRef(null);
6+
7+
const doAutoScroll = () => {
8+
if(isAutoScrollEnabled && textAreaRef.current) {
9+
textAreaRef.current.scrollTop = textAreaRef.current.scrollHeight;
10+
}
11+
}
12+
13+
return {isAutoScrollEnabled, setIsAutoScrollEnabled, textAreaRef, doAutoScroll}
14+
}

‎front/src/pods/student/student.component.tsx‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Checkbox from '@material-ui/core/Checkbox';
66
import FormControlLabel from '@material-ui/core/FormControlLabel';
77

88
import * as innerClasses from './student.styles';
9+
import { useAutoScroll } from '../../common/hooks/use-Auto-Scroll';
910

1011
interface Props {
1112
room: string;
@@ -15,24 +16,17 @@ interface Props {
1516
export const StudentComponent: React.FC<Props> = props => {
1617
const { room, log } = props;
1718

18-
const [isAutoScrollEnabled, setAutoScrollEnabled] = React.useState(true);
19-
20-
const textAreaRef = React.useRef(null);
19+
const {isAutoScrollEnabled, setIsAutoScrollEnabled, textAreaRef, doAutoScroll} = useAutoScroll();
2120

2221
React.useEffect(() => {
23-
if (isAutoScrollEnabled && textAreaRef.current) {
24-
textAreaRef.current.scrollTop = textAreaRef.current.scrollHeight;
25-
}
22+
doAutoScroll();
2623
}, [log]);
2724

2825
return (
26+
2927
<>
3028
<main className={innerClasses.root}>
31-
<Typography
32-
className={innerClasses.sessionName}
33-
variant="body1"
34-
role="heading"
35-
>
29+
<Typography className={innerClasses.sessionName} variant="body1" role="heading">
3630
Session name: {room ?? ''}
3731
</Typography>
3832
<label className={innerClasses.label} htmlFor="session">
@@ -51,7 +45,7 @@ export const StudentComponent: React.FC<Props> = props => {
5145
control={
5246
<Checkbox
5347
checked={isAutoScrollEnabled}
54-
onChange={e => setAutoScrollEnabled(e.target.checked)}
48+
onChange={e => setIsAutoScrollEnabled(e.target.checked)}
5549
color="primary"
5650
/>
5751
}

‎front/src/pods/trainer/components/session.component.tsx‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import FormControlLabel from '@material-ui/core/FormControlLabel';
99
import Checkbox from '@material-ui/core/Checkbox';
1010

1111
import * as innerClasses from './session.styles';
12-
12+
import { useAutoScroll } from '../../../common/hooks/use-Auto-Scroll';
1313
interface Props {
1414
log: string;
1515
handleSendFullContentLog: (fullContent: string) => void;
@@ -34,15 +34,11 @@ const getFullContent = (currenSessionContent: string) => {
3434
export const SessionComponent: React.FC<Props> = props => {
3535
const { log, handleSendFullContentLog, className } = props;
3636

37-
const [isAutoScrollEnabled, setAutoScrollEnabled] = React.useState(true);
38-
39-
const textAreaRef = React.useRef(null);
37+
const {isAutoScrollEnabled, setIsAutoScrollEnabled, textAreaRef, doAutoScroll} = useAutoScroll();
4038

4139
React.useEffect(() => {
4240
handleSetSessionContent(log);
43-
if (isAutoScrollEnabled && textAreaRef.current) {
44-
textAreaRef.current.scrollTop = textAreaRef.current.scrollHeight;
45-
}
41+
doAutoScroll();
4642
}, [log]);
4743

4844
return (
@@ -63,7 +59,7 @@ export const SessionComponent: React.FC<Props> = props => {
6359
control={
6460
<Checkbox
6561
checked={isAutoScrollEnabled}
66-
onChange={e => setAutoScrollEnabled(e.target.checked)}
62+
onChange={e => setIsAutoScrollEnabled(e.target.checked)}
6763
color="primary"
6864
/>
6965
}

0 commit comments

Comments
 (0)