MCPcopy Create free account
hub / github.com/R44VC0RP/opencode.cafe / parseAndApplyJson

Function parseAndApplyJson

app/submit/page.tsx:227–271  ·  view source on GitHub ↗
(jsonString: string)

Source from the content-addressed store, hash-verified

225 }
226
227 const parseAndApplyJson = (jsonString: string) => {
228 setJsonError(null)
229
230 if (!jsonString.trim()) {
231 setJsonError("Please enter JSON data")
232 return false
233 }
234
235 try {
236 const parsed = JSON.parse(jsonString) as JsonExtension
237
238 // Validate type if provided
239 if (parsed.type && !VALID_TYPES.includes(parsed.type as typeof VALID_TYPES[number])) {
240 setJsonError(`Invalid type "${parsed.type}". Valid types: ${VALID_TYPES.join(", ")}`)
241 return false
242 }
243
244 // Convert tags array to comma-separated string if needed
245 let tagsString = ""
246 if (Array.isArray(parsed.tags)) {
247 tagsString = parsed.tags.join(", ")
248 } else if (typeof parsed.tags === "string") {
249 tagsString = parsed.tags
250 }
251
252 // Apply parsed data to form
253 setFormData({
254 type: parsed.type || "",
255 productId: parsed.productId?.toLowerCase().replace(/[^a-z-]/g, "") || "",
256 displayName: parsed.displayName || "",
257 description: parsed.description || "",
258 repoUrl: parsed.repoUrl || "",
259 homepageUrl: parsed.homepageUrl || "",
260 tags: tagsString,
261 installation: parsed.installation || "",
262 })
263
264 // Switch to manual mode to show the populated form
265 setInputMode("manual")
266 return true
267 } catch {
268 setJsonError("Invalid JSON format. Please check your syntax.")
269 return false
270 }
271 }
272
273 const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
274 const file = e.target.files?.[0]

Callers 2

handleFileUploadFunction · 0.85
SubmitFormFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected