1
- import { emptyTransition , Transition } from '@atlassianlabs/jira-pi-common-models' ;
2
1
import { Box , Button , CircularProgress , Typography } from '@material-ui/core' ;
3
- import React , { useCallback , useContext , useEffect , useState } from 'react' ;
2
+ import React from 'react' ;
4
3
import { AnalyticsView } from 'src/analyticsTypes' ;
5
4
6
- import { StartWorkActionType } from '../../../../lib/ipc/fromUI/startWork' ;
7
- import { RepoData } from '../../../../lib/ipc/toUI/startWork' ;
8
- import { Branch } from '../../../../typings/git' ;
9
5
import { AtlascodeErrorBoundary } from '../../common/ErrorBoundary' ;
10
- import { ErrorControllerContext } from '../../common/errorController' ;
11
6
import { ErrorDisplay } from '../../common/ErrorDisplay' ;
12
7
import { StartWorkControllerContext , useStartWorkController } from '../startWorkController' ;
13
8
import {
@@ -17,178 +12,22 @@ import {
17
12
TaskInfoSection ,
18
13
UpdateStatusSection ,
19
14
} from './components' ;
20
- import { generateBranchName , getDefaultSourceBranch } from './utils/branchUtils ' ;
15
+ import { useStartWorkFormState } from './hooks/useStartWorkFormState ' ;
21
16
22
17
const StartWorkPageV3 : React . FunctionComponent = ( ) => {
23
18
const [ state , controller ] = useStartWorkController ( ) ;
24
- const errorController = useContext ( ErrorControllerContext ) ;
25
- const [ pushBranchEnabled , setPushBranchEnabled ] = useState ( true ) ;
26
- const [ localBranch , setLocalBranch ] = useState ( '' ) ;
27
- const [ sourceBranch , setSourceBranch ] = useState < Branch > ( { type : 0 , name : '' } ) ;
28
- const [ selectedRepository , setSelectedRepository ] = useState < RepoData | undefined > ( state . repoData [ 0 ] ) ;
29
- const [ selectedBranchType , setSelectedBranchType ] = useState < { kind : string ; prefix : string } > ( {
30
- kind : '' ,
31
- prefix : '' ,
32
- } ) ;
33
- const [ upstream , setUpstream ] = useState ( '' ) ;
34
- const [ transitionIssueEnabled , setTransitionIssueEnabled ] = useState ( true ) ;
35
- const [ selectedTransition , setSelectedTransition ] = useState < Transition > ( emptyTransition ) ;
36
- const [ branchSetupEnabled , setBranchSetupEnabled ] = useState ( true ) ;
37
- const [ submitState , setSubmitState ] = useState < 'initial' | 'submitting' | 'submit-success' > ( 'initial' ) ;
38
- const [ submitResponse , setSubmitResponse ] = useState < {
39
- transistionStatus ?: string ;
40
- branch ?: string ;
41
- upstream ?: string ;
42
- } > ( { } ) ;
43
- const [ snackbarOpen , setSnackbarOpen ] = useState ( false ) ;
44
19
45
- // Initialize form with default values
46
- useEffect ( ( ) => {
47
- if ( state . repoData . length > 0 ) {
48
- const defaultRepo = state . repoData [ 0 ] ;
49
- setSelectedRepository ( defaultRepo ) ;
50
- const defaultSourceBranch = getDefaultSourceBranch ( defaultRepo ) ;
51
- setSourceBranch ( defaultSourceBranch ) ;
52
-
53
- // Set default branch type
54
- if ( defaultRepo . branchTypes && defaultRepo . branchTypes . length > 0 ) {
55
- setSelectedBranchType ( defaultRepo . branchTypes [ 0 ] ) ;
56
- }
57
-
58
- if ( ! upstream ) {
59
- setUpstream ( defaultRepo . workspaceRepo . mainSiteRemote . remote . name ) ;
60
- }
61
- }
62
- } , [ state . repoData , upstream ] ) ;
63
-
64
- // Generate branch name when dependencies change
65
- useEffect ( ( ) => {
66
- if ( selectedRepository && selectedBranchType . prefix ) {
67
- const generatedName = generateBranchName (
68
- selectedRepository ,
69
- selectedBranchType ,
70
- state . issue ,
71
- state . customTemplate ,
72
- ) ;
73
- setLocalBranch ( generatedName ) ;
74
- }
75
- } , [ selectedRepository , selectedBranchType , state . issue , state . customTemplate ] ) ;
76
-
77
- const handleRepositoryChange = useCallback (
78
- ( repository : RepoData ) => {
79
- setSelectedRepository ( repository ) ;
80
- const defaultSourceBranch = getDefaultSourceBranch ( repository ) ;
81
- setSourceBranch ( defaultSourceBranch ) ;
82
-
83
- if ( repository . branchTypes && repository . branchTypes . length > 0 ) {
84
- setSelectedBranchType ( repository . branchTypes [ 0 ] ) ;
85
- }
86
-
87
- if ( ! upstream ) {
88
- setUpstream ( repository . workspaceRepo . mainSiteRemote . remote . name ) ;
89
- }
90
- } ,
91
- [ upstream ] ,
92
- ) ;
93
-
94
- const handleBranchTypeChange = useCallback ( ( branchType : { kind : string ; prefix : string } ) => {
95
- setSelectedBranchType ( branchType ) ;
96
- } , [ ] ) ;
97
-
98
- const handleUpstreamChange = useCallback ( ( newUpstream : string ) => {
99
- setUpstream ( newUpstream ) ;
100
- } , [ ] ) ;
101
-
102
- const handleTransitionIssueEnabledChange = useCallback ( ( enabled : boolean ) => {
103
- setTransitionIssueEnabled ( enabled ) ;
104
- } , [ ] ) ;
105
-
106
- const handleSelectedTransitionChange = useCallback ( ( transition : Transition ) => {
107
- setSelectedTransition ( transition ) ;
108
- } , [ ] ) ;
109
-
110
- const handleBranchSetupEnabledChange = useCallback ( ( enabled : boolean ) => {
111
- setBranchSetupEnabled ( enabled ) ;
112
- } , [ ] ) ;
113
-
114
- const handleSnackbarClose = useCallback ( ( ) => {
115
- setSnackbarOpen ( false ) ;
116
- } , [ ] ) ;
117
-
118
- const handleCreateBranch = useCallback ( async ( ) => {
119
- setSubmitState ( 'submitting' ) ;
120
-
121
- try {
122
- if ( ! selectedRepository ) {
123
- throw new Error ( 'No repository selected' ) ;
124
- }
125
-
126
- const response = await controller . startWork (
127
- transitionIssueEnabled ,
128
- selectedTransition ,
129
- branchSetupEnabled ,
130
- selectedRepository . workspaceRepo ,
131
- sourceBranch ,
132
- localBranch ,
133
- upstream ,
134
- pushBranchEnabled ,
135
- ) ;
136
-
137
- // Send message to refresh tree views after successful start work
138
- controller . postMessage ( {
139
- type : StartWorkActionType . RefreshTreeViews ,
140
- } ) ;
141
-
142
- setSubmitResponse ( response ) ;
143
- setSubmitState ( 'submit-success' ) ;
144
- setSnackbarOpen ( true ) ;
145
- } catch ( error ) {
146
- console . error ( 'Error creating branch:' , error ) ;
147
- errorController . showError ( error ) ;
148
- setSubmitState ( 'initial' ) ;
149
- }
150
- } , [
151
- selectedRepository ,
152
- controller ,
153
- transitionIssueEnabled ,
154
- selectedTransition ,
155
- branchSetupEnabled ,
156
- sourceBranch ,
157
- localBranch ,
158
- upstream ,
159
- pushBranchEnabled ,
160
- errorController ,
161
- ] ) ;
162
-
163
- const formState = {
164
- pushBranchEnabled,
165
- localBranch,
166
- sourceBranch,
167
- selectedRepository,
168
- selectedBranchType,
169
- upstream,
170
- branchSetupEnabled,
171
- } ;
172
-
173
- const formActions = {
174
- onPushBranchChange : setPushBranchEnabled ,
175
- onLocalBranchChange : setLocalBranch ,
176
- onSourceBranchChange : setSourceBranch ,
177
- onRepositoryChange : handleRepositoryChange ,
178
- onBranchTypeChange : handleBranchTypeChange ,
179
- onUpstreamChange : handleUpstreamChange ,
180
- onBranchSetupEnabledChange : handleBranchSetupEnabledChange ,
181
- } ;
182
-
183
- const updateStatusFormState = {
184
- transitionIssueEnabled,
185
- selectedTransition,
186
- } ;
187
-
188
- const updateStatusFormActions = {
189
- onTransitionIssueEnabledChange : handleTransitionIssueEnabledChange ,
190
- onSelectedTransitionChange : handleSelectedTransitionChange ,
191
- } ;
20
+ const {
21
+ formState,
22
+ formActions,
23
+ updateStatusFormState,
24
+ updateStatusFormActions,
25
+ handleCreateBranch,
26
+ handleSnackbarClose,
27
+ submitState,
28
+ submitResponse,
29
+ snackbarOpen,
30
+ } = useStartWorkFormState ( state , controller ) ;
192
31
193
32
return (
194
33
< StartWorkControllerContext . Provider value = { controller } >
0 commit comments