-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenapiImportModal.tsx
More file actions
92 lines (85 loc) 路 3.25 KB
/
Copy pathOpenapiImportModal.tsx
File metadata and controls
92 lines (85 loc) 路 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
import { useState } from "react"
import { toast } from "sonner"
import { BaseUrlVariableToggle } from "./BaseUrlVariableToggle"
import { DEFAULT_BASE_URL_VARIABLE } from "./openapiImportShared"
interface OpenapiImportModalProps {
open: boolean
onOpenChange: (open: boolean) => void
onImport: (openapiDoc: unknown, baseUrl: string, baseUrlVariable?: string) => void
}
export function OpenapiImportModal({ open, onOpenChange, onImport }: OpenapiImportModalProps) {
const [rawJSON, setRawJSON] = useState("")
const [baseUrl, setBaseUrl] = useState("")
const [useVariable, setUseVariable] = useState(true)
const handleImport = () => {
if (!rawJSON.trim()) {
toast.error("Please paste the OpenAPI JSON content.")
return
}
let apiDoc
try {
apiDoc = JSON.parse(rawJSON)
} catch (err) {
toast.error("Invalid JSON. Please check the pasted content.")
return
}
if (!baseUrl.trim()) {
toast.error("Please enter a valid base URL.")
return
}
try {
onImport(apiDoc, baseUrl, useVariable ? DEFAULT_BASE_URL_VARIABLE : undefined)
setRawJSON("")
setBaseUrl("")
setUseVariable(true)
} catch (error) {
console.error("Error importing OpenAPI:", error)
toast.error(error instanceof Error ? error.message : "Failed to import OpenAPI specification")
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[600px] bg-background border-border">
<DialogHeader>
<DialogTitle className="text-foreground">Import OpenAPI JSON (Raw)</DialogTitle>
<DialogDescription className="text-muted-foreground">
Paste your OpenAPI JSON below and provide the base URL for your API.
</DialogDescription>
</DialogHeader>
<div className="flex flex-col gap-4">
<Textarea
placeholder="Paste the OpenAPI JSON here..."
value={rawJSON}
onChange={(e) => setRawJSON(e.target.value)}
className="h-48 font-mono text-sm bg-background text-foreground border-border placeholder:text-muted-foreground"
/>
<Input
placeholder="Enter the base URL (e.g., https://api.example.com)"
value={baseUrl}
onChange={(e) => setBaseUrl(e.target.value)}
className="bg-background text-foreground border-border placeholder:text-muted-foreground"
/>
<BaseUrlVariableToggle
checked={useVariable}
onCheckedChange={setUseVariable}
baseUrl={baseUrl}
/>
</div>
<DialogFooter className="mt-4 flex justify-end gap-2">
<Button
variant="outline"
onClick={() => onOpenChange(false)}
className="text-foreground hover:text-foreground border-border"
>
Cancel
</Button>
<Button onClick={handleImport}>Import</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}