Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: resolve TDZ error — move finishDrill before submitAnswer, use ref for cross-callback calls

Adam Eivy3d352f0b6368
diff --git a/client/src/hooks/usePostSession.js b/client/src/hooks/usePostSession.js
index 645f132d71..f61427a5fe 100644
--- a/client/src/hooks/usePostSession.js
+++ b/client/src/hooks/usePostSession.js
@@ -25,6 +25,7 @@ export function usePostSession() {
const [savedSession, setSavedSession] = useState(null);
const questionStartRef = useRef(Date.now());
const drillStartRef = useRef(Date.now());
+ const finishDrillRef = useRef(null);
const startSession = useCallback(async (drillConfigs) => {
// drillConfigs: [{ type, config, timeLimitSec }]
@@ -49,6 +50,46 @@ export function usePostSession() {
setState(STATES.DRILLING);
}, []);
+ const finishDrill = useCallback((finalAnswers) => {
+ const totalMs = Date.now() - drillStartRef.current;
+ const timeLimitMs = (currentDrill?.timeLimitSec || 120) * 1000;
+
+ // Compute score
+ const correct = finalAnswers.filter(a => a.correct).length;
+ const total = finalAnswers.length;
+ const correctRatio = total > 0 ? correct / total : 0;
+ const answered = finalAnswers.filter(a => a.answered !== null);
+ const totalResponseMs = answered.reduce((sum, a) => sum + a.responseMs, 0);
+ const avgResponseMs = answered.length > 0 ? totalResponseMs / answered.length : timeLimitMs;
+ const speedBonus = Math.max(0, 1 - avgResponseMs / timeLimitMs);
+ const score = Math.min(100, Math.max(0, Math.round((correctRatio * 0.8 + speedBonus * 0.2) * 100)));
+
+ const result = {
+ module: 'mental-math',
+ type: currentDrill.type,
+ config: currentDrill.config,
+ questions: finalAnswers,
+ score,
+ totalMs
+ };
+
+ const newResults = [...drillResults, result];
+ setDrillResults(newResults);
+
+ // Check if there are more drills
+ if (currentDrillIndex + 1 < drills.length) {
+ setState(STATES.BETWEEN_DRILLS);
+ } else {
+ // Session complete
+ const avgScore = Math.round(newResults.reduce((s, r) => s + r.score, 0) / newResults.length);
+ setSessionScore(avgScore);
+ setState(STATES.COMPLETE);
+ }
+ }, [currentDrill, drillResults, currentDrillIndex, drills]);
+
+ // Keep ref current so submitAnswer and timeExpired always call the latest finishDrill
+ finishDrillRef.current = finishDrill;
+
const submitAnswer = useCallback((value) => {
if (state !== STATES.DRILLING || !currentDrill) return;
@@ -78,54 +119,17 @@ export function usePostSession() {
// Check if drill is complete
if (currentQuestionIndex + 1 >= currentDrill.questions.length) {
- finishDrill(newAnswers);
+ finishDrillRef.current(newAnswers);
} else {
setCurrentQuestionIndex(currentQuestionIndex + 1);
questionStartRef.current = Date.now();
}
- }, [state, currentDrill, currentQuestionIndex, answers, finishDrill]);
+ }, [state, currentDrill, currentQuestionIndex, answers]);
const skipQuestion = useCallback(() => {
submitAnswer(null);
}, [submitAnswer]);
- const finishDrill = useCallback((finalAnswers) => {
- const totalMs = Date.now() - drillStartRef.current;
- const timeLimitMs = (currentDrill?.timeLimitSec || 120) * 1000;
-
- // Compute score
- const correct = finalAnswers.filter(a => a.correct).length;
- const total = finalAnswers.length;
- const correctRatio = total > 0 ? correct / total : 0;
- const answered = finalAnswers.filter(a => a.answered !== null);
- const totalResponseMs = answered.reduce((sum, a) => sum + a.responseMs, 0);
- const avgResponseMs = answered.length > 0 ? totalResponseMs / answered.length : timeLimitMs;
- const speedBonus = Math.max(0, 1 - avgResponseMs / timeLimitMs);
- const score = Math.min(100, Math.max(0, Math.round((correctRatio * 0.8 + speedBonus * 0.2) * 100)));
-
- const result = {
- module: 'mental-math',
- type: currentDrill.type,
- config: currentDrill.config,
- questions: finalAnswers,
- score,
- totalMs
- };
-
- const newResults = [...drillResults, result];
- setDrillResults(newResults);
-
- // Check if there are more drills
- if (currentDrillIndex + 1 < drills.length) {
- setState(STATES.BETWEEN_DRILLS);
- } else {
- // Session complete
- const avgScore = Math.round(newResults.reduce((s, r) => s + r.score, 0) / newResults.length);
- setSessionScore(avgScore);
- setState(STATES.COMPLETE);
- }
- }, [currentDrill, drillResults, currentDrillIndex, drills]);
-
const nextDrill = useCallback(async () => {
const nextIndex = currentDrillIndex + 1;
setCurrentDrillIndex(nextIndex);
@@ -160,8 +164,8 @@ export function usePostSession() {
const finalAnswers = [...answers, ...remaining];
setAnswers(finalAnswers);
- finishDrill(finalAnswers);
- }, [state, currentDrill, currentQuestionIndex, answers, finishDrill]);
+ finishDrillRef.current(finalAnswers);
+ }, [state, currentDrill, currentQuestionIndex, answers]);
const saveSession = useCallback(async (tags = {}) => {
setState(STATES.SAVING);