mirror of
https://github.com/Novattz/creamlinux-installer.git
synced 2026-01-24 12:22:49 -05:00
unlocker select hook #61
This commit is contained in:
71
src/hooks/useUnlockerSelection.ts
Normal file
71
src/hooks/useUnlockerSelection.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { useState, useCallback } from 'react'
|
||||
import { Game } from '@/types'
|
||||
import { ActionType } from '@/components/buttons'
|
||||
|
||||
export interface UnlockerSelectionState {
|
||||
visible: boolean
|
||||
gameId: string | null
|
||||
gameTitle: string | null
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for managing unlocker selection on native games
|
||||
*/
|
||||
export function useUnlockerSelection() {
|
||||
const [selectionState, setSelectionState] = useState<UnlockerSelectionState>({
|
||||
visible: false,
|
||||
gameId: null,
|
||||
gameTitle: null,
|
||||
})
|
||||
|
||||
// Store the callback to call when user makes a selection
|
||||
const [selectionCallback, setSelectionCallback] = useState<((action: ActionType) => void) | null>(
|
||||
null
|
||||
)
|
||||
|
||||
// Show the dialog and store the callback
|
||||
const showUnlockerSelection = useCallback(
|
||||
(game: Game, callback: (action: ActionType) => void) => {
|
||||
setSelectionState({
|
||||
visible: true,
|
||||
gameId: game.id,
|
||||
gameTitle: game.title,
|
||||
})
|
||||
// Wrap in function to avoid stale closure
|
||||
setSelectionCallback(() => callback)
|
||||
},
|
||||
[]
|
||||
)
|
||||
|
||||
// User selected CreamLinux
|
||||
const handleSelectCreamLinux = useCallback(() => {
|
||||
setSelectionState({ visible: false, gameId: null, gameTitle: null })
|
||||
if (selectionCallback) {
|
||||
selectionCallback('install_cream')
|
||||
}
|
||||
setSelectionCallback(null)
|
||||
}, [selectionCallback])
|
||||
|
||||
// User selected SmokeAPI
|
||||
const handleSelectSmokeAPI = useCallback(() => {
|
||||
setSelectionState({ visible: false, gameId: null, gameTitle: null })
|
||||
if (selectionCallback) {
|
||||
selectionCallback('install_smoke')
|
||||
}
|
||||
setSelectionCallback(null)
|
||||
}, [selectionCallback])
|
||||
|
||||
// Close dialog without selection
|
||||
const closeDialog = useCallback(() => {
|
||||
setSelectionState({ visible: false, gameId: null, gameTitle: null })
|
||||
setSelectionCallback(null)
|
||||
}, [])
|
||||
|
||||
return {
|
||||
selectionState,
|
||||
showUnlockerSelection,
|
||||
handleSelectCreamLinux,
|
||||
handleSelectSmokeAPI,
|
||||
closeDialog,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user