import { Code, DatabaseIcon, Play } from 'lucide-react' import { motion } from 'motion/react' import { useEffect, useState } from 'react' import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts' import { Button, ChartContainer, ChartTooltip, ChartTooltipContent, cn, SQL_ICON } from 'ui' import CodeBlock from '../CodeBlock/CodeBlock' interface SqlSnippetProps { id: string sql: string title?: string resultType?: 'chart' | 'table' mockData?: any } // Example mock data matching the Results component format const MOCK_TABLE_DATA = { rows: [ { id: 1, name: 'John Doe', email: 'john@example.com', created_at: '2024-01-15T10:00:00Z', status: 'active', metadata: { role: 'admin', last_login: '2024-01-14' }, }, { id: 2, name: 'Jane Smith', email: 'jane@example.com', created_at: '2024-01-14T15:30:00Z', status: 'pending', metadata: null, }, { id: 3, name: 'Bob Wilson', email: 'bob@example.com', created_at: '2024-01-13T09:15:00Z', status: 'active', metadata: { role: 'user', last_login: '2024-01-12' }, }, ], } const MOCK_CHART_DATA = [ { name: 'Jan', value: 400 }, { name: 'Feb', value: 300 }, { name: 'Mar', value: 600 }, { name: 'Apr', value: 800 }, { name: 'May', value: 700 }, ] export const SqlSnippet = ({ id, sql, title = 'SQL Query', resultType = 'table', mockData, }: SqlSnippetProps) => { const [showCode, setShowCode] = useState(!mockData) const [isExecuting, setIsExecuting] = useState(false) const [showResults, setShowResults] = useState(false) const [results, setResults] = useState(null) const handleExecute = () => { setIsExecuting(true) setShowResults(false) // Simulate SQL execution delay setTimeout(() => { setIsExecuting(false) if (mockData) { setShowResults(true) // Use provided mock data or default mock data setResults( resultType === 'chart' ? mockData || MOCK_CHART_DATA : mockData?.rows || MOCK_TABLE_DATA.rows ) } }, 1000) } // Auto-execute on mount useEffect(() => { handleExecute() }, []) const ResultsDisplay = () => { if (resultType === 'chart') { return (
} />
) } return ( <>
{results?.length > 0 && Object.keys(results[0]).map((key) => ( ))} {results?.map((row: any, i: number) => ( {Object.values(row).map((value: any, j: number) => ( ))} ))}
{key}
{value === null ? 'NULL' : typeof value === 'object' ? JSON.stringify(value) : String(value)}

{results.length} rows {results.length >= 100 && ' (Limited to only 100 rows)'}

) } return (

{title}

{showCode && (
{sql}
)} {isExecuting ? ( Executing query
. . .
) : showResults && results ? ( ) : null}
) }