Skip to content

Commit

Permalink
feat: save file system option on create, update, delete files
Browse files Browse the repository at this point in the history
  • Loading branch information
thenick775 committed Nov 17, 2024
1 parent 4701b2a commit 71e8200
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 16 deletions.
3 changes: 3 additions & 0 deletions gbajs3/src/components/modals/cheats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ModalBody } from './modal-body.tsx';
import { ModalFooter } from './modal-footer.tsx';
import { ModalHeader } from './modal-header.tsx';
import { useEmulatorContext, useModalContext } from '../../hooks/context.tsx';
import { useAddCallbacks } from '../../hooks/emulator/use-add-callbacks.tsx';
import {
EmbeddedProductTour,
type TourSteps
Expand Down Expand Up @@ -85,6 +86,7 @@ export const CheatsModal = () => {
const { setIsModalOpen } = useModalContext();
const { emulator } = useEmulatorContext();
const [viewRawCheats, setViewRawCheats] = useState(false);
const { syncActionIfEnabled } = useAddCallbacks();
const baseId = useId();
const defaultCheat = { desc: '', code: '', enable: false };

Expand Down Expand Up @@ -197,6 +199,7 @@ export const CheatsModal = () => {

if (cheatsFile)
emulator?.uploadCheats(cheatsFile, () => {
syncActionIfEnabled();
emulator.autoLoadCheats();
refreshForm();
});
Expand Down
5 changes: 4 additions & 1 deletion gbajs3/src/components/modals/file-system.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ModalBody } from './modal-body.tsx';
import { ModalFooter } from './modal-footer.tsx';
import { ModalHeader } from './modal-header.tsx';
import { useEmulatorContext, useModalContext } from '../../hooks/context.tsx';
import { useAddCallbacks } from '../../hooks/emulator/use-add-callbacks.tsx';
import {
EmbeddedProductTour,
type TourSteps
Expand All @@ -25,15 +26,17 @@ const FlexModalBody = styled(ModalBody)`
export const FileSystemModal = () => {
const { setIsModalOpen } = useModalContext();
const { emulator } = useEmulatorContext();
const { syncActionIfEnabled } = useAddCallbacks();
const [allFiles, setAllFiles] = useState<FileNode | undefined>();
const baseId = useId();

const deleteFile = useCallback(
(path: string) => {
emulator?.deleteFile(path);
setAllFiles(emulator?.listAllFiles());
syncActionIfEnabled();
},
[emulator]
[emulator, syncActionIfEnabled]
);

const downloadFile = (path: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('<FileSystemOptionsForm />', () => {
// form and form fields
expect(screen.getByLabelText('File System Options Form')).toBeVisible();
expect(screen.getByText('Save file system on in-game save')).toBeVisible();
expect(
screen.getByText('Save file system on creates / updates / deletes')
).toBeVisible();
});

it('saves file system options', async () => {
Expand All @@ -37,7 +40,8 @@ describe('<FileSystemOptionsForm />', () => {

vi.spyOn(addCallbacksHooks, 'useAddCallbacks').mockImplementation(() => ({
addCallbacks: vi.fn(),
addCallbacksAndSaveSettings: addCallbacksAndSaveSettingsSpy
addCallbacksAndSaveSettings: addCallbacksAndSaveSettingsSpy,
syncActionIfEnabled: vi.fn()
}));

renderWithContext(<FileSystemOptionsForm id="testId" />);
Expand All @@ -50,6 +54,9 @@ describe('<FileSystemOptionsForm />', () => {
await userEvent.click(
screen.getByLabelText('Save file system on in-game save')
);
await userEvent.click(
screen.getByLabelText('Save file system on creates / updates / deletes')
);
await userEvent.click(screen.getByLabelText('Enable Notifications'));
// submit form
await userEvent.click(submitButton);
Expand All @@ -58,7 +65,8 @@ describe('<FileSystemOptionsForm />', () => {
expect(addCallbacksAndSaveSettingsSpy).toHaveBeenCalledWith(
{
saveFileSystemOnInGameSave: true,
notificationsEnabled: true
notificationsEnabled: true,
saveFileSystemOnCreateUpdateDelete: true
},
expect.anything()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export const FileSystemOptionsForm = ({ id }: FileSystemOptionsFormProps) => {
watcher={watch('saveFileSystemOnInGameSave')}
{...register('saveFileSystemOnInGameSave')}
/>
<ManagedCheckbox
label="Save file system on creates / updates / deletes"
watcher={watch('saveFileSystemOnCreateUpdateDelete')}
{...register('saveFileSystemOnCreateUpdateDelete')}
/>
<ManagedSwitch
label="Enable Notifications"
watcher={watch('notificationsEnabled')}
Expand Down
5 changes: 4 additions & 1 deletion gbajs3/src/components/modals/load-rom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ModalBody } from './modal-body.tsx';
import { ModalFooter } from './modal-footer.tsx';
import { ModalHeader } from './modal-header.tsx';
import { useEmulatorContext, useModalContext } from '../../hooks/context.tsx';
import { useAddCallbacks } from '../../hooks/emulator/use-add-callbacks.tsx';
import { useRunGame } from '../../hooks/emulator/use-run-game.tsx';
import { useListRoms } from '../../hooks/use-list-roms.tsx';
import { useLoadRom } from '../../hooks/use-load-rom.tsx';
Expand Down Expand Up @@ -94,19 +95,21 @@ export const LoadRomModal = () => {
const [currentRomLoading, setCurrentRomLoading] = useState<string | null>(
null
);
const { syncActionIfEnabled } = useAddCallbacks();

const shouldUploadRom = !romLoading && !!romFile && !!currentRomLoading;

useEffect(() => {
if (shouldUploadRom) {
const runCallback = () => {
syncActionIfEnabled();
runGame(emulator?.filePaths().gamePath + '/' + romFile.name);
};

emulator?.uploadRom(romFile, runCallback);
setCurrentRomLoading(null);
}
}, [emulator, shouldUploadRom, romFile, runGame]);
}, [emulator, shouldUploadRom, romFile, runGame, syncActionIfEnabled]);

const tourSteps: TourSteps = [
{
Expand Down
6 changes: 4 additions & 2 deletions gbajs3/src/components/modals/load-save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ModalBody } from './modal-body.tsx';
import { ModalFooter } from './modal-footer.tsx';
import { ModalHeader } from './modal-header.tsx';
import { useEmulatorContext, useModalContext } from '../../hooks/context.tsx';
import { useAddCallbacks } from '../../hooks/emulator/use-add-callbacks.tsx';
import { useListSaves } from '../../hooks/use-list-saves.tsx';
import { useLoadSave } from '../../hooks/use-load-save.tsx';
import {
Expand Down Expand Up @@ -78,6 +79,7 @@ export const LoadSaveModal = () => {
const { setIsModalOpen } = useModalContext();
const { emulator } = useEmulatorContext();
const saveListId = useId();
const { syncActionIfEnabled } = useAddCallbacks();
const {
data: saveList,
isLoading: saveListLoading,
Expand All @@ -97,10 +99,10 @@ export const LoadSaveModal = () => {

useEffect(() => {
if (shouldUploadSave) {
emulator?.uploadSaveOrSaveState(saveFile);
emulator?.uploadSaveOrSaveState(saveFile, syncActionIfEnabled);
setCurrentSaveLoading(null);
}
}, [emulator, shouldUploadSave, saveFile]);
}, [emulator, shouldUploadSave, saveFile, syncActionIfEnabled]);

const tourSteps: TourSteps = [
{
Expand Down
4 changes: 4 additions & 0 deletions gbajs3/src/components/modals/save-states.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ModalBody } from './modal-body.tsx';
import { ModalFooter } from './modal-footer.tsx';
import { ModalHeader } from './modal-header.tsx';
import { useEmulatorContext, useModalContext } from '../../hooks/context.tsx';
import { useAddCallbacks } from '../../hooks/emulator/use-add-callbacks.tsx';
import { saveStateSlotLocalStorageKey } from '../controls/consts.tsx';
import {
EmbeddedProductTour,
Expand Down Expand Up @@ -95,6 +96,7 @@ export const SaveStatesModal = () => {
saveStateSlotLocalStorageKey,
0
);
const { syncActionIfEnabled } = useAddCallbacks();
const baseId = useId();
const {
register,
Expand Down Expand Up @@ -223,6 +225,7 @@ export const SaveStatesModal = () => {
const slot = parseInt(slotString);
emulator?.deleteSaveState(slot);
refreshSaveStates();
syncActionIfEnabled();
}
}}
>
Expand All @@ -248,6 +251,7 @@ export const SaveStatesModal = () => {
refreshSaveStates();
setCurrentSlot((prevState) => prevState + 1);
setSaveStateError(null);
syncActionIfEnabled();
} else {
setSaveStateError('Failed to create save state');
}
Expand Down
3 changes: 3 additions & 0 deletions gbajs3/src/components/modals/upload-cheats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ModalBody } from './modal-body.tsx';
import { ModalFooter } from './modal-footer.tsx';
import { ModalHeader } from './modal-header.tsx';
import { useEmulatorContext, useModalContext } from '../../hooks/context.tsx';
import { useAddCallbacks } from '../../hooks/emulator/use-add-callbacks.tsx';
import {
EmbeddedProductTour,
type TourSteps
Expand All @@ -30,6 +31,7 @@ export const UploadCheatsModal = () => {
formState: { isSubmitSuccessful },
control
} = useForm<InputProps>();
const { syncActionIfEnabled } = useAddCallbacks();
const cheatsFormId = useId();

const onDrop = useCallback(
Expand All @@ -43,6 +45,7 @@ export const UploadCheatsModal = () => {
const onSubmit: SubmitHandler<InputProps> = ({ cheatFiles }) => {
cheatFiles.forEach((cheatFiles) => emulator?.uploadCheats(cheatFiles));
reset();
syncActionIfEnabled();
};

const tourSteps: TourSteps = [
Expand Down
6 changes: 5 additions & 1 deletion gbajs3/src/components/modals/upload-public-external-roms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ModalBody } from './modal-body.tsx';
import { ModalFooter } from './modal-footer.tsx';
import { ModalHeader } from './modal-header.tsx';
import { useEmulatorContext, useModalContext } from '../../hooks/context.tsx';
import { useAddCallbacks } from '../../hooks/emulator/use-add-callbacks.tsx';
import { useRunGame } from '../../hooks/emulator/use-run-game.tsx';
import { useLoadExternalRom } from '../../hooks/use-load-external-rom.tsx';
import { ErrorWithIcon } from '../shared/error-with-icon.tsx';
Expand Down Expand Up @@ -63,6 +64,7 @@ export const UploadPublicExternalRomsModal = ({
const [currentRomURL, setCurrentRomURL] = useState<string | null>(null);
const uploadRomButtonId = useId();
const runGame = useRunGame();
const { syncActionIfEnabled } = useAddCallbacks();

const {
data: externalRomFile,
Expand All @@ -74,6 +76,7 @@ export const UploadPublicExternalRomsModal = ({
useEffect(() => {
if (!isExternalRomLoading && externalRomFile && currentRomURL) {
const runCallback = () => {
syncActionIfEnabled();
const hasSucceeded = runGame(
emulator?.filePaths().gamePath + '/' + externalRomFile.name
);
Expand All @@ -93,7 +96,8 @@ export const UploadPublicExternalRomsModal = ({
emulator,
externalRomFile,
isExternalRomLoading,
setIsModalOpen
setIsModalOpen,
syncActionIfEnabled
]);

return (
Expand Down
7 changes: 6 additions & 1 deletion gbajs3/src/components/modals/upload-rom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ModalBody } from './modal-body.tsx';
import { ModalFooter } from './modal-footer.tsx';
import { ModalHeader } from './modal-header.tsx';
import { useEmulatorContext, useModalContext } from '../../hooks/context.tsx';
import { useAddCallbacks } from '../../hooks/emulator/use-add-callbacks.tsx';
import { useRunGame } from '../../hooks/emulator/use-run-game.tsx';
import { useLoadExternalRom } from '../../hooks/use-load-external-rom.tsx';
import {
Expand Down Expand Up @@ -50,13 +51,15 @@ export const UploadRomModal = () => {
} = useLoadExternalRom();
const uploadRomFormId = useId();
const runGame = useRunGame();
const { syncActionIfEnabled } = useAddCallbacks();

const shouldUploadExternalRom =
!isExternalRomLoading && !!externalRomFile && !!currentRomURL;

useEffect(() => {
if (shouldUploadExternalRom) {
const runCallback = () => {
syncActionIfEnabled();
const hasSucceeded = runGame(
emulator?.filePaths().gamePath + '/' + externalRomFile.name
);
Expand All @@ -72,7 +75,8 @@ export const UploadRomModal = () => {
externalRomFile,
emulator,
setIsModalOpen,
runGame
runGame,
syncActionIfEnabled
]);

const onDrop = useCallback(
Expand All @@ -91,6 +95,7 @@ export const UploadRomModal = () => {
}

const runCallback = () => {
syncActionIfEnabled();
const hasSucceeded = runGame(
emulator?.filePaths().gamePath + '/' + romFile.name
);
Expand Down
3 changes: 3 additions & 0 deletions gbajs3/src/components/modals/upload-saves.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ModalBody } from './modal-body.tsx';
import { ModalFooter } from './modal-footer.tsx';
import { ModalHeader } from './modal-header.tsx';
import { useEmulatorContext, useModalContext } from '../../hooks/context.tsx';
import { useAddCallbacks } from '../../hooks/emulator/use-add-callbacks.tsx';
import {
EmbeddedProductTour,
type TourSteps
Expand All @@ -26,6 +27,7 @@ const validFileExtensions = [
export const UploadSavesModal = () => {
const { setIsModalOpen } = useModalContext();
const { emulator } = useEmulatorContext();
const { syncActionIfEnabled } = useAddCallbacks();
const {
reset,
handleSubmit,
Expand All @@ -46,6 +48,7 @@ export const UploadSavesModal = () => {
const onSubmit: SubmitHandler<InputProps> = ({ saveFiles }) => {
saveFiles.forEach((saveFile) => emulator?.uploadSaveOrSaveState(saveFile));
reset();
syncActionIfEnabled();
};

const tourSteps: TourSteps = [
Expand Down
Loading

0 comments on commit 71e8200

Please sign in to comment.