diff --git a/web/src/components/features/modal/NewChatModal/NewChatModal.tsx b/web/src/components/features/modal/NewChatModal/NewChatModal.tsx deleted file mode 100644 index 19849c0d6..000000000 --- a/web/src/components/features/modal/NewChatModal/NewChatModal.tsx +++ /dev/null @@ -1,284 +0,0 @@ -import React, { useEffect, useMemo, useRef, useState } from 'react'; -import { Button, Modal, Input, InputRef, ConfigProvider, ThemeConfig } from 'antd'; -import { AppMaterialIcons } from '@/components/ui'; -import { useMemoizedFn, useMount, useThrottleFn } from 'ahooks'; -import { useAntToken } from '@/styles/useAntToken'; -import { useBusterNewChatContextSelector } from '@/context/Chats'; -import { inputHasText, timeout } from '@/lib'; -import { useBusterSearchContextSelector } from '@/context/Search'; -import type { BusterSearchResult } from '@/api/asset_interfaces'; -import { useBusterNotifications } from '@/context/BusterNotifications'; -import { NewChatModalDataSourceSelect } from './NewChatModalDatasourceSelect'; -import { NoDatasets } from './NoDatasets'; -import { useAppLayoutContextSelector } from '@/context/BusterAppLayout'; -import { useGetDatasets } from '@/api/buster_rest/datasets'; -import { NewDatasetModal } from '../NewDatasetModal'; -const { TextArea } = Input; - -const themeConfig: ThemeConfig = { - components: { - Modal: { - paddingMD: 4, - paddingContentHorizontalLG: 4 - } - } -}; - -const modalClassNames = { - body: 'p-0!' -}; - -export const NewChatModal = React.memo<{ - open: boolean; - onClose: () => void; -}>(({ open, onClose }) => { - const token = useAntToken(); - const onChangePage = useAppLayoutContextSelector((x) => x.onChangePage); - const { openErrorNotification } = useBusterNotifications(); - const { isFetched: isFetchedDatasets, data: datasetsList } = useGetDatasets(); - const onBusterSearch = useBusterSearchContextSelector((x) => x.onBusterSearch); - - const [selectedChatDataSource, setSelectedChatDataSource] = useState<{ - id: string; - name: string; - } | null>(null); - const [openNewDatasetModal, setOpenNewDatasetModal] = useState(false); - const [prompt, setPrompt] = useState(''); - const [suggestedPrompts, setSuggestedPrompts] = useState([]); - const [activeItem, setActiveItem] = useState(null); - const [defaultSuggestedPrompts, setDefaultSuggestedPrompts] = useState([]); - const lastKeyPressed = useRef(null); - const hasDatasets = datasetsList.length > 0 && isFetchedDatasets; - const shownPrompts = prompt.length > 1 ? suggestedPrompts : defaultSuggestedPrompts; - - const memoizedHasDatasetStyle = useMemo(() => { - return { - padding: `${token.paddingSM}px ${token.paddingSM}px`, - paddingTop: token.paddingSM, - paddingBottom: 0 - }; - }, []); - - const getSuggestedChatPrompts = useMemoizedFn(async (prompt: string) => { - const res = await onBusterSearch({ - query: prompt - }); - return res; - }); - - const { run: debouncedGetSuggestedChatPrompts } = useThrottleFn( - async (v: string) => { - try { - // const prompts = await getSuggestedChatPrompts(v); - // setSuggestedPrompts(prompts); - // return prompts; - return []; - } catch (e) { - openErrorNotification(e); - } - }, - { wait: 350 } - ); - - const getDefaultSuggestedPrompts = useMemoizedFn(() => { - getSuggestedChatPrompts('').then((prompts) => { - setDefaultSuggestedPrompts(prompts); - }); - }); - - useEffect(() => { - if (open) { - if (defaultSuggestedPrompts.length === 0) { - getDefaultSuggestedPrompts(); - } - - const handleKeyPress = (event: KeyboardEvent) => { - lastKeyPressed.current = event.code; - }; - document.addEventListener('keydown', handleKeyPress); - - return () => { - document.removeEventListener('keydown', handleKeyPress); - }; - } - }, [open]); - - return ( - - - {hasDatasets && ( -
- - - -
- )} - - {!hasDatasets && ( - - )} - - {/* {hasDatasets && showSuggested && } */} - - {/* {hasDatasets && ( - - )} */} -
- - {!hasDatasets && ( - setOpenNewDatasetModal(false)} - afterCreate={onClose} - /> - )} -
- ); -}); -NewChatModal.displayName = 'NewChatModal'; - -const NewChatInput: React.FC<{ - setSuggestedPrompts: (prompts: BusterSearchResult[]) => void; - debouncedGetSuggestedChatPrompts: (prompt: string) => Promise; - shownPrompts: BusterSearchResult[]; - lastKeyPressed: React.MutableRefObject; - activeItem: number | null; - prompt: string; - setPrompt: (prompt: string) => void; - selectedChatDataSource: { - id: string; - name: string; - } | null; -}> = React.memo( - ({ - setSuggestedPrompts, - debouncedGetSuggestedChatPrompts, - activeItem, - shownPrompts, - lastKeyPressed, - prompt, - setPrompt, - selectedChatDataSource - }) => { - const token = useAntToken(); - const inputRef = useRef(null); - const onStartNewChat = useBusterNewChatContextSelector((x) => x.onStartNewChat); - const onSelectSearchAsset = useBusterNewChatContextSelector((x) => x.onSelectSearchAsset); - const [loadingNewChat, setLoadingNewChat] = useState(false); - - const onStartNewChatPreflight = useMemoizedFn(async () => { - setLoadingNewChat(true); - await onStartNewChat({ prompt, datasetId: selectedChatDataSource?.id }); - await timeout(250); - setPrompt(''); - setLoadingNewChat(false); - }); - - const onChangeText = useMemoizedFn((e: React.ChangeEvent) => { - const value = e.currentTarget.value; - setPrompt(value); - if (value.length < 1) { - setSuggestedPrompts([]); - } else { - debouncedGetSuggestedChatPrompts(e.currentTarget.value); - } - }); - - const onPressEnter = useMemoizedFn((v: React.KeyboardEvent) => { - const value = v.currentTarget.value; - const lastKeyPressedWasUpOrDown = - lastKeyPressed.current === 'ArrowUp' || lastKeyPressed.current === 'ArrowDown'; - - if ( - typeof activeItem === 'number' && - shownPrompts[activeItem]?.name && - lastKeyPressedWasUpOrDown - ) { - const foundAsset = shownPrompts[activeItem]; - if (foundAsset) { - onSelectSearchAsset(foundAsset); - } - v.stopPropagation(); - v.preventDefault(); - return; - } - if (v.shiftKey) { - return; - } - onStartNewChatPreflight(); - }); - - const onClickSubmitButton = useMemoizedFn(() => { - onStartNewChatPreflight(); - }); - - useMount(() => { - setTimeout(() => { - inputRef.current?.focus(); - }, 175); - }); - - const autoSizeMemoized = useMemo(() => { - return { minRows: 1, maxRows: 16 }; - }, []); - - return ( -
-