Consultant: clear input on send and show the asked question above the answer
The question stayed in the input after sending. Now it clears automatically and the submitted question appears as a chat bubble above the response, so the panel reads like a normal prompt/chat (your message on top, the assistant's answer below).
This commit is contained in:
1 parent
8cf6ca8cb5
commit
bae032aa75
1 file changed
+16
-4
@@ -27,6 +27,7 @@ export default function AiConsultant({
|
||||
const tm = useTranslations('menu');
|
||||
const locale = useLocale();
|
||||
const [question, setQuestion] = useState('');
|
||||
const [asked, setAsked] = useState(''); // the submitted question, shown above the answer
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [answer, setAnswer] = useState<{ intro?: string; recommendations: Recommendation[] } | null>(
|
||||
null,
|
||||
@@ -43,7 +44,10 @@ export default function AiConsultant({
|
||||
}, [buildSuggestions, seed, answer, resolveDish]);
|
||||
|
||||
async function ask() {
|
||||
if (!question.trim() || loading) return;
|
||||
const q = question.trim();
|
||||
if (!q || loading) return;
|
||||
setAsked(q); // show it above the answer (chat-style)
|
||||
setQuestion(''); // clear the input automatically
|
||||
setLoading(true);
|
||||
setError(false);
|
||||
setAnswer(null);
|
||||
@@ -51,7 +55,7 @@ export default function AiConsultant({
|
||||
const res = await fetch(`/api/${slug}/ai-consult`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ question, locale }),
|
||||
body: JSON.stringify({ question: q, locale }),
|
||||
});
|
||||
if (!res.ok) throw new Error('failed');
|
||||
setAnswer(await res.json());
|
||||
@@ -101,8 +105,16 @@ export default function AiConsultant({
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="min-h-[6rem] flex-1 overflow-y-auto no-scrollbar">
|
||||
{!answer && !loading && <p className="text-sm text-[var(--text)]/60">{t('intro')}</p>}
|
||||
<div className="flex min-h-[6rem] flex-1 flex-col gap-3 overflow-y-auto no-scrollbar">
|
||||
{!asked && !loading && <p className="text-sm text-[var(--text)]/60">{t('intro')}</p>}
|
||||
{asked && (
|
||||
<div
|
||||
className="max-w-[85%] self-end rounded-2xl px-4 py-2 text-sm text-white"
|
||||
style={{ backgroundColor: '#1e3a5f' }}
|
||||
>
|
||||
{asked}
|
||||
</div>
|
||||
)}
|
||||
{loading && <p className="animate-pulse text-sm text-[var(--text)]/60">{t('thinking')}</p>}
|
||||
{error && <p className="text-sm text-red-600">⚠️</p>}
|
||||
{answer && (
|
||||
|
||||
Reference in new issue
Block a user