From 457ab9dbdac1f0151a1272dc55efb8249aa92ce5 Mon Sep 17 00:00:00 2001 From: Dhravya Date: Wed, 10 Apr 2024 00:26:22 -0700 Subject: [PATCH] filter spaces support --- apps/web/src/components/Main.tsx | 29 ++++++++--- .../src/components/Sidebar/FilterCombobox.tsx | 51 ++++++++++--------- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/apps/web/src/components/Main.tsx b/apps/web/src/components/Main.tsx index 4e0392c9..62db6afe 100644 --- a/apps/web/src/components/Main.tsx +++ b/apps/web/src/components/Main.tsx @@ -10,6 +10,7 @@ import { cn, countLines } from '@/lib/utils'; import { ChatHistory } from '../../types/memory'; import { ChatAnswer, ChatMessage, ChatQuestion } from './ChatMessage'; import { useRouter, useSearchParams } from 'next/navigation'; +import { useMemory } from '@/contexts/MemoryContext'; function supportsDVH() { try { @@ -29,6 +30,8 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { const { width } = useViewport(); const [isAiLoading, setIsAiLoading] = useState(false); + const { spaces } = useMemory(); + // Variable to keep track of the chat history in this session const [chatHistory, setChatHistory] = useState([]); @@ -37,7 +40,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { const textArea = useRef(null); const main = useRef(null); - const [selectedSpaces, setSelectedSpaces] = useState([]); + const [selectedSpaces, setSelectedSpaces] = useState([]); useEffect(() => { const search = searchParams.get('q'); @@ -199,12 +202,19 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { ]; }); - const response = await fetch(`/api/chat?q=${_value}&spaces=${selectedSpaces.join(",")}`, { - method: 'POST', - body: JSON.stringify({ - chatHistory: modifyChatHistory(chatHistory), - }), - }); + const actualSelectedSpaces = selectedSpaces.map( + (space) => spaces.find((s) => s.id === space)?.title ?? '', + ); + + const response = await fetch( + `/api/chat?q=${_value}&spaces=${actualSelectedSpaces.join(',')}`, + { + method: 'POST', + body: JSON.stringify({ + chatHistory: modifyChatHistory(chatHistory), + }), + }, + ); if (response.status !== 200) { setIsAiLoading(false); @@ -294,6 +304,8 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { textArea.current?.querySelector('textarea')?.focus(); }} className="hidden md:flex" + selectedSpaces={selectedSpaces} + setSelectedSpaces={setSelectedSpaces} /> @@ -104,7 +107,7 @@ export function FilterCombobox({ key={space.id} value={space.id.toString()} onSelect={(val) => { - setValues((prev) => + setSelectedSpaces((prev: number[]) => prev.includes(parseInt(val)) ? prev.filter((v) => v !== parseInt(val)) : [...prev, parseInt(val)], @@ -122,11 +125,11 @@ export function FilterCombobox({ > {space.title} - {values.includes(space.id)} + {selectedSpaces.includes(space.id)}