1
- import React , { useState } from 'react' ;
2
- import { Button , Dialog } from '@material-ui/core' ;
1
+ import React , { useEffect , useState } from 'react' ;
2
+ import { Button , Dialog , DialogTitle , FormControl , Input , InputLabel , ListSubheader , MenuItem , Select } from '@material-ui/core' ;
3
3
4
- import { UUID } from '../types' ;
4
+ import { Repository , UUID } from '../types' ;
5
+ import { useSelector } from 'react-redux' ;
6
+ import { RootState } from '../store/root' ;
7
+ import { useBranchStatus } from '../store/hooks/useBranchStatus' ;
5
8
6
9
type DialogProps = {
7
10
open : boolean ;
8
- options : string [ ] ;
11
+ repos : Repository [ ] ;
9
12
onClose : ( canceled : boolean , selected : [ UUID , UUID ] ) => void ;
10
13
}
11
14
12
15
export const MergePickerDialog : React . FunctionComponent < DialogProps > = props => {
13
- const [ selectedLeft ] = useState ( '' ) ;
14
- const [ selectedRight ] = useState ( '' ) ;
16
+ const [ base , setBase ] = useState < { repo : UUID , branch : string } > ( { repo : '' , branch : '' } ) ;
17
+ const [ compare , setCompare ] = useState < { repo : UUID , branch : string } > ( { repo : '' , branch : '' } ) ;
18
+ const baseStatus = useBranchStatus ( base . repo , base . branch ) ;
19
+ const compareStatus = useBranchStatus ( compare . repo , compare . branch ) ;
20
+
21
+ useEffect ( ( ) => {
22
+ if ( base . repo !== '' && compare . repo !== '' ) console . log ( `ready to analyze...` ) ;
23
+ } , [ base , compare ] ) ;
24
+
25
+ const setSelected = ( target : 'base' | 'compare' , value : string ) => {
26
+ const repoBranch = { repo : value . split ( '/' ) [ 0 ] , branch : value . split ( '/' ) [ 1 ] } ;
27
+ ( target === 'base' ) ? setBase ( repoBranch ) : setCompare ( repoBranch ) ;
28
+ } ;
29
+
30
+ const analyze = ( ) => {
31
+ console . log ( `base: ${ base ?. repo } /${ base ?. branch } (cards: ${ baseStatus . cards . length } , modified: ${ baseStatus . modified . length } )` ) ;
32
+ console . log ( `compare: ${ compare ?. repo } /${ compare ?. branch } (cards: ${ compareStatus . cards . length } , modified: ${ compareStatus . modified . length } )` ) ;
33
+ } ;
15
34
16
35
return (
17
36
< Dialog id = 'picker-dialog' open = { props . open } onClose = { ( ) => props . onClose ( false , [ '' , '' ] ) } >
18
- < div > selectLeft: { selectedLeft } </ div >
19
- < div > selectRight: { selectedRight } </ div >
37
+ < div className = 'diff-container' >
38
+ < DialogTitle style = { { gridArea : 'header' } } > Select branches to compare</ DialogTitle >
39
+ < FormControl aria-label = 'Left Form' style = { { gridArea : 'left' , width : 100 } } >
40
+ < InputLabel htmlFor = 'merge-select-left' id = 'merge-select-left-label' > Base</ InputLabel >
41
+ < Select id = 'merge-select-left' aria-labelledby = 'merge-select-left-label' value = { base . repo !== '' ? `${ base ?. repo } /${ base ?. branch } ` : '' }
42
+ autoWidth = { true } onChange = { ( e ) => setSelected ( 'base' , e . target . value as string ) } input = { < Input /> } >
43
+ < MenuItem value = '' > < em > None</ em > </ MenuItem >
44
+ { props . repos . map ( repo => [
45
+ < ListSubheader key = { `${ repo . id } -subheader` } > { repo . name } </ ListSubheader > ,
46
+ ...( repo . local . map ( branch => < MenuItem key = { `${ repo . id } /${ branch } ` } value = { `${ repo . id } /${ branch } ` } > { branch } </ MenuItem > ) )
47
+ ] ) }
48
+ </ Select >
49
+ </ FormControl >
50
+ < FormControl aria-label = 'Right Form' style = { { gridArea : 'right' , width : 100 } } >
51
+ < InputLabel htmlFor = 'merge-select-right' id = 'merge-select-right-label' > Compare</ InputLabel >
52
+ < Select id = 'merge-select-right' aria-labelledby = 'merge-select-right-label' value = { compare . repo !== '' ? `${ compare ?. repo } /${ compare ?. branch } ` : '' }
53
+ autoWidth = { true } onChange = { ( e ) => setSelected ( 'compare' , e . target . value as string ) } input = { < Input /> } >
54
+ < MenuItem value = '' > < em > None</ em > </ MenuItem >
55
+ { props . repos . map ( repo => [
56
+ < ListSubheader key = { `${ repo . id } -subheader` } > { repo . name } </ ListSubheader > ,
57
+ ...( repo . local . map ( branch => < MenuItem key = { `${ repo . id } /${ branch } ` } value = { `${ repo . id } /${ branch } ` } > { branch } </ MenuItem > ) )
58
+ ] ) }
59
+ </ Select >
60
+ </ FormControl >
61
+ < button onClick = { analyze } > View Selected...</ button >
62
+ </ div >
20
63
</ Dialog >
21
64
) ;
22
65
}
23
66
24
67
const MergePickerButton : React . FunctionComponent = ( ) => {
25
68
const [ open , setOpen ] = useState ( false ) ;
69
+ const repos = useSelector ( ( state : RootState ) => Object . values ( state . repos ) ) ;
26
70
27
71
const handleClose = ( ) => {
28
72
setOpen ( ! open ) ;
@@ -31,7 +75,7 @@ const MergePickerButton: React.FunctionComponent = () => {
31
75
return (
32
76
< >
33
77
< Button id = 'diffpicker-button' variant = 'contained' color = 'primary' onClick = { ( ) => setOpen ( ! open ) } > Merge Branches...</ Button >
34
- < MergePickerDialog open = { open } options = { [ 'stuff' ] } onClose = { handleClose } />
78
+ { open ? < MergePickerDialog open = { open } repos = { repos } onClose = { handleClose } /> : null }
35
79
</ >
36
80
) ;
37
81
}
0 commit comments