-
Notifications
You must be signed in to change notification settings - Fork 140
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
fix: grid settings #470
Merged
Merged
fix: grid settings #470
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { useCallback, useEffect } from 'react'; | ||
import useLocalStorage from '../../../../hooks/useLocalStorage'; | ||
import mixpanel from 'mixpanel-browser'; | ||
import { atom, useRecoilState, AtomEffect } from 'recoil'; | ||
import { debugGridSettings } from '../../../../debugFlags'; | ||
|
||
const SETTINGS_KEY = 'viewSettings'; | ||
export interface GridSettings { | ||
showGridAxes: boolean; | ||
showHeadings: boolean; | ||
|
@@ -20,6 +21,40 @@ export const defaultGridSettings: GridSettings = { | |
presentationMode: false, | ||
}; | ||
|
||
// Persist the GrdiSettings | ||
const localStorageEffect: AtomEffect<GridSettings> = ({ setSelf, onSet }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GridSettings* |
||
// Initialize from localStorage | ||
// Note: presentationMode is always off on a fresh page reload | ||
const savedValue = localStorage.getItem(SETTINGS_KEY); | ||
if (savedValue != null) { | ||
const settings = JSON.parse(savedValue); | ||
const newSettings = { ...settings, presentationMode: false }; | ||
localStorage.setItem(SETTINGS_KEY, JSON.stringify(newSettings)); | ||
if (debugGridSettings) console.log('[gridSettings] initializing with values from localStorage', newSettings); | ||
setSelf(newSettings); | ||
window.dispatchEvent(new Event('grid-settings')); | ||
} | ||
|
||
onSet((newValue, _, isReset) => { | ||
if (debugGridSettings) console.log('[gridSettings] setting new value', newValue); | ||
isReset ? localStorage.removeItem(SETTINGS_KEY) : localStorage.setItem(SETTINGS_KEY, JSON.stringify(newValue)); | ||
}); | ||
}; | ||
|
||
// Emit an event so pixi app can respond and pull latest values from localStorage | ||
const emitGridSettingsChange: AtomEffect<GridSettings> = ({ onSet }) => { | ||
onSet((newValue) => { | ||
if (debugGridSettings) console.log('[gridSettings] emitting `grid-settings` event'); | ||
window.dispatchEvent(new Event('grid-settings')); | ||
}); | ||
}; | ||
|
||
const gridSettingsAtom = atom({ | ||
key: 'gridSettings', | ||
default: defaultGridSettings, | ||
effects: [localStorageEffect, emitGridSettingsChange], | ||
}); | ||
|
||
interface GridSettingsReturn { | ||
showGridAxes: boolean; | ||
showHeadings: boolean; | ||
|
@@ -36,91 +71,62 @@ interface GridSettingsReturn { | |
} | ||
|
||
export const useGridSettings = (): GridSettingsReturn => { | ||
const [settings, setSettings] = useLocalStorage('viewSettings', defaultGridSettings); | ||
const [settings, setSettings] = useRecoilState(gridSettingsAtom); | ||
|
||
useEffect(() => { | ||
if (settings) { | ||
window.dispatchEvent(new Event('grid-settings')); | ||
} | ||
}, [settings]); | ||
|
||
const setShowGridAxes = useCallback( | ||
(value: boolean) => { | ||
if (value !== settings.showGridAxes) { | ||
const setShowGridAxes = (value: boolean) => | ||
setSettings((currentState) => { | ||
if (value !== currentState.showGridAxes) { | ||
mixpanel.track('[Grid].[Settings].setShowGridAxes', { value }); | ||
setSettings({ | ||
...settings, | ||
showGridAxes: value, | ||
}); | ||
return { ...currentState, showGridAxes: value }; | ||
} | ||
}, | ||
[settings, setSettings] | ||
); | ||
return currentState; | ||
}); | ||
|
||
const setShowHeadings = useCallback( | ||
(value: boolean) => { | ||
if (value !== settings.showHeadings) { | ||
const setShowHeadings = (value: boolean) => | ||
setSettings((currentState) => { | ||
if (value !== currentState.showHeadings) { | ||
mixpanel.track('[Grid].[Settings].setShowHeadings', { value }); | ||
setSettings({ | ||
...settings, | ||
showHeadings: value, | ||
}); | ||
return { ...currentState, showHeadings: value }; | ||
} | ||
}, | ||
[settings, setSettings] | ||
); | ||
return currentState; | ||
}); | ||
|
||
const setShowGridLines = useCallback( | ||
(value: boolean) => { | ||
if (value !== settings.showGridLines) { | ||
const setShowGridLines = (value: boolean) => | ||
setSettings((currentState) => { | ||
if (value !== currentState.showGridLines) { | ||
mixpanel.track('[Grid].[Settings].setShowGridLines', { value }); | ||
setSettings({ | ||
...settings, | ||
showGridLines: value, | ||
}); | ||
return { ...currentState, showGridLines: value }; | ||
} | ||
}, | ||
[settings, setSettings] | ||
); | ||
return currentState; | ||
}); | ||
|
||
const setShowCellTypeOutlines = useCallback( | ||
(value: boolean) => { | ||
if (value !== settings.showCellTypeOutlines) { | ||
const setShowCellTypeOutlines = (value: boolean) => | ||
setSettings((currentState) => { | ||
if (value !== currentState.showCellTypeOutlines) { | ||
mixpanel.track('[Grid].[Settings].setShowCellTypeOutlines', { value }); | ||
setSettings({ | ||
...settings, | ||
showCellTypeOutlines: value, | ||
}); | ||
return { ...settings, showCellTypeOutlines: value }; | ||
} | ||
}, | ||
[settings, setSettings] | ||
); | ||
return currentState; | ||
}); | ||
|
||
const setShowA1Notation = useCallback( | ||
(value: boolean) => { | ||
if (value !== settings.showA1Notation) { | ||
const setShowA1Notation = (value: boolean) => | ||
setSettings((currentState) => { | ||
if (value !== currentState.showA1Notation) { | ||
mixpanel.track('[Grid].[Settings].setShowA1Notation', { value }); | ||
setSettings({ | ||
...settings, | ||
showA1Notation: value, | ||
}); | ||
return { ...currentState, showA1Notation: value }; | ||
} | ||
}, | ||
[settings, setSettings] | ||
); | ||
return currentState; | ||
}); | ||
|
||
const setPresentationMode = useCallback( | ||
(value: boolean) => { | ||
if (value !== settings.presentationMode) { | ||
const setPresentationMode = (value: boolean) => { | ||
setSettings((currentState) => { | ||
if (value !== currentState.presentationMode) { | ||
mixpanel.track('[Grid].[Settings].setPresentationMode', { value }); | ||
setSettings({ | ||
...settings, | ||
presentationMode: value, | ||
}); | ||
return { ...currentState, presentationMode: value }; | ||
} | ||
}, | ||
[settings, setSettings] | ||
); | ||
return currentState; | ||
}); | ||
}; | ||
|
||
return { | ||
...settings, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this is state in recoil now, we can colocate this logic with how/when the state is set in our recoil file. No need for a side effect in react.