-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vision): add "save result as json/csv" buttons (#6158)
* feat(vision): add download as json/csv buttons * fix(vision): use blob urls for downloads (#6213) * fix(vision): use Translate component to avoid splitting i18n strings * fix(vision): clean up i18n resources for result saving feature --------- Co-authored-by: Espen Hovlandsdal <[email protected]>
- Loading branch information
Showing
7 changed files
with
173 additions
and
6 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
57 changes: 57 additions & 0 deletions
57
packages/@sanity/vision/src/components/SaveResultButtons.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,57 @@ | ||
import {DocumentSheetIcon} from '@sanity/icons' | ||
import {Button, Tooltip} from '@sanity/ui' | ||
import {type MouseEvent} from 'react' | ||
import {useTranslation} from 'sanity' | ||
|
||
import {visionLocaleNamespace} from '../i18n' | ||
|
||
interface SaveButtonProps { | ||
blobUrl: string | undefined | ||
} | ||
|
||
function preventSave(evt: MouseEvent<HTMLButtonElement>) { | ||
return evt.preventDefault() | ||
} | ||
|
||
export function SaveCsvButton({blobUrl}: SaveButtonProps) { | ||
const {t} = useTranslation(visionLocaleNamespace) | ||
const isDisabled = !blobUrl | ||
|
||
const button = ( | ||
<Button | ||
as="a" | ||
disabled={isDisabled} | ||
download={isDisabled ? undefined : 'query-result.csv'} | ||
href={blobUrl} | ||
icon={DocumentSheetIcon} | ||
mode="ghost" | ||
onClick={isDisabled ? preventSave : undefined} | ||
// eslint-disable-next-line @sanity/i18n/no-attribute-string-literals | ||
text="CSV" // String is a File extension | ||
tone="default" | ||
/> | ||
) | ||
|
||
return isDisabled ? ( | ||
<Tooltip content={t('result.save-result-as-csv.not-csv-encodable')} placement="top"> | ||
{button} | ||
</Tooltip> | ||
) : ( | ||
button | ||
) | ||
} | ||
|
||
export function SaveJsonButton({blobUrl}: SaveButtonProps) { | ||
return ( | ||
<Button | ||
as="a" | ||
download={'query-result.json'} | ||
href={blobUrl} | ||
icon={DocumentSheetIcon} | ||
mode="ghost" | ||
// eslint-disable-next-line @sanity/i18n/no-attribute-string-literals | ||
text="JSON" // String is a File extension | ||
tone="default" | ||
/> | ||
) | ||
} |
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
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() | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.