import { useState, useEffect, useCallback } from "react"; import { __ } from "@wordpress/i18n"; import type { ConversationSummary, ConversationDetail, UsageInfo, } from "./data/types"; import { ApiError, fetchConversations, fetchConversation, fetchUsage, } from "./data/api"; import ConversationList from "./components/ConversationList"; import ConversationDetailView from "./components/ConversationDetail"; import UsageCard from "./components/UsageCard"; import ConnectionRequired from "./components/ConnectionRequired"; import styles from "./App.module.css"; import layoutStyles from "./components/HistoryLayout.module.css"; export default function App() { const [conversations, setConversations] = useState( [], ); const [selectedId, setSelectedId] = useState(null); const [detail, setDetail] = useState(null); const [usage, setUsage] = useState(null); const [loadingConversations, setLoadingConversations] = useState(true); const [loadingDetail, setLoadingDetail] = useState(false); const [error, setError] = useState(null); const [connectionLost, setConnectionLost] = useState(false); const requiresConnection = !window.flexibleChatHistory.isConnected || connectionLost; useEffect(() => { if (requiresConnection) { return; } fetchConversations() .then(setConversations) .catch((err: unknown) => { if (err instanceof ApiError && err.status === 401) { setConnectionLost(true); return; } setError( __( "Unable to load conversations. Please try again.", "flexible-chat", ), ); }) .finally(() => setLoadingConversations(false)); fetchUsage() .then(setUsage) .catch((err: unknown) => { if (err instanceof ApiError && err.status === 401) { setConnectionLost(true); } }); }, [requiresConnection]); const handleSelect = useCallback((id: string) => { setSelectedId(id); setLoadingDetail(true); setError(null); fetchConversation(id) .then(setDetail) .catch((err: unknown) => { if (err instanceof ApiError && err.status === 401) { setConnectionLost(true); return; } setError( __( "Unable to load conversation. Please try again.", "flexible-chat", ), ); }) .finally(() => setLoadingDetail(false)); }, []); if (requiresConnection) { return ; } return (
{error && (

{error}

)} {selectedId ? ( loadingDetail ? (
) : ( detail && ( ) ) ) : (

{__( "Select a conversation to view details.", "flexible-chat", )}

)}
); }