-
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(Attributes): add download button [YTFRONT-4310]
- Loading branch information
1 parent
c4bd229
commit 6710c0b
Showing
19 changed files
with
268 additions
and
16 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
30 changes: 30 additions & 0 deletions
30
packages/ui/src/ui/components/DownloadAttributesButton/DownloadFileButton.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,30 @@ | ||
import React, {FC} from 'react'; | ||
import {Button} from '@gravity-ui/uikit'; | ||
import Icon from '../../components/Icon/Icon'; | ||
|
||
type Props = { | ||
content: string; | ||
name: string; | ||
type?: string; | ||
}; | ||
|
||
export const DownloadFileButton: FC<Props> = ({name, type, content}) => { | ||
const handleClick = () => { | ||
const blob = new Blob([content], {type}); | ||
const url = URL.createObjectURL(blob); | ||
const link = document.createElement('a'); | ||
link.href = url; | ||
link.download = name; | ||
document.body.appendChild(link); | ||
link.click(); | ||
|
||
document.body.removeChild(link); | ||
URL.revokeObjectURL(url); | ||
}; | ||
|
||
return ( | ||
<Button view="flat" onClick={handleClick}> | ||
<Icon awesome="download" size={16} /> Download | ||
</Button> | ||
); | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/ui/src/ui/components/DownloadAttributesButton/YsonDownloadButton.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,22 @@ | ||
import React, {FC, useMemo} from 'react'; | ||
import {UnipikaSettings} from '../Yson/StructuredYson/StructuredYsonTypes'; | ||
import {DownloadFileButton} from './DownloadFileButton'; | ||
import {attributesToString} from './helpers/attributesToString'; | ||
|
||
type Props = { | ||
name?: string; | ||
value?: unknown; | ||
settings: UnipikaSettings; | ||
}; | ||
|
||
export const YsonDownloadButton: FC<Props> = ({value, name, settings}) => { | ||
const fileName = name || 'data'; | ||
|
||
const fileContent: string = useMemo(() => { | ||
return attributesToString(value, settings); | ||
}, [settings, value]); | ||
|
||
return ( | ||
<DownloadFileButton content={fileContent} name={`${fileName}.txt`} type="application/txt" /> | ||
); | ||
}; |
13 changes: 13 additions & 0 deletions
13
packages/ui/src/ui/components/DownloadAttributesButton/helpers/attributesToString.ts
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,13 @@ | ||
import unipika from '../../../common/thor/unipika'; | ||
import {UnipikaSettings} from '../../Yson/StructuredYson/StructuredYsonTypes'; | ||
|
||
export const attributesToString = (attributes: unknown, settings: UnipikaSettings) => { | ||
if (attributes === undefined || !settings) return ''; | ||
|
||
const convertedValue = | ||
settings.format === 'raw-json' | ||
? unipika.converters.raw(attributes, settings) | ||
: unipika.converters.yson(attributes, settings); | ||
|
||
return unipika.format(convertedValue, {...settings, asHTML: false}); | ||
}; |
2 changes: 2 additions & 0 deletions
2
packages/ui/src/ui/components/DownloadAttributesButton/index.ts
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,2 @@ | ||
export {DownloadFileButton} from './DownloadFileButton'; | ||
export {YsonDownloadButton} from './YsonDownloadButton'; |
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
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
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
3 changes: 3 additions & 0 deletions
3
packages/ui/src/ui/pages/navigation/helpers/pathToFileName.ts
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,3 @@ | ||
export const pathToFileName = (path: string) => { | ||
return path.replace(/\/+/g, '_'); | ||
}; |
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
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
Oops, something went wrong.