PluginProbe
Code Snippets / 3.10.2
Code Snippets v3.10.2
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 / common / cloud / CloudSnippetDownloadButton.tsx

CloudSnippetDownloadButton.tsx in Code Snippets 3.10.2, at js/components/common/cloud/CloudSnippetDownloadButton.tsx

80 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { useState } from 'react'
2 import { __ } from '@wordpress/i18n'
3 import { Spinner } from '@wordpress/components'
4 import { useRestAPI } from '../../../hooks/useRestAPI'
5 import { REST_BASES } from '../../../utils/restAPI'
6 import { getSnippetEditUrl, isProSnippet } from '../../../utils/snippets/snippets'
7 import { isLicensed } from '../../../utils/screen'
8 import { Button } from '../Button'
9 import { ErrorTooltip } from '../Tooltip'
10 import { UpsellDialog } from '../UpsellDialog'
11 import { useCloudSnippetDownloads } from './WithCloudSnippetDownloadsContext'
12 import type { CloudSnippetSchema } from '../../../types/schema/CloudSnippetSchema'
13
14 export interface DownloadSnippetResponse {
15 success: boolean
16 snippet_id: number
17 link_id: number
18 }
19
20 export interface CloudSnippetDownloadButtonProps {
21 onDownloaded: VoidFunction
22 snippet: CloudSnippetSchema
23 }
24
25 export const CloudSnippetDownloadButton: React.FC<CloudSnippetDownloadButtonProps> = ({ snippet, onDownloaded }) => {
26 const { api } = useRestAPI()
27 const { downloadRecords, updateDownloadRecord } = useCloudSnippetDownloads()
28 const [errorMessage, setErrorMessage] = useState<string>()
29 const [isUpsellOpen, setIsUpsellOpen] = useState(false)
30 const { isDownloading = false, localId } = downloadRecords[snippet.id] ?? {}
31 const localSnippetId = snippet.local_id ?? localId
32
33 if (localSnippetId) {
34 return (
35 <a className="button button-primary" href={getSnippetEditUrl({ id: localSnippetId })} target="_blank" rel="noopener noreferrer">
36 {__('Edit', 'code-snippets')}
37 </a>
38 )
39 }
40
41 if (isProSnippet(snippet) && !isLicensed()) {
42 return (
43 <>
44 <Button className="cloud-pro-button" onClick={() => setIsUpsellOpen(true)}>
45 {__('Pro Only', 'code-snippets')}
46 </Button>
47 <UpsellDialog isOpen={isUpsellOpen} setIsOpen={setIsUpsellOpen} />
48 </>
49 )
50 }
51
52 const handleDownload = () => {
53 updateDownloadRecord(snippet.id, { isDownloading: true })
54 setErrorMessage(undefined)
55
56 api.post<DownloadSnippetResponse>(`${REST_BASES.cloud.snippets}/${snippet.id}/download`)
57 .then(response => {
58 updateDownloadRecord(snippet.id, { localId: response.snippet_id })
59 onDownloaded()
60 })
61 .catch((error: unknown) => {
62 setErrorMessage('string' === typeof error
63 ? error
64 : __('An error occurred while trying to download the snippet.', 'code-snippets'))
65 })
66 .finally(() => updateDownloadRecord(snippet.id, { isDownloading: false }))
67 }
68
69 return (
70 <>
71 {isDownloading && <Spinner />}
72 {errorMessage && <ErrorTooltip message={errorMessage} />}
73
74 <Button primary onClick={handleDownload} disabled={isDownloading}>
75 {__('Download', 'code-snippets')}
76 </Button>
77 </>
78 )
79 }
80