From bda54a4d40dccfdcba02a0e0d4c216aade588f6a Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sun, 3 Dec 2023 16:41:52 +0100 Subject: [PATCH] chore: use Providers to send functions deep into components --- src/HullListing/HullListing.stories.tsx | 6 +-- src/HullListing/HullListing.tsx | 44 ++++++++----------- .../ShipSnapshotProvider.tsx | 19 +++++++- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/HullListing/HullListing.stories.tsx b/src/HullListing/HullListing.stories.tsx index fb54419..40f42e5 100644 --- a/src/HullListing/HullListing.stories.tsx +++ b/src/HullListing/HullListing.stories.tsx @@ -6,7 +6,7 @@ import { fullFit } from '../../.storybook/fits'; import { HullListing } from './'; import { EsiProvider } from '../EsiProvider'; import { EveDataProvider } from '../EveDataProvider'; -import { EsiFit, ShipSnapshotProvider } from '../ShipSnapshotProvider'; +import { ShipSnapshotProvider } from '../ShipSnapshotProvider'; import { DogmaEngineProvider } from '../DogmaEngineProvider'; const meta: Meta = { @@ -18,7 +18,7 @@ const meta: Meta = { export default meta; type Story = StoryObj; -const withEsiProvider: Decorator<{ changeHull: (typeId: number) => void, changeFit: (fit: EsiFit) => void }> = (Story, context) => { +const withEsiProvider: Decorator> = (Story, context) => { return ( @@ -36,8 +36,6 @@ const withEsiProvider: Decorator<{ changeHull: (typeId: number) => void, changeF export const Default: Story = { args: { - changeHull: (typeId: number) => console.log(`changeHull(${typeId})`), - changeFit: (fit: EsiFit) => console.log(`changeFit(${fit})`), }, decorators: [withEsiProvider], parameters: { diff --git a/src/HullListing/HullListing.tsx b/src/HullListing/HullListing.tsx index 0dfa7a2..65342c0 100644 --- a/src/HullListing/HullListing.tsx +++ b/src/HullListing/HullListing.tsx @@ -34,7 +34,9 @@ const factionIdToRace: Record = { 1: "Non-Empire", } as const; -const Hull = (props: { typeId: number, entry: ListingFit, changeHull: (typeId: number) => void, changeFit: (fit: EsiFit) => void }) => { +const Hull = (props: { typeId: number, entry: ListingFit }) => { + const shipSnapShot = React.useContext(ShipSnapshotContext); + const getChildren = React.useCallback(() => { if (props.entry.fits.length === 0) { return ; @@ -42,31 +44,26 @@ const Hull = (props: { typeId: number, entry: ListingFit, changeHull: (typeId: n let index = 0; return <>{props.entry.fits.map((fit) => { index += 1; - return props.changeFit(fit)} />; + return shipSnapShot.changeFit(fit)} />; })}; } - }, [props]); + }, [props, shipSnapShot]); - function onClick(e: React.MouseEvent) { + const onClick = React.useCallback((e: React.MouseEvent) => { e.stopPropagation(); - props.changeHull(props.typeId); - } + shipSnapShot.changeHull(props.typeId); + }, [props, shipSnapShot]); const headerAction = ; const header = ; return ; } -const HullRace = (props: { raceId: number, entries: ListingHulls, changeHull: (typeId: number) => void, changeFit: (fit: EsiFit) => void }) => { +const HullRace = (props: { raceId: number, entries: ListingHulls }) => { const getChildren = React.useCallback(() => { - const changeProps = { - changeHull: props.changeHull, - changeFit: props.changeFit, - }; - return <>{Object.keys(props.entries).sort((a, b) => props.entries[a].name.localeCompare(props.entries[b].name)).map((typeId) => { const entry = props.entries[typeId]; - return + return })}; }, [props]); @@ -76,19 +73,14 @@ const HullRace = (props: { raceId: number, entries: ListingHulls, changeHull: (t return ; } -const HullGroup = (props: { name: string, entries: ListingGroup, changeHull: (typeId: number) => void, changeFit: (fit: EsiFit) => void }) => { +const HullGroup = (props: { name: string, entries: ListingGroup }) => { const getChildren = React.useCallback(() => { - const changeProps = { - changeHull: props.changeHull, - changeFit: props.changeFit, - }; - return <> - - - - - + + + + + ; }, [props]); @@ -99,7 +91,7 @@ const HullGroup = (props: { name: string, entries: ListingGroup, changeHull: (ty /** * Show all the fittings for the current ESI character. */ -export const HullListing = (props: { changeHull: (typeId: number) => void, changeFit: (fit: EsiFit) => void }) => { +export const HullListing = () => { const esi = React.useContext(EsiContext); const eveData = React.useContext(EveDataContext); const shipSnapShot = React.useContext(ShipSnapshotContext); @@ -202,7 +194,7 @@ export const HullListing = (props: { changeHull: (typeId: number) => void, chang
{Object.keys(hullGroups).sort().map((groupName) => { const groupData = hullGroups[groupName]; - return + return })}
diff --git a/src/ShipSnapshotProvider/ShipSnapshotProvider.tsx b/src/ShipSnapshotProvider/ShipSnapshotProvider.tsx index b74947e..fbc5492 100644 --- a/src/ShipSnapshotProvider/ShipSnapshotProvider.tsx +++ b/src/ShipSnapshotProvider/ShipSnapshotProvider.tsx @@ -45,11 +45,15 @@ interface ShipSnapshot { fit?: EsiFit; + changeHull: (typeId: number) => void; + changeFit: (fit: EsiFit) => void; setItemState: (flag: number, state: string) => void; } export const ShipSnapshotContext = React.createContext({ loaded: undefined, + changeHull: () => {}, + changeFit: () => {}, setItemState: () => {}, }); @@ -68,6 +72,8 @@ export interface ShipSnapshotProps { export const ShipSnapshotProvider = (props: ShipSnapshotProps) => { const [shipSnapshot, setShipSnapshot] = React.useState({ loaded: undefined, + changeHull: () => {}, + changeFit: () => {}, setItemState: () => {}, }); const [currentFit, setCurrentFit] = React.useState(undefined); @@ -95,6 +101,15 @@ export const ShipSnapshotProvider = (props: ShipSnapshotProps) => { }) }, [currentFit]); + const changeHull = React.useCallback((typeId: number) => { + setCurrentFit({ + "name": "New Ship", + "description": "", + "ship_type_id": typeId, + "items": [] + }) + }, []); + React.useEffect(() => { if (!dogmaEngine.loaded) return; if (!currentFit || !props.skills) return; @@ -106,9 +121,11 @@ export const ShipSnapshotProvider = (props: ShipSnapshotProps) => { hull: snapshot.hull, items: snapshot.items, fit: currentFit, + changeHull, + changeFit: setCurrentFit, setItemState, }); - }, [dogmaEngine, currentFit, props.skills, setItemState]); + }, [dogmaEngine, currentFit, props.skills, changeHull, setItemState]); React.useEffect(() => { setCurrentFit(props.fit);