Skip to content

Commit 24181d9

Browse files
Sacquervinzscam
andauthored
feat check localstorage if no search param (#24)
* fix: broken tests * feat: use localstorage to populate, if there's no yarn plugin search param * chore: removed unnecessary export keyword * update with suggested changes Co-authored-by: Vincenzo Scamporlino <vincenzos@spotify.com> * update with suggested changes Co-authored-by: Vincenzo Scamporlino <vincenzos@spotify.com> * update with suggested changes Co-authored-by: Vincenzo Scamporlino <vincenzos@spotify.com> * update with suggested changes Co-authored-by: Vincenzo Scamporlino <vincenzos@spotify.com> * update with suggested changes Co-authored-by: Vincenzo Scamporlino <vincenzos@spotify.com> * update with suggested changes Co-authored-by: Vincenzo Scamporlino <vincenzos@spotify.com> * fix: tests should adhere more to upstream --------- Co-authored-by: Vincenzo Scamporlino <vincenzos@spotify.com>
1 parent 34d77aa commit 24181d9

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/SettingsProvider.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import React, { ReactNode, useContext } from 'react'
1+
import React, { ReactNode, useContext, useEffect } from 'react'
22
import useLocalStorage from 'react-use/lib/useLocalStorage'
33
import useSearchParam from 'react-use/lib/useSearchParam'
44

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

8-
const INITIAL_STATE = {
8+
interface SETTINGS {
9+
[SHOW_LATEST_RCS]: boolean
10+
[USE_YARN_PLUGIN]: boolean
11+
}
12+
13+
type State = {
14+
settings: SETTINGS
15+
setSettings(settings: SETTINGS): void
16+
}
17+
const INITIAL_STATE: State = {
918
settings: {
1019
[`${SHOW_LATEST_RCS}`]: false,
1120
[`${USE_YARN_PLUGIN}`]: false,
1221
},
13-
setSettings: (setting: any) => {},
22+
setSettings: () => {},
1423
}
1524

1625
export const SettingsContext = React.createContext(INITIAL_STATE)
@@ -21,15 +30,22 @@ export const SettingsProvider = React.memo(function ({
2130
children: ReactNode
2231
}) {
2332
const useYarnPluginParam = useSearchParam('yarnPlugin')
33+
const shouldPopulateYarnPluginParam = useYarnPluginParam === null
2434
const useYarnPlugin =
25-
useYarnPluginParam !== null ? !!Number(useYarnPluginParam) : false
35+
!shouldPopulateYarnPluginParam ? !!Number(useYarnPluginParam) : false
2636

2737
const [settings, setLocalStorageSettings] = useLocalStorage(
2838
'backstage:upgrade-helper:settings',
2939
INITIAL_STATE.settings
3040
)
3141

32-
const setSettings = (settings: typeof INITIAL_STATE.settings) => {
42+
useEffect(() => {
43+
if (shouldPopulateYarnPluginParam) {
44+
updateURL({ yarnPlugin: settings?.[USE_YARN_PLUGIN] ?? false })
45+
}
46+
}, [shouldPopulateYarnPluginParam, settings?.[USE_YARN_PLUGIN]])
47+
48+
const setSettings = (settings: SETTINGS) => {
3349
const { [USE_YARN_PLUGIN]: newUseYarnPlugin, ...localStorageSettings } =
3450
settings
3551

src/__tests__/utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getVersionsContentInDiff, getChangelogURL } from '../utils'
44

55
const fixtureVersions = ['0.59', '0.58', '0.57', '0.56'].map((version) => ({
66
version,
7-
createApp: '',
7+
createApp: undefined!,
88
}))
99

1010
describe('getVersionsContentInDiff', () => {

0 commit comments

Comments
 (0)