1- import { type ReactNode , useCallback , useEffect , useState } from "react" ;
1+ import { type ReactNode , useEffect , useState } from "react" ;
22
33import { Button } from "@/components/ui/button" ;
44import {
@@ -19,18 +19,14 @@ import {
1919import { Textarea } from "@/components/ui/textarea" ;
2020import { Paragraph } from "@/components/ui/typography" ;
2121import { cn } from "@/lib/utils" ;
22+ import {
23+ detectLanguage ,
24+ isLanguageOption ,
25+ LANGUAGE_OPTIONS ,
26+ } from "@/utils/detectLanguage" ;
2227
2328import CodeEditor from "../CodeViewer/CodeEditor" ;
2429
25- const LANGUAGE_OPTIONS = [
26- { value : "plaintext" , label : "Plain Text" } ,
27- { value : "yaml" , label : "YAML" } ,
28- { value : "python" , label : "Python" } ,
29- { value : "javascript" , label : "JavaScript" } ,
30- { value : "json" , label : "JSON" } ,
31- { value : "sql" , label : "SQL" } ,
32- ] ;
33-
3430interface MultilineTextInputDialogProps {
3531 title : ReactNode ;
3632 description ?: string ;
@@ -57,34 +53,39 @@ export const MultilineTextInputDialog = ({
5753 onConfirm,
5854} : MultilineTextInputDialogProps ) => {
5955 const [ value , setValue ] = useState ( initialValue ) ;
60- const [ selectedLanguage , setSelectedLanguage ] = useState ( "plaintext" ) ;
56+ const [ selectedLanguage , setSelectedLanguage ] = useState ( ( ) =>
57+ detectLanguage ( initialValue ) ,
58+ ) ;
6159
62- const handleConfirm = useCallback ( ( ) => {
60+ const handleConfirm = ( ) => {
6361 onConfirm ( value ) ;
64- } , [ value , onConfirm ] ) ;
62+ } ;
6563
66- const handleCancel = useCallback ( ( ) => {
64+ const handleCancel = ( ) => {
6765 setValue ( initialValue ) ;
6866 onCancel ( ) ;
69- } , [ initialValue , onCancel ] ) ;
67+ } ;
7068
71- const setCursorToEnd = useCallback (
72- ( ref : HTMLTextAreaElement | null ) => {
73- if ( ref && open ) {
74- ref . focus ( ) ;
75- ref . setSelectionRange ( ref . value . length , ref . value . length ) ;
76- }
77- } ,
78- [ open ] ,
79- ) ;
69+ const handleSelectValueChange = ( v : string ) => {
70+ if ( isLanguageOption ( v ) ) {
71+ setSelectedLanguage ( v ) ;
72+ }
73+ } ;
74+
75+ const setCursorToEnd = ( ref : HTMLTextAreaElement | null ) => {
76+ if ( ref && open ) {
77+ ref . focus ( ) ;
78+ ref . setSelectionRange ( ref . value . length , ref . value . length ) ;
79+ }
80+ } ;
8081
8182 useEffect ( ( ) => {
8283 setValue ( initialValue ) ;
8384 } , [ initialValue ] ) ;
8485
8586 useEffect ( ( ) => {
86- setSelectedLanguage ( "plaintext" ) ;
87- } , [ highlightSyntax ] ) ;
87+ setSelectedLanguage ( detectLanguage ( initialValue ) ) ;
88+ } , [ initialValue ] ) ;
8889
8990 return (
9091 < Dialog open = { open } onOpenChange = { onCancel } >
@@ -97,7 +98,7 @@ export const MultilineTextInputDialog = ({
9798 { highlightSyntax && (
9899 < Select
99100 value = { selectedLanguage }
100- onValueChange = { setSelectedLanguage }
101+ onValueChange = { handleSelectValueChange }
101102 >
102103 < SelectTrigger className = "w-40" >
103104 < SelectValue />
0 commit comments