1- import React from 'react' ;
1+ import { useEffect , useRef } from 'react' ;
22import { Button , Dropdown , Modal } from 'semantic-ui-react' ;
33import Loading from "./Loading" ;
44
5- export default class Copybook extends React . Component {
6- code = React . createRef ( ) ;
7- editor = React . createRef ( ) ;
8- visible = false ;
5+ export default function Copybook ( props ) {
6+ const codeEl = useRef ( null ) ;
7+ const editorEl = useRef ( null ) ;
98
10- constructor ( props ) {
11- super ( props ) ;
12- }
9+ useEffect ( ( ) => {
10+ renderPrettyPrint ( ) ;
11+ } , [ props . copybookFileContent , props . copybookVisible ] ) ;
1312
14- componentDidUpdate ( prevProps , prevState , snapshot ) {
15- if ( prevProps . copybookFileContent != this . props . copybookFileContent
16- || ( ! this . visiable && this . props . copybookVisible ) ) {
17- this . renderPrettyPrint ( ) ;
18- this . visible = true ;
19- }
13+ function handleClose ( ) {
14+ props . onCloseCopybook ( ) ;
2015 }
2116
22- componentDidMount ( ) {
23- this . renderPrettyPrint ( ) ;
24- }
25-
26- handleClose = ( ) => {
27- this . visiable = false ;
28- this . props . onCloseCopybook ( ) ;
29- }
30-
31- handleDropdownChange = ( e , { searchQuery, value } ) => {
32- if ( value != this . props . copybookSelectedFile . path ) {
33- this . props . onRequestCopybookFile (
34- this . props . copybookFileList . find ( f => f . path === value )
17+ function handleDropdownChange ( e , { searchQuery, value } ) {
18+ if ( value != props . copybookSelectedFile . path ) {
19+ props . onRequestCopybookFile (
20+ props . copybookFileList . find ( f => f . path === value )
3521 ) ;
3622 }
3723 }
3824
39- renderPrettyPrint = ( ) => {
25+ function renderPrettyPrint ( ) {
4026 setTimeout ( ( ) => {
41- if ( this . editor . current ) {
42- this . editor . current . innerHTML = '' ;
27+ if ( editorEl . current ) {
28+ editorEl . current . innerHTML = '' ;
4329 }
44- if ( this . code . current ) {
45- this . code . current . classList . remove ( 'prettyprinted' ) ;
30+ if ( codeEl . current ) {
31+ codeEl . current . classList . remove ( 'prettyprinted' ) ;
4632 setTimeout ( ( ) => PR . prettyPrint ( ) , 100 ) ;
4733 }
48- } , this . code . current ? 0 : 1000 ) ;
34+ } , codeEl . current ? 0 : 1000 ) ;
4935 }
5036
51- renderDropdownItem ( ) {
52- if ( ! this . props . copybookFileList ) {
37+ function renderDropdownItem ( ) {
38+ if ( ! props . copybookFileList ) {
5339 return null ;
5440 }
55- return this . props . copybookFileList . map ( ( file , idx ) => {
41+ return props . copybookFileList . map ( ( file , idx ) => {
5642 return {
5743 key : file . path ,
5844 value : file . path ,
@@ -61,47 +47,43 @@ export default class Copybook extends React.Component {
6147 } ) ;
6248 }
6349
64- render ( ) {
65- if ( ! this . props . copybookVisible
66- || ! this . props . copybookFileList
67- || ! this . props . copybookFileContent ) {
68- return (
69- < Modal open = { this . props . copybookVisible } onClose = { this . handleClose }
70- centered = { false } closeIcon className = 'copybook fix-modal' size = 'large' >
71- < Modal . Header >
72- < div className = 'copybook__title' > Daily Algorithm Copybook</ div >
73- </ Modal . Header >
74- < Modal . Content >
75- < Loading />
76- < pre > < code className = 'prettyprint' ref = { this . code } > </ code > </ pre >
77- </ Modal . Content >
78- </ Modal >
79- ) ;
80- }
81- const copybookSelectedFile = this . props . copybookSelectedFile ;
50+ if ( ! props . copybookVisible || ! props . copybookFileList || ! props . copybookFileContent ) {
8251 return (
83- < Modal open = { this . props . copybookVisible } onClose = { this . handleClose }
84- centered = { false } closeIcon className = 'copybook fix-modal' size = 'large' >
52+ < Modal open = { props . copybookVisible } onClose = { handleClose }
53+ centered = { false } closeIcon className = { props . className } size = 'large' >
8554 < Modal . Header >
86- < div className = 'copybook__title' > Daily Algorithm Copybook</ div >
87- < Button size = 'tiny' as = 'a' basic
88- href = { this . props . copybookSelectedFile . link }
89- target = '_blank' > View In GitHub</ Button >
90- < Dropdown
91- search
92- selection
93- onChange = { this . handleDropdownChange }
94- value = { copybookSelectedFile . path }
95- options = { this . renderDropdownItem ( ) } />
55+ < div className = 'title' > Daily Algorithm Copybook</ div >
9656 </ Modal . Header >
9757 < Modal . Content >
98- { this . props . requestingCopybook ? < Loading /> : '' }
99- < pre >
100- < code className = 'prettyprint' ref = { this . code } > { this . props . copybookFileContent . content } </ code >
101- < div className = 'copybook__editor' contentEditable = { true } ref = { this . editor } > </ div >
102- </ pre >
58+ < Loading />
59+ < pre > < code className = 'prettyprint' ref = { codeEl } > </ code > </ pre >
10360 </ Modal . Content >
10461 </ Modal >
105- )
62+ ) ;
10663 }
64+
65+ return (
66+ < Modal open = { props . copybookVisible } onClose = { handleClose }
67+ centered = { false } closeIcon className = { props . className } size = 'large' >
68+ < Modal . Header >
69+ < div className = 'title' > Daily Algorithm Copybook</ div >
70+ < Button size = 'tiny' as = 'a' basic
71+ href = { props . copybookSelectedFile . link }
72+ target = '_blank' > View In GitHub</ Button >
73+ < Dropdown
74+ search
75+ selection
76+ onChange = { handleDropdownChange }
77+ value = { props . copybookSelectedFile . path }
78+ options = { renderDropdownItem ( ) } />
79+ </ Modal . Header >
80+ < Modal . Content >
81+ { props . copybookRequesting ? < Loading /> : '' }
82+ < pre >
83+ < code className = 'prettyprint' ref = { codeEl } > { props . copybookFileContent . content } </ code >
84+ < div className = 'editor' contentEditable = { true } ref = { editorEl } > </ div >
85+ </ pre >
86+ </ Modal . Content >
87+ </ Modal >
88+ )
10789}
0 commit comments