Skip to content

Commit

Permalink
eject button (with debug console.log)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlee337 committed Feb 7, 2025
1 parent 9e67ac4 commit 0e09aa1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,40 @@ export default function setupIPCs(mainWindow: BrowserWindow): void {
return chosenReplaysDir;
});

const maybeEject = (currentDir: string) => {
const key = Array.from(knownUsbs.keys()).find((usbKey) =>
currentDir.startsWith(usbKey),
);
if (key) {
return new Promise<boolean>((resolve, reject) => {
eject(key, (error: Error, stdout: string | Buffer) => {
if (error) {
reject(error);
} else {
console.log(`ejected ${key}`);

Check warning on line 159 in src/main/ipc.ts

View workflow job for this annotation

GitHub Actions / ubuntu

Unexpected console statement

Check warning on line 159 in src/main/ipc.ts

View workflow job for this annotation

GitHub Actions / macos

Unexpected console statement

Check warning on line 159 in src/main/ipc.ts

View workflow job for this annotation

GitHub Actions / windows

Unexpected console statement
console.log(stdout);

Check warning on line 160 in src/main/ipc.ts

View workflow job for this annotation

GitHub Actions / ubuntu

Unexpected console statement

Check warning on line 160 in src/main/ipc.ts

View workflow job for this annotation

GitHub Actions / macos

Unexpected console statement

Check warning on line 160 in src/main/ipc.ts

View workflow job for this annotation

GitHub Actions / windows

Unexpected console statement
resolve(true);
}
});
});
}
return Promise.resolve(false);
};

ipcMain.removeHandler('deleteReplaysDir');
ipcMain.handle('deleteReplaysDir', async () => {
if (replayDirs.length > 0) {
const currentDir = replayDirs[replayDirs.length - 1];
await rm(currentDir, { recursive: true });
const key = Array.from(knownUsbs.keys()).find(
(usbKey) =>
currentDir === usbKey ||
currentDir.startsWith(`${usbKey}${path.sep}`),
);
if (key) {
await new Promise<void>((resolve, reject) => {
eject(key, (error: Error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
await maybeEject(currentDir);
}
});

ipcMain.removeHandler('maybeEject');
ipcMain.handle('maybeEject', () =>
maybeEject(replayDirs[replayDirs.length - 1]),
);

ipcMain.removeHandler('getReplaysInDir');
ipcMain.handle('getReplaysInDir', async () => {
if (replayDirs.length === 0) {
Expand Down
1 change: 1 addition & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const electronHandler = {
chooseReplaysDir: (): Promise<string> =>
ipcRenderer.invoke('chooseReplaysDir'),
deleteReplaysDir: (): Promise<void> => ipcRenderer.invoke('deleteReplaysDir'),
maybeEject: (): Promise<boolean> => ipcRenderer.invoke('maybeEject'),
getReplaysInDir: (): Promise<{
replays: Replay[];
invalidReplays: InvalidReplay[];
Expand Down
12 changes: 12 additions & 0 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
DeleteForever,
DeleteForeverOutlined,
Edit,
Eject,
FolderOpen,
HourglassTop,
Refresh,
Expand Down Expand Up @@ -1450,6 +1451,17 @@ function Hello() {
value={dir || 'Set replays folder...'}
style={{ flexGrow: 1 }}
/>
{dir && (
<Tooltip arrow title="Eject (if USB)">
<IconButton
onClick={async () => {
await window.electron.maybeEject();
}}
>
<Eject />
</IconButton>
</Tooltip>
)}
{dir &&
dirExists &&
!gettingReplays &&
Expand Down

0 comments on commit 0e09aa1

Please sign in to comment.