1- import { FieldErrors , FieldPathByValue , FormProvider , Resolver , useForm } from "react-hook-form" ;
2- import PubliccodeYmlLanguages from "./PubliccodeYmlLanguages" ;
3-
41import { Col , Container , Icon , notify , Row } from "design-react-kit" ;
52import { set } from "lodash" ;
63import { useCallback , useEffect , useState } from "react" ;
4+ import { FieldErrors , FieldPathByValue , FormProvider , Resolver , useForm } from "react-hook-form" ;
5+ import useFormPersist from "react-hook-form-persist" ;
76import { useTranslation } from "react-i18next" ;
7+ import { RequiredDeep } from "type-fest" ;
88import YAML from "yaml" ;
99import licenses from "../../generated/licenses.json" ;
1010import { allLangs } from "../../i18n" ;
@@ -13,11 +13,19 @@ import { DEFAULT_COUNTRY_SECTIONS } from "../contents/constants";
1313import * as countrySection from "../contents/countrySpecificSection" ;
1414import developmentStatus from "../contents/developmentStatus" ;
1515import maintenanceTypes from "../contents/maintenanceTypes" ;
16+ import mimeTypes from "../contents/mime-types" ;
1617import platforms from "../contents/platforms" ;
1718import PublicCode , { defaultItaly , LATEST_VERSION , PublicCodeWithDeprecatedFields } from "../contents/publiccode" ;
19+ import { getPubliccodeYmlVersionList } from "../contents/publiccode-yml-version" ;
1820import softwareTypes from "../contents/softwareTypes" ;
21+ import fileImporter from "../importers/file.importer" ;
22+ import importFromGitlab from "../importers/gitlab.importer" ;
23+ import importStandard from "../importers/standard.importer" ;
1924import linter from "../linter" ;
25+ import publicCodeAdapter from "../publiccode-adapter" ;
26+ import { isMinorThanLatest , toSemVerObject } from "../semver" ;
2027import { useAppDispatch , useAppSelector } from "../store" ;
28+ import { resetPubliccodeYmlLanguages , setPubliccodeYmlLanguages } from "../store/publiccodeYmlLanguages" ;
2129import { validator } from "../validator" ;
2230import EditorBoolean from "./EditorBoolean" ;
2331import EditorContacts from "./EditorContacts" ;
@@ -30,21 +38,13 @@ import EditorMultiselect from "./EditorMultiselect";
3038import EditorRadio from "./EditorRadio" ;
3139import EditorScreenshots from "./EditorScreenshots" ;
3240import EditorSelect from "./EditorSelect" ;
41+ import EditorUsedBy from "./EditorUsedBy" ;
3342import { Footer } from "./Foot" ;
3443import Head from "./Head" ;
3544import InfoBox from "./InfoBox" ;
36- import { YamlModal } from "./YamlModal" ;
37-
38- import useFormPersist from "react-hook-form-persist" ;
39- import { RequiredDeep } from "type-fest" ;
40- import mimeTypes from "../contents/mime-types" ;
41- import { getPubliccodeYmlVersionList } from "../contents/publiccode-yml-version" ;
42- import { isMinorThanLatest , toSemVerObject } from "../semver" ;
43- import { resetPubliccodeYmlLanguages , setPubliccodeYmlLanguages } from "../store/publiccodeYmlLanguages" ;
44- import yamlSerializer from "../yaml-serializer" ;
45- import { removeDuplicate } from "../yaml-upload" ;
46- import EditorUsedBy from "./EditorUsedBy" ;
45+ import PubliccodeYmlLanguages from "./PubliccodeYmlLanguages" ;
4746import { WarningModal } from "./WarningModal" ;
47+ import { YamlModal } from "./YamlModal" ;
4848
4949const PUBLIC_CODE_EDITOR_WARNINGS = 'PUBLIC_CODE_EDITOR_WARNINGS'
5050
@@ -245,23 +245,19 @@ export default function Editor() {
245245 const setFormDataAfterImport = async (
246246 fetchData : ( ) => Promise < PublicCode | null >
247247 ) => {
248- const publicCode = await fetchData ( ) ;
249-
250- if ( publicCode ) {
251- const values = { ...defaultValues , ...publicCode } as PublicCode ;
252-
253- if ( publicCode . usedBy ) {
254- values . usedBy = removeDuplicate ( publicCode . usedBy )
255- }
248+ try {
249+ const publicCode = await fetchData ( ) . then ( publicCode => {
250+ return publicCodeAdapter ( { publicCode, defaultValues : defaultValues as unknown as Partial < PublicCode > } )
251+ } ) ;
256252
257253 setLanguages ( publicCode ) ;
258- reset ( values ) ;
254+ reset ( publicCode ) ;
259255
260256 checkPubliccodeYmlVersion ( publicCode ) ;
261257
262258 setPublicCodeImported ( true ) ;
263259
264- const res = await checkWarnings ( values )
260+ const res = await checkWarnings ( publicCode )
265261
266262 setWarnings ( Array . from ( res . warnings ) . map ( ( [ key , { message } ] ) => ( { key, message } ) ) ) ;
267263
@@ -278,22 +274,41 @@ export default function Editor() {
278274 duration : _5_SECONDS
279275 } )
280276 }
277+
278+
279+ } catch ( error : unknown ) {
280+ notify ( 'Import error' , ( error as Error ) . message , {
281+ dismissable : true ,
282+ state : "error" ,
283+ } )
281284 }
282285 } ;
283286
284287 const loadFileYamlHandler = async ( file : File ) => {
285- const fetchDataFn = ( ) => yamlSerializer ( file . stream ( ) ) ;
288+ const fetchDataFn = ( ) => fileImporter ( file ) ;
286289
287290 await setFormDataAfterImport ( fetchDataFn ) ;
288291 } ;
289292
290- const loadRemoteYamlHandler = async ( url : string ) => {
291- const fetchDataFn = ( ) =>
292- fetch ( url )
293- . then ( ( res ) => res . body )
294- . then ( ( res ) => res && yamlSerializer ( res ) ) ;
293+ const loadRemoteYamlHandler = async ( urlValue : string ) => {
294+
295+ try {
296+ const url = new URL ( urlValue ) ;
297+
298+ const isGitlabRepo = url . hostname . includes ( 'gitlab.com' )
299+
300+ const fetchDataFn = isGitlabRepo
301+ ? async ( ) => await importFromGitlab ( url )
302+ : async ( ) => await importStandard ( url )
303+
304+ await setFormDataAfterImport ( fetchDataFn ) ;
305+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
306+ } catch ( error ) {
307+ notify ( t ( 'editor.notvalidurl' ) , t ( 'editor.notvalidurl' ) , {
308+ state : 'error'
309+ } )
310+ }
295311
296- await setFormDataAfterImport ( fetchDataFn ) ;
297312 } ;
298313 //#endregion
299314
0 commit comments