|
| 1 | +import {Box, Text} from 'ink' |
| 2 | +import {useContext, useEffect, useState} from 'react' |
| 3 | + |
| 4 | +import {CommandContext, GameContext, JobFollow, JobLogTail, JobProgress, JobStatusTable} from '@cli/components/index.js' |
| 5 | +import {Job, JobStatus, ShipGameFlags} from '@cli/types/index.js' |
| 6 | +import {useProjectJobListener, useStartShipOnMount} from '@cli/utils/hooks/index.js' |
| 7 | + |
| 8 | +import {KeyboardShortcuts} from './KeyboardShortcuts.js' |
| 9 | +import {ShipResult} from './ShipResult.js' |
| 10 | + |
| 11 | +interface Props { |
| 12 | + onComplete: (completedJobs: Job[]) => void |
| 13 | + onError: (error: any) => void |
| 14 | +} |
| 15 | + |
| 16 | +export const Ship = ({onComplete, onError}: Props): JSX.Element | null => { |
| 17 | + const {command} = useContext(CommandContext) |
| 18 | + const {gameId} = useContext(GameContext) |
| 19 | + if (!command || !gameId) return null |
| 20 | + return <ShipCommand onComplete={onComplete} onError={onError} command={command} gameId={gameId} /> |
| 21 | +} |
| 22 | + |
| 23 | +interface ShipCommandProps extends Props { |
| 24 | + command: any |
| 25 | + gameId: string |
| 26 | +} |
| 27 | + |
| 28 | +const ShipCommand = ({onComplete, onError, command, gameId}: ShipCommandProps) => { |
| 29 | + const [showLog, setShowLog] = useState<boolean>(false) |
| 30 | + const flags = command && (command.getFlags() as ShipGameFlags) |
| 31 | + const {jobs: startedJobs, shipLog} = useStartShipOnMount(command, flags, onError) |
| 32 | + const [failedJobs, setFailedJobs] = useState<Job[]>([]) |
| 33 | + const [successJobs, setSuccessJobs] = useState<Job[]>([]) |
| 34 | + const [isComplete, setIsComplete] = useState<boolean>(false) |
| 35 | + |
| 36 | + const handleJobCompleted = (job: Job) => setSuccessJobs((prev) => [...prev, job]) |
| 37 | + const handleJobFailed = (job: Job) => setFailedJobs((prev) => [...prev, job]) |
| 38 | + |
| 39 | + const {jobsById} = useProjectJobListener({ |
| 40 | + projectId: gameId, |
| 41 | + onJobCompleted: handleJobCompleted, |
| 42 | + onJobFailed: handleJobFailed, |
| 43 | + }) |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + // Detect when all jobs done and trigger onComplete or onError |
| 47 | + const totalCompleted = successJobs.length + failedJobs.length |
| 48 | + if (startedJobs && totalCompleted === startedJobs.length) { |
| 49 | + setIsComplete(true) |
| 50 | + setTimeout(() => { |
| 51 | + const didFail = failedJobs.length > 0 |
| 52 | + if (didFail) { |
| 53 | + onError(new Error('One or more jobsById failed.')) |
| 54 | + } else { |
| 55 | + onComplete(successJobs) |
| 56 | + } |
| 57 | + }, 500) |
| 58 | + } |
| 59 | + }, [successJobs, failedJobs, startedJobs]) |
| 60 | + |
| 61 | + // Use startedJobs just for the ids - the "live" objects come from jobsById |
| 62 | + const inProgressJobs = startedJobs |
| 63 | + ?.map((startedJob) => jobsById[startedJob.id]) |
| 64 | + .filter((job) => job !== undefined) |
| 65 | + .filter((job) => job && [JobStatus.PENDING, JobStatus.PROCESSING].includes(job.status)) |
| 66 | + |
| 67 | + if (flags?.follow) { |
| 68 | + if (startedJobs && startedJobs.length > 0) { |
| 69 | + return <JobFollow jobId={startedJobs[0].id} projectId={gameId} /> |
| 70 | + } |
| 71 | + return <></> |
| 72 | + } |
| 73 | + |
| 74 | + return ( |
| 75 | + <Box flexDirection="column"> |
| 76 | + {startedJobs === null && <Text>{shipLog}</Text>} |
| 77 | + {inProgressJobs && |
| 78 | + inProgressJobs.map((job) => ( |
| 79 | + <Box flexDirection="column" key={job.id} marginBottom={1}> |
| 80 | + <JobStatusTable isWatching={true} jobId={job.id} projectId={job.project.id} /> |
| 81 | + <Box flexDirection="column"> |
| 82 | + <JobProgress job={job} /> |
| 83 | + </Box> |
| 84 | + {showLog && ( |
| 85 | + <Box marginTop={1}> |
| 86 | + <JobLogTail isWatching={true} jobId={job.id} length={10} projectId={job.project.id} /> |
| 87 | + </Box> |
| 88 | + )} |
| 89 | + </Box> |
| 90 | + ))} |
| 91 | + {jobsById && !isComplete && ( |
| 92 | + <> |
| 93 | + <KeyboardShortcuts gameId={gameId} jobs={inProgressJobs} onToggleJobLogs={setShowLog} /> |
| 94 | + <Text bold>Please wait while ShipThis builds your game...</Text> |
| 95 | + </> |
| 96 | + )} |
| 97 | + {isComplete && <ShipResult gameId={gameId} failedJobs={failedJobs} gameFlags={flags} />} |
| 98 | + </Box> |
| 99 | + ) |
| 100 | +} |
0 commit comments