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

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

43 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { useCallback, useState } from 'react'
2 import { createContextHook } from '../../../utils/bootstrap'
3 import { fetchQueryParam, updateQueryParams } from '../../../utils/urls'
4 import type { PropsWithChildren } from 'react'
5
6 const IMPORTER_QUERY_PARAM = 'from'
7
8 export interface MigrationOptionsContext {
9 autoAddTags: boolean
10 selectedImporter: string
11 setAutoAddTags: (value: boolean) => void
12 setSelectedImporter: (newImporter: string) => void
13 setTagValue: (value: string) => void
14 tagValue: string
15 }
16
17 const [Context, useMigrationOptions] = createContextHook<MigrationOptionsContext>('useMigrationOptions')
18
19 export const WithMigrationOptions: React.FC<PropsWithChildren> = ({ children }) => {
20 const [selectedImporter, setSelectedImporterValue] = useState<string>(() => fetchQueryParam(IMPORTER_QUERY_PARAM) ?? '')
21 const [autoAddTags, setAutoAddTags] = useState(false)
22 const [tagValue, setTagValue] = useState<string>(() => `imported-${selectedImporter}`)
23
24 const setSelectedImporter = useCallback((newImporter: string) => {
25 updateQueryParams({ [IMPORTER_QUERY_PARAM]: newImporter })
26 setTagValue(`imported-${newImporter}`)
27 setSelectedImporterValue(newImporter)
28 }, [])
29
30 const value: MigrationOptionsContext = {
31 autoAddTags,
32 selectedImporter,
33 setSelectedImporter,
34 setAutoAddTags,
35 setTagValue,
36 tagValue
37 }
38
39 return <Context.Provider value={value}>{children}</Context.Provider>
40 }
41
42 export { useMigrationOptions }
43