Skip to content

Commit

Permalink
implemented apply and close
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoBM committed Apr 10, 2023
1 parent 78059cb commit 1ad269f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
11 changes: 8 additions & 3 deletions src/common/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ export type WindowAction =
| { type: 'change-color'; icons?: string; background?: string }
| { type: 'close' };

type ProcessRenamesOptions = {
exit?: boolean;
};

type IPCScanPaths = { type: 'scan-paths'; payload: string[] };
type IPCScanResults = { type: 'scan-results'; payload: ScanResults };
type IPCProcessRenames = {
type: 'process-renames';
payload: RenameOperation[];
payload: { renames: RenameOperation[]; options: ProcessRenamesOptions };
};
type IPCRenameResults = { type: 'rename-results'; payload: RenameResults };
type IPCRendererReady = { type: 'renderer-ready' };
Expand Down Expand Up @@ -102,11 +106,12 @@ export function scanResultsMessage(
}

export function processRenamesMessage(
renames: RenameOperation[]
renames: RenameOperation[],
options: ProcessRenamesOptions
): Channel<IPCProcessRenames> {
return channel<IPCProcessRenames>({
type: 'process-renames',
payload: renames,
payload: { renames, options },
});
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ ipcMain.on('ipc-communication', async (event, message: IPCMainMessage) => {
break;
case 'process-renames':
try {
const results = await processRenameOperations(message.payload);
event.reply(...renameResultsMessage(results));
const results = await processRenameOperations(message.payload.renames);

if (results.success && message.payload.options.exit) {
app.quit();
} else {
event.reply(...renameResultsMessage(results));
}
} catch (error) {
event.reply(...mainErrorMessage((error as Error).message));
}
Expand Down
1 change: 0 additions & 1 deletion src/renderer/hooks/useMainData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export function useMainData(): [MainData, Dispatch<SetStateAction<MainData>>] {
);
break;
case 'main-error':
alert(message.payload);
setMainData({
...mainData,
error: message.payload,
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/hooks/useRenamesAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,9 @@ function createContext(
}

function createFunction(code: string, context: EntryStats | {} = {}) {
const setup = `'use strict'; const console = {}, window = {}, self = {}, top = {}, document = {}, location = {}, navigation = {}, navigator = {}, parent = {}, localStorage = {}, sessionStorage = {}, indexedDB = {}, open = {}, close = {}, openDatabase = {}, fetch = {}, Storage = {}, cookieStore = {}, CookieStore = {}, alert = {};`;
// const newCode = `return function() {${setup} const f = ${code}; return f.bind(this);}.bind(this)()`;
const setup = `'use strict'; var console = {}, window = {}, self = {}, top = {}, document = {}, location = {}, navigation = {}, navigator = {}, parent = {}, localStorage = {}, sessionStorage = {}, indexedDB = {}, open = {}, close = {}, openDatabase = {}, fetch = {}, Storage = {}, cookieStore = {}, CookieStore = {}, alert = {};`;

const newCode = `return function() {${setup} return (${code}).bind(this);}.bind(this)()`;

return new Function(newCode).bind(context)();
}
19 changes: 11 additions & 8 deletions src/renderer/screens/RenameScreen/RenameScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,17 @@ describe('RenameScreen', () => {
'ipc-communication',
{
type: 'process-renames',
payload: [
{
isDirectory: false,
basePath: '/',
from: 'test2.test',
to: 'test_2.test',
},
],
payload: {
renames: [
{
isDirectory: false,
basePath: '/',
from: 'test2.test',
to: 'test_2.test',
},
],
options: { exit: false },
},
}
);
});
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/screens/RenameScreen/RenameScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export default function RenameScreen() {
const operations = computeRenameOperations(entries, rowSelection);

window.electron.ipcRenderer.sendMessage(
...processRenamesMessage(operations)
...processRenamesMessage(operations, {
exit: option.value === 'apply-and-close',
})
);
// TODO: implement apply and close
};
Expand Down

0 comments on commit 1ad269f

Please sign in to comment.