URL selector cancel button cancels ongoing url selection

Changelog-entry: URL selector cancel button cancels ongoing url selection
Change-type: patch
This commit is contained in:
Alexis Svinartchouk 2020-08-25 11:51:28 +02:00
parent fff9452509
commit 92dfdc6edd

View File

@ -290,7 +290,7 @@ export class SourceSelector extends React.Component<
await this.selectImageByPath({ await this.selectImageByPath({
imagePath, imagePath,
SourceType: isURL ? sourceDestination.Http : sourceDestination.File, SourceType: isURL ? sourceDestination.Http : sourceDestination.File,
}); }).promise;
} }
private reselectImage() { private reselectImage() {
@ -346,12 +346,24 @@ export class SourceSelector extends React.Component<
} }
} }
private async selectImageByPath({ imagePath, SourceType }: SourceOptions) { private selectImageByPath({
imagePath,
SourceType,
}: SourceOptions): { promise: Promise<void>; cancel: () => void } {
let cancelled = false;
return {
cancel: () => {
cancelled = true;
},
promise: (async () => {
try { try {
imagePath = await replaceWindowsNetworkDriveLetter(imagePath); imagePath = await replaceWindowsNetworkDriveLetter(imagePath);
} catch (error) { } catch (error) {
analytics.logException(error); analytics.logException(error);
} }
if (cancelled) {
return;
}
let source; let source;
if (SourceType === sourceDestination.File) { if (SourceType === sourceDestination.File) {
@ -377,13 +389,22 @@ export class SourceSelector extends React.Component<
try { try {
const innerSource = await source.getInnerSource(); const innerSource = await source.getInnerSource();
if (cancelled) {
return;
}
const metadata = (await innerSource.getMetadata()) as sourceDestination.Metadata & { const metadata = (await innerSource.getMetadata()) as sourceDestination.Metadata & {
hasMBR: boolean; hasMBR: boolean;
partitions: MBRPartition[] | GPTPartition[]; partitions: MBRPartition[] | GPTPartition[];
path: string; path: string;
extension: string; extension: string;
}; };
if (cancelled) {
return;
}
const partitionTable = await innerSource.getPartitionTable(); const partitionTable = await innerSource.getPartitionTable();
if (cancelled) {
return;
}
if (partitionTable) { if (partitionTable) {
metadata.hasMBR = true; metadata.hasMBR = true;
metadata.partitions = partitionTable.partitions; metadata.partitions = partitionTable.partitions;
@ -414,6 +435,8 @@ export class SourceSelector extends React.Component<
// Noop // Noop
} }
} }
})(),
};
} }
private async openImageSelector() { private async openImageSelector() {
@ -427,22 +450,22 @@ export class SourceSelector extends React.Component<
analytics.logEvent('Image selector closed'); analytics.logEvent('Image selector closed');
return; return;
} }
this.selectImageByPath({ await this.selectImageByPath({
imagePath, imagePath,
SourceType: sourceDestination.File, SourceType: sourceDestination.File,
}); }).promise;
} catch (error) { } catch (error) {
exceptionReporter.report(error); exceptionReporter.report(error);
} }
} }
private onDrop(event: React.DragEvent<HTMLDivElement>) { private async onDrop(event: React.DragEvent<HTMLDivElement>) {
const [file] = event.dataTransfer.files; const [file] = event.dataTransfer.files;
if (file) { if (file) {
this.selectImageByPath({ await this.selectImageByPath({
imagePath: file.path, imagePath: file.path,
SourceType: sourceDestination.File, SourceType: sourceDestination.File,
}); }).promise;
} }
} }
@ -486,6 +509,9 @@ export class SourceSelector extends React.Component<
const imageName = selectionState.getImageName(); const imageName = selectionState.getImageName();
const imageSize = selectionState.getImageSize(); const imageSize = selectionState.getImageSize();
const imageLogo = selectionState.getImageLogo(); const imageLogo = selectionState.getImageLogo();
let cancelURLSelection = () => {
// noop
};
return ( return (
<> <>
@ -587,6 +613,7 @@ export class SourceSelector extends React.Component<
{showURLSelector && ( {showURLSelector && (
<URLSelector <URLSelector
cancel={() => { cancel={() => {
cancelURLSelection();
this.setState({ this.setState({
showURLSelector: false, showURLSelector: false,
}); });
@ -596,16 +623,17 @@ export class SourceSelector extends React.Component<
// if no file was resolved from the dialog. // if no file was resolved from the dialog.
if (!imageURL) { if (!imageURL) {
analytics.logEvent('URL selector closed'); analytics.logEvent('URL selector closed');
this.setState({ } else {
showURLSelector: false, let promise;
}); ({
return; promise,
} cancel: cancelURLSelection,
} = this.selectImageByPath({
await this.selectImageByPath({
imagePath: imageURL, imagePath: imageURL,
SourceType: sourceDestination.Http, SourceType: sourceDestination.Http,
}); }));
await promise;
}
this.setState({ this.setState({
showURLSelector: false, showURLSelector: false,
}); });