1- import { useEffect , useMemo , useState } from 'react' ;
1+ import { useEffect , useMemo , useState , useCallback } from 'react' ;
22import { Button , Divider , Icon , Text } from '@ovhcloud/ods-react' ;
33import { useTranslation } from 'react-i18next' ;
44import { SshKeyHelper } from './sshKey/SshKeyHelper.component' ;
@@ -7,12 +7,13 @@ import { deps } from '@/deps/deps';
77import { useProjectId } from '@/hooks/project/useProjectId' ;
88import { selectSshKeys } from '../view-models/sshKeysViewModel' ;
99import AddSshKey from './sshKey/AddSshKey.component' ;
10- import { SubmitHandler } from 'react-hook-form' ;
10+ import { SubmitHandler , useFormContext , useWatch } from 'react-hook-form' ;
1111import { TAddSshKeyForm } from '../CreateInstance.schema' ;
1212import Banner from '@/components/banner/Banner.component' ;
1313import SshKeySelector , {
1414 TCustomSelectSshKeyData ,
1515} from './sshKey/SshKeySelector.component' ;
16+ import { TInstanceCreationForm } from '../CreateInstance.page' ;
1617
1718type TSshKeyProps = {
1819 microRegion : string ;
@@ -21,9 +22,10 @@ type TSshKeyProps = {
2122const SshKey = ( { microRegion } : TSshKeyProps ) => {
2223 const projectId = useProjectId ( ) ;
2324 const { t } = useTranslation ( 'creation' ) ;
25+
2426 const [ openSshKeyform , setOpenSshKeyForm ] = useState < boolean > ( false ) ;
2527 const [ newSshKeys , setNewSshKeys ] = useState < TCustomSelectSshKeyData [ ] > ( [ ] ) ;
26- const [ openSshKeyAddedMessage , setopenSshKeyAddedMessage ] = useState < boolean > (
28+ const [ openSshKeyAddedMessage , setOpenSshKeyAddedMessage ] = useState < boolean > (
2729 false ,
2830 ) ;
2931
@@ -34,16 +36,31 @@ const SshKey = ({ microRegion }: TSshKeyProps) => {
3436 [ isLoading , microRegion , projectId ] ,
3537 ) ;
3638
39+ const { control, setValue } = useFormContext < TInstanceCreationForm > ( ) ;
40+ const selectedSshKeyId = useWatch ( {
41+ control,
42+ name : 'sshKeyId' ,
43+ } ) ;
44+
3745 const unavailableSshKeyIds = useMemo (
3846 ( ) => [ ...newSshKeys , ...sshKeys ] . map ( ( { value } ) => value ) ,
3947 [ newSshKeys , sshKeys ] ,
4048 ) ;
4149
50+ const sshKeyItems = useMemo ( ( ) => {
51+ const existingSshKeys = sshKeys . map ( ( item ) => ( {
52+ ...item ,
53+ newSshPublicKey : null ,
54+ } ) ) ;
55+
56+ return [ ...newSshKeys , ...existingSshKeys ] ;
57+ } , [ newSshKeys , sshKeys ] ) ;
58+
4259 const handleOpenSshKeyForm = ( ) => setOpenSshKeyForm ( true ) ;
4360
4461 const handleCloseSshKeyForm = ( ) => setOpenSshKeyForm ( false ) ;
4562
46- const handleCloseSshKeyAddedMessage = ( ) => setopenSshKeyAddedMessage ( false ) ;
63+ const handleCloseSshKeyAddedMessage = ( ) => setOpenSshKeyAddedMessage ( false ) ;
4764
4865 const handleAddSshKey : SubmitHandler < TAddSshKeyForm > = ( {
4966 sshKeyId,
@@ -53,24 +70,43 @@ const SshKey = ({ microRegion }: TSshKeyProps) => {
5370 {
5471 label : sshKeyId ,
5572 value : sshKeyId ,
56- sshPublicKey,
73+ newSshPublicKey : sshPublicKey ,
5774 } ,
5875 ...prevSshKeys ,
5976 ] ) ;
6077
6178 handleCloseSshKeyForm ( ) ;
62- setopenSshKeyAddedMessage ( true ) ;
79+ setOpenSshKeyAddedMessage ( true ) ;
6380 } ;
6481
6582 const handleCancelAddSshKey = ( ) => {
6683 if ( sshKeys . length === 0 && newSshKeys . length === 0 ) return ;
6784 handleCloseSshKeyForm ( ) ;
6885 } ;
6986
87+ const updateSshKeyFields = useCallback (
88+ ( sshKey : TCustomSelectSshKeyData ) => {
89+ setValue ( 'sshKeyId' , sshKey . value ) ;
90+ setValue ( 'newSshPublicKey' , sshKey . newSshPublicKey ) ;
91+ } ,
92+ [ setValue ] ,
93+ ) ;
94+
95+ const handleSelectSshKey = ( sshKey : TCustomSelectSshKeyData ) => {
96+ updateSshKeyFields ( sshKey ) ;
97+ handleCloseSshKeyAddedMessage ( ) ;
98+ } ;
99+
70100 useEffect ( ( ) => {
71101 if ( ! isLoading && sshKeys . length === 0 ) setOpenSshKeyForm ( true ) ;
72102 } , [ isLoading , sshKeys ] ) ;
73103
104+ useEffect ( ( ) => {
105+ const selectedSshKey = sshKeyItems [ 0 ] ;
106+
107+ if ( selectedSshKey ) updateSshKeyFields ( selectedSshKey ) ;
108+ } , [ sshKeyItems , updateSshKeyFields ] ) ;
109+
74110 return (
75111 < section >
76112 < Divider spacing = "64" />
@@ -114,9 +150,9 @@ const SshKey = ({ microRegion }: TSshKeyProps) => {
114150 ) : (
115151 < >
116152 < SshKeySelector
117- sshKeysItems = { sshKeys }
118- newSshKeys = { newSshKeys }
119- onValueChange = { handleCloseSshKeyAddedMessage }
153+ items = { sshKeyItems }
154+ value = { selectedSshKeyId ? [ selectedSshKeyId ] : [ ] }
155+ onValueChange = { handleSelectSshKey }
120156 />
121157 < Button variant = "ghost" onClick = { handleOpenSshKeyForm } >
122158 < Icon name = "plus" />
0 commit comments