2
0

🐛 Refresh variable list on focus

This commit is contained in:
Baptiste Arnaud
2023-04-19 16:41:35 +02:00
parent fc561430a0
commit b614544909
5 changed files with 32 additions and 25 deletions

View File

@@ -162,6 +162,11 @@ export const VariableSearchInput = ({
return setKeyboardFocusIndex(undefined) return setKeyboardFocusIndex(undefined)
} }
const openDropdown = () => {
if (inputValue === '') setFilteredItems(variables)
onOpen()
}
return ( return (
<Flex ref={dropdownRef} w="full"> <Flex ref={dropdownRef} w="full">
<Popover <Popover
@@ -177,7 +182,7 @@ export const VariableSearchInput = ({
ref={inputRef} ref={inputRef}
value={inputValue} value={inputValue}
onChange={onInputChange} onChange={onInputChange}
onFocus={onOpen} onFocus={openDropdown}
onKeyDown={handleKeyUp} onKeyDown={handleKeyUp}
placeholder={inputProps.placeholder ?? 'Select a variable'} placeholder={inputProps.placeholder ?? 'Select a variable'}
autoComplete="off" autoComplete="off"

View File

@@ -1,5 +1,5 @@
import { Flex, Stack, useColorModeValue } from '@chakra-ui/react' import { Flex, Stack, useColorModeValue } from '@chakra-ui/react'
import React, { useEffect, useRef, useState } from 'react' import React, { useCallback, useEffect, useRef, useState } from 'react'
import { Plate, PlateProvider, usePlateEditorRef } from '@udecode/plate-core' import { Plate, PlateProvider, usePlateEditorRef } from '@udecode/plate-core'
import { editorStyle, platePlugins } from '@/lib/plate' import { editorStyle, platePlugins } from '@/lib/plate'
import { BaseEditor, BaseSelection, Transforms } from 'slate' import { BaseEditor, BaseSelection, Transforms } from 'slate'
@@ -38,17 +38,8 @@ const TextBubbleEditorContent = ({
handler: closeEditor, handler: closeEditor,
}) })
useEffect(() => { const computeTargetCoord = useCallback(() => {
if (!isVariableDropdownOpen) return if (rememberedSelection.current) return { top: 0, left: 0 }
const el = varDropdownRef.current
if (!el) return
const { top, left } = computeTargetCoord()
el.style.top = `${top}px`
el.style.left = `${left}px`
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isVariableDropdownOpen])
const computeTargetCoord = () => {
const selection = window.getSelection() const selection = window.getSelection()
const relativeParent = textEditorRef.current const relativeParent = textEditorRef.current
if (!selection || !relativeParent) return { top: 0, left: 0 } if (!selection || !relativeParent) return { top: 0, left: 0 }
@@ -59,7 +50,17 @@ const TextBubbleEditorContent = ({
top: selectionBoundingRect.bottom - relativeRect.top, top: selectionBoundingRect.bottom - relativeRect.top,
left: selectionBoundingRect.left - relativeRect.left, left: selectionBoundingRect.left - relativeRect.left,
} }
} }, [])
useEffect(() => {
if (!isVariableDropdownOpen) return
const el = varDropdownRef.current
if (!el) return
const { top, left } = computeTargetCoord()
if (top === 0 && left === 0) return
el.style.top = `${top}px`
el.style.left = `${left}px`
}, [computeTargetCoord, isVariableDropdownOpen])
const handleVariableSelected = (variable?: Variable) => { const handleVariableSelected = (variable?: Variable) => {
setIsVariableDropdownOpen(false) setIsVariableDropdownOpen(false)
@@ -110,6 +111,7 @@ const TextBubbleEditorContent = ({
style: editorStyle(useColorModeValue('white', colors.gray[850])), style: editorStyle(useColorModeValue('white', colors.gray[850])),
autoFocus: true, autoFocus: true,
onFocus: () => { onFocus: () => {
rememberedSelection.current = null
if (!isFirstFocus) return if (!isFirstFocus) return
if (editor.children.length === 0) return if (editor.children.length === 0) return
selectEditor(editor, { selectEditor(editor, {

View File

@@ -65,16 +65,16 @@ export const ChatCompletionMessageItem = ({ item, onItemChange }: Props) => {
/> />
{item.role === 'Messages sequence ✨' ? ( {item.role === 'Messages sequence ✨' ? (
<> <>
<VariableSearchInput
initialVariableId={item.content?.assistantMessagesVariableId}
onSelectVariable={changeAssistantVariableId}
placeholder="Assistant messages variable"
/>
<VariableSearchInput <VariableSearchInput
initialVariableId={item.content?.userMessagesVariableId} initialVariableId={item.content?.userMessagesVariableId}
onSelectVariable={changeUserVariableId} onSelectVariable={changeUserVariableId}
placeholder="User messages variable" placeholder="User messages variable"
/> />
<VariableSearchInput
initialVariableId={item.content?.assistantMessagesVariableId}
onSelectVariable={changeAssistantVariableId}
placeholder="Assistant messages variable"
/>
</> </>
) : ( ) : (
<Textarea <Textarea

View File

@@ -88,7 +88,7 @@ export const OpenAIChatCompletionSettings = ({
<AccordionIcon /> <AccordionIcon />
</AccordionButton> </AccordionButton>
<AccordionPanel> <AccordionPanel pt="4">
<TableList <TableList
initialItems={options.messages} initialItems={options.messages}
Item={ChatCompletionMessageItem} Item={ChatCompletionMessageItem}
@@ -125,7 +125,7 @@ export const OpenAIChatCompletionSettings = ({
<AccordionIcon /> <AccordionIcon />
</AccordionButton> </AccordionButton>
<AccordionPanel> <AccordionPanel pt="4">
<TableList <TableList
initialItems={options.responseMapping} initialItems={options.responseMapping}
Item={ChatCompletionResponseItem} Item={ChatCompletionResponseItem}

View File

@@ -127,7 +127,7 @@ export const WebhookAdvancedConfigForm = ({
Query params Query params
<AccordionIcon /> <AccordionIcon />
</AccordionButton> </AccordionButton>
<AccordionPanel> <AccordionPanel pt="4">
<TableList<KeyValue> <TableList<KeyValue>
initialItems={webhook.queryParams} initialItems={webhook.queryParams}
onItemsChange={handleQueryParamsChange} onItemsChange={handleQueryParamsChange}
@@ -141,7 +141,7 @@ export const WebhookAdvancedConfigForm = ({
Headers Headers
<AccordionIcon /> <AccordionIcon />
</AccordionButton> </AccordionButton>
<AccordionPanel> <AccordionPanel pt="4">
<TableList<KeyValue> <TableList<KeyValue>
initialItems={webhook.headers} initialItems={webhook.headers}
onItemsChange={handleHeadersChange} onItemsChange={handleHeadersChange}
@@ -176,7 +176,7 @@ export const WebhookAdvancedConfigForm = ({
Variable values for test Variable values for test
<AccordionIcon /> <AccordionIcon />
</AccordionButton> </AccordionButton>
<AccordionPanel> <AccordionPanel pt="4">
<TableList<VariableForTest> <TableList<VariableForTest>
initialItems={ initialItems={
options?.variablesForTest ?? { byId: {}, allIds: [] } options?.variablesForTest ?? { byId: {}, allIds: [] }
@@ -208,7 +208,7 @@ export const WebhookAdvancedConfigForm = ({
Save in variables Save in variables
<AccordionIcon /> <AccordionIcon />
</AccordionButton> </AccordionButton>
<AccordionPanel> <AccordionPanel pt="4">
<TableList<ResponseVariableMapping> <TableList<ResponseVariableMapping>
initialItems={options.responseVariableMapping} initialItems={options.responseVariableMapping}
onItemsChange={handleResponseMappingChange} onItemsChange={handleResponseMappingChange}