Skip to content

Commit

Permalink
Set inital question from url
Browse files Browse the repository at this point in the history
  • Loading branch information
mruwnik committed Feb 20, 2024
1 parent 9c808ce commit be96ead
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 9 additions & 2 deletions web/src/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry[]>([]);

const [query, setQuery] = useState(() => randomQuestion());
const [query, setQuery] = useState(() => initialQuery || randomQuestion());
const [current, setCurrent] = useState<CurrentSearch>();
const [followups, setFollowups] = useState<Followup[]>([]);
const [controller, setController] = useState(() => new AbortController());
Expand Down
15 changes: 13 additions & 2 deletions web/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 (
<Page page="index">
Expand All @@ -30,7 +35,13 @@ const Home: NextPage = () => {
welcomed.
</h2>

<Chat sessionId={sessionId} settings={{ mode: settings.mode }} />
{router.isReady && (
<Chat
sessionId={sessionId}
settings={{ mode: settings.mode }}
initialQuery={initialQuery}
/>
)}
</Page>
);
};
Expand Down

0 comments on commit be96ead

Please sign in to comment.