From ed88f42935e659484ded632f51067f8688b43f9a Mon Sep 17 00:00:00 2001 From: Rostislav Melkumyan Date: Wed, 8 May 2024 17:39:14 +0200 Subject: [PATCH] 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 --- packages/@sanity/vision/package.json | 1 + .../src/components/SaveResultButtons.tsx | 57 +++++++++++++++++++ .../src/components/VisionGui.styled.tsx | 21 ++++++- .../vision/src/components/VisionGui.tsx | 33 +++++++++-- packages/@sanity/vision/src/i18n/resources.ts | 4 ++ .../@sanity/vision/src/util/getBlobUrl.ts | 42 ++++++++++++++ pnpm-lock.yaml | 21 +++++++ 7 files changed, 173 insertions(+), 6 deletions(-) create mode 100644 packages/@sanity/vision/src/components/SaveResultButtons.tsx create mode 100644 packages/@sanity/vision/src/util/getBlobUrl.ts diff --git a/packages/@sanity/vision/package.json b/packages/@sanity/vision/package.json index bd6c84ab06c..6a1fbbca6a6 100644 --- a/packages/@sanity/vision/package.json +++ b/packages/@sanity/vision/package.json @@ -66,6 +66,7 @@ "@sanity/ui": "^2.1.6", "@uiw/react-codemirror": "^4.11.4", "is-hotkey-esm": "^1.0.0", + "json-2-csv": "^5.5.1", "json5": "^2.2.3", "lodash": "^4.17.21", "quick-lru": "^5.1.1" diff --git a/packages/@sanity/vision/src/components/SaveResultButtons.tsx b/packages/@sanity/vision/src/components/SaveResultButtons.tsx new file mode 100644 index 00000000000..b8cb466ae46 --- /dev/null +++ b/packages/@sanity/vision/src/components/SaveResultButtons.tsx @@ -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) { + return evt.preventDefault() +} + +export function SaveCsvButton({blobUrl}: SaveButtonProps) { + const {t} = useTranslation(visionLocaleNamespace) + const isDisabled = !blobUrl + + const button = ( +