-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update nmr-load-save to version 0.32.0 refactor: read and write settings in local storage
- Loading branch information
1 parent
d8bfc22
commit cabfa87
Showing
24 changed files
with
194 additions
and
156 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { v4 } from '@lukeed/uuid'; | ||
import lodashMerge from 'lodash/merge'; | ||
import { Workspace } from 'nmr-load-save'; | ||
|
||
import { usePreferences } from '../context/PreferencesContext'; | ||
import { | ||
Settings, | ||
readSettings, | ||
updateSettings, | ||
} from '../reducer/preferences/preferencesReducer'; | ||
import { workspaceDefaultProperties } from '../workspaces/workspaceDefaultProperties'; | ||
|
||
export function useWorkspaceAction() { | ||
const { dispatch, current, workspace } = usePreferences(); | ||
|
||
function setActiveWorkspace(workspace: string) { | ||
const settings = readSettings() || { | ||
currentWorkspace: null, | ||
version: 0, | ||
workspaces: {}, | ||
}; | ||
settings.currentWorkspace = workspace; | ||
updateSettings(settings); | ||
|
||
dispatch({ | ||
type: 'SET_ACTIVE_WORKSPACE', | ||
payload: { | ||
workspace, | ||
}, | ||
}); | ||
} | ||
|
||
function addNewWorkspace( | ||
workspaceName: string, | ||
data?: Omit<Workspace, 'version' | 'label'>, | ||
) { | ||
const workSpaceData = data ?? current; | ||
const newWorkSpace = lodashMerge( | ||
{}, | ||
workspaceDefaultProperties, | ||
workSpaceData, | ||
{ | ||
version: 1, | ||
label: workspaceName, | ||
source: 'user', | ||
}, | ||
); | ||
const workspaceKey = v4(); | ||
const localData = readSettings() || { workspaces: {} }; | ||
const settings = { | ||
...localData, | ||
currentWorkspace: workspaceKey, | ||
workspaces: { ...localData?.workspaces, [workspaceKey]: newWorkSpace }, | ||
}; | ||
updateSettings(settings as Settings); | ||
|
||
dispatch({ | ||
type: 'ADD_WORKSPACE', | ||
payload: { | ||
workspaceKey, | ||
data: newWorkSpace, | ||
}, | ||
}); | ||
} | ||
|
||
function removeWorkspace(key: string) { | ||
const settings = readSettings(); | ||
if (settings) { | ||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete settings.workspaces[key]; | ||
if (key === settings.currentWorkspace) { | ||
settings.currentWorkspace = 'default'; | ||
} | ||
|
||
updateSettings(settings); | ||
} | ||
dispatch({ | ||
type: 'REMOVE_WORKSPACE', | ||
payload: { | ||
workspace: key, | ||
}, | ||
}); | ||
} | ||
|
||
function saveWorkspace(data?: Partial<Workspace>) { | ||
if (data) { | ||
const settings = readSettings() || { | ||
version: 0, | ||
currentWorkspace: null, | ||
workspaces: {}, | ||
}; | ||
|
||
updateSettings({ | ||
...settings, | ||
workspaces: { | ||
...settings.workspaces, | ||
[workspace.current]: data, | ||
}, | ||
} as Settings); | ||
} | ||
dispatch({ | ||
type: 'SET_PREFERENCES', | ||
...(data && { payload: data }), | ||
}); | ||
} | ||
|
||
return { | ||
addNewWorkspace, | ||
removeWorkspace, | ||
saveWorkspace, | ||
setActiveWorkspace, | ||
}; | ||
} |
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
Oops, something went wrong.