'use client' import * as React from 'react' import { Button } from '@admin/components/ui/button' import { Textarea } from '@admin/components/ui/textarea' import { TrashIcon } from 'lucide-react' export interface MathEditorControlsProps { disabled?: boolean initialValue?: string onApply: (value: string) => boolean | undefined onComplete?: () => void onRemove?: () => boolean | undefined } export function MathEditorControls({ disabled, initialValue = '', onApply, onComplete, onRemove }: MathEditorControlsProps) { const [value, setValue] = React.useState(initialValue) const textareaRef = React.useRef(null) const canApply = Boolean(value.trim()) && !disabled React.useEffect(() => { if (disabled) return textareaRef.current?.focus({ preventScroll: true }) }, [disabled]) const applyMath = React.useCallback(() => { if (!canApply) return const didApply = onApply(value) if (didApply === false) return if (!initialValue) { setValue('') } onComplete?.() }, [canApply, initialValue, onApply, onComplete, value]) const removeMath = React.useCallback(() => { if (disabled || !onRemove) return const didRemove = onRemove() if (didRemove === false) return onComplete?.() }, [disabled, onComplete, onRemove]) const handleKeyDown = React.useCallback( (event: React.KeyboardEvent) => { if (event.key !== 'Enter' || (!event.metaKey && !event.ctrlKey)) return event.preventDefault() applyMath() }, [applyMath] ) return (