11import { Loader } from '@kinvolk/headlamp-plugin/lib/CommonComponents' ;
2+ import { useClustersConf } from '@kinvolk/headlamp-plugin/lib/k8s' ;
23import { Card } from '@mui/material' ;
34import { Typography } from '@mui/material' ;
45import Box from '@mui/material/Box' ;
@@ -60,8 +61,30 @@ export default function CommandDialog({
6061} : CommandDialogProps ) {
6162 const [ clusterName , setClusterName ] = React . useState ( initialClusterName ) ;
6263 const [ driver , setDriver ] = React . useState ( '' ) ;
64+ const [ nameTaken , setNameTaken ] = React . useState ( false ) ;
6365
6466 const history = useHistory ( ) ;
67+ const clusters = useClustersConf ( ) || { } ;
68+ const clusterNames = Object . keys ( clusters ) ;
69+
70+ React . useEffect ( ( ) => {
71+ if ( ! initialClusterName ) {
72+ setClusterName ( generateClusterName ( clusterNames ) ) ;
73+ }
74+ } , [ initialClusterName , clusterNames ] ) ;
75+
76+ function generateClusterName ( existingNames : string [ ] ) : string {
77+ const baseName = 'minikube' ;
78+ let newName = baseName ;
79+ let counter = 1 ;
80+
81+ while ( existingNames . includes ( newName ) ) {
82+ newName = `${ baseName } -${ counter } ` ;
83+ counter ++ ;
84+ }
85+
86+ return newName ;
87+ }
6588
6689 if ( acting && open && ! running ) {
6790 if ( askClusterName ) {
@@ -88,9 +111,13 @@ export default function CommandDialog({
88111 label = "Cluster Name"
89112 value = { clusterName }
90113 onChange = { function handleNameChange ( event : React . ChangeEvent < HTMLInputElement > ) {
91- setClusterName ( event . target . value ) ;
114+ const name = event . target . value ;
115+ setClusterName ( name ) ;
116+ setNameTaken ( clusterNames . includes ( name ) ) ;
92117 } }
93118 variant = "outlined"
119+ error = { nameTaken }
120+ helperText = { nameTaken ? 'Cluster name is already taken' : '' }
94121 />
95122 </ Box >
96123 </ FormControl >
@@ -123,6 +150,7 @@ export default function CommandDialog({
123150 } }
124151 variant = "contained"
125152 color = "primary"
153+ disabled = { nameTaken && askClusterName }
126154 >
127155 { `${ command } ` }
128156 </ Button >
0 commit comments