import React, { useState, useEffect, useRef } from 'react' import { useAskAI } from '../hooks/useAskAI.js' import LoadingSkeleton from './LoadingSkeleton.jsx' export default function AskAI({ adminData, user, agent, onClose }) { const { askQuestion, loading, error, chatHistory, clearChatHistory, loadChatHistory } = useAskAI(adminData, user, agent) const [question, setQuestion] = useState('') const [isComposing, setIsComposing] = useState(false) const chatEndRef = useRef(null) useEffect(() => { // チャット履歴を読み込み loadChatHistory() }, [loadChatHistory]) useEffect(() => { // 新しいメッセージが追加されたら一番下にスクロール if (chatEndRef.current) { chatEndRef.current.scrollIntoView({ behavior: 'smooth' }) } }, [chatHistory]) const handleSubmit = async (e) => { e.preventDefault() if (!question.trim() || loading) return try { await askQuestion(question) setQuestion('') } catch (err) { // エラーはuseAskAIで処理済み } } const handleKeyDown = (e) => { if (e.key === 'Enter' && !e.shiftKey && !isComposing) { e.preventDefault() handleSubmit(e) } if (e.key === 'Escape') { onClose?.() } } const formatTimestamp = (timestamp) => { return new Date(timestamp).toLocaleString('ja-JP', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }) } const renderMessage = (entry, index) => (