| 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 |
|