Skip to content

Commit

Permalink
Fix order lost on page reload
Browse files Browse the repository at this point in the history
On page reload, the providers were synced in the order they were loaded.

This means that the default payment provider setting was lost.

Fixed this by syncing order to local storage and on page reload, only syncing providers when they were initialized (else the order would have been lost again).
  • Loading branch information
ekzyis committed Feb 9, 2024
1 parent fdbc792 commit 9077fb3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
29 changes: 26 additions & 3 deletions components/webln/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,30 @@ const syncProvider = (array, provider) => {
]
}

const storageKey = 'webln:providers'

function RawWebLNProvider ({ children }) {
const lnbits = useLNbits()
const nwc = useNWC()
const [enabledProviders, setEnabledProviders] = useState([lnbits, nwc].filter(({ enabled }) => enabled))
const availableProviders = [lnbits, nwc]
const [enabledProviders, setEnabledProviders] = useState([])

// restore order on page reload
useEffect(() => {
const storedOrder = window.localStorage.getItem(storageKey)
if (!storedOrder) return
const providerNames = JSON.parse(storedOrder)
setEnabledProviders(providers => {
return providerNames.map(name => {
for (const p of availableProviders) {
if (p.name === name) return p
}
console.warn(`Stored provider with name ${name} not available`)
return null
})
})
}, [])

// keep list in sync with underlying providers
useEffect(() => {
setEnabledProviders(providers => {
Expand All @@ -33,14 +53,17 @@ function RawWebLNProvider ({ children }) {
// This can be the case if we're syncing from a page reload
// where the providers are initially not enabled.
// If provider is no longer enabled, it is removed from the list.
const newProviders = [lnbits, nwc].reduce(syncProvider, providers)
const isInitialized = p => p.initialized
const newProviders = availableProviders.filter(isInitialized).reduce(syncProvider, providers)
const newOrder = newProviders.map(({ name }) => name)
window.localStorage.setItem(storageKey, JSON.stringify(newOrder))
return newProviders
})
}, [lnbits, nwc])

// sanity check
for (const p of enabledProviders) {
if (!p.enabled) {
if (!p.enabled && p.initialized) {
console.warn('Expected provider to be enabled but is not:', p.name)
}
}
Expand Down
5 changes: 4 additions & 1 deletion components/webln/lnbits.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function LNbitsProvider ({ children }) {
const [url, setUrl] = useState('')
const [adminKey, setAdminKey] = useState('')
const [enabled, setEnabled] = useState()
const [initialized, setInitialized] = useState(false)

const name = 'LNbits'
const storageKey = 'webln:provider:lnbits'
Expand Down Expand Up @@ -115,6 +116,8 @@ export function LNbitsProvider ({ children }) {
console.error('invalid LNbits config:', err)
setEnabled(false)
throw err
} finally {
setInitialized(true)
}
}, [])

Expand Down Expand Up @@ -150,7 +153,7 @@ export function LNbitsProvider ({ children }) {
loadConfig().catch(console.error)
}, [])

const value = { name, url, adminKey, saveConfig, clearConfig, enabled, getInfo, sendPayment }
const value = { name, url, adminKey, initialized, enabled, saveConfig, clearConfig, getInfo, sendPayment }
return (
<LNbitsContext.Provider value={value}>
{children}
Expand Down
5 changes: 4 additions & 1 deletion components/webln/nwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function NWCProvider ({ children }) {
const [relayUrl, setRelayUrl] = useState()
const [secret, setSecret] = useState()
const [enabled, setEnabled] = useState()
const [initialized, setInitialized] = useState(false)
const [relay, setRelay] = useState()

const name = 'NWC'
Expand Down Expand Up @@ -40,6 +41,8 @@ export function NWCProvider ({ children }) {
console.error('invalid NWC config:', err)
setEnabled(false)
throw err
} finally {
setInitialized(true)
}
}, [])

Expand Down Expand Up @@ -171,7 +174,7 @@ export function NWCProvider ({ children }) {
loadConfig().catch(console.error)
}, [])

const value = { name, nwcUrl, relayUrl, walletPubkey, secret, saveConfig, clearConfig, enabled, getInfo, sendPayment }
const value = { name, nwcUrl, relayUrl, walletPubkey, secret, initialized, enabled, saveConfig, clearConfig, getInfo, sendPayment }
return (
<NWCContext.Provider value={value}>
{children}
Expand Down

0 comments on commit 9077fb3

Please sign in to comment.