What I was dealing with was a little different so I just created a wait/retry routine that detected if the chain was available.
Not sure if you noticed this message in browser console:
inpage.js:1 MetaMask: MetaMask will soon stop reloading pages on network change.
For more information, see: https://docs.metamask.io/guide/ethereum-provider.html#ethereum-autorefreshonnetworkchange
Set 'ethereum.autoRefreshOnNetworkChange' to 'false' to silence this warning.
Follow the link provided: https://docs.metamask.io/guide/ethereum-provider.html#table-of-contents This page contains a lot of good information about how to detect wallet availability, network availability, account switches, etc. Especialy check out the Events section of the page. This section has examples for how to detect when the account is changed.