-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Grzegorz Tańczyk
committed
Apr 21, 2024
1 parent
c68fcee
commit 89ec24b
Showing
4 changed files
with
99 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, { createContext, useContext, useReducer } from 'react'; | ||
|
||
import { LaunchSite } from '../world/world-state-types'; | ||
|
||
type SelectionDispatchAction = | ||
| { | ||
action: 'clear'; | ||
} | ||
| { | ||
action: 'set'; | ||
object: LaunchSite; | ||
}; | ||
|
||
type Selection = { | ||
selectedObject?: LaunchSite; | ||
}; | ||
|
||
const initialSelection: Selection = {}; | ||
|
||
const selectionReducer: React.Reducer<Selection, SelectionDispatchAction> = ( | ||
selection: Selection, | ||
action: SelectionDispatchAction, | ||
) => { | ||
if (action.action === 'clear') { | ||
return initialSelection; | ||
} else if (action.action === 'set') { | ||
return { ...selection, selectedObject: action.object }; | ||
} else { | ||
return selection; | ||
} | ||
}; | ||
|
||
// definition of react context for selection | ||
const SelectionContext = createContext<Selection>(initialSelection); | ||
|
||
// definition of dispatch function for selection context | ||
const SelectionDispatchContext = createContext<React.Dispatch<SelectionDispatchAction>>(selectionReducer); | ||
|
||
export function SelectionContextWrapper({ children }: { children: React.ReactNode }) { | ||
const [selection, reducer] = useReducer(selectionReducer, initialSelection); | ||
|
||
return ( | ||
<SelectionContext.Provider value={selection}> | ||
<SelectionDispatchContext.Provider value={reducer}>{children}</SelectionDispatchContext.Provider> | ||
</SelectionContext.Provider> | ||
); | ||
} | ||
|
||
export function useObjectSelection(object: LaunchSite) { | ||
const dispatch = useContext(SelectionDispatchContext); | ||
const selection = useContext(SelectionContext); | ||
|
||
return [selection.selectedObject?.id === object.id, () => dispatch({ action: 'set', object })] as const; | ||
} | ||
|
||
export function useClearSelection() { | ||
const dispatch = useContext(SelectionDispatchContext); | ||
|
||
return () => dispatch({ action: 'clear' }); | ||
} |
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