Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-wallet-storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rainbow-me/rainbowkit': patch
---

Handle unavailable localStorage when reading and writing recent wallet ids.
31 changes: 31 additions & 0 deletions packages/rainbowkit/src/wallets/latestWalletId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
addLatestWalletId,
clearLatestWalletId,
getLatestWalletId,
} from './latestWalletId';

function mockUnavailableLocalStorage() {
vi.spyOn(window, 'localStorage', 'get').mockImplementation(() => {
throw new Error('localStorage unavailable');
});
}

describe('latestWalletId', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('returns an empty wallet id when localStorage is unavailable', () => {
mockUnavailableLocalStorage();

expect(getLatestWalletId()).toBe('');
});

it('ignores latest wallet updates when localStorage is unavailable', () => {
mockUnavailableLocalStorage();

expect(() => addLatestWalletId('rainbow')).not.toThrow();
expect(() => clearLatestWalletId()).not.toThrow();
});
});
18 changes: 9 additions & 9 deletions packages/rainbowkit/src/wallets/latestWalletId.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {
getStorageItem,
removeStorageItem,
setStorageItem,
} from './safeLocalStorage';

const storageKey = 'rk-latest-id';

export function getLatestWalletId(): string {
return typeof window !== 'undefined'
? window.localStorage.getItem(storageKey) || ''
: '';
return getStorageItem(storageKey) || '';
}

export function addLatestWalletId(walletId: string): void {
if (typeof window !== 'undefined') {
window.localStorage.setItem(storageKey, walletId);
}
setStorageItem(storageKey, walletId);
}

export function clearLatestWalletId(): void {
if (typeof window !== 'undefined') {
window.localStorage.removeItem(storageKey);
}
removeStorageItem(storageKey);
}
26 changes: 26 additions & 0 deletions packages/rainbowkit/src/wallets/recentWalletIds.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { addRecentWalletId, getRecentWalletIds } from './recentWalletIds';

function mockUnavailableLocalStorage() {
vi.spyOn(window, 'localStorage', 'get').mockImplementation(() => {
throw new Error('localStorage unavailable');
});
}

describe('recentWalletIds', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('returns no recent wallet ids when localStorage is unavailable', () => {
mockUnavailableLocalStorage();

expect(getRecentWalletIds()).toEqual([]);
});

it('ignores recent wallet updates when localStorage is unavailable', () => {
mockUnavailableLocalStorage();

expect(() => addRecentWalletId('rainbow')).not.toThrow();
});
});
10 changes: 4 additions & 6 deletions packages/rainbowkit/src/wallets/recentWalletIds.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getStorageItem, setStorageItem } from './safeLocalStorage';

const storageKey = 'rk-recent';

function safeParseJsonArray<T>(string: string | null): T[] {
Expand All @@ -10,9 +12,7 @@ function safeParseJsonArray<T>(string: string | null): T[] {
}

export function getRecentWalletIds(): string[] {
return typeof window !== 'undefined'
? safeParseJsonArray(window.localStorage.getItem(storageKey))
: [];
return safeParseJsonArray(getStorageItem(storageKey));
}

function dedupe<T>(array: T[]): T[] {
Expand All @@ -22,7 +22,5 @@ function dedupe<T>(array: T[]): T[] {
export function addRecentWalletId(walletId: string): void {
const newValue = dedupe([walletId, ...getRecentWalletIds()]);

if (typeof window !== 'undefined') {
window.localStorage.setItem(storageKey, JSON.stringify(newValue));
}
setStorageItem(storageKey, JSON.stringify(newValue));
Comment thread
daixihegu marked this conversation as resolved.
}
29 changes: 29 additions & 0 deletions packages/rainbowkit/src/wallets/safeLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function getSafeLocalStorage(): Storage | undefined {
if (typeof window === 'undefined') return;

try {
return window.localStorage;
} catch {
return;
}
}

export function getStorageItem(key: string): string | null {
try {
return getSafeLocalStorage()?.getItem(key) ?? null;
} catch {
return null;
}
}

export function setStorageItem(key: string, value: string): void {
try {
getSafeLocalStorage()?.setItem(key, value);
} catch {}
}

export function removeStorageItem(key: string): void {
try {
getSafeLocalStorage()?.removeItem(key);
} catch {}
}