Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use localstorage instead of url params for yarn checkbox #22

Closed
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
25 changes: 4 additions & 21 deletions src/SettingsProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
import React, { useCallback, useContext } from 'react'
import useLocalStorage from 'react-use/lib/useLocalStorage'
import useSearchParam from 'react-use/lib/useSearchParam'

import { SHOW_LATEST_RCS, USE_YARN_PLUGIN } from './utils'
import { updateURL } from './utils/update-url'

const INITIAL_STATE = {
[`${SHOW_LATEST_RCS}`]: false
[`${SHOW_LATEST_RCS}`]: false,
[`${USE_YARN_PLUGIN}`]: false
}

export const SettingsContext = React.createContext(INITIAL_STATE)

export const SettingsProvider = React.memo(function({ children }) {
const useYarnPlugin = useSearchParam('yarnPlugin')

const [settings, setLocalStorageSettings] = useLocalStorage(
'backstage:upgrade-helper:settings',
INITIAL_STATE
)

const setUseYarnPlugin = useYarnPlugin => {
updateURL({ yarnPlugin: useYarnPlugin })
}

const setSettings = useCallback(settings => {
const {
[USE_YARN_PLUGIN]: newUseYarnPlugin,
...localStorageSettings
} = settings

if (newUseYarnPlugin !== useYarnPlugin) {
setUseYarnPlugin(newUseYarnPlugin)
}

setLocalStorageSettings(localStorageSettings)
setLocalStorageSettings(settings)
})

return (
<SettingsContext.Provider
value={{
settings: {
...settings,
[USE_YARN_PLUGIN]: !!JSON.parse(useYarnPlugin)
...settings
},
setSettings
}}
Expand Down