Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<ImportResponse, ImportRequest>(`${REST_BASES.import.plugins}/${importer}/import`, request)
const requestUrl = `${REST_BASES.import.plugins}/${encodeURIComponent(sanitizedImporter)}/import`
const response = await api.post<ImportResponse, ImportRequest>(requestUrl, request)

if (1 > response.imported.length) {
throw new Error(__('No snippets were imported.', 'code-snippets'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +23,7 @@ export interface MigrationOptionsContext {
const [Context, useMigrationOptions] = createContextHook<MigrationOptionsContext>('useMigrationOptions')

export const WithMigrationOptions: React.FC<PropsWithChildren> = ({ children }) => {
const [selectedImporter, setSelectedImporterValue] = useState<string>(() => fetchQueryParam(IMPORTER_QUERY_PARAM) ?? '')
const [selectedImporter, setSelectedImporterValue] = useState<string>(fetchImporterFromQueryParam)
const [autoAddTags, setAutoAddTags] = useState(false)
const [tagValue, setTagValue] = useState<string>(() => `imported-${selectedImporter}`)

Expand Down
Loading