import { useState, useMemo } from "react"; import { krowSDK } from "@/api/krowSDK"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import ApiResponse from "@/components/ApiResponse"; import { Textarea } from "@/components/ui/textarea"; import { Label } from "@/components/ui/label"; const entityNames = Object.keys(krowSDK.entities).sort(); const getPrettifiedJSON = (entity, method) => { // Basic placeholder payloads. A more advanced SDK could provide detailed examples. const payloads = { get: { id: "some-id" }, create: { data: { property: "value" } }, update: { id: "some-id", data: { property: "new-value" } }, delete: { id: "some-id" }, filter: { where: { property: { _eq: "value" } } }, list: {} }; return JSON.stringify(payloads[method] || {}, null, 2); }; const EntityTester = () => { const [selectedEntity, setSelectedEntity] = useState(null); const [selectedMethod, setSelectedMethod] = useState(null); const [response, setResponse] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const [jsonInput, setJsonInput] = useState(""); const [jsonError, setJsonError] = useState(null); const availableMethods = useMemo(() => { if (!selectedEntity) return []; return Object.keys(krowSDK.entities[selectedEntity]); }, [selectedEntity]); const handleEntityChange = (entity) => { setSelectedEntity(entity); setSelectedMethod(null); setJsonInput(""); setJsonError(null); setResponse(null); setError(null); }; const handleMethodSelect = (method) => { setSelectedMethod(method); setJsonInput(getPrettifiedJSON(selectedEntity, method)); setJsonError(null); setResponse(null); setError(null); }; const handleJsonInputChange = (e) => { setJsonInput(e.target.value); try { JSON.parse(e.target.value); setJsonError(null); } catch (err) { setJsonError("Invalid JSON format"); } }; const executeApi = async () => { if (!selectedEntity || !selectedMethod || jsonError) return; setLoading(true); setResponse(null); setError(null); try { const params = JSON.parse(jsonInput); const sdkMethod = krowSDK.entities[selectedEntity][selectedMethod]; const res = await sdkMethod(params); setResponse(res); } catch (err) { setError(err.response?.data || err.message); } finally { setLoading(false); } }; const renderMethodForm = () => { if (!selectedMethod) { return (

Select a method to begin.

); } return (

Parameters for /{selectedEntity}/{selectedMethod}

{/* This is a textarea for JSON input. A more advanced implementation could dynamically generate a form based on the expected parameters of each SDK method, but that requires metadata about each method's signature which is not currently available in the mock client. */}