/**
* Chatbot Page — main entry point with internal sidebar navigation.
*
* Returns { sidebar, content } to be rendered inside AppLayout.
*/
import { useState, useCallback, useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
commentContent,
people,
help,
chartBar,
cog,
postContent,
} from '@wordpress/icons';
import AppLayout from '../../Layout/AppLayout';
import InternalSidebar from '../../Layout/InternalSidebar';
import BotList from './views/BotList';
import BotEditor from './views/BotEditor';
import Conversations from './views/Conversations';
import ConversationView from './views/ConversationView';
import KnowledgeBase from './views/KnowledgeBase';
import ChatbotAnalytics from './views/Analytics';
import ChatbotSettings from './views/Settings';
import useApi from '../../../hooks/useApi';
const ChatbotPage = () => {
const parseHash = () => {
const hash = window.location.hash.replace( '#', '' );
if ( ! hash ) return { key: 'analytics', params: {} };
const [ key, id ] = hash.split( '/' );
return { key, params: id ? { id: parseInt( id ) || id } : {} };
};
const initial = parseHash();
const [ view, setView ] = useState( initial.key );
const [ viewParams, setViewParams ] = useState( initial.params );
const { get } = useApi();
const [ activeCount, setActiveCount ] = useState( 0 );
/* Fetch active conversation count for sidebar badge */
const fetchActiveCount = useCallback( async () => {
try {
const res = await get( '/chatbot/conversations/active' );
setActiveCount( Array.isArray( res ) ? res.length : ( res.count || 0 ) );
} catch ( e ) {
// silent
}
}, [ get ] );
useEffect( () => {
fetchActiveCount();
}, [ fetchActiveCount ] );
/* Refresh badge count every 15 seconds */
const acRef = useRef( fetchActiveCount );
acRef.current = fetchActiveCount;
useEffect( () => {
const interval = setInterval( () => acRef.current(), 15000 );
return () => clearInterval( interval );
}, [] );
/* Listen to hash changes for deep linking (e.g. from dashboard widget) */
useEffect( () => {
const onHashChange = () => {
const { key, params } = parseHash();
setView( key );
setViewParams( params );
};
window.addEventListener( 'hashchange', onHashChange );
return () => window.removeEventListener( 'hashchange', onHashChange );
}, [] );
const navigate = useCallback( ( key, params = {} ) => {
setView( key );
setViewParams( params );
window.location.hash = params.id ? `${ key }/${ params.id }` : key;
}, [] );
const sidebarItems = [
{ key: 'analytics', label: __( 'Analytics', 'ai-marketing-expert' ), icon: chartBar },
{ key: 'bots', label: __( 'Chatbots', 'ai-marketing-expert' ), icon: commentContent },
{
key: 'conversations',
label: __( 'Conversations', 'ai-marketing-expert' ),
icon: people,
badge: activeCount || 0,
},
{ key: 'knowledge', label: __( 'Knowledge Base', 'ai-marketing-expert' ), icon: postContent },
{ key: 'settings', label: __( 'Settings', 'ai-marketing-expert' ), icon: cog },
];
const renderContent = () => {
switch ( view ) {
case 'analytics':
return ;
case 'bots':
return ;
case 'edit-bot':
return navigate( 'bots' ) } onNavigate={ navigate } />;
case 'new-bot':
return navigate( 'bots' ) } onNavigate={ navigate } />;
case 'conversations':
return ;
case 'conversation':
return navigate( 'conversations' ) } onNavigate={ navigate } />;
case 'knowledge':
return ;
case 'settings':
return ;
default:
return ;
}
};
const getActiveSidebarKey = () => {
if ( view === 'conversation' ) return 'conversations';
if ( view === 'edit-bot' || view === 'new-bot' ) return 'bots';
return view;
};
const sidebar = (
);
return (
{ renderContent() }
);
};
export default ChatbotPage;