import { head } from "lodash-es"; import { useEffect, useState } from "react"; import DataTable from "react-data-table-component"; import { toast } from "react-hot-toast"; import { Connection } from "@/types"; import Icon from "./Icon"; interface Props { connection: Connection; databaseName: string; statement: string; close: () => void; } type RawQueryResult = { [key: string]: any; }; const ExecuteStatementModal = (props: Props) => { const { close, connection, databaseName, statement } = props; const [rawResults, setRawResults] = useState([]); const [isLoading, setIsLoading] = useState(true); const columns = Object.keys(head(rawResults) || {}).map((key) => { return { name: key, selector: (row: RawQueryResult) => row[key], }; }); useEffect(() => { const executeStatement = async () => { try { const response = await fetch("/api/connection/execute", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ connection, db: databaseName, statement, }), }); const result = await response.json(); setIsLoading(false); setRawResults(result); } catch (error) { console.error(error); toast.error("Failed to execute statement"); } }; executeStatement(); }, []); return (

Execute query

{isLoading ? (
) : rawResults.length === 0 ? (
No data return.
) : (
)}
); }; export default ExecuteStatementModal;