-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vision): use blob urls for downloads (#6213)
- Loading branch information
Showing
4 changed files
with
90 additions
and
24 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/@sanity/vision/src/components/DownloadCsvButton.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,37 @@ | ||
import {DocumentSheetIcon} from '@sanity/icons' | ||
import {Button, Tooltip} from '@sanity/ui' | ||
import {type MouseEvent} from 'react' | ||
import {useTranslation} from 'sanity' | ||
|
||
import {visionLocaleNamespace} from '../i18n' | ||
|
||
function preventDownload(evt: MouseEvent<HTMLButtonElement>) { | ||
return evt.preventDefault() | ||
} | ||
|
||
export function DownloadCsvButton({csvUrl}: {csvUrl: string | undefined}) { | ||
const {t} = useTranslation(visionLocaleNamespace) | ||
const isDisabled = !csvUrl | ||
|
||
const button = ( | ||
<Button | ||
as="a" | ||
disabled={isDisabled} | ||
download={isDisabled ? undefined : 'query-result.csv'} | ||
href={csvUrl} | ||
icon={DocumentSheetIcon} | ||
mode="ghost" | ||
onClick={isDisabled ? preventDownload : undefined} | ||
text={t('action.download-result-as-csv')} | ||
tone="default" | ||
/> | ||
) | ||
|
||
return isDisabled ? ( | ||
<Tooltip content={t('action.download-result-as-csv.not-csv-encodable')} placement="top"> | ||
{button} | ||
</Tooltip> | ||
) : ( | ||
button | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {json2csv} from 'json-2-csv' | ||
|
||
function getBlobUrl(content: string, mimeType: string): string { | ||
return URL.createObjectURL( | ||
new Blob([content], { | ||
type: mimeType, | ||
}), | ||
) | ||
} | ||
|
||
function getMemoizedBlobUrlResolver(mimeType: string, stringEncoder: (input: any) => string) { | ||
return (() => { | ||
let prevResult = '' | ||
let prevContent = '' | ||
return (input: unknown) => { | ||
const content = stringEncoder(input) | ||
if (typeof content !== 'string' || content === '') { | ||
return undefined | ||
} | ||
|
||
if (content === prevContent) { | ||
return prevResult | ||
} | ||
|
||
prevContent = content | ||
if (prevResult) { | ||
URL.revokeObjectURL(prevResult) | ||
} | ||
|
||
prevResult = getBlobUrl(content, mimeType) | ||
return prevResult | ||
} | ||
})() | ||
} | ||
|
||
export const getJsonBlobUrl = getMemoizedBlobUrlResolver('application/json', (input) => | ||
JSON.stringify(input, null, 2), | ||
) | ||
|
||
export const getCsvBlobUrl = getMemoizedBlobUrlResolver('text/csv', (input) => { | ||
return json2csv(Array.isArray(input) ? input : [input]).trim() | ||
}) |