Skip to content

Commit

Permalink
refactor: reusable logic
Browse files Browse the repository at this point in the history
- tentative, not sure if I like the readability sacrifice
  • Loading branch information
Nick VanCise authored and Nick VanCise committed Feb 13, 2024
1 parent caef5f4 commit d72a36f
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions gbajs3/src/components/controls/virtual-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ export const VirtualControls = () => {
};
};

const toastOnCondition = (
condition: boolean,
successMessage: string,
errorMessage: string
) => {
if (areNotificationsEnabled)
toast[condition ? 'success' : 'error'](
condition ? successMessage : errorMessage,
{ id: virtualControlToastId }
);
};

const virtualButtons = [
{
keyId: 'A',
Expand Down Expand Up @@ -333,17 +345,12 @@ export const VirtualControls = () => {
children: <BiSolidBookmark />,
onClick: () => {
const wasSuccessful = emulator?.loadSaveState(currentSaveStateSlot);
if (areNotificationsEnabled) {
if (wasSuccessful) {
toast.success(`Loaded slot: ${currentSaveStateSlot}`, {
id: virtualControlToastId
});
} else {
toast.error(`Failed to load slot: ${currentSaveStateSlot}`, {
id: virtualControlToastId
});
}
}

toastOnCondition(
!!wasSuccessful,
`Loaded slot: ${currentSaveStateSlot}`,
`Failed to load slot: ${currentSaveStateSlot}`
);
},
width: 40,
initialPosition: initialPositionForKey('loadstate-button'),
Expand All @@ -358,17 +365,12 @@ export const VirtualControls = () => {
children: <BiSave />,
onClick: () => {
const wasSuccessful = emulator?.createSaveState(currentSaveStateSlot);
if (areNotificationsEnabled) {
if (wasSuccessful) {
toast.success(`Saved slot: ${currentSaveStateSlot}`, {
id: virtualControlToastId
});
} else {
toast.error(`Failed to save slot: ${currentSaveStateSlot}`, {
id: virtualControlToastId
});
}
}

toastOnCondition(
!!wasSuccessful,
`Saved slot: ${currentSaveStateSlot}`,
`Failed to save slot: ${currentSaveStateSlot}`
);
},
width: 40,
initialPosition: initialPositionForKey('savestate-button'),
Expand Down

0 comments on commit d72a36f

Please sign in to comment.