import { useI18n } from '@wordpress/react-i18n'; import { useMemo, useState, ReactNode } from 'react'; import { useDispatch, useSelector } from 'calypso/state'; import { recordTracksEvent } from 'calypso/state/analytics/actions'; import { getSelectedSiteId } from 'calypso/state/ui/selectors'; import { LogEntry, useCodeDeploymentsRunLogDetailQuery } from './use-code-deployment-run-log-query'; import { DeploymentRun } from './use-code-deployment-run-query'; interface DeploymentRunLogsProps { logEntries: LogEntry[]; run: DeploymentRun; } const LogDetail = ( { children }: { children?: ReactNode } ) => (
{ children }
);
const DeploymentRunLog = ( { entry, run }: { entry: LogEntry; run: DeploymentRun } ) => {
const { __ } = useI18n();
const dispatch = useDispatch();
const [ detailExpanded, setDetailExpanded ] = useState( false );
const siteId = useSelector( getSelectedSiteId );
const openDetail = () => setDetailExpanded( ( v ) => ! v );
const commandIdentifier = entry.context?.command.command_identifier;
const hasDetail = !! commandIdentifier;
const {
data: logDetail,
isLoading,
isError,
} = useCodeDeploymentsRunLogDetailQuery(
siteId,
run.code_deployment_id,
run.id,
commandIdentifier ?? null,
{
enabled: detailExpanded && hasDetail,
refetchOnWindowFocus: false,
retry: false,
}
);
const detail = useMemo( () => {
if ( ! logDetail ) {
return false;
}
const { stdout, stderr } = logDetail;
if ( stdout?.length === 0 && stderr?.length === 0 ) {
return false;
}
return (
<>
{ stdout?.join( '\n' ) }
{ stderr?.join( '\n' ) }
>
);
}, [ logDetail ] );
const getDetail = () => {
if ( detail ) {
return