|
| 1 | +/* eslint-disable no-restricted-globals -- localStorage is intentionally used for browser storage */ |
| 2 | +/** |
| 3 | + * Utility for managing active provider state in localStorage. |
| 4 | + * This tracks which connection type(s) are currently active to ensure |
| 5 | + * proper state restoration after page refresh. |
| 6 | + */ |
| 7 | + |
| 8 | +const STORAGE_KEY = 'browser-playground.active-provider'; |
| 9 | + |
| 10 | +export type ProviderType = 'multichain' | 'legacy-evm' | 'wagmi'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Gets the currently active providers from localStorage. |
| 14 | + * |
| 15 | + * @returns Array of active provider types, or empty array if none |
| 16 | + */ |
| 17 | +export function getActiveProviders(): ProviderType[] { |
| 18 | + try { |
| 19 | + const stored = localStorage.getItem(STORAGE_KEY); |
| 20 | + if (!stored) { |
| 21 | + return []; |
| 22 | + } |
| 23 | + const parsed = JSON.parse(stored); |
| 24 | + if (Array.isArray(parsed)) { |
| 25 | + return parsed as ProviderType[]; |
| 26 | + } |
| 27 | + return []; |
| 28 | + } catch (error) { |
| 29 | + console.error( |
| 30 | + '[activeProviderStorage] Failed to get active providers:', |
| 31 | + error, |
| 32 | + ); |
| 33 | + return []; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Checks if a specific provider is marked as active. |
| 39 | + * |
| 40 | + * @param provider - The provider type to check |
| 41 | + * @returns true if the provider is active |
| 42 | + */ |
| 43 | +export function isProviderActive(provider: ProviderType): boolean { |
| 44 | + return getActiveProviders().includes(provider); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Sets a provider as active. Handles mutual exclusivity between |
| 49 | + * legacy-evm and wagmi (since they share the same underlying provider). |
| 50 | + * |
| 51 | + * @param provider - The provider type to set as active |
| 52 | + */ |
| 53 | +export function setProviderActive(provider: ProviderType): void { |
| 54 | + try { |
| 55 | + const current = getActiveProviders(); |
| 56 | + |
| 57 | + // Handle mutual exclusivity between legacy-evm and wagmi |
| 58 | + // They share the same underlying EVM provider, so only one can be active |
| 59 | + let updated: ProviderType[]; |
| 60 | + if (provider === 'legacy-evm') { |
| 61 | + updated = current.filter( |
| 62 | + (providerType) => |
| 63 | + providerType !== 'wagmi' && providerType !== 'legacy-evm', |
| 64 | + ); |
| 65 | + updated.push('legacy-evm'); |
| 66 | + } else if (provider === 'wagmi') { |
| 67 | + updated = current.filter( |
| 68 | + (providerType) => |
| 69 | + providerType !== 'legacy-evm' && providerType !== 'wagmi', |
| 70 | + ); |
| 71 | + updated.push('wagmi'); |
| 72 | + } else if (current.includes(provider)) { |
| 73 | + // multichain already in list, no change needed |
| 74 | + updated = current; |
| 75 | + } else { |
| 76 | + // multichain can coexist with either |
| 77 | + updated = [...current, provider]; |
| 78 | + } |
| 79 | + |
| 80 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(updated)); |
| 81 | + } catch (error) { |
| 82 | + console.error( |
| 83 | + `[activeProviderStorage] Failed to set provider "${provider}" as active:`, |
| 84 | + error, |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Removes a specific provider from the active list. |
| 91 | + * |
| 92 | + * @param provider - The provider type to remove |
| 93 | + */ |
| 94 | +export function removeProviderActive(provider: ProviderType): void { |
| 95 | + try { |
| 96 | + const current = getActiveProviders(); |
| 97 | + const updated = current.filter((providerType) => providerType !== provider); |
| 98 | + if (updated.length === 0) { |
| 99 | + localStorage.removeItem(STORAGE_KEY); |
| 100 | + } else { |
| 101 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(updated)); |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + console.error( |
| 105 | + `[activeProviderStorage] Failed to remove provider "${provider}" from active list:`, |
| 106 | + error, |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Clears all active provider state from localStorage. |
| 113 | + * Called when disconnecting all connections. |
| 114 | + */ |
| 115 | +export function clearAllActiveProviders(): void { |
| 116 | + try { |
| 117 | + localStorage.removeItem(STORAGE_KEY); |
| 118 | + } catch (error) { |
| 119 | + console.error( |
| 120 | + '[activeProviderStorage] Failed to clear all active providers:', |
| 121 | + error, |
| 122 | + ); |
| 123 | + } |
| 124 | +} |
0 commit comments