-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from polywrap/rollback/progress-bar
rollback: progress bar init
- Loading branch information
Showing
4 changed files
with
31 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,56 @@ | ||
import { Tables } from "@/supabase/dbTypes"; | ||
import { COMPLETED_TEXTS } from "@/utils/logs"; | ||
import { useState, useEffect } from "react"; | ||
|
||
export function useProgressTime( | ||
stepTimes: number[], | ||
logs: Array<Tables<"logs">>, | ||
prompt: string, | ||
logs: Array<Tables<"logs">> | ||
) { | ||
const [startTime] = useState(Date.now()); | ||
const [startTime, setStartTime] = useState(Date.now()); | ||
const [progressInformation, setProgressInformation] = useState({ | ||
logs, | ||
time: 0, | ||
progress: 0, | ||
}); | ||
|
||
// Capture the start time when the hook is first called or when stepTimes or currentStep changes | ||
const totalTime = stepTimes.reduce((a, b) => a + b, 0); | ||
let currentStep = logs.findIndex( | ||
(x) => x.status === "IN_PROGRESS" | ||
); | ||
|
||
useEffect(() => { | ||
const totalTime = stepTimes.reduce((a, b) => a + b, 0); | ||
setStartTime(Date.now()) | ||
}, [currentStep]) | ||
|
||
useEffect(() => { | ||
const intervalId = setInterval(function () { | ||
const now = Date.now(); | ||
const secondsFromStart = (now - startTime) / 1000; // Convert ms to seconds | ||
let currentStep = progressInformation.logs.findIndex( | ||
(x) => x.status === "IN_PROGRESS" | ||
); | ||
const elapsedTimeSinceStart = (now - startTime) / 1000; // Convert ms to seconds | ||
|
||
if (elapsedTimeSinceStart > stepTimes[currentStep]) { | ||
return | ||
} | ||
|
||
const elapsedTimeInSteps = stepTimes | ||
.slice(0, currentStep + 1) | ||
.slice(0, currentStep) | ||
.reduce((a, b) => a + b, 0); | ||
|
||
const timeRemaining = Math.max(totalTime - secondsFromStart, 0); | ||
|
||
const progress = (secondsFromStart / totalTime) * 100; | ||
if (timeRemaining <= 1) { | ||
clearInterval(intervalId); | ||
return; | ||
} | ||
const totalElapsedTime = elapsedTimeSinceStart + elapsedTimeInSteps; | ||
|
||
if ( | ||
secondsFromStart > elapsedTimeInSteps && | ||
stepTimes.length > currentStep && | ||
currentStep !== -1 | ||
) { | ||
setProgressInformation(({ logs }) => { | ||
const newLogs = [...logs]; | ||
newLogs[currentStep].status = "COMPLETED"; | ||
newLogs[currentStep].value = COMPLETED_TEXTS[newLogs[currentStep].step_name] | ||
if (currentStep === 0) { | ||
newLogs[currentStep].value += " to " + prompt | ||
} | ||
const timeRemaining = Math.max(totalTime - totalElapsedTime, 0); // Prevent negative time | ||
const progress = (totalElapsedTime / totalTime) * 100; | ||
|
||
if (stepTimes.length > currentStep + 1) { | ||
newLogs[currentStep + 1].status = "IN_PROGRESS"; | ||
} | ||
return { | ||
time: timeRemaining, | ||
progress: progress, | ||
logs: newLogs, | ||
}; | ||
}); | ||
return; | ||
if (timeRemaining < 1) { | ||
return | ||
} | ||
|
||
setProgressInformation((i) => ({ | ||
...i, | ||
time: timeRemaining, | ||
progress: progress, | ||
})); | ||
|
||
}, 1000); | ||
return () => clearInterval(intervalId); | ||
}, [stepTimes]); | ||
}, [stepTimes, currentStep]); | ||
|
||
return progressInformation; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters