-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DownloadManager): add setting for filename [YTFRONT-3564]
- Loading branch information
1 parent
b052987
commit 285d075
Showing
10 changed files
with
678 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ui/pages/navigation/content/Table/DownloadManager/DownloadShortInfo/DownloadShortInfo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
import moment, {Moment} from 'moment'; | ||
import {useSelector} from 'react-redux'; | ||
|
||
import format from '../../../../../../common/hammer/format'; | ||
import MetaTable from '../../../../../../components/MetaTable/MetaTable'; | ||
|
||
import {RootState} from '../../../../../../store/reducers'; | ||
import {getDownloadTableInfo} from '../../../../../../store/selectors/navigation/content/download-manager'; | ||
|
||
interface Props { | ||
id: string; | ||
filename: string; | ||
} | ||
|
||
export function DownloadShortInfo({id, filename}: Props) { | ||
const [time, setTime] = useState<Moment>(moment()); | ||
const {startTime, loading, loaded} = useSelector((state: RootState) => | ||
getDownloadTableInfo(state, id), | ||
); | ||
|
||
useEffect(() => { | ||
if (loaded && !loading) return; | ||
const interval = setInterval(() => { | ||
setTime(moment()); | ||
}, 1000); | ||
|
||
return () => clearInterval(interval); | ||
}, [loaded, loading]); | ||
|
||
const diff = moment(time).diff(startTime); | ||
|
||
return ( | ||
<MetaTable | ||
items={[ | ||
{ | ||
key: 'Filename', | ||
value: filename, | ||
visible: Boolean(filename), | ||
}, | ||
{ | ||
key: 'Duration', | ||
value: format.TimeDuration(diff), | ||
visible: Boolean(format.TimeDuration(diff)), | ||
}, | ||
]} | ||
/> | ||
); | ||
} |
99 changes: 99 additions & 0 deletions
99
packages/ui/src/ui/store/actions/navigation/content/table/download-manager.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React from 'react'; | ||
import {ThunkAction} from 'redux-thunk'; | ||
import {UnknownAction} from 'redux'; | ||
import {Text, Toaster} from '@gravity-ui/uikit'; | ||
import axios from 'axios'; | ||
|
||
import {AppStoreProvider} from '../../../../../containers/App/AppStoreProvider'; | ||
import {DownloadShortInfo} from '../../../../../pages/navigation/content/Table/DownloadManager/DownloadShortInfo/DownloadShortInfo'; | ||
|
||
import {RootState} from '../../../../reducers'; | ||
import {downloadManagerActions} from '../../../../reducers/navigation/content/table/download-manager'; | ||
|
||
import {downloadFileFromResponse} from '../../../../../utils/download-file'; | ||
|
||
const requestDownloadFile = async (url: string) => | ||
axios({ | ||
method: 'get', | ||
url: url, | ||
responseType: 'blob', | ||
withCredentials: true, | ||
}); | ||
|
||
const toaster = new Toaster(); | ||
|
||
const HIDING_TIMING = 5000; | ||
|
||
export const downloadFile = ( | ||
url: string, | ||
filename: string, | ||
): ThunkAction<Promise<void>, RootState, any, UnknownAction> => { | ||
const id = crypto.randomUUID(); | ||
|
||
return async (dispatch, _getState) => { | ||
try { | ||
dispatch(downloadManagerActions.onRequest({id})); | ||
updateToaster(id, 'loading', {filename}); | ||
|
||
const response = await requestDownloadFile(url); | ||
|
||
downloadFileFromResponse(filename, response); | ||
|
||
dispatch(downloadManagerActions.onSuccess({id})); | ||
updateToaster(id, 'success'); | ||
} catch (error: any) { | ||
let errorMessage = error; | ||
if (error.response) { | ||
const errorText = await error.response.data.text(); | ||
errorMessage = JSON.parse(errorText).message; | ||
} | ||
|
||
dispatch(downloadManagerActions.onFailure({id, error})); | ||
updateToaster(id, 'failure', {errorMessage}); | ||
} | ||
|
||
setTimeout(() => { | ||
dispatch(downloadManagerActions.onCleanup({id})); | ||
}, HIDING_TIMING * 2); | ||
}; | ||
}; | ||
|
||
const updateToaster = ( | ||
id: string, | ||
status: 'loading' | 'success' | 'failure', | ||
options?: { | ||
filename?: string; | ||
errorMessage?: string; | ||
}, | ||
) => { | ||
if (status === 'loading') { | ||
toaster.add({ | ||
title: 'Downloading', | ||
name: `downloadingFile_${id}`, | ||
theme: 'info', | ||
content: ( | ||
<AppStoreProvider> | ||
<DownloadShortInfo id={id} filename={options?.filename || ''} /> | ||
</AppStoreProvider> | ||
), | ||
autoHiding: false, | ||
}); | ||
} | ||
|
||
if (status === 'success') { | ||
toaster.update(`downloadingFile_${id}`, { | ||
title: 'Success', | ||
theme: 'success', | ||
autoHiding: HIDING_TIMING, | ||
}); | ||
} | ||
|
||
if (status === 'failure') { | ||
toaster.update(`downloadingFile_${id}`, { | ||
title: 'Failure', | ||
theme: 'danger', | ||
content: <Text>{options?.errorMessage || ''}</Text>, | ||
autoHiding: HIDING_TIMING, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.