@@ -2,6 +2,7 @@ import React, { useState, useEffect, useRef } from 'react';
22import { X , AlertCircle , CheckCircle , Copy , ExternalLink , ChevronDown } from 'lucide-react' ;
33import { useIndicatorOptions } from '../hooks/useIndicators' ;
44import InfoTooltip from './InfoTooltip' ;
5+ import { slugify , buildSuggestedToolJson , stringifySuggestedToolJson } from '../utils/suggestToolJson' ;
56
67const APPLICATION_CATEGORIES = [
78 { id : 'rs:AnalysisCode' , label : 'Analysis Code' } ,
@@ -35,97 +36,13 @@ const INITIAL_FORM = {
3536 maintainer : '' ,
3637} ;
3738
38- function slugify ( str ) {
39- return str
40- . toLowerCase ( )
41- . trim ( )
42- . replace ( / [ ^ a - z 0 - 9 ] + / g, '-' )
43- . replace ( / ( ^ - | - $ ) / g, '' ) ;
44- }
45-
4639function formatDimensionLabel ( dim ) {
4740 return dim
4841 . split ( '_' )
4942 . map ( w => w . charAt ( 0 ) . toUpperCase ( ) + w . slice ( 1 ) )
5043 . join ( ' ' ) ;
5144}
5245
53- function buildJson ( form ) {
54- const slug = slugify ( form . name ) ;
55- const obj = {
56- '@context' : 'https://w3id.org/everse/rs#' ,
57- '@id' : `https://w3id.org/everse/tools/${ slug } ` ,
58- '@type' : 'SoftwareApplication' ,
59- } ;
60-
61- // applicationCategory
62- if ( form . applicationCategory . length === 1 ) {
63- obj . applicationCategory = { '@id' : form . applicationCategory [ 0 ] , '@type' : '@id' } ;
64- } else if ( form . applicationCategory . length > 1 ) {
65- obj . applicationCategory = form . applicationCategory . map ( id => ( { '@id' : id , '@type' : '@id' } ) ) ;
66- }
67-
68- // Programming languages
69- const langs = form . appliesToProgrammingLanguage
70- . split ( ',' )
71- . map ( l => l . trim ( ) )
72- . filter ( Boolean ) ;
73- if ( langs . length > 0 ) {
74- obj . appliesToProgrammingLanguage = langs ;
75- }
76-
77- // author
78- if ( form . author . trim ( ) ) {
79- obj . author = form . author . trim ( ) ;
80- }
81-
82- obj . description = form . description ;
83-
84- // hasQualityDimension
85- if ( form . hasQualityDimension . length === 1 ) {
86- obj . hasQualityDimension = { '@id' : `dim:${ form . hasQualityDimension [ 0 ] } ` , '@type' : '@id' } ;
87- } else if ( form . hasQualityDimension . length > 1 ) {
88- obj . hasQualityDimension = form . hasQualityDimension . map ( d => ( { '@id' : `dim:${ d } ` , '@type' : '@id' } ) ) ;
89- }
90-
91- // measuresQualityIndicator
92- if ( form . measuresQualityIndicator . length === 1 ) {
93- obj . measuresQualityIndicator = { '@id' : form . measuresQualityIndicator [ 0 ] , '@type' : '@id' } ;
94- } else if ( form . measuresQualityIndicator . length > 1 ) {
95- obj . measuresQualityIndicator = form . measuresQualityIndicator . map ( i => ( { '@id' : i , '@type' : '@id' } ) ) ;
96- }
97-
98- // improvesQualityIndicator
99- if ( form . improvesQualityIndicator . length === 1 ) {
100- obj . improvesQualityIndicator = { '@id' : form . improvesQualityIndicator [ 0 ] , '@type' : '@id' } ;
101- } else if ( form . improvesQualityIndicator . length > 1 ) {
102- obj . improvesQualityIndicator = form . improvesQualityIndicator . map ( i => ( { '@id' : i , '@type' : '@id' } ) ) ;
103- }
104-
105- // howToUse
106- if ( form . howToUse . length > 0 ) {
107- obj . howToUse = form . howToUse ;
108- }
109-
110- obj . isAccessibleForFree = form . isAccessibleForFree ;
111- obj . license = form . license ;
112-
113- // maintainer
114- if ( form . maintainer . trim ( ) ) {
115- obj . maintainer = form . maintainer . trim ( ) ;
116- }
117-
118- obj . name = form . name ;
119- obj . url = form . url ;
120-
121- // usedBy
122- if ( form . usedBy . length > 0 ) {
123- obj . usedBy = form . usedBy ;
124- }
125-
126- return obj ;
127- }
128-
12946const MultiSelectDropdown = ( { options, value, onChange, placeholder, loading, error } ) => {
13047 const [ isOpen , setIsOpen ] = useState ( false ) ;
13148 const [ search , setSearch ] = useState ( '' ) ;
@@ -301,8 +218,8 @@ const SuggestToolForm = ({ isOpen, onClose }) => {
301218 return errs ;
302219 } ;
303220
304- const handleCopyToClipboard = ( ) => {
305- const json = JSON . stringify ( buildJson ( form ) , null , 2 ) ;
221+ const handleCopyToClipboard = async ( ) => {
222+ const json = await stringifySuggestedToolJson ( form ) ;
306223 navigator . clipboard . writeText ( json ) . then ( ( ) => {
307224 setCopied ( true ) ;
308225 setTimeout ( ( ) => setCopied ( false ) , 2000 ) ;
@@ -326,7 +243,7 @@ const SuggestToolForm = ({ isOpen, onClose }) => {
326243 if ( e . target === backdropRef . current ) onClose ( ) ;
327244 } ;
328245
329- const jsonPreview = JSON . stringify ( buildJson ( form ) , null , 2 ) ;
246+ const jsonPreview = JSON . stringify ( buildSuggestedToolJson ( form ) , null , 2 ) ;
330247
331248 return (
332249 < div
0 commit comments