Skip to content

Commit 724724e

Browse files
committed
fixup: use electron.webUtils.getPathForFile for file inputs
1 parent 0f7079b commit 724724e

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

packages/compass-components/src/components/file-input.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe('FileInput', function () {
284284

285285
it('calls onChange with the chosen file', function () {
286286
expect(spy.callCount).to.equal(1);
287-
expect(spy.firstCall.args[0]).to.deep.equal(['new/file/path']);
287+
expect(spy.firstCall.args[0]).to.deep.equal([undefined]); // cannot get the actual path without electron
288288
});
289289
});
290290

packages/compass-components/src/components/file-input.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@ const disabledDescriptionDarkStyles = css({
134134

135135
type FileInputVariant = 'default' | 'small' | 'vertical';
136136

137-
// https://www.electronjs.org/docs/latest/api/file-object
138-
type FileWithPath = File & {
139-
/** Electron-specific property that contains an absolute path to the file */
140-
path: string;
141-
};
142-
143137
// Matches Electron's file dialog options.
144138
export type ElectronFileDialogOptions = {
145139
title?: string;
@@ -292,6 +286,20 @@ export function createElectronFileInputBackend<ElectronWindow>(
292286
};
293287
}
294288

289+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
290+
let _electron: typeof import('electron') | undefined | null;
291+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
292+
function getElectronWebUtils(): typeof import('electron').webUtils | undefined {
293+
if (_electron === undefined) {
294+
try {
295+
_electron = require('electron');
296+
} catch {
297+
_electron = null;
298+
}
299+
}
300+
return _electron?.webUtils;
301+
}
302+
295303
function FileInput({
296304
autoOpen = false,
297305
id,
@@ -357,7 +365,9 @@ function FileInput({
357365
(evt: React.ChangeEvent<HTMLInputElement>) => {
358366
const fileList = Array.from(evt.currentTarget.files ?? []);
359367
const files = fileList.map((file) => {
360-
return (file as FileWithPath).path;
368+
// https://github.com/electron/electron/blob/83d704009687956fb4b69cb13ab03664d7950118/docs/breaking-changes.md#removed-filepath
369+
// eslint-disable-next-line @typescript-eslint/no-var-requires
370+
return getElectronWebUtils()?.getPathForFile(file);
361371
});
362372
onChange(files);
363373
},

0 commit comments

Comments
 (0)