grungnet/src/hooks/useMapSelection.tsx

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-07-21 23:16:44 -04:00
import { useState } from "react";
import SelectionMenu from "../components/selection/SelectionMenu";
import SelectTool from "../components/tools/SelectTool";
import { SelectionItemsChangeEventHandler } from "../types/Events";
import { Map, MapToolId } from "../types/Map";
import { MapState } from "../types/MapState";
2021-07-21 23:16:44 -04:00
import { Selection } from "../types/Select";
import { SelectToolSettings } from "../types/Select";
function useMapSelection(
map: Map | null,
mapState: MapState | null,
2021-07-21 23:16:44 -04:00
onSelectionItemsChange: SelectionItemsChangeEventHandler,
selectedToolId: MapToolId,
settings: SelectToolSettings
) {
const [isSelectionMenuOpen, setIsSelectionMenuOpen] =
useState<boolean>(false);
const [selection, setSelection] = useState<Selection | null>(null);
function handleSelectionMenuOpen(open: boolean) {
setIsSelectionMenuOpen(open);
}
const selectionTool = (
<SelectTool
active={selectedToolId === "select"}
toolSettings={settings}
onSelectionItemsChange={onSelectionItemsChange}
selection={selection}
onSelectionChange={setSelection}
onSelectionMenuOpen={handleSelectionMenuOpen}
/>
);
const selectionMenu = (
<SelectionMenu
isOpen={isSelectionMenuOpen}
onRequestClose={() => setIsSelectionMenuOpen(false)}
selection={selection}
onSelectionItemsChange={onSelectionItemsChange}
map={map}
mapState={mapState}
2021-07-21 23:16:44 -04:00
/>
);
return { selectionTool, selectionMenu };
}
export default useMapSelection;