From a7278c801ac11847ec9f1c3c7514b2f3132aef86 Mon Sep 17 00:00:00 2001 From: Nicholai Date: Sun, 18 Jan 2026 04:53:55 -0700 Subject: [PATCH] Refactor Hubert chat input and improve message persistence - Create dedicated HubertInput component with reliable auto-resize - Save all messages automatically to D1 database - Add visitor name saving via model tool call - Add migration for visitor name column - Fix conversation creation in new-visitor endpoint Co-Authored-By: Claude Opus 4.5 --- src/components/HubertChat.tsx | 104 ++----------- src/components/HubertInput.tsx | 125 +++++++++++++++ src/db/migrations/002_add_visitor_name.sql | 4 + src/pages/api/hubert/chat.ts | 169 ++++++++++++++++----- src/pages/api/hubert/new-visitor.ts | 23 ++- 5 files changed, 291 insertions(+), 134 deletions(-) create mode 100644 src/components/HubertInput.tsx create mode 100644 src/db/migrations/002_add_visitor_name.sql diff --git a/src/components/HubertChat.tsx b/src/components/HubertChat.tsx index a35619e..46fef2e 100644 --- a/src/components/HubertChat.tsx +++ b/src/components/HubertChat.tsx @@ -1,8 +1,8 @@ -import React, { useRef, useEffect, useState } from 'react'; +import React from 'react'; import { marked } from 'marked'; import DOMPurify from 'dompurify'; -import { motion } from 'framer-motion'; import { useHubertChat } from '../hooks/useHubertChat'; +import HubertInput from './HubertInput'; // Configure marked for safe rendering marked.setOptions({ @@ -29,34 +29,6 @@ export default function HubertChat() { messagesEndRef, } = useHubertChat({ initTimeout: 8000, chatTimeout: 30000 }); - const textareaRef = useRef(null); - const [inputHeight, setInputHeight] = useState(40); - - // Auto-resize textarea and track height for dynamic border radius - const adjustTextareaHeight = () => { - const textarea = textareaRef.current; - if (textarea) { - textarea.style.height = 'auto'; - const newHeight = Math.min(textarea.scrollHeight, 200); - textarea.style.height = `${newHeight}px`; - setInputHeight(newHeight); - } - }; - - useEffect(() => { - adjustTextareaHeight(); - }, [input]); - - // Check if multiline for padding adjustments - const isMultiline = inputHeight > 48; - - const handleKeyDown = (e: React.KeyboardEvent) => { - if (e.key === 'Enter' && !e.shiftKey) { - e.preventDefault(); - sendMessage(); - } - }; - // Initial/Loading state - centered branding with input if (isInitializing && !initError) { return ( @@ -102,35 +74,13 @@ export default function HubertChat() { {/* Input bar */}
- -