PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
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 / ImportMenu / MigrateForm / MigrateForm.tsx

MigrateForm.tsx in Code Snippets 4.0.0-beta.1, at js/components/ImportMenu/MigrateForm/MigrateForm.tsx

124 lines 4.2 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 { createInterpolateElement } from '@wordpress/element'
3 import { __, sprintf } from '@wordpress/i18n'
4 import { ImportCard } from '../common/ImportCard'
5 import { ImporterSelector } from './ImporterSelector'
6 import { ImportOptions } from './ImportOptions'
7 import { SimpleSnippetTable } from './SimpleSnippetTable'
8 import { MigrationStep, WithMigrationData, useMigrationData } from './WithMigrationData'
9 import { WithMigrationOptions, useMigrationOptions } from './WithMigrationOptions'
10 import type { ReactNode } from 'react'
11
12 interface StatusDisplayProps {
13 type: 'error' | 'success'
14 title: ReactNode
15 children: ReactNode
16 }
17
18 const StatusDisplay: React.FC<StatusDisplayProps> = ({ type, title, children }) =>
19 <ImportCard
20 variant="controls"
21 className="import-section-status"
22 role={'error' === type ? 'alert' : 'status'}
23 aria-live={'error' === type ? 'assertive' : 'polite'}
24 >
25 <div className={type} aria-hidden="true"><span>{'error' === type ? 'âś•' : 'âś“'}</span></div>
26 <div>
27 <h4>{title}</h4>
28 <p>{children}</p>
29 </div>
30 </ImportCard>
31
32 const StatusMessages: React.FC = () => {
33 const { selectedImporter } = useMigrationOptions()
34 const { error, isWorking, snippetSelection, importedIds } = useMigrationData()
35
36 return (
37 <>
38 {error?.step === MigrationStep.FetchSnippets && (
39 <StatusDisplay type="error" title={__('Error loading snippets', 'code-snippets')}>
40 {error.message}
41 </StatusDisplay>)}
42
43 {error?.step === MigrationStep.MigrateSnippets && (
44 <StatusDisplay type="error" title={__('Error importing snippets', 'code-snippets')}>
45 {error.message}
46 </StatusDisplay>)}
47
48 {0 < importedIds.length && (
49 // translators: %d: number of imported snippets.
50 <StatusDisplay type="success" title={sprintf(__('%d snippets imported!', 'code-snippets'), importedIds.length)}>
51 {createInterpolateElement(
52 __('Selected snippets have been successfully imported to your <a>Code Snippets library</a>.', 'code-snippets'),
53 {
54 // eslint-disable-next-line jsx-a11y/anchor-has-content, jsx-a11y/control-has-associated-label
55 a: <a href={window.CODE_SNIPPETS?.urls.manage} />
56 }
57 )}
58 </StatusDisplay>)}
59
60 {selectedImporter &&
61 isWorking !== MigrationStep.FetchSnippets && error?.step !== MigrationStep.FetchSnippets &&
62 0 === snippetSelection.availableItems.length && 0 === importedIds.length && (
63 <ImportCard className="no-snippets-card" role="status" aria-live="polite">
64 <div className="card-inner">
65 <div className="card-icon" aria-hidden="true">đź“­</div>
66 <h4>{__('No snippets found', 'code-snippets')}</h4>
67 <p>{__('No snippets were found for the selected plugin. Make sure the plugin is installed and has snippets configured.', 'code-snippets')}</p>
68 </div>
69 </ImportCard>)}
70 </>
71 )
72 }
73
74 const MigrateFormInner: React.FC = () => {
75 const { selectedImporter } = useMigrationOptions()
76 const { isWorking, error, importers, snippetSelection, importSnippets, changeSelectedImporter } = useMigrationData()
77
78 if (isWorking === MigrationStep.LoadImporters) {
79 return <p role="status" aria-live="polite">{__('Loading importers…', 'code-snippets')}</p>
80 }
81
82 if (error?.step === MigrationStep.LoadImporters) {
83 return (
84 <div className="notice notice-error">
85 {/* translators: %s: error message. */}
86 <p>{sprintf(__('Error loading importers: %s', 'code-snippets'), error.message)}</p>
87 </div>
88 )
89 }
90
91 return (
92 <>
93 <p>{__('If you are using another snippets plugin, you can import those existing snippets to your Code Snippets library.', 'code-snippets')}</p>
94
95 <ImporterSelector
96 value={selectedImporter}
97 options={importers}
98 isLoading={isWorking === MigrationStep.FetchSnippets}
99 onChange={value => changeSelectedImporter(value)}
100 />
101
102 <StatusMessages />
103
104 {0 < snippetSelection.availableItems.length && (
105 <>
106 <ImportOptions />
107
108 <SimpleSnippetTable
109 onImport={importSnippets}
110 selection={snippetSelection}
111 isImporting={isWorking === MigrationStep.MigrateSnippets}
112 />
113 </>)}
114 </>
115 )
116 }
117
118 export const MigrateForm: React.FC = () =>
119 <WithMigrationOptions>
120 <WithMigrationData>
121 <MigrateFormInner />
122 </WithMigrationData>
123 </WithMigrationOptions>
124