Skip to content

Commit

Permalink
Add delete confirmation and and restore option to canceled downloads (#…
Browse files Browse the repository at this point in the history
…1531)

* Close file handlers after usage
Fixing deprected warnings in stdout

* Add delete confirmation and and restore option to canceled downloads
  • Loading branch information
Eskaan committed Jan 29, 2023
1 parent 7fd15c7 commit 9d84085
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 23 deletions.
1 change: 1 addition & 0 deletions scripts/createDeploy.js
Expand Up @@ -77,6 +77,7 @@ const createDeployFiles = async () => {
if (err) {
reject();
}
destination.close();
resolve();
});
});
Expand Down
3 changes: 3 additions & 0 deletions src/app/desktop/utils/downloader.js
Expand Up @@ -103,8 +103,10 @@ const downloadFileInstance = async (fileName, url, sha1, legacyPath) => {

data.on('end', () => {
wStream.end();
wStream.close();
if (legacyPath) {
wStreamLegacy.end();
wStreamLegacy.close();
}
resolve();
});
Expand Down Expand Up @@ -148,6 +150,7 @@ export const downloadFile = async (fileName, url, onProgress) => {
return new Promise((resolve, reject) => {
data.on('end', () => {
out.end();
out.close();
resolve();
});

Expand Down
7 changes: 6 additions & 1 deletion src/common/modals/InstanceDeleteConfirmation.js
Expand Up @@ -67,7 +67,12 @@ const InstanceDeleteConfirmation = ({ instanceName }) => {
>
No, Abort
</Button>
<Button onClick={deleteInstance} loading={loading}>
<Button
danger
type="primary"
onClick={deleteInstance}
loading={loading}
>
Yes, Delete
</Button>
</div>
Expand Down
28 changes: 23 additions & 5 deletions src/common/modals/InstanceDownloadFailed.js
Expand Up @@ -8,7 +8,7 @@ import {
removeDownloadFromQueue,
updateInstanceConfig
} from '../reducers/actions';
import { closeModal } from '../reducers/modals/actions';
import { openModal, closeModal } from '../reducers/modals/actions';
import { _getInstancesPath, _getTempPath } from '../utils/selectors';
import { rollBackInstanceZip } from '../utils';

Expand All @@ -28,7 +28,15 @@ const InstanceDownloadFailed = ({
? `${instanceName.substring(0, 20)}...`
: instanceName;

const cancelDownload = async () => {
const deleteDownload = async () => {
await dispatch(removeDownloadFromQueue(instanceName, true));

dispatch(closeModal());
await new Promise(resolve => setTimeout(resolve, 1000));
dispatch(openModal('InstanceDeleteConfirmation', { instanceName }));
};

const restoreDownload = async () => {
await dispatch(removeDownloadFromQueue(instanceName, true));
setLoading(true);
await new Promise(resolve => setTimeout(resolve, 1000));
Expand Down Expand Up @@ -87,11 +95,21 @@ const InstanceDownloadFailed = ({
<Button
variant="contained"
color="primary"
onClick={cancelDownload}
loading={loading}
onClick={deleteDownload}
disabled={loading}
>
Cancel Download
Delete Instance
</Button>
{isUpdate && (
<Button
variant="contained"
color="primary"
onClick={restoreDownload}
loading={loading}
>
Restore instance
</Button>
)}
<Button danger type="primary" onClick={retry} disabled={loading}>
Retry Download
</Button>
Expand Down
30 changes: 17 additions & 13 deletions src/common/modals/OptedOutModsList.js
Expand Up @@ -202,17 +202,19 @@ const OptedOutModsList = ({
title="Opted out mods list"
>
<Container>
<div
css={`
text-align: left;
margin-bottom: 2rem;
`}
>
Hey oh! It looks like some developers opted out from showing their
mods on third-party launchers. We can still attempt to download them
automatically. Please click continue and wait for all downloads to
finish. Please don&apos;t click anything inside the browser.
</div>
{!cloudflareBlock && (
<div
css={`
text-align: left;
margin-bottom: 2rem;
`}
>
Hey oh! It looks like some developers opted out from showing their
mods on third-party launchers. We can still attempt to download them
automatically. Please click continue and wait for all downloads to
finish. Please don&apos;t click anything inside the browser.
</div>
)}
<ModsContainer>
{optedOutMods &&
optedOutMods.map(mod => {
Expand All @@ -231,7 +233,6 @@ const OptedOutModsList = ({
{cloudflareBlock && (
<p
css={`
width: 90%;
margin: 20px auto 0 auto;
`}
>
Expand All @@ -254,7 +255,9 @@ const OptedOutModsList = ({
<Button
danger
type="text"
disabled={downloading || loadedMods.length !== 0}
disabled={
(missingMods.length > 0 && !cloudflareBlock) || downloading
}
onClick={() => {
dispatch(closeModal());
setTimeout(
Expand Down Expand Up @@ -290,6 +293,7 @@ const OptedOutModsList = ({
mods: optedOutMods,
instancePath
});
setDownloading(false);
}}
css={`
background-color: ${props => props.theme.palette.colors.green};
Expand Down
10 changes: 7 additions & 3 deletions src/common/reducers/actions.js
Expand Up @@ -1068,6 +1068,7 @@ export function updateInstanceConfig(
if (readBuff.every(v => v === 0)) {
throw new Error('Corrupted file');
}
newFile.close();
await fs.rename(tempP, p);
};

Expand Down Expand Up @@ -2232,6 +2233,7 @@ export function downloadInstance(instanceName) {

await dispatch(removeDownloadFromQueue(instanceName));
dispatch(addNextInstanceToCurrentDownload());
await remove(tempInstancePath);
} catch (err) {
console.error(err);
// Show error modal and decide what to do
Expand All @@ -2243,8 +2245,6 @@ export function downloadInstance(instanceName) {
isUpdate
})
);
} finally {
await remove(tempInstancePath);
}
};
}
Expand Down Expand Up @@ -2704,12 +2704,15 @@ export const startListener = () => {
!changesTracker[completePath].completed &&
(event.action === 2 || event.action === 0 || event.action === 1)
) {
let filehandle;
try {
await new Promise(resolve => setTimeout(resolve, 300));
await fs.open(completePath, 'r+');
filehandle = await fs.open(completePath, 'r+');
changesTracker[completePath].completed = true;
} catch {
// Do nothing, simply not completed..
} finally {
await filehandle?.close();
}
}
})
Expand Down Expand Up @@ -3762,6 +3765,7 @@ export const checkForPortableUpdates = () => {
if (err) {
reject(err);
}
destination.close();
resolve();
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/index.js
Expand Up @@ -232,7 +232,7 @@ export const makeInstanceRestorePoint = async (
await access(newInstancePath);
await remove(newInstancePath);
} catch (e) {
console.warn(e);
console.log('Expected ENOENT:', e);
}
await makeDir(newInstancePath);
try {
Expand Down

0 comments on commit 9d84085

Please sign in to comment.