Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use Providers to send functions deep into components #36

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/HullListing/HullListing.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof HullListing> = {
Expand All @@ -18,7 +18,7 @@ const meta: Meta<typeof HullListing> = {
export default meta;
type Story = StoryObj<typeof HullListing>;

const withEsiProvider: Decorator<{ changeHull: (typeId: number) => void, changeFit: (fit: EsiFit) => void }> = (Story, context) => {
const withEsiProvider: Decorator<Record<string, never>> = (Story, context) => {
return (
<EveDataProvider>
<EsiProvider setSkills={console.log}>
Expand All @@ -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: {
Expand Down
44 changes: 18 additions & 26 deletions src/HullListing/HullListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,36 @@ const factionIdToRace: Record<number, string> = {
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 <TreeLeaf level={4} content={"No Item"} />;
} else {
let index = 0;
return <>{props.entry.fits.map((fit) => {
index += 1;
return <TreeLeaf key={`${fit.ship_type_id}-${index}`} level={4} content={fit.name} onClick={() => props.changeFit(fit)} />;
return <TreeLeaf key={`${fit.ship_type_id}-${index}`} level={4} content={fit.name} onClick={() => shipSnapShot.changeFit(fit)} />;
})}</>;
}
}, [props]);
}, [props, shipSnapShot]);

function onClick(e: React.MouseEvent<HTMLDivElement, MouseEvent>) {
const onClick = React.useCallback((e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
e.stopPropagation();
props.changeHull(props.typeId);
}
shipSnapShot.changeHull(props.typeId);
}, [props, shipSnapShot]);

const headerAction = <TreeHeaderAction icon="simulate" onClick={onClick} />;
const header = <TreeHeader icon={`https://images.evetech.net/types/${props.typeId}/icon?size=32`} text={props.entry.name} action={headerAction} />;
return <TreeListing level={3} header={header} height={32} getChildren={getChildren} />;
}

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 <Hull key={typeId} typeId={parseInt(typeId)} entry={entry} {...changeProps} />
return <Hull key={typeId} typeId={parseInt(typeId)} entry={entry} />
})}</>;
}, [props]);

Expand All @@ -76,19 +73,14 @@ const HullRace = (props: { raceId: number, entries: ListingHulls, changeHull: (t
return <TreeListing level={2} header={header} getChildren={getChildren} />;
}

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 <>
<HullRace raceId={500003} entries={props.entries.Amarr} {...changeProps} />
<HullRace raceId={500001} entries={props.entries.Caldari} {...changeProps} />
<HullRace raceId={500004} entries={props.entries.Gallente} {...changeProps} />
<HullRace raceId={500002} entries={props.entries.Minmatar} {...changeProps} />
<HullRace raceId={1} entries={props.entries.NonEmpire} {...changeProps} />
<HullRace raceId={500003} entries={props.entries.Amarr} />
<HullRace raceId={500001} entries={props.entries.Caldari} />
<HullRace raceId={500004} entries={props.entries.Gallente} />
<HullRace raceId={500002} entries={props.entries.Minmatar} />
<HullRace raceId={1} entries={props.entries.NonEmpire} />
</>;
}, [props]);

Expand All @@ -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);
Expand Down Expand Up @@ -202,7 +194,7 @@ export const HullListing = (props: { changeHull: (typeId: number) => void, chang
<div className={styles.listingContent}>
{Object.keys(hullGroups).sort().map((groupName) => {
const groupData = hullGroups[groupName];
return <HullGroup key={groupName} name={groupName} entries={groupData} changeHull={props.changeHull} changeFit={props.changeFit} />
return <HullGroup key={groupName} name={groupName} entries={groupData} />
})}
</div>
</div>
Expand Down
19 changes: 18 additions & 1 deletion src/ShipSnapshotProvider/ShipSnapshotProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShipSnapshot>({
loaded: undefined,
changeHull: () => {},
changeFit: () => {},
setItemState: () => {},
});

Expand All @@ -68,6 +72,8 @@ export interface ShipSnapshotProps {
export const ShipSnapshotProvider = (props: ShipSnapshotProps) => {
const [shipSnapshot, setShipSnapshot] = React.useState<ShipSnapshot>({
loaded: undefined,
changeHull: () => {},
changeFit: () => {},
setItemState: () => {},
});
const [currentFit, setCurrentFit] = React.useState<EsiFit | undefined>(undefined);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down