feat: persist device label and fallback to it when device id is not found #1736
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.
Overview
Device ids reported by browser are not guaranteed to state the same in between browser sessions or device reconnects. Because of that persisting device preferences based on device id is not always reliable.
In this PR we add a fallback mode to
usePeristedDevicePreferences
hook: if persisted device id is not found, it will try to find device by its label.We also now save up to three latest device preferences. That allows us to prefer devices that were recently used, even if the most recently used device is no longer available.
Implementation notes
usePeristedDevicePreferences
hook is updated to fall back to device labels if device id is not found.Using labels requires browser permission, so applying and persisting settings is now postponed until permission is granted (or, more precisely, until devices list observable is populated).
Applying and persisting is now merged into a single hook to simplify timing: we shouldn't persist settings until they are applied; we also should apply settings only once.
Every device preference can be an array of up to three preferences. We go through this array looking for a device that is currently available.
To preserve user data, we still support non-array preferences. They are migrated to an array format on write.