|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { atomOneDark } from 'react-syntax-highlighter/dist/cjs/styles/hljs'; |
| 3 | +import Highlight from 'react-syntax-highlighter'; |
| 4 | +import JSZip from 'jszip'; |
| 5 | +import { saveAs } from 'file-saver'; |
| 6 | + |
| 7 | +async function fetchData() { |
| 8 | + const response = await fetch('/data/getting-started-examples.json'); |
| 9 | + const data = await response.json(); |
| 10 | + |
| 11 | + const defaultSchemaData = data.find((data: any) => data.default === true); |
| 12 | + |
| 13 | + const schemaResp = await fetch(defaultSchemaData.file); |
| 14 | + const schemaData = await schemaResp.json(); |
| 15 | + |
| 16 | + const defaultInstanceData = defaultSchemaData.instances.find( |
| 17 | + (instance: any) => instance.default === true, |
| 18 | + ); |
| 19 | + |
| 20 | + const instanceResp = await fetch(defaultInstanceData.file); |
| 21 | + const instanceData = await instanceResp.json(); |
| 22 | + |
| 23 | + return { |
| 24 | + data, |
| 25 | + schemaData, |
| 26 | + instanceData, |
| 27 | + initialInstance: defaultSchemaData.instances, |
| 28 | + initialDetails: [defaultInstanceData.details, defaultInstanceData.valid], |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +interface SchemaOption { |
| 33 | + file: string; |
| 34 | + instances: InstanceOption[]; |
| 35 | +} |
| 36 | + |
| 37 | +interface InstanceOption { |
| 38 | + file: string; |
| 39 | + details: string; |
| 40 | + valid: string; |
| 41 | +} |
| 42 | + |
| 43 | +const GettingStarted = () => { |
| 44 | + useEffect(() => { |
| 45 | + fetchData() |
| 46 | + .then( |
| 47 | + ({ |
| 48 | + data, |
| 49 | + schemaData, |
| 50 | + instanceData, |
| 51 | + initialInstance, |
| 52 | + initialDetails, |
| 53 | + }) => { |
| 54 | + setOptions(data); |
| 55 | + setFetchedSchema(schemaData); |
| 56 | + setFetchedInstance(instanceData); |
| 57 | + setInstances(initialInstance); |
| 58 | + setDetails(initialDetails); |
| 59 | + }, |
| 60 | + ) |
| 61 | + .catch((e) => console.log('Error: ', e)); |
| 62 | + }, []); |
| 63 | + |
| 64 | + const [options, setOptions] = useState<SchemaOption[]>([]); |
| 65 | + const [instances, setInstances] = useState<InstanceOption[]>([]); |
| 66 | + const [details, setDetails] = useState<string[]>(['', '']); |
| 67 | + const [fetchedSchema, setFetchedSchema] = useState(); |
| 68 | + const [fetchedInstance, setFetchedInstance] = useState(); |
| 69 | + |
| 70 | + const handleSchemaChange = async ( |
| 71 | + e: React.ChangeEvent<HTMLSelectElement>, |
| 72 | + ) => { |
| 73 | + const selectedSchema = options.find( |
| 74 | + (schema) => schema.file === e.target.value, |
| 75 | + ); |
| 76 | + |
| 77 | + if (selectedSchema) { |
| 78 | + const schemaResponse = await fetch(e.target.value); |
| 79 | + const schData = await schemaResponse.json(); |
| 80 | + |
| 81 | + setFetchedSchema(schData); |
| 82 | + setInstances(selectedSchema.instances); |
| 83 | + |
| 84 | + const instResp = await fetch(selectedSchema.instances[0].file); |
| 85 | + const instData = await instResp.json(); |
| 86 | + setFetchedInstance(instData); |
| 87 | + } else { |
| 88 | + setInstances([]); |
| 89 | + setFetchedSchema(null!); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + const handleInstanceChange = async ( |
| 94 | + e: React.ChangeEvent<HTMLSelectElement>, |
| 95 | + ) => { |
| 96 | + const selectedInstance = instances.find( |
| 97 | + (instance) => instance.file === e.target.value, |
| 98 | + ) as InstanceOption; |
| 99 | + |
| 100 | + if (selectedInstance) { |
| 101 | + const instanceResponse = await fetch(e.target.value); |
| 102 | + const instanceData = await instanceResponse.json(); |
| 103 | + |
| 104 | + setFetchedInstance(instanceData); |
| 105 | + setDetails([selectedInstance.details, selectedInstance.valid]); |
| 106 | + } else { |
| 107 | + setFetchedInstance(undefined); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + const createZip = async () => { |
| 112 | + try { |
| 113 | + const zip = new JSZip(); |
| 114 | + zip.file('schema.json', JSON.stringify(fetchedSchema, null, 2)); |
| 115 | + zip.file('instance.json', JSON.stringify(fetchedInstance, null, 2)); |
| 116 | + |
| 117 | + zip.generateAsync({ type: 'blob' }).then((content) => { |
| 118 | + saveAs(content, 'getting-started-examples.zip'); |
| 119 | + }); |
| 120 | + } catch (e) { |
| 121 | + console.log('Error zipping files', e); |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + return ( |
| 126 | + <> |
| 127 | + <div className='relative'> |
| 128 | + <div className='flex flex-col'> |
| 129 | + <div className='flex items-end flex-row justify-between mt-5 mb-3 '> |
| 130 | + <h2 className='text-h6 font-semibold mb-1'>JSON Schema</h2> |
| 131 | + <div className='select-wrap'> |
| 132 | + <label className='mr-2 max-sm:text-[12px]'> |
| 133 | + Select a Schema: |
| 134 | + </label> |
| 135 | + <select |
| 136 | + name='Select a JSON Schema Validator' |
| 137 | + className='p-2 border dark:border-slate-300 border-slate-800 dark:bg-slate-900 rounded-md max-sm:text-[12px] plausible-event-name==activation-explore-tools' |
| 138 | + id='Examples' |
| 139 | + onChange={handleSchemaChange} |
| 140 | + > |
| 141 | + {options.map((option: any, id: number) => ( |
| 142 | + <option key={id} value={option.file}> |
| 143 | + {option.name} |
| 144 | + </option> |
| 145 | + ))} |
| 146 | + </select> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + |
| 150 | + <div className='overflow-x-auto flex-basis-0 max-w-full min-w-0 shrink lg:max-w-[800px] xl:max-w-[900px]'> |
| 151 | + <Highlight |
| 152 | + wrapLines={true} |
| 153 | + wrapLongLines={true} |
| 154 | + customStyle={{ |
| 155 | + borderRadius: 10, |
| 156 | + paddingTop: 15, |
| 157 | + paddingBottom: 10, |
| 158 | + paddingLeft: 10, |
| 159 | + marginBottom: 20, |
| 160 | + maxWidth: '100%', |
| 161 | + }} |
| 162 | + lineNumberStyle={{ |
| 163 | + marginRight: 10, |
| 164 | + }} |
| 165 | + style={atomOneDark} |
| 166 | + showLineNumbers |
| 167 | + startingLineNumber={1} |
| 168 | + lineProps={() => { |
| 169 | + const isHighlighted = false; |
| 170 | + return { |
| 171 | + className: `${isHighlighted ? 'bg-code-editor-dark-highlight block ml-10 w-full' : ''} pr-8`, |
| 172 | + }; |
| 173 | + }} |
| 174 | + codeTagProps={{ |
| 175 | + className: 'mr-8', |
| 176 | + }} |
| 177 | + > |
| 178 | + {JSON.stringify(fetchedSchema, null, 2)} |
| 179 | + </Highlight> |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + |
| 183 | + <div className='flex flex-col'> |
| 184 | + <div className='flex items-end flex-row justify-between mt-5 mb-3 '> |
| 185 | + <h2 className='text-h6 font-semibold mb-1'>JSON Instance</h2> |
| 186 | + <div className='select-wrap'> |
| 187 | + <label className='mr-2 max-sm:text-[12px]'> |
| 188 | + Select an Instance: |
| 189 | + </label> |
| 190 | + <select |
| 191 | + name='Select a JSON Schema Validator' |
| 192 | + className='p-2 border dark:border-slate-300 border-slate-800 dark:bg-slate-900 rounded-md max-sm:text-[12px] plausible-event-name==activation-explore-tools' |
| 193 | + id='Examples' |
| 194 | + onChange={handleInstanceChange} |
| 195 | + > |
| 196 | + {instances.map((instance: any, id: number) => ( |
| 197 | + <option key={id} value={instance.file}> |
| 198 | + {instance.name} |
| 199 | + </option> |
| 200 | + ))} |
| 201 | + </select> |
| 202 | + </div> |
| 203 | + </div> |
| 204 | + <div className='overflow-x-auto flex-basis-0 max-w-full min-w-0 shrink lg:max-w-[800px] xl:max-w-[900px]'> |
| 205 | + <Highlight |
| 206 | + wrapLines={true} |
| 207 | + wrapLongLines={true} |
| 208 | + customStyle={{ |
| 209 | + borderRadius: 10, |
| 210 | + paddingTop: 15, |
| 211 | + paddingBottom: 10, |
| 212 | + paddingLeft: 10, |
| 213 | + marginBottom: 20, |
| 214 | + maxWidth: '100%', |
| 215 | + }} |
| 216 | + lineNumberStyle={{ |
| 217 | + marginRight: 10, |
| 218 | + }} |
| 219 | + style={atomOneDark} |
| 220 | + showLineNumbers |
| 221 | + startingLineNumber={1} |
| 222 | + lineProps={() => { |
| 223 | + const isHighlighted = false; |
| 224 | + return { |
| 225 | + className: `${isHighlighted ? 'bg-code-editor-dark-highlight block ml-10 w-full' : ''} pr-8`, |
| 226 | + }; |
| 227 | + }} |
| 228 | + codeTagProps={{ |
| 229 | + className: 'mr-8', |
| 230 | + }} |
| 231 | + > |
| 232 | + {JSON.stringify(fetchedInstance, null, 2)} |
| 233 | + </Highlight> |
| 234 | + </div> |
| 235 | + <h2 className='text-h6 font-semibold'>Validation Result</h2> |
| 236 | + <div className='flex bg-[#282c34] justify-between items-center text-white font-medium flex-row border p-5 rounded-xl'> |
| 237 | + <p>{details[0]}</p> |
| 238 | + |
| 239 | + {details[1] ? ( |
| 240 | + <img src='/icons/green-tick.svg' alt='green tick' /> |
| 241 | + ) : ( |
| 242 | + <img src='/icons/red-cross.svg' alt='red cross' /> |
| 243 | + )} |
| 244 | + </div> |
| 245 | + </div> |
| 246 | + |
| 247 | + <button |
| 248 | + className='absolute right-0 my-4 text-[17px] bg-startBlue text-white px-3 py-1 rounded' |
| 249 | + onClick={createZip} |
| 250 | + > |
| 251 | + Download |
| 252 | + </button> |
| 253 | + </div> |
| 254 | + </> |
| 255 | + ); |
| 256 | +}; |
| 257 | + |
| 258 | +export default GettingStarted; |
0 commit comments