-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract scenario management logic into custom hook
- Move scenario types to dedicated types/game.ts file - Create useScenarioManager hook to handle scenario state and fetching - Clean up game.tsx by removing redundant code and imports - Improve type safety and code organization - Fix typo in choiseScenarios -> choiceScenarios
- Loading branch information
Showing
3 changed files
with
99 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useState, useRef, useEffect } from 'react'; | ||
import { supabase } from "~/lib/supabase/client"; | ||
import type { ClientScenario } from '~/types/game'; | ||
|
||
export function useScenarioManager(startingScenarioId: number) { | ||
const [currentScenario, setCurrentScenario] = useState<ClientScenario | undefined>(); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [nextCard, setNextCard] = useState<ClientScenario | undefined>(); | ||
|
||
const choiceScenarios = useRef<{ | ||
optionA: ClientScenario | undefined; | ||
optionB: ClientScenario | undefined; | ||
}>({ | ||
optionA: undefined, | ||
optionB: undefined, | ||
}); | ||
|
||
const generateScenario = async (scenarioId: number) => { | ||
const { data } = await supabase.functions.invoke("generateScenario", { | ||
body: { scenarioId }, | ||
}); | ||
return data.data; | ||
}; | ||
|
||
const prefetchNextScenarios = async (scenario: ClientScenario) => { | ||
for (const key of ["optionA", "optionB"] as const) { | ||
const nextScenario = await generateScenario(scenario[key].id); | ||
choiceScenarios.current = { | ||
...choiceScenarios.current, | ||
[key]: nextScenario, | ||
}; | ||
} | ||
}; | ||
|
||
const initializeScenario = async () => { | ||
try { | ||
setIsLoading(true); | ||
const generatedScenario = await generateScenario(startingScenarioId); | ||
setCurrentScenario(generatedScenario); | ||
await prefetchNextScenarios(generatedScenario); | ||
} catch (error) { | ||
console.error("Failed to load scenario:", error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
initializeScenario(); | ||
}, []); | ||
|
||
return { | ||
currentScenario, | ||
setCurrentScenario, | ||
isLoading, | ||
choiceScenarios, | ||
nextCard, | ||
setNextCard, | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type ClientScenario = { | ||
situation: string; | ||
optionA: { text: string; id: number }; | ||
optionB: { text: string; id: number }; | ||
}; |