Skip to content

Commit

Permalink
Add builds context
Browse files Browse the repository at this point in the history
  • Loading branch information
evamik committed Dec 17, 2024
1 parent 9c6d4c3 commit 72499a5
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/renderer/contexts/buildsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {
createContext,
FC,
PropsWithChildren,
useContext,
useEffect,
useMemo,
useState,
} from 'react';

export interface TBuild {
id: number;
name: string;
items: string[];
}

interface SelectedBuilds {
[account: string]: {
[hero: string]: number;
};
}

interface BuildsContext {
builds: {
[id: number]: TBuild;
};
selectedBuilds: SelectedBuilds;
saveBuild: (build: TBuild) => void;
saveSelectedBuild: (
playerName: string,
hero: string,
buildId: number,
) => void;
}

export const BuildsContext = createContext({} as BuildsContext);

export const BuildsProvider: FC<PropsWithChildren> = ({ children }) => {
const [builds, setBuildsState] = useState<{ [id: number]: TBuild }>({});
const [selectedBuilds, setSelectedBuilds] = useState<SelectedBuilds>({});

useEffect(() => {
window.electron.ipcRenderer.on('get_all_builds', (arg: any) => {
setBuildsState(arg || {});
});

window.electron.ipcRenderer.on('get_selected_builds', (arg: any) => {
setSelectedBuilds(arg || {});
});

loadBuilds();
}, []);

const loadBuilds = () => {
window.electron.ipcRenderer.sendMessage('get_all_builds');
window.electron.ipcRenderer.sendMessage('get_selected_builds');
};

const saveBuild = (build: TBuild) => {
window.electron.ipcRenderer.sendMessage('save_build', build);
setBuildsState({
...builds,
[build.id]: build,
});
};

const saveSelectedBuild = (
playerName: string,
hero: string,
buildId: number,
) => {
window.electron.ipcRenderer.sendMessage('save_selected_build', {
playerName,
playerClass: hero,
buildId,
});
setSelectedBuilds({
...selectedBuilds,
[playerName]: {
...selectedBuilds[playerName],
[hero]: buildId,
},
});
};

const value = useMemo(
() => ({
builds,
selectedBuilds,
saveBuild,
saveSelectedBuild,
}),
[builds, selectedBuilds, saveBuild, saveSelectedBuild],
);

return (
<BuildsContext.Provider value={value}>{children}</BuildsContext.Provider>
);
};

export const useBuildsContext = () => useContext(BuildsContext);

0 comments on commit 72499a5

Please sign in to comment.