Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: use query engine options for recommended questions #141

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/(main)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useSettingContext } from '@/components/website-setting-provider';
import { withReCaptcha } from '@/components/security-setting-provider';

export default function Page() {
const { loading, ask } = useAsk();
const { loading, setEngine, ask, engine } = useAsk();
const { website: { homepage }, security } = useSettingContext();

return (
Expand All @@ -20,7 +20,7 @@ export default function Page() {
<p className="font-light dark:text-gray-300 text-gray-500 mb-4 w-4/5 md:w-auto text-center">
{homepage?.description || ''}
</p>
<Ask className="px-4 w-full lg:w-2/3" loading={loading} ask={ask} />
<Ask className="px-4 w-full lg:w-2/3" loading={loading} ask={ask} engine={engine} setEngine={setEngine} />
{homepage?.example_questions && (<ul className="flex gap-2 flex-wrap px-4 w-full lg:w-2/3">
{homepage.example_questions.map(item => (
<li key={item.text}>
Expand Down
5 changes: 2 additions & 3 deletions src/components/ask.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { MessageInput } from '@/components/message-input';
import { SecuritySettingContext, withReCaptcha } from '@/components/security-setting-provider';
import { type UseAskReturns } from '@/components/use-ask';
import { useContext, useRef, useState } from 'react';
import { useContext, useRef } from 'react';

export function Ask ({ className, loading, ask }: { className?: string } & UseAskReturns) {
export function Ask ({ className, loading, ask, engine, setEngine }: { className?: string } & UseAskReturns) {
const ref = useRef<HTMLTextAreaElement>(null);
const security = useContext(SecuritySettingContext);
const [engine, setEngine] = useState<number>();

return (
<form
Expand Down
19 changes: 13 additions & 6 deletions src/components/use-ask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { __setMessage } from '@/app/(main)/(public)/c/[id]/internal';
import { handleErrors } from '@/lib/fetch';
import { useSession } from 'next-auth/react';
import { useRouter } from 'next/navigation';
import { useCallback, useState, useTransition } from 'react';
import { mutate } from 'swr'
import { useCallback, useRef, useState, useTransition } from 'react';
import { mutate } from 'swr';

export function useAsk(onFinish?: () => void) {
export function useAsk (onFinish?: () => void) {
const session = useSession();
const router = useRouter();
const [loading, setLoading] = useState(false);
const [engine, setEngine] = useState<number>();
const engineRef = useRef<number>();
const [transitioning, startTransition] = useTransition();

const ask = useCallback((message: string, options?: {
Expand All @@ -22,15 +24,15 @@ export function useAsk(onFinish?: () => void) {
body: JSON.stringify({
messages: [],
name: message,
engine: options?.engine,
engine: engineRef?.current ?? options?.engine,
}),
headers: {...options?.headers},
headers: { ...options?.headers },
}).then(handleErrors)
.then(res => res.json())
.then(res => {
__setMessage(message);
startTransition(() => {
onFinish?.()
onFinish?.();
router.push(`/c/${encodeURIComponent(res.url_key)}`);
});
})
Expand All @@ -45,6 +47,11 @@ export function useAsk(onFinish?: () => void) {

return {
ask,
engine,
setEngine: (engine: number | undefined) => {
engineRef.current = engine;
setEngine(engine);
},
loading: disabled,
};
}
Expand Down