PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.0
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / components / EditorSidebar / actions / ExportButtons.tsx

ExportButtons.tsx in Code Snippets 3.10.0, at js/components/EditorSidebar/actions/ExportButtons.tsx

57 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React from 'react'
2 import { __ } from '@wordpress/i18n'
3 import { useRestAPI } from '../../../hooks/useRestAPI'
4 import { Button } from '../../common/Button'
5 import { downloadSnippetExportFile } from '../../../utils/files'
6 import { useSnippetForm } from '../../../hooks/useSnippetForm'
7 import type { Snippet } from '../../../types/Snippet'
8 import type { SnippetsExport } from '../../../types/schema/SnippetsExport'
9
10 interface ExportButtonProps {
11 name: string
12 label: string
13 makeRequest: (snippet: Snippet) => Promise<SnippetsExport | string>
14 }
15
16 const ExportButton: React.FC<ExportButtonProps> = ({ name, label, makeRequest }) => {
17 const { snippet, isWorking, setIsWorking, handleRequestError } = useSnippetForm()
18
19 const handleClick = () => {
20 setIsWorking(true)
21
22 makeRequest(snippet)
23 .then(response => downloadSnippetExportFile(response, snippet))
24 // translators: %s: error message.
25 .catch((error: unknown) => handleRequestError(error, __('Could not download export file.', 'code-snippets')))
26 .finally(() => setIsWorking(false))
27 }
28
29 return (
30 <Button name={name} onClick={handleClick} disabled={isWorking}>
31 {label}
32 </Button>
33 )
34 }
35
36 export const ExportButtons: React.FC = () => {
37 const { snippetsAPI } = useRestAPI()
38
39 return (
40 <div className="snippet-export-buttons">
41 <ExportButton
42 name="export_snippet"
43 label={__('Export', 'code-snippets')}
44 makeRequest={snippetsAPI.export}
45 />
46
47 {window.CODE_SNIPPETS_EDIT?.enableDownloads
48 ? <ExportButton
49 name="export_snippet_code"
50 label={__('Export Code', 'code-snippets')}
51 makeRequest={snippetsAPI.exportCode}
52 />
53 : null}
54 </div>
55 )
56 }
57