From b0ed767c2742d51ab53e42cae7e09473b6895f3f Mon Sep 17 00:00:00 2001 From: JT Turner Date: Thu, 30 Apr 2020 22:07:20 -0700 Subject: [PATCH] Fix channel reply switching channels. --- CHANGELOG.md | 1 + ui/effects/use-persisted-state.js | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b327d770..d902a28f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/ui/effects/use-persisted-state.js b/ui/effects/use-persisted-state.js index 378e843bc..c45838688 100644 --- a/ui/effects/use-persisted-state.js +++ b/ui/effects/use-persisted-state.js @@ -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)]; }