-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: sync useLocalStorage across components and tabs (#977)
* Refactor: external store in useLocalStorage * Comment * Sync separately * Use the storage event for syncing * Rm comment * Restore prev behavior wrt undefined * Simplify the undefined check * Allow undefined and rm initialValue * Don't set undefined * Local storage to return null when not found * Initial value * Rm initial value again * usePendingCreation * Rm redundant code
- Loading branch information
Showing
15 changed files
with
154 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import useLocalStorage from '@/services/local-storage/useLocalStorage' | ||
import type { PendingSafeData } from './types' | ||
|
||
const SAFE_PENDING_CREATION_STORAGE_KEY = 'pendingSafe' | ||
|
||
const usePendingCreation = () => { | ||
return useLocalStorage<PendingSafeData | undefined>(SAFE_PENDING_CREATION_STORAGE_KEY) | ||
} | ||
|
||
export default usePendingCreation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/services/local-storage/__tests__/useLocalStorage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { renderHook, act } from '@/tests/test-utils' | ||
import local from '../local' | ||
import useLocalStorage from '../useLocalStorage' | ||
|
||
describe('useLocalStorage', () => { | ||
beforeEach(() => { | ||
window.localStorage.clear() | ||
}) | ||
|
||
it('should set the value', () => { | ||
const { result } = renderHook(() => useLocalStorage('test-key')) | ||
const [value, setValue] = result.current | ||
|
||
expect(value).toBe(undefined) | ||
|
||
act(() => { | ||
setValue('test') | ||
}) | ||
|
||
expect(result.current[0]).toBe('test') | ||
|
||
act(() => { | ||
setValue('test2') | ||
}) | ||
|
||
expect(result.current[0]).toBe('test2') | ||
}) | ||
|
||
it('should set the value using a callback', () => { | ||
const { result } = renderHook(() => useLocalStorage<string>('test-key')) | ||
const [value, setValue] = result.current | ||
|
||
expect(value).toBe(undefined) | ||
|
||
act(() => { | ||
setValue('test2') | ||
}) | ||
|
||
act(() => { | ||
setValue((prevVal) => { | ||
return prevVal === 'test2' ? 'test3' : 'wrong' | ||
}) | ||
}) | ||
|
||
expect(result.current[0]).toBe('test3') | ||
}) | ||
|
||
it('should read from LS on initial call', () => { | ||
local.setItem('test-key', 'ls') | ||
|
||
const { result } = renderHook(() => useLocalStorage('test-key')) | ||
|
||
expect(result.current[0]).toBe('ls') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters