diff --git a/site/src/components/WorkspaceBuildProgress/WorkspaceBuildProgress.tsx b/site/src/components/WorkspaceBuildProgress/WorkspaceBuildProgress.tsx index f9685b8951da4..f1d61ee26ea15 100644 --- a/site/src/components/WorkspaceBuildProgress/WorkspaceBuildProgress.tsx +++ b/site/src/components/WorkspaceBuildProgress/WorkspaceBuildProgress.tsx @@ -11,23 +11,19 @@ dayjs.extend(duration) const estimateFinish = ( startedAt: Dayjs, - templateAverage?: number, + buildEstimate: number, ): [number, string] => { - if (templateAverage === undefined) { - return [0, "Unknown"] - } - const realPercentage = dayjs().diff(startedAt) / templateAverage + const realPercentage = dayjs().diff(startedAt) / buildEstimate - // Showing a full bar is frustrating. - const maxPercentage = 0.99 + const maxPercentage = 1 if (realPercentage > maxPercentage) { - return [maxPercentage, "Any moment now..."] + return [maxPercentage * 100, "Any moment now..."] } return [ - realPercentage, + realPercentage * 100, `~${Math.ceil( - dayjs.duration((1 - realPercentage) * templateAverage).asSeconds(), + dayjs.duration((1 - realPercentage) * buildEstimate).asSeconds(), )} seconds remaining...`, ] } @@ -62,49 +58,55 @@ export const WorkspaceBuildProgress: FC = ({ }) => { const styles = useStyles() const job = workspace.latest_build.job - const [progressValue, setProgressValue] = useState(0) + const [progressValue, setProgressValue] = useState(0) // By default workspace is updated every second, which can cause visual stutter // when the build estimate is a few seconds. The timer ensures no observable // stutter in all cases. useEffect(() => { const updateProgress = () => { - if (job.status !== "running") { - setProgressValue(0) + if (job.status !== "running" || buildEstimate === undefined) { + setProgressValue(undefined) return } - setProgressValue( - estimateFinish(dayjs(job.started_at), buildEstimate)[0] * 100, - ) + const est = estimateFinish(dayjs(job.started_at), buildEstimate)[0] + setProgressValue(est) } - setTimeout(updateProgress, 100) + setTimeout(updateProgress, 5) }, [progressValue, job, buildEstimate]) - // buildEstimate may be undefined if the template is new or coderd hasn't - // finished initial metrics collection. - if (buildEstimate === undefined) { - return ( -
- -
-
{`Build ${job.status}`}
-
Unknown ETA
-
-
- ) - } - return (
{`Build ${job.status}`}
- {job.status === "running" && - estimateFinish(dayjs(job.started_at), buildEstimate)[1]} + {(() => { + if (job.status !== "running") { + return "" + } else if (buildEstimate !== undefined) { + return estimateFinish(dayjs(job.started_at), buildEstimate)[1] + } else { + return "Unknown ETA" + } + })()}
@@ -116,6 +118,9 @@ const useStyles = makeStyles((theme) => ({ paddingLeft: theme.spacing(0.2), paddingRight: theme.spacing(0.2), }, + noTransition: { + transition: "none", + }, barHelpers: { display: "flex", justifyContent: "space-between",