@@ -23,6 +23,7 @@ import {
2323} from '@postgres.ai/shared/helpers/getEntropy'
2424
2525import { Snapshot } from '@postgres.ai/shared/types/api/entities/snapshot'
26+ import { Branch } from '@postgres.ai/shared/types/api/endpoints/getBranches'
2627import { useCreatedStores , MainStoreApi } from './useCreatedStores'
2728import { useForm , FormValues } from './useForm'
2829import { getCliCloneStatus , getCliCreateCloneCommand } from './utils'
@@ -48,9 +49,10 @@ export const CreateClone = observer((props: Props) => {
4849 const history = useHistory ( )
4950 const stores = useCreatedStores ( props . api )
5051 const timer = useTimer ( )
51- const [ branchesList , setBranchesList ] = useState < string [ ] > ( [ ] )
52+ const [ branchesList , setBranchesList ] = useState < Branch [ ] > ( [ ] )
5253 const [ snapshots , setSnapshots ] = useState ( [ ] as Snapshot [ ] )
5354 const [ isLoadingSnapshots , setIsLoadingSnapshots ] = useState ( false )
55+ const [ selectedBranchKey , setSelectedBranchKey ] = useState < string > ( '' )
5456
5557 // Form.
5658 const onSubmit = async ( values : FormValues ) => {
@@ -74,8 +76,8 @@ export const CreateClone = observer((props: Props) => {
7476 }
7577 }
7678
77- const fetchBranchSnapshotsData = async ( branchName : string , initialSnapshotId ?: string ) => {
78- const snapshotsRes = ( await stores . main . getSnapshots ( props . instanceId , branchName ) ) ?? [ ]
79+ const fetchBranchSnapshotsData = async ( branchName : string , dataset ?: string , initialSnapshotId ?: string ) => {
80+ const snapshotsRes = ( await stores . main . getSnapshots ( props . instanceId , branchName , dataset ) ) ?? [ ]
7981 setSnapshots ( snapshotsRes )
8082
8183 const selectedSnapshot = snapshotsRes . find ( s => s . id === initialSnapshotId ) || snapshotsRes [ 0 ]
@@ -86,11 +88,15 @@ export const CreateClone = observer((props: Props) => {
8688 const handleSelectBranch = async (
8789 e : React . ChangeEvent < { value : string } > ,
8890 ) => {
89- const selectedBranch = e . target . value
90- formik . setFieldValue ( 'branch' , selectedBranch )
91+ const compositeKey = e . target . value
92+ const [ branchName , dataset ] = compositeKey . split ( '|' )
93+
94+ setSelectedBranchKey ( compositeKey )
95+ formik . setFieldValue ( 'branch' , branchName )
96+ formik . setFieldValue ( 'dataset' , dataset )
9197
9298 if ( props . api . getSnapshots ) {
93- await fetchBranchSnapshotsData ( selectedBranch )
99+ await fetchBranchSnapshotsData ( branchName , dataset )
94100 }
95101 }
96102
@@ -103,18 +109,27 @@ export const CreateClone = observer((props: Props) => {
103109
104110 const branches = ( await stores . main . getBranches ( props . instanceId ) ) ?? [ ]
105111
106- let initiallySelectedBranch = branches [ 0 ] ?. name ;
112+ let initiallySelectedBranch = branches [ 0 ] ;
107113
108- if ( initialBranch && branches . find ( ( branch ) => branch . name === initialBranch ) ) {
109- initiallySelectedBranch = initialBranch ;
114+ if ( initialBranch ) {
115+ const foundBranch = branches . find ( ( branch ) => branch . name === initialBranch )
116+ if ( foundBranch ) {
117+ initiallySelectedBranch = foundBranch
118+ }
110119 }
111120
112- setBranchesList ( branches . map ( ( branch ) => branch . name ) )
113- formik . setFieldValue ( 'branch' , initiallySelectedBranch )
121+ setBranchesList ( branches )
122+ formik . setFieldValue ( 'branch' , initiallySelectedBranch ?. name ?? '' )
123+ formik . setFieldValue ( 'dataset' , initiallySelectedBranch ?. dataset ?? '' )
124+
125+ if ( initiallySelectedBranch ) {
126+ const compositeKey = `${ initiallySelectedBranch . name } |${ initiallySelectedBranch . dataset } `
127+ setSelectedBranchKey ( compositeKey )
128+ }
114129
115- if ( props . api . getSnapshots ) {
116- await fetchBranchSnapshotsData ( initiallySelectedBranch , initialSnapshotId )
117- } else {
130+ if ( props . api . getSnapshots && initiallySelectedBranch ) {
131+ await fetchBranchSnapshotsData ( initiallySelectedBranch . name , initiallySelectedBranch . dataset , initialSnapshotId )
132+ } else if ( ! props . api . getSnapshots ) {
118133 const allSnapshots = stores . main ?. snapshots ?. data ?? [ ]
119134 const sortedSnapshots = allSnapshots . slice ( ) . sort ( compareSnapshotsDesc )
120135 setSnapshots ( sortedSnapshots )
@@ -212,15 +227,15 @@ export const CreateClone = observer((props: Props) => {
212227 < Select
213228 fullWidth
214229 label = "Branch"
215- value = { formik . values . branch }
230+ value = { selectedBranchKey }
216231 disabled = { ! branchesList || isCreatingClone }
217232 onChange = { handleSelectBranch }
218233 error = { Boolean ( formik . errors . branch ) }
219234 items = {
220- branchesList ?. map ( ( snapshot ) => {
235+ branchesList ?. map ( ( branch ) => {
221236 return {
222- value : snapshot ,
223- children : snapshot ,
237+ value : ` ${ branch . name } | ${ branch . dataset } ` ,
238+ children : ` ${ branch . name } ( ${ branch . dataset } )` ,
224239 }
225240 } ) ?? [ ]
226241 }
0 commit comments