From be96ead83ee6300a0fb5dc0d7787619ef850d0e9 Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Tue, 20 Feb 2024 20:18:47 +0100 Subject: [PATCH] Set inital question from url --- web/src/components/chat.tsx | 11 +++++++++-- web/src/pages/index.tsx | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/web/src/components/chat.tsx b/web/src/components/chat.tsx index f21faae..c478874 100644 --- a/web/src/components/chat.tsx +++ b/web/src/components/chat.tsx @@ -102,14 +102,21 @@ const makeHistory = (query: string, entries: Entry[]): HistoryEntry[] => { type ChatParams = { sessionId: string; settings: LLMSettings; + initialQuery?: string; onQuery?: (q: string) => any; onNewEntry?: (history: Entry[]) => any; }; -const Chat = ({ sessionId, settings, onQuery, onNewEntry }: ChatParams) => { +const Chat = ({ + initialQuery, + sessionId, + settings, + onQuery, + onNewEntry, +}: ChatParams) => { const [entries, setEntries] = useState([]); - const [query, setQuery] = useState(() => randomQuestion()); + const [query, setQuery] = useState(() => initialQuery || randomQuestion()); const [current, setCurrent] = useState(); const [followups, setFollowups] = useState([]); const [controller, setController] = useState(() => new AbortController()); diff --git a/web/src/pages/index.tsx b/web/src/pages/index.tsx index 17865f0..65c4a22 100644 --- a/web/src/pages/index.tsx +++ b/web/src/pages/index.tsx @@ -1,5 +1,6 @@ -import { type NextPage } from "next"; import { useState, useEffect } from "react"; +import { type NextPage } from "next"; +import { useRouter } from "next/router"; import Link from "next/link"; import useSettings from "../hooks/useSettings"; @@ -12,11 +13,15 @@ const MAX_FOLLOWUPS = 4; const Home: NextPage = () => { const [sessionId, setSessionId] = useState(""); const { settings, setMode } = useSettings(); + const router = useRouter(); // initial load useEffect(() => { setSessionId(crypto.randomUUID()); }, []); + const initialQuery = Array.isArray(router?.query?.question) + ? router?.query?.question[0] + : router?.query?.question; return ( @@ -30,7 +35,13 @@ const Home: NextPage = () => { welcomed. - + {router.isReady && ( + + )} ); };