| 1 |
import { useState } from 'react' |
| 2 |
import { __ } from '@wordpress/i18n' |
| 3 |
import { useImportersAPI, type ImportableSnippet } from '../../../../hooks/useImportersAPI' |
| 4 |
import { isNetworkAdmin } from '../../../../utils/screen' |
| 5 |
|
| 6 |
export const useSnippetImport = () => { |
| 7 |
const [snippets, setSnippets] = useState<ImportableSnippet[]>([]) |
| 8 |
const [isLoadingSnippets, setIsLoadingSnippets] = useState(false) |
| 9 |
const [snippetsError, setSnippetsError] = useState<string | null>(null) |
| 10 |
const [isImporting, setIsImporting] = useState(false) |
| 11 |
const [importError, setImportError] = useState<string | null>(null) |
| 12 |
const [importSuccess, setImportSuccess] = useState<number[]>([]) |
| 13 |
|
| 14 |
const importersAPI = useImportersAPI() |
| 15 |
|
| 16 |
const loadSnippets = async (importerName: string): Promise<boolean> => { |
| 17 |
if (!importerName) { |
| 18 |
alert(__('Please select an importer.', 'code-snippets')) |
| 19 |
return false |
| 20 |
} |
| 21 |
|
| 22 |
setIsLoadingSnippets(true) |
| 23 |
setSnippetsError(null) |
| 24 |
setSnippets([]) |
| 25 |
clearResults() |
| 26 |
|
| 27 |
try { |
| 28 |
const response = await importersAPI.fetchSnippets(importerName) |
| 29 |
setSnippets(response.data) |
| 30 |
return true |
| 31 |
} catch (err) { |
| 32 |
setSnippetsError(err instanceof Error ? err.message : 'Unknown error') |
| 33 |
return false |
| 34 |
} finally { |
| 35 |
setIsLoadingSnippets(false) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
const importSnippets = async ( |
| 40 |
importerName: string, |
| 41 |
selectedSnippetIds: number[], |
| 42 |
autoAddTags: boolean, |
| 43 |
tagValue: string |
| 44 |
): Promise<boolean> => { |
| 45 |
if (selectedSnippetIds.length === 0) { |
| 46 |
alert(__('Please select snippets to import.', 'code-snippets')) |
| 47 |
return false |
| 48 |
} |
| 49 |
|
| 50 |
if (!importerName) { |
| 51 |
alert(__('Please select an importer.', 'code-snippets')) |
| 52 |
return false |
| 53 |
} |
| 54 |
|
| 55 |
setIsImporting(true) |
| 56 |
setImportError(null) |
| 57 |
setImportSuccess([]) |
| 58 |
|
| 59 |
try { |
| 60 |
const response = await importersAPI.importSnippets(importerName, { |
| 61 |
ids: selectedSnippetIds, |
| 62 |
network: isNetworkAdmin(), |
| 63 |
auto_add_tags: autoAddTags, |
| 64 |
tag_value: autoAddTags ? tagValue : undefined |
| 65 |
}) |
| 66 |
|
| 67 |
setImportSuccess(response.data.imported) |
| 68 |
|
| 69 |
if (response.data.imported.length > 0) { |
| 70 |
setSnippets([]) |
| 71 |
return true |
| 72 |
} else { |
| 73 |
alert(__('No snippets were imported.', 'code-snippets')) |
| 74 |
return false |
| 75 |
} |
| 76 |
} catch (err) { |
| 77 |
setImportError(err instanceof Error ? err.message : 'Unknown error') |
| 78 |
return false |
| 79 |
} finally { |
| 80 |
setIsImporting(false) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
const clearResults = () => { |
| 85 |
setImportSuccess([]) |
| 86 |
setImportError(null) |
| 87 |
} |
| 88 |
|
| 89 |
const resetAll = () => { |
| 90 |
setSnippets([]) |
| 91 |
clearResults() |
| 92 |
setSnippetsError(null) |
| 93 |
} |
| 94 |
|
| 95 |
return { |
| 96 |
snippets, |
| 97 |
isLoadingSnippets, |
| 98 |
snippetsError, |
| 99 |
isImporting, |
| 100 |
importError, |
| 101 |
importSuccess, |
| 102 |
loadSnippets, |
| 103 |
importSnippets, |
| 104 |
clearResults, |
| 105 |
resetAll |
| 106 |
} |
| 107 |
} |
| 108 |
|