import { useCallback, useEffect, useState } from 'react'; import { __ } from '@wordpress/i18n'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Modal } from '@/components/ui/modal'; import { ajax } from '@/lib/api'; import type { HistoryUser } from './types'; interface HistoryEntry { created_at: string; user_message: string; bot_response: string; } interface HistoryDialogProps { /** The user to show; null keeps the dialog closed. */ user: HistoryUser | null; nonce: string; onClose: () => void; } /** One user's conversation, optionally filtered by date range. */ export default function HistoryDialog( { user, nonce, onClose }: HistoryDialogProps ) { const [ from, setFrom ] = useState( '' ); const [ to, setTo ] = useState( '' ); const [ history, setHistory ] = useState( null ); // null while loading. const load = useCallback( async ( dateFrom: string, dateTo: string ) => { if ( ! user ) { return; } setHistory( null ); try { const response = await ajax<{ history: HistoryEntry[] }>( 'wtofe_chatbot_get_user_history', { nonce, user_id: user.user_id, date_from: dateFrom, date_to: dateTo }, 'GET' ); setHistory( response?.success && response.data ? response.data.history : [] ); } catch { setHistory( [] ); } }, [ nonce, user ] ); useEffect( () => { if ( user ) { setFrom( '' ); setTo( '' ); load( '', '' ); } }, [ user, load ] ); const filters = (
); return ( { null === history &&

{ __( 'Loading…', 'ai-content-generator-for-woocommerce' ) }

} { history && ! history.length &&

{ __( 'No messages found.', 'ai-content-generator-for-woocommerce' ) }

} { !! history?.length && (
{ history.map( ( entry, index ) => (
{ entry.created_at }
{ __( 'User:', 'ai-content-generator-for-woocommerce' ) } { entry.user_message }
{ __( 'Bot:', 'ai-content-generator-for-woocommerce' ) } { entry.bot_response }
) ) }
) }
); }