-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix reader location not persisted if tab closed quickly #599
This change needs to be persisted remotely so that the value can be synced to other devices running Zotero. To achieve this, we use a fetch request with the `keepalive` option, ensuring the request is executed even after the tab is closed.
- Loading branch information
Showing
5 changed files
with
52 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import api from 'zotero-api-client'; | ||
|
||
import { useSelector } from 'react-redux'; | ||
|
||
// Normally components don't need to track version number of the setting (to avoid re-renders when | ||
// the version changes) and updates are dispatched asynchonously (and might be queued, e.g., to | ||
// avoid out-of-order updates). However, to support updating the value on tab/window close, we need | ||
// a synchroneus version with keepalive fetch call. This hook tracks version of the setting and | ||
// provides a method to update the setting synchronously, but using this hook might cause re-renders. | ||
export const useTrackedSettingsKey = (settingsKey, libraryKey) => { | ||
const config = useSelector(state => state.config); | ||
const value = useSelector(state => state.libraries[libraryKey]?.settings?.entries?.[settingsKey]?.value ?? null); | ||
const version = useSelector(state => state.libraries[libraryKey]?.settings?.entries?.[settingsKey]?.version ?? null); | ||
|
||
const update = (newValue) => { | ||
if(newValue !== value) { | ||
api(config.apiKey, config.apiConfig) | ||
.library(libraryKey) | ||
.version(version) | ||
.settings(settingsKey) | ||
.put({ value: newValue }, { keepalive: true }); | ||
} | ||
} | ||
|
||
return { value, update }; | ||
}; |