Fix channel reply switching channels.

This commit is contained in:
JT Turner 2020-04-30 22:07:20 -07:00 committed by Sean Yesmunt
parent f873b99875
commit b0ed767c27
2 changed files with 27 additions and 1 deletions

View file

@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add fallback when images fail to load _community pr!_ ([#4019](https://github.com/lbryio/lbry-desktop/pull/4019))
- Fetch new content when clicking LBRY logo while on homepage _community pr!_ ([#4031](https://github.com/lbryio/lbry-desktop/pull/4031))
- Aligns text across browsers and desktop _community pr!_ ([#4050](https://github.com/lbryio/lbry-desktop/pull/4050))
- Reselect channel as "replying as" when switching channels ([#3926](https://github.com/lbryio/lbry-desktop/issues/3926))
## [0.45.0] - [2020-04-21]

View file

@ -1,9 +1,20 @@
import { useState, useEffect } from 'react';
const listeners = {};
function getSetAllValues(key, setValue) {
if (!key) {
// If no key just return the normal setValue function
return setValue;
}
return value => listeners[key].forEach(fn => fn(value));
}
export default function usePersistedState(key, firstTimeDefault) {
// If no key is passed in, act as a normal `useState`
let defaultValue;
let localStorageAvailable;
try {
localStorageAvailable = Boolean(window.localStorage);
} catch (e) {
@ -32,11 +43,25 @@ export default function usePersistedState(key, firstTimeDefault) {
const [value, setValue] = useState(defaultValue);
if (key && !Array.isArray(listeners[key])) {
listeners[key] = [];
}
useEffect(() => {
if (key && localStorageAvailable) {
localStorage.setItem(key, typeof value === 'object' ? JSON.stringify(value) : value);
}
if (key) {
// add hook on mount
listeners[key].push(setValue);
}
return () => {
if (key) {
// remove hook on unmount
listeners[key] = listeners[key].filter(listener => listener !== setValue);
}
};
}, [key, value, localStorageAvailable]);
return [value, setValue];
return [value, getSetAllValues(key, setValue)];
}