1 import CRC32 from 'crc-32';
2 import localforage from 'localforage';
3 import PropTypes from 'prop-types';
4 import React from 'react';
5 import toastr from 'toastr';
7 import i18n from '../i18n';
9 const AlttpBaseRomContext = React.createContext(null);
11 const AlttpBaseRomProvider = ({ children }) => {
12 const [rom, setRom] = React.useState(null);
14 const setRomCallback = React.useCallback(buffer => {
16 const crc = CRC32.buf(new Uint8Array(buffer));
17 if (crc === 0x3322EFFC) {
19 localforage.setItem('alttpBaseRom', buffer);
20 toastr.success(i18n.t('alttp.baseRomSet'));
22 toastr.error(i18n.t('alttp.baseRomInvalid'));
26 localforage.removeItem('alttpBaseRom');
27 toastr.success(i18n.t('alttp.baseRomRemoved'));
31 React.useEffect(async () => {
32 const stored = await localforage.getItem('alttpBaseRom');
34 const crc = CRC32.buf(new Uint8Array(stored));
35 if (crc == 0x3322EFFC) {
41 return <AlttpBaseRomContext.Provider value={{ rom, setRom: setRomCallback }}>
43 </AlttpBaseRomContext.Provider>;
46 AlttpBaseRomProvider.propTypes = {
47 children: PropTypes.node,
50 export const useAlttpBaseRom = () => React.useContext(AlttpBaseRomContext);
52 export default AlttpBaseRomProvider;