3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- import React , { ReactNode } from 'react' ;
6
+ import React , { ReactNode , useEffect , useState } from 'react' ;
7
7
import yaml from 'js-yaml' ;
8
8
import jsonpath from 'jsonpath' ;
9
9
import { capitalize , escape , findKey , get , isEmpty , set , unset } from 'lodash' ;
@@ -159,27 +159,38 @@ export function isValidWorkflow(workflowObj: any): boolean {
159
159
160
160
// Determines if a file used for import workflow is compatible with the current data source version.
161
161
export async function isCompatibleWorkflow (
162
- workflowObj : any ,
162
+ workflowObj : any ,
163
163
dataSourceId ?: string | undefined
164
164
) : Promise < boolean > {
165
165
const compatibility = workflowObj ?. version ?. compatibility ;
166
166
167
167
// Default to true when compatibility cannot be assessed (empty/invalid compatibility array or MDS disabled.)
168
- if ( ! Array . isArray ( compatibility ) || compatibility . length === 0 || dataSourceId === undefined ) {
168
+ if (
169
+ ! Array . isArray ( compatibility ) ||
170
+ compatibility . length === 0 ||
171
+ dataSourceId === undefined
172
+ ) {
169
173
return true ;
170
174
}
171
175
172
- const dataSourceVersion = await getEffectiveVersion ( dataSourceId ) ;
173
- const [ effectiveMajorVersion , effectiveMinorVersion ] = dataSourceVersion . split ( '.' ) . map ( Number ) ;
174
-
176
+ const dataSourceVersion = await getEffectiveVersion ( dataSourceId ) ;
177
+ const [
178
+ effectiveMajorVersion ,
179
+ effectiveMinorVersion ,
180
+ ] = dataSourceVersion . split ( '.' ) . map ( Number ) ;
181
+
175
182
// Checks if any version in compatibility array matches the current dataSourceVersion (major.minor)
176
- return compatibility . some ( compatibleVersion => {
177
- const [ compatibleMajor , compatibleMinor ] = compatibleVersion . split ( '.' ) . map ( Number ) ;
178
- return effectiveMajorVersion === compatibleMajor && effectiveMinorVersion === compatibleMinor ;
183
+ return compatibility . some ( ( compatibleVersion ) => {
184
+ const [ compatibleMajor , compatibleMinor ] = compatibleVersion
185
+ . split ( '.' )
186
+ . map ( Number ) ;
187
+ return (
188
+ effectiveMajorVersion === compatibleMajor &&
189
+ effectiveMinorVersion === compatibleMinor
190
+ ) ;
179
191
} ) ;
180
192
}
181
193
182
-
183
194
export function isValidUiWorkflow ( workflowObj : any ) : boolean {
184
195
return (
185
196
isValidWorkflow ( workflowObj ) &&
@@ -909,13 +920,13 @@ export function getFieldValue(jsonObj: {}, fieldName: string): any | undefined {
909
920
return undefined ;
910
921
}
911
922
912
- // Get the version from the selected data source
923
+ // Get the version from the selected data source, if found
913
924
export const getEffectiveVersion = async (
914
925
dataSourceId : string | undefined
915
- ) : Promise < string > => {
926
+ ) : Promise < string | undefined > => {
916
927
try {
917
928
if ( dataSourceId === undefined ) {
918
- throw new Error ( 'Data source is required' ) ;
929
+ throw new Error ( ) ;
919
930
}
920
931
921
932
if ( dataSourceId === '' ) {
@@ -927,16 +938,26 @@ export const getEffectiveVersion = async (
927
938
'data-source' ,
928
939
dataSourceId
929
940
) ;
930
- const version =
931
- dataSource ?. attributes ?. dataSourceVersion || MIN_SUPPORTED_VERSION ;
932
- return version ;
941
+ return dataSource ?. attributes ?. dataSourceVersion ;
933
942
} catch ( error ) {
934
- console . error ( 'Error getting version:' , error ) ;
935
- return MIN_SUPPORTED_VERSION ;
943
+ console . error ( 'Error getting version: ' , error ) ;
944
+ return undefined ;
936
945
}
937
946
} ;
938
947
939
-
948
+ export function useMissingDataSourceVersion (
949
+ dataSourceId : string | undefined ,
950
+ dataSourceVersion : string | undefined
951
+ ) : boolean {
952
+ const [ missingVersion , setMissingVersion ] = useState < boolean > ( false ) ;
953
+ useEffect ( ( ) => {
954
+ setMissingVersion (
955
+ dataSourceId !== undefined && dataSourceVersion === undefined
956
+ ) ;
957
+ } , [ dataSourceId , dataSourceVersion ] ) ;
958
+ return missingVersion ;
959
+ }
960
+
940
961
/**
941
962
* Formats version string to show only major.minor numbers
942
963
* Example: "3.0.0-alpha1" -> "3.0"
@@ -946,4 +967,3 @@ export function formatDisplayVersion(version: string): string {
946
967
const [ major , minor ] = version . split ( '.' ) ;
947
968
return `${ major } .${ minor } ` ;
948
969
}
949
-
0 commit comments