insomnia/packages/insomnia-app/app/common/select-file-or-folder.ts
Dimitri Mitropoulos 5f4c19da35
[TypeScript] Phase 1 & 2 (#3370)
Co-authored-by: Opender Singh <opender.singh@konghq.com>
2021-05-12 18:35:00 +12:00

67 lines
1.5 KiB
TypeScript

import { OpenDialogOptions, remote } from 'electron';
interface Options {
itemTypes?: Array<'file' | 'directory'>;
extensions?: Array<string>;
}
interface FileSelection {
filePath: string;
canceled: boolean;
}
const selectFileOrFolder = async ({ itemTypes, extensions }: Options): Promise<FileSelection> => {
// If no types are selected then default to just files and not directories
const types = itemTypes || ['file'];
let title = 'Select ';
if (types.includes('file')) {
title += ' File';
if (types.length > 2) {
title += ' or';
}
}
if (types.includes('directory')) {
title += ' Directory';
}
const options: OpenDialogOptions = {
title: title,
buttonLabel: 'Select',
// @ts-expect-error -- TSCONVERSION we should update this to accept other properties types as well, which flow all the way up to plugins
properties: types.map(type => {
if (type === 'file') {
return 'openFile';
}
if (type === 'directory') {
return 'openDirectory';
}
}),
filters: [
{
name: 'All Files',
extensions: ['*'],
},
],
};
// If extensions are provided then filter for just those extensions
if (extensions?.length) {
options.filters = [
{
name: 'Files',
extensions: extensions,
},
];
}
const { canceled, filePaths } = await remote.dialog.showOpenDialog(options);
return {
filePath: filePaths[0],
canceled,
};
};
export default selectFileOrFolder;