diff --git a/src/js/components/ImportMenu/MigrateForm/WithMigrationData.tsx b/src/js/components/ImportMenu/MigrateForm/WithMigrationData.tsx index b32803757..89a79ce73 100644 --- a/src/js/components/ImportMenu/MigrateForm/WithMigrationData.tsx +++ b/src/js/components/ImportMenu/MigrateForm/WithMigrationData.tsx @@ -5,7 +5,7 @@ import { useSelection } from '../../../hooks/useSelection' import { createContextHook } from '../../../utils/bootstrap' import { REST_BASES } from '../../../utils/restAPI' import { isNetworkAdmin } from '../../../utils/screen' -import { useMigrationOptions } from './WithMigrationOptions' +import { sanitizeImporterName, useMigrationOptions } from './WithMigrationOptions' import type { RestAPI } from '../../../hooks/useRestAPI' import type { UseSelection } from '../../../hooks/useSelection' import type { PropsWithChildren } from 'react' @@ -52,11 +52,14 @@ const makeMigrationRequest = async (api: RestAPI, importer: string, request: Imp throw new Error(__('Please select snippets to import.', 'code-snippets')) } - if (!importer) { + const sanitizedImporter = sanitizeImporterName(importer) + + if (!sanitizedImporter) { throw new Error(__('Please select an importer.', 'code-snippets')) } - const response = await api.post(`${REST_BASES.import.plugins}/${importer}/import`, request) + const requestUrl = `${REST_BASES.import.plugins}/${encodeURIComponent(sanitizedImporter)}/import` + const response = await api.post(requestUrl, request) if (1 > response.imported.length) { throw new Error(__('No snippets were imported.', 'code-snippets')) @@ -125,11 +128,20 @@ export const WithMigrationData: React.FC = ({ children }) => .then(response => Object.values(response))) }, [api, fetchImporters]) + const isAvailableImporter = importers.some(importer => importer.is_active && importer.name === selectedImporter) + + useEffect(() => { + if (selectedImporter && 0 < importers.length && !isAvailableImporter) { + setSelectedImporter('') + } + }, [importers.length, isAvailableImporter, selectedImporter, setSelectedImporter]) + useEffect(() => { - if (selectedImporter) { - fetchSnippets(() => api.get(`${REST_BASES.import.plugins}/${selectedImporter}`)) + if (isAvailableImporter) { + fetchSnippets(() => + api.get(`${REST_BASES.import.plugins}/${encodeURIComponent(selectedImporter)}`)) } - }, [api, selectedImporter, fetchSnippets]) + }, [api, isAvailableImporter, selectedImporter, fetchSnippets]) const importSnippets = () => { const request: ImportRequest = { @@ -144,7 +156,7 @@ export const WithMigrationData: React.FC = ({ children }) => snippetSelection.clearSelection() } - doImport(() => makeMigrationRequest(api, selectedImporter, request), handleImportSuccess) + doImport(() => makeMigrationRequest(api, isAvailableImporter ? selectedImporter : '', request), handleImportSuccess) } const changeSelectedImporter = (newImporter: string) => { diff --git a/src/js/components/ImportMenu/MigrateForm/WithMigrationOptions.tsx b/src/js/components/ImportMenu/MigrateForm/WithMigrationOptions.tsx index 314530966..25cfd709a 100644 --- a/src/js/components/ImportMenu/MigrateForm/WithMigrationOptions.tsx +++ b/src/js/components/ImportMenu/MigrateForm/WithMigrationOptions.tsx @@ -5,6 +5,12 @@ import type { PropsWithChildren } from 'react' const IMPORTER_QUERY_PARAM = 'from' +export const sanitizeImporterName = (name: string): string => + name.toLowerCase().replace(/[^a-z0-9-]/g, '') + +const fetchImporterFromQueryParam = () => + sanitizeImporterName(fetchQueryParam(IMPORTER_QUERY_PARAM) ?? '') + export interface MigrationOptionsContext { autoAddTags: boolean selectedImporter: string @@ -17,14 +23,15 @@ export interface MigrationOptionsContext { const [Context, useMigrationOptions] = createContextHook('useMigrationOptions') export const WithMigrationOptions: React.FC = ({ children }) => { - const [selectedImporter, setSelectedImporterValue] = useState(() => fetchQueryParam(IMPORTER_QUERY_PARAM) ?? '') + const [selectedImporter, setSelectedImporterValue] = useState(fetchImporterFromQueryParam) const [autoAddTags, setAutoAddTags] = useState(false) const [tagValue, setTagValue] = useState(() => `imported-${selectedImporter}`) const setSelectedImporter = useCallback((newImporter: string) => { - updateQueryParams({ [IMPORTER_QUERY_PARAM]: newImporter }) - setTagValue(`imported-${newImporter}`) - setSelectedImporterValue(newImporter) + const importer = sanitizeImporterName(newImporter) + updateQueryParams({ [IMPORTER_QUERY_PARAM]: importer }) + setTagValue(`imported-${importer}`) + setSelectedImporterValue(importer) }, []) const value: MigrationOptionsContext = {