-
Notifications
You must be signed in to change notification settings - Fork 120
frontend: useAccountSync hook #4121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thisconnect
merged 4 commits into
BitBoxSwiss:master
from
thisconnect:frontend-sync-hook
May 13, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9449856
frontend: add useaccountsynced hook
thisconnect 38b8e48
frontend: show syncing accounts message in btcdirect
thisconnect 3c7fa11
frontend: ensure account is synced before loading pocket widget
thisconnect cb0e309
frontend: ensure account is synced before loading bitrefill widget
thisconnect File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,149 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { act, renderHook, waitFor } from '@testing-library/react'; | ||
| import { useState } from 'react'; | ||
| import { useAccountSynced } from './account'; | ||
| import { syncdone } from '@/api/accountsync'; | ||
| import * as utils from './mount'; | ||
|
thisconnect marked this conversation as resolved.
|
||
|
|
||
| vi.mock('@/api/accountsync', () => ({ | ||
| syncdone: vi.fn(() => () => {}), | ||
| })); | ||
|
|
||
| const mockSyncdone = vi.mocked(syncdone); | ||
|
|
||
| const useMountedRefSpy = vi.spyOn(utils, 'useMountedRef'); | ||
|
|
||
| describe('useAccountSynced', () => { | ||
| beforeEach(() => { | ||
| useMountedRefSpy.mockReturnValue({ current: true }); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('returns undefined while loading', () => { | ||
| const mockApiCall = vi.fn().mockReturnValue(new Promise(() => {})); | ||
|
|
||
| const { result } = renderHook(() => ( | ||
| useAccountSynced('account-code-1' as any, mockApiCall) | ||
| )); | ||
|
|
||
| expect(result.current).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns api result on success', async () => { | ||
| const mockApiCall = vi.fn().mockResolvedValue('result'); | ||
|
|
||
| const { result } = renderHook(() => ( | ||
| useAccountSynced('account-code-1' as any, mockApiCall) | ||
| )); | ||
|
|
||
| await waitFor(() => expect(result.current).toBe('result')); | ||
|
|
||
| expect(mockApiCall).toHaveBeenCalledTimes(1); | ||
| expect(mockSyncdone).toHaveBeenCalledWith( | ||
| 'account-code-1', | ||
| expect.any(Function), | ||
| ); | ||
| }); | ||
|
|
||
| it('clears previous result when code changes', async () => { | ||
| let resolveApiCall: ((value: string) => void) | undefined; | ||
|
|
||
| const mockApiCall = vi.fn().mockImplementation(() => ( | ||
| new Promise<string>((resolve) => { | ||
| resolveApiCall = resolve; | ||
| }) | ||
| )); | ||
|
|
||
| const { result } = renderHook(() => { | ||
| const [code, setCode] = useState('account-code-1'); | ||
|
|
||
| const value = useAccountSynced( | ||
| code as any, | ||
| mockApiCall, | ||
| ); | ||
|
|
||
| return { | ||
| value, | ||
| setCode, | ||
| }; | ||
| }); | ||
|
|
||
| act(() => { | ||
| resolveApiCall?.('result-1'); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.value).toBe('result-1'); | ||
| }); | ||
|
|
||
| act(() => { | ||
| result.current.setCode('account-code-2'); | ||
| }); | ||
|
|
||
| expect(result.current.value).toBeUndefined(); | ||
|
|
||
| act(() => { | ||
| resolveApiCall?.('result-2'); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.value).toBe('result-2'); | ||
| }); | ||
|
|
||
| expect(mockApiCall).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('ignores stale api responses', async () => { | ||
| let resolveFirst: ((value: string) => void) | undefined; | ||
| let resolveSecond: ((value: string) => void) | undefined; | ||
|
|
||
| const mockApiCall = vi | ||
| .fn() | ||
| .mockImplementationOnce(() => ( | ||
| new Promise<string>((resolve) => { | ||
| resolveFirst = resolve; | ||
| }) | ||
| )) | ||
| .mockImplementationOnce(() => ( | ||
| new Promise<string>((resolve) => { | ||
| resolveSecond = resolve; | ||
| }) | ||
| )); | ||
|
|
||
| const { result } = renderHook(() => { | ||
| const [code, setCode] = useState('account-code-1'); | ||
|
|
||
| const value = useAccountSynced( | ||
| code as any, | ||
| mockApiCall, | ||
| ); | ||
|
|
||
| return { | ||
| value, | ||
| setCode, | ||
| }; | ||
| }); | ||
|
|
||
| act(() => { | ||
| result.current.setCode('account-code-2'); | ||
| }); | ||
|
|
||
| act(() => { | ||
| resolveFirst?.('stale-result'); | ||
| }); | ||
|
|
||
| expect(result.current.value).toBeUndefined(); | ||
|
|
||
| act(() => { | ||
| resolveSecond?.('fresh-result'); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.value).toBe('fresh-result'); | ||
| }); | ||
|
|
||
| expect(result.current.value).not.toBe('stale-result'); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,38 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { useCallback, useEffect, useRef, useState } from 'react'; | ||
| import type { AccountCode } from '@/api/account'; | ||
| import { syncdone } from '@/api/accountsync'; | ||
| import { useMountedRef } from './mount'; | ||
|
thisconnect marked this conversation as resolved.
|
||
|
|
||
| export const useAccountSynced = <T, >( | ||
| code: AccountCode, | ||
| apiCall: () => Promise<T>, | ||
| ): T | undefined => { | ||
| const isMounted = useMountedRef(); | ||
| const apiRequestId = useRef(0); | ||
| const [result, setResult] = useState<T | undefined>(undefined); | ||
|
|
||
| const callApi = useCallback(async () => { | ||
| const requestId = ++apiRequestId.current; | ||
| try { | ||
| const response = await apiCall(); | ||
| if ( | ||
| isMounted.current | ||
| && requestId === apiRequestId.current | ||
| ) { | ||
| setResult(response); | ||
| } | ||
|
thisconnect marked this conversation as resolved.
|
||
| } catch (error) { | ||
| console.error(error); | ||
| } | ||
| }, [apiCall, isMounted]); | ||
|
thisconnect marked this conversation as resolved.
|
||
|
|
||
| useEffect(() => { | ||
| setResult(undefined); | ||
| callApi(); | ||
| return syncdone(code, callApi); | ||
| }, [code, callApi]); | ||
|
|
||
| return result; | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once the backend gets cleaned up it would be nice to explicitly type all the known errors here. cc @bznein