From a4ac66f0ba1a413ea18ae3828737313f373eee4e Mon Sep 17 00:00:00 2001 From: Dhravya Date: Mon, 15 Apr 2024 14:48:49 -0700 Subject: enter to send --- apps/web/src/components/Main.tsx | 200 +++++++++++++++++++-------------------- 1 file changed, 98 insertions(+), 102 deletions(-) (limited to 'apps/web/src/components') diff --git a/apps/web/src/components/Main.tsx b/apps/web/src/components/Main.tsx index 060ae28b..07a36a1e 100644 --- a/apps/web/src/components/Main.tsx +++ b/apps/web/src/components/Main.tsx @@ -45,7 +45,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { const [selectedSpaces, setSelectedSpaces] = useState([]); - const [isStreaming, setIsStreaming] = useState(false) + const [isStreaming, setIsStreaming] = useState(false); useEffect(() => { const search = searchParams.get("q"); @@ -70,7 +70,6 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { // window.scrollTo(0, document.body.scrollHeight); // } // } - // window.visualViewport?.addEventListener("resize", onResize); // return () => { // window.visualViewport?.removeEventListener("resize", onResize); @@ -187,27 +186,32 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { }), }, ); - - console.log("sources", sourcesResponse) - const sourcesInJson = getIdsFromSource(((await sourcesResponse.json()) as { - ids: string[] - }).ids) ?? []; + console.log("sources", sourcesResponse); + const sourcesInJson = + getIdsFromSource( + ( + (await sourcesResponse.json()) as { + ids: string[]; + } + ).ids, + ) ?? []; - const notesInSources = sourcesInJson.filter( - (urls) => urls.startsWith("https://notes.supermemory.dhr.wtf/") - ) - const nonNotes = sourcesInJson.filter( - i => !notesInSources.includes(i) - ) + const notesInSources = sourcesInJson.filter((urls) => + urls.startsWith("https://notes.supermemory.dhr.wtf/"), + ); + const nonNotes = sourcesInJson.filter((i) => !notesInSources.includes(i)); - const fetchedTitles = await getMemoriesFromUrl(notesInSources); - - const sources = [ - ...nonNotes.map(n => ({ isNote: false, source: n ?? "" })), - ...fetchedTitles.map(n => ({ isNote: true, source: n.title ?? "" })) - ] + const fetchedTitles = await getMemoriesFromUrl(notesInSources); + + const sources = [ + ...nonNotes.map((n) => ({ isNote: false, source: n ?? "" })), + ...fetchedTitles.map((n) => ({ + isNote: true, + source: n.title ?? "", + })), + ]; setIsAiLoading(false); setChatHistory((prev) => { @@ -218,7 +222,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { ...lastMessage, answer: { parts: lastMessage.answer.parts, - sources + sources, }, }, ]; @@ -243,7 +247,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { return; } - setIsStreaming(true) + setIsStreaming(true); if (response.body) { let reader = response.body?.getReader(); @@ -263,12 +267,11 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { return reader?.read().then(processText); }); - } }; const onSend = () => { - if (value.trim().length < 1) return + if (value.trim().length < 1) return; setLayout("chat"); getSearchResults(); }; @@ -280,7 +283,6 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { e.target.rows = Math.min(5, lines); } - return ( <> @@ -293,12 +295,12 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) { askQuestion={onSend} setValue={setValue} value={value} - selectedSpaces={selectedSpaces} - setSelectedSpaces={setSelectedSpaces} + selectedSpaces={selectedSpaces} + setSelectedSpaces={setSelectedSpaces} /> ) : (
Smort logo -
- {width <= 768 && } -
+
+ {width <= 768 && } +

Ask your second brain

- { - textArea.current?.querySelector("textarea")?.focus(); - }} - side="top" - align="start" - className="bg-[#252525] mr-auto md:hidden" - selectedSpaces={selectedSpaces} - setSelectedSpaces={setSelectedSpaces} - /> - { - if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) { - onSend(); - } - }, - }} - > -
- - { - textArea.current?.querySelector("textarea")?.focus(); - }} - className="hidden md:flex" - selectedSpaces={selectedSpaces} - setSelectedSpaces={setSelectedSpaces} - /> - -
-
- - { + textArea.current?.querySelector("textarea")?.focus(); + }} + side="top" + align="start" + className="mr-auto bg-[#252525] md:hidden" + selectedSpaces={selectedSpaces} + setSelectedSpaces={setSelectedSpaces} + /> + { + if (e.key === "Enter") { + onSend(); + } + }, + }} + > +
+ { + textArea.current?.querySelector("textarea")?.focus(); + }} + className="hidden md:flex" + selectedSpaces={selectedSpaces} + setSelectedSpaces={setSelectedSpaces} + /> + +
+
+ +
-
-
+ + )} - {width <= 768 && } + {width <= 768 && }
); @@ -431,8 +430,8 @@ export function Chat({ askQuestion, setValue, value, - selectedSpaces, - setSelectedSpaces + selectedSpaces, + setSelectedSpaces, }: { sidebarOpen: boolean; isLoading?: boolean; @@ -440,8 +439,8 @@ export function Chat({ askQuestion: () => void; setValue: (value: string) => void; value: string; - selectedSpaces: number[]; - setSelectedSpaces: React.Dispatch>; + selectedSpaces: number[]; + setSelectedSpaces: React.Dispatch>; }) { const textArea = useRef(null); @@ -451,7 +450,7 @@ export function Chat({ const lines = countLines(e.target); e.target.rows = Math.min(5, lines); } - + const { width } = useViewport(); return ( @@ -461,13 +460,10 @@ export function Chat({ "sidebar relative flex w-full flex-col items-end gap-5 px-5 pt-5 transition-[padding-left,padding-top,padding-right] delay-200 duration-200 md:items-center md:gap-10 md:px-72 [&[data-sidebar-open='true']]:pr-10 [&[data-sidebar-open='true']]:delay-0 md:[&[data-sidebar-open='true']]:pl-[calc(2.5rem+30vw)]", )} > - -
- {width <= 768 && } -
-
+
+ {width <= 768 && } +
+
{chatHistory.map((msg, i) => ( {msg.question} @@ -488,7 +484,7 @@ export function Chat({ data-sidebar-open={sidebarOpen} className="absolute flex w-full items-center justify-center" > -
+
{ @@ -496,7 +492,7 @@ export function Chat({ }} side="top" align="start" - className="bg-[#252525] mr-auto" + className="mr-auto bg-[#252525]" selectedSpaces={selectedSpaces} setSelectedSpaces={setSelectedSpaces} /> -- cgit v1.2.3