|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { AlignLeft, Loader2, Play } from "lucide-react"; |
| 4 | +import { useExtracted } from "next-intl"; |
| 5 | +import { useTheme } from "next-themes"; |
| 6 | +import { useEffect, useRef, useState } from "react"; |
| 7 | +import { Light as SyntaxHighlighter } from "react-syntax-highlighter"; |
| 8 | +import sql from "react-syntax-highlighter/dist/esm/languages/hljs/sql"; |
| 9 | +import { vs, vs2015 } from "react-syntax-highlighter/dist/esm/styles/hljs"; |
| 10 | +import { Button } from "../../../../components/ui/button"; |
| 11 | +import { cn } from "../../../../lib/utils"; |
| 12 | + |
| 13 | +SyntaxHighlighter.registerLanguage("sql", sql); |
| 14 | + |
| 15 | +type QueryEditorProps = { |
| 16 | + value: string; |
| 17 | + disabled: boolean; |
| 18 | + isRunning: boolean; |
| 19 | + onChange: (value: string) => void; |
| 20 | + onFormat: () => void; |
| 21 | + onRun: () => void; |
| 22 | +}; |
| 23 | + |
| 24 | +export function QueryEditor({ value, disabled, isRunning, onChange, onFormat, onRun }: QueryEditorProps) { |
| 25 | + const t = useExtracted(); |
| 26 | + const { resolvedTheme } = useTheme(); |
| 27 | + const [isDark, setIsDark] = useState(false); |
| 28 | + const highlightRef = useRef<HTMLDivElement>(null); |
| 29 | + const lineNumberRef = useRef<HTMLDivElement>(null); |
| 30 | + const lineCount = Math.max(1, value.split("\n").length); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + setIsDark(resolvedTheme === "dark" || document.documentElement.classList.contains("dark")); |
| 34 | + }, [resolvedTheme]); |
| 35 | + |
| 36 | + const handleScroll = (event: React.UIEvent<HTMLTextAreaElement>) => { |
| 37 | + const { scrollLeft, scrollTop } = event.currentTarget; |
| 38 | + if (highlightRef.current) { |
| 39 | + highlightRef.current.style.transform = `translate(${-scrollLeft}px, ${-scrollTop}px)`; |
| 40 | + } |
| 41 | + if (lineNumberRef.current) { |
| 42 | + lineNumberRef.current.style.transform = `translateY(${-scrollTop}px)`; |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="flex min-h-[280px] flex-col overflow-hidden rounded-lg border border-neutral-150 bg-white shadow-sm dark:border-neutral-850 dark:bg-neutral-900"> |
| 48 | + <div className="flex h-10 items-center justify-between border-b border-neutral-150 bg-neutral-50 px-3 dark:border-neutral-800 dark:bg-neutral-950"> |
| 49 | + <div className="flex items-center gap-2"> |
| 50 | + <div className="h-2 w-2 rounded-full bg-emerald-500" /> |
| 51 | + <div className="text-sm font-medium text-neutral-900 dark:text-neutral-100">{t("Query")}</div> |
| 52 | + <div className="rounded border border-neutral-200 bg-white px-1.5 py-0.5 text-[11px] font-medium text-neutral-500 dark:border-neutral-800 dark:bg-neutral-900 dark:text-neutral-400"> |
| 53 | + SQL |
| 54 | + </div> |
| 55 | + </div> |
| 56 | + <div className="flex items-center gap-1.5"> |
| 57 | + <Button |
| 58 | + type="button" |
| 59 | + size="smIcon" |
| 60 | + variant="ghost" |
| 61 | + onClick={onFormat} |
| 62 | + disabled={disabled || !value.trim()} |
| 63 | + title="Format query" |
| 64 | + aria-label="Format query" |
| 65 | + > |
| 66 | + <AlignLeft className="h-4 w-4" /> |
| 67 | + </Button> |
| 68 | + <Button size="sm" onClick={onRun} disabled={disabled || !value.trim()}> |
| 69 | + {isRunning ? <Loader2 className="h-4 w-4 animate-spin" /> : <Play className="h-4 w-4" />} |
| 70 | + {t("Run")} |
| 71 | + </Button> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + |
| 75 | + <div className="grid min-h-0 flex-1 grid-cols-[42px_minmax(0,1fr)] bg-[#fbfcfd] dark:bg-[#090d16]"> |
| 76 | + <div className="relative overflow-hidden border-r border-neutral-150 bg-neutral-50 dark:border-neutral-800 dark:bg-neutral-950"> |
| 77 | + <div |
| 78 | + ref={lineNumberRef} |
| 79 | + className="select-none px-2 py-2.5 text-right font-mono text-[11px] leading-[18px] text-neutral-400 dark:text-neutral-600" |
| 80 | + aria-hidden="true" |
| 81 | + > |
| 82 | + {Array.from({ length: lineCount }, (_, index) => ( |
| 83 | + <div key={index} className="h-[18px]"> |
| 84 | + {index + 1} |
| 85 | + </div> |
| 86 | + ))} |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + <div className="relative min-h-[240px] overflow-hidden"> |
| 90 | + <div className="pointer-events-none absolute inset-0 overflow-hidden"> |
| 91 | + <div |
| 92 | + ref={highlightRef} |
| 93 | + className="min-w-full px-3 py-2.5 font-mono text-[12px] leading-[18px]" |
| 94 | + style={{ width: "max-content" }} |
| 95 | + > |
| 96 | + <SyntaxHighlighter |
| 97 | + key={isDark ? "sql-dark" : "sql-light"} |
| 98 | + language="sql" |
| 99 | + style={isDark ? vs2015 : vs} |
| 100 | + customStyle={{ |
| 101 | + margin: 0, |
| 102 | + padding: 0, |
| 103 | + background: "transparent", |
| 104 | + color: isDark ? "#dcdcdc" : "#111827", |
| 105 | + fontSize: "12px", |
| 106 | + lineHeight: "18px", |
| 107 | + overflow: "visible", |
| 108 | + whiteSpace: "pre", |
| 109 | + }} |
| 110 | + codeTagProps={{ |
| 111 | + style: { |
| 112 | + fontFamily: "inherit", |
| 113 | + fontSize: "12px", |
| 114 | + lineHeight: "18px", |
| 115 | + whiteSpace: "pre", |
| 116 | + color: isDark ? "#dcdcdc" : "#111827", |
| 117 | + }, |
| 118 | + }} |
| 119 | + > |
| 120 | + {value || " "} |
| 121 | + </SyntaxHighlighter> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + <textarea |
| 125 | + value={value} |
| 126 | + onChange={event => onChange(event.target.value)} |
| 127 | + onScroll={handleScroll} |
| 128 | + disabled={disabled} |
| 129 | + spellCheck={false} |
| 130 | + wrap="off" |
| 131 | + className={cn( |
| 132 | + "absolute inset-0 min-h-[240px] resize-none overflow-auto border-0 bg-transparent px-3 py-2.5 font-mono text-[12px] leading-[18px] text-transparent outline-none caret-neutral-900", |
| 133 | + "selection:bg-blue-500/20 placeholder:text-neutral-400 focus:ring-0 disabled:cursor-not-allowed disabled:opacity-60", |
| 134 | + "dark:caret-neutral-100 dark:selection:bg-blue-400/25 dark:placeholder:text-neutral-600" |
| 135 | + )} |
| 136 | + /> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + |
| 140 | + <div className="flex h-7 items-center justify-between border-t border-neutral-150 bg-neutral-50 px-3 text-[11px] text-neutral-500 dark:border-neutral-800 dark:bg-neutral-950 dark:text-neutral-500"> |
| 141 | + <span>{lineCount} lines</span> |
| 142 | + <span>{value.length.toLocaleString()} chars</span> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + ); |
| 146 | +} |
0 commit comments