Stop timer if deployment failed

This commit is contained in:
Shreerang Kale 2024-10-24 10:06:10 +05:30 committed by Adw8
parent 7a226dfbfd
commit d96a7af224
3 changed files with 45 additions and 49 deletions

View File

@ -1,3 +1,4 @@
import { useEffect } from 'react';
import { useStopwatch } from 'react-timer-hook'; import { useStopwatch } from 'react-timer-hook';
import FormatMillisecond, { FormatMilliSecondProps } from './FormatMilliSecond'; import FormatMillisecond, { FormatMilliSecondProps } from './FormatMilliSecond';
@ -12,14 +13,21 @@ const setStopWatchOffset = (time: string) => {
interface StopwatchProps extends Omit<FormatMilliSecondProps, 'time'> { interface StopwatchProps extends Omit<FormatMilliSecondProps, 'time'> {
offsetTimestamp: Date; offsetTimestamp: Date;
isPaused: boolean;
} }
const Stopwatch = ({ offsetTimestamp, ...props }: StopwatchProps) => { const Stopwatch = ({ offsetTimestamp, isPaused, ...props }: StopwatchProps) => {
const { totalSeconds } = useStopwatch({ const { totalSeconds, pause } = useStopwatch({
autoStart: true, autoStart: true,
offsetTimestamp: offsetTimestamp, offsetTimestamp: offsetTimestamp,
}); });
useEffect(() => {
if (isPaused) {
pause();
}
}, [isPaused]);
return <FormatMillisecond time={totalSeconds * 1000} {...props} />; return <FormatMillisecond time={totalSeconds * 1000} {...props} />;
}; };

View File

@ -1,5 +1,7 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useNavigate, useParams, useSearchParams } from 'react-router-dom'; import { useNavigate, useParams, useSearchParams } from 'react-router-dom';
import axios from 'axios';
import { Deployment } from 'gql-client';
import { DeployStep, DeployStatus } from './DeployStep'; import { DeployStep, DeployStatus } from './DeployStep';
import { Stopwatch, setStopWatchOffset } from '../../StopWatch'; import { Stopwatch, setStopWatchOffset } from '../../StopWatch';
@ -35,6 +37,7 @@ const Deploy = () => {
const projectId = searchParams.get('projectId'); const projectId = searchParams.get('projectId');
const [open, setOpen] = React.useState(false); const [open, setOpen] = React.useState(false);
const [deployment, setDeployment] = useState<Deployment>();
const [record, setRecord] = useState<Record>(); const [record, setRecord] = useState<Record>();
const handleOpen = () => setOpen(!open); const handleOpen = () => setOpen(!open);
@ -46,61 +49,47 @@ const Deploy = () => {
navigate(`/${orgSlug}/projects/create`); navigate(`/${orgSlug}/projects/create`);
}, []); }, []);
const showSteps = useMemo(() => { const isDeploymentFailed = useMemo(() => {
if (!record) { if (!record) {
return true; return false;
} }
// Not checking for `REMOVED` status as this status is received for a brief period before receiving `DEPLOYED` status // Not checking for `REMOVED` status as this status is received for a brief period before receiving `DEPLOYED` status
if (record.lastState === 'CANCELLED' || record.lastState === 'ERROR') { if (record.lastState === 'CANCELLED' || record.lastState === 'ERROR') {
return false; return true;
} else { } else {
return true;
}
}, [record]);
const showTimer = useMemo(() => {
if (!record) {
return true;
}
// Not checking for `REMOVED` status as this status is received for a brief period before receiving `DEPLOYED` status
if (
record.lastState === 'CANCELLED' ||
record.lastState === 'ERROR' ||
record.lastState === 'DEPLOYED'
) {
return false; return false;
} else {
return true;
} }
}, [record]); }, [record]);
const fetchDeploymentRecords = useCallback(async () => { const fetchDeploymentRecords = useCallback(async () => {
if (!deployment) {
return;
}
try {
const response = await axios.get(
`${deployment.deployer.deployerApiUrl}/${deployment.applicationDeploymentRequestId}`,
);
const record: Record = response.data;
setRecord(record);
} catch (err: any) {
console.log('Error fetching data from deployer', err);
}
}, [deployment]);
const fetchDeployment = useCallback(async () => {
if (!projectId) { if (!projectId) {
return; return;
} }
const { deployments } = await client.getDeployments(projectId); const { deployments } = await client.getDeployments(projectId);
const deployment = deployments[0]; setDeployment(deployments[0]);
try {
const response = await fetch(
`${deployment.deployer.deployerApiUrl}/${deployment.applicationDeploymentRequestId}`,
);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const record: Record = await response.json();
setRecord(record);
} catch (err: any) {
console.log('Error fetching data from deployer', err);
}
}, [client, projectId]); }, [client, projectId]);
useEffect(() => { useEffect(() => {
fetchDeployment();
fetchDeploymentRecords(); fetchDeploymentRecords();
const interval = setInterval(() => { const interval = setInterval(() => {
@ -110,7 +99,7 @@ const Deploy = () => {
return () => { return () => {
clearInterval(interval); clearInterval(interval);
}; };
}, [fetchDeploymentRecords]); }, [fetchDeployment, fetchDeploymentRecords]);
useEffect(() => { useEffect(() => {
if (!record) { if (!record) {
@ -129,14 +118,13 @@ const Deploy = () => {
<Heading as="h4" className="md:text-lg font-medium"> <Heading as="h4" className="md:text-lg font-medium">
Deployment started ... Deployment started ...
</Heading> </Heading>
{showTimer && ( <div className="flex items-center gap-1.5">
<div className="flex items-center gap-1.5"> <ClockOutlineIcon size={16} className="text-elements-mid-em" />
<ClockOutlineIcon size={16} className="text-elements-mid-em" /> <Stopwatch
<Stopwatch offsetTimestamp={setStopWatchOffset(Date.now().toString())}
offsetTimestamp={setStopWatchOffset(Date.now().toString())} isPaused={isDeploymentFailed}
/> />
</div> </div>
)}
</div> </div>
<Button <Button
onClick={handleOpen} onClick={handleOpen}
@ -153,7 +141,7 @@ const Deploy = () => {
/> />
</div> </div>
{showSteps ? ( {!isDeploymentFailed ? (
<div> <div>
<DeployStep <DeployStep
title={record ? 'Submitted' : 'Submitting'} title={record ? 'Submitted' : 'Submitting'}

View File

@ -60,7 +60,7 @@ const DeployStep = ({
{status === DeployStatus.PROCESSING && ( {status === DeployStatus.PROCESSING && (
<div className="flex items-center gap-1.5"> <div className="flex items-center gap-1.5">
<ClockOutlineIcon size={16} className="text-elements-low-em" /> <ClockOutlineIcon size={16} className="text-elements-low-em" />
<Stopwatch offsetTimestamp={setStopWatchOffset(startTime!)} /> <Stopwatch offsetTimestamp={setStopWatchOffset(startTime!)} isPaused={false}/>
</div> </div>
)} )}
{status === DeployStatus.COMPLETE && ( {status === DeployStatus.COMPLETE && (