@@ -2,11 +2,18 @@ import React, { Fragment, useState } from 'react';
22import { PropTypes } from 'prop-types' ;
33import { Dialog , Transition , Listbox } from '@headlessui/react' ;
44import Button from 'components/atoms/common/Button' ;
5- import { BUTTON_TYPES } from 'constants/Common' ;
5+ import { BUTTON_TYPES , GENAI_MODELS } from 'constants/Common' ;
66import { CheckIcon , ChevronUpDownIcon } from '@heroicons/react/20/solid' ;
77import { Square3Stack3DIcon } from '@heroicons/react/24/outline' ;
8+ import { generateFlowData } from '../flow/flowtestai' ;
9+ import { init } from '../flow' ;
10+ import useCanvasStore from 'stores/CanvasStore' ;
11+ import { toast } from 'react-toastify' ;
12+
13+ const GenerateFlowTestModal = ( { closeFn = ( ) => null , open = false , collectionId } ) => {
14+ const setNodes = useCanvasStore ( ( state ) => state . setNodes ) ;
15+ const setEdges = useCanvasStore ( ( state ) => state . setEdges ) ;
816
9- const GenerateFlowTestModal = ( { closeFn = ( ) => null , open = false } ) => {
1017 const [ selectedModel , setSelectedModel ] = useState ( null ) ;
1118 const [ textareaValue , setTextareaValue ] = useState ( '' ) ;
1219
@@ -26,7 +33,7 @@ const GenerateFlowTestModal = ({ closeFn = () => null, open = false }) => {
2633 </ Transition . Child >
2734
2835 < div className = 'fixed inset-0 overflow-y-auto' >
29- < div className = 'flex min-h-full items-center justify-center p-4 text-center' >
36+ < div className = 'flex items-center justify-center min-h-full p-4 text-center' >
3037 < Transition . Child
3138 as = { Fragment }
3239 enter = 'ease-out duration-300'
@@ -36,69 +43,51 @@ const GenerateFlowTestModal = ({ closeFn = () => null, open = false }) => {
3643 leaveFrom = 'opacity-100 scale-100'
3744 leaveTo = 'opacity-0 scale-95'
3845 >
39- < Dialog . Panel className = 'w-full max-w-md transform overflow-hidden rounded-2xl bg-white p-6 text-left align-middle shadow-xl transition-all ' >
46+ < Dialog . Panel className = 'w-full max-w-md p-6 overflow-hidden text-left align-middle transition-all transform bg-white shadow-xl rounded-2xl ' >
4047 < Dialog . Title
4148 as = 'h3'
42- className = 'border-b border-neutral-300 pb-4 text-center text- lg font-semibold text-gray-900'
49+ className = 'pb-4 text-lg font-semibold text-center text- gray-900 border-b border-neutral-300 '
4350 >
44- Use our AI to generate the flowtest
51+ Use our AI to generate the flow
4552 </ Dialog . Title >
4653 < div className = 'mt-6' >
4754 < Listbox value = { selectedModel } onChange = { setSelectedModel } >
48- < div className = 'relative flex h -full w -full' >
49- < Listbox . Button className = 'flex w-full cursor-default items-center justify-between gap-4 rounded border border-neutral-300 py-4 ' >
50- < div className = 'min-w-32 pl-2 text-left' > { selectedModel ? selectedModel : 'Select model' } </ div >
51- < ChevronUpDownIcon className = 'h -5 w -5' aria-hidden = 'true' />
55+ < div className = 'relative flex w -full h -full' >
56+ < Listbox . Button className = 'flex items-center justify-between w-full gap-4 py-4 border rounded cursor-default border-neutral-300' >
57+ < div className = 'pl-2 text-left min-w-32 ' > { selectedModel ? selectedModel : 'Select model' } </ div >
58+ < ChevronUpDownIcon className = 'w -5 h -5' aria-hidden = 'true' />
5259 </ Listbox . Button >
5360 < Transition
5461 as = { Fragment }
5562 leave = 'transition ease-in duration-100'
5663 leaveFrom = 'opacity-100'
5764 leaveTo = 'opacity-0'
5865 >
59- < Listbox . Options className = 'absolute right-0 top-6 z-10 mt-1 max-h-60 w-full overflow-auto rounded bg-white py-1 text-base shadow-lg ring-1 ring-black/5' >
60- < Listbox . Option
61- className = { ( { active } ) =>
62- `relative cursor-default select-none py-2 pl-10 pr-4 ${
63- active ? 'bg-amber-100 text-amber-900' : 'text-gray-900'
64- } `
65- }
66- value = { 'Model 1' }
67- >
68- { ( { selected } ) => (
69- < >
70- < span className = { `block truncate ${ selected ? 'font-medium' : 'font-normal' } ` } >
71- Model 1
72- </ span >
73- { selected ? (
74- < span className = 'absolute inset-y-0 left-0 flex items-center pl-3 text-amber-600' >
75- < CheckIcon className = 'h-5 w-5' aria-hidden = 'true' />
76- </ span >
77- ) : null }
78- </ >
79- ) }
80- </ Listbox . Option >
81- < Listbox . Option
82- className = { ( { active } ) =>
83- `relative cursor-default select-none py-2 pl-10 pr-4 ${
84- active ? 'bg-amber-100 text-amber-900' : 'text-gray-900'
85- } `
86- }
87- value = { 'Model 2' }
88- >
89- { ( { selected } ) => (
90- < >
91- < span className = { `block truncate ${ selected ? 'font-medium' : 'font-normal' } ` } >
92- Model 2
93- </ span >
94- { selected ? (
95- < span className = 'absolute inset-y-0 left-0 flex items-center pl-3 text-amber-600' >
96- < CheckIcon className = 'h-5 w-5' aria-hidden = 'true' />
66+ < Listbox . Options className = 'absolute right-0 z-10 w-full py-1 mt-1 overflow-auto text-base bg-white rounded shadow-lg top-6 max-h-60 ring-1 ring-black/5' >
67+ { Object . values ( GENAI_MODELS ) . map ( ( modelType ) => (
68+ < Listbox . Option
69+ key = { modelType }
70+ className = { ( { active } ) =>
71+ `relative cursor-default select-none py-2 pl-10 pr-4 ${
72+ active ? 'bg-amber-100 text-amber-900' : 'text-gray-900'
73+ } `
74+ }
75+ value = { modelType }
76+ >
77+ { ( { selected } ) => (
78+ < >
79+ < span className = { `block truncate ${ selected ? 'font-medium' : 'font-normal' } ` } >
80+ { modelType }
9781 </ span >
98- ) : null }
99- </ >
100- ) }
101- </ Listbox . Option >
82+ { selected ? (
83+ < span className = 'absolute inset-y-0 left-0 flex items-center pl-3 text-amber-600' >
84+ < CheckIcon className = 'w-5 h-5' aria-hidden = 'true' />
85+ </ span >
86+ ) : null }
87+ </ >
88+ ) }
89+ </ Listbox . Option >
90+ ) ) }
10291 </ Listbox . Options >
10392 </ Transition >
10493 </ div >
@@ -107,12 +96,12 @@ const GenerateFlowTestModal = ({ closeFn = () => null, open = false }) => {
10796 < textarea
10897 id = 'gen-ai-text'
10998 className = 'block w-full rounded border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 outline-blue-300 focus:border-blue-100 focus:ring-blue-100'
110- placeholder = 'Text '
99+ placeholder = 'Describe your flow step by step '
111100 onChange = { ( event ) => setTextareaValue ( event . target . value ) }
112101 />
113102 </ div >
114103 </ div >
115- < div className = 'mt-6 flex items-center gap-2' >
104+ < div className = 'flex items-center gap-2 mt-6 ' >
116105 < Button btnType = { BUTTON_TYPES . error } isDisabled = { false } onClickHandle = { closeFn } fullWidth = { true } >
117106 Cancel
118107 </ Button >
@@ -121,8 +110,19 @@ const GenerateFlowTestModal = ({ closeFn = () => null, open = false }) => {
121110 isDisabled = { false }
122111 fullWidth = { true }
123112 onClickHandle = { ( ) => {
124- console . log ( `\n Model selected : ${ selectedModel } \n` ) ;
125- console . log ( `\n Model Text : ${ textareaValue } \n` ) ;
113+ generateFlowData ( textareaValue , selectedModel , collectionId )
114+ . then ( ( flowData ) => {
115+ const result = init ( flowData ) ;
116+ console . log ( result ) ;
117+ setNodes ( result . nodes ) ;
118+ setEdges ( result . edges ) ;
119+ closeFn ( ) ;
120+ } )
121+ . catch ( ( error ) => {
122+ console . log ( error ) ;
123+ toast . error ( `Error while generating flow data` ) ;
124+ closeFn ( ) ;
125+ } ) ;
126126 } }
127127 >
128128 Generate
0 commit comments