forked from comit-network/xmr-btc-swap
-
Notifications
You must be signed in to change notification settings - Fork 78
fix(gui): offer flickering #1065
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
Merged
Changes from all commits
Commits
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
67 changes: 67 additions & 0 deletions
67
...renderer/components/pages/swap/swap/init/deposit_and_choose_offer/useCachedMakerOffers.ts
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,67 @@ | ||
| import { useEffect, useMemo, useRef, useState } from "react"; | ||
| import { QuoteWithAddress } from "models/tauriModel"; | ||
| import { useAppSelector, usePendingSelectMakerApproval } from "store/hooks"; | ||
| import _ from "lodash"; | ||
| import { | ||
| OfferSortMode, | ||
| SortedMakerEntry, | ||
| sortApprovalsAndKnownQuotes, | ||
| } from "utils/sortUtils"; | ||
|
|
||
| const REFRESH_INTERVAL_MS = 5_000; | ||
|
|
||
| // The sorted list re-shuffles whenever the backend streams an approval or | ||
| // quote update. We snapshot it and only refresh on a fixed cadence so cards | ||
| // don't visibly flicker. The snapshot is also refreshed immediately on | ||
| // sort-mode change, on Bitcoin balance change, and whenever a new peer | ||
| // appears, so newly-discovered makers and freshly-deposited funds don't get | ||
| // stuck behind the cadence. | ||
| export function useCachedMakerOffers( | ||
| known_quotes: QuoteWithAddress[], | ||
| sortMode: OfferSortMode, | ||
| offersPerPage: number, | ||
| ): SortedMakerEntry[] { | ||
| const pendingApprovals = usePendingSelectMakerApproval(); | ||
| const bitcoinBalance = useAppSelector((state) => state.bitcoinWallet.balance); | ||
|
|
||
| const liveOffers = useMemo( | ||
| () => | ||
| sortApprovalsAndKnownQuotes( | ||
| pendingApprovals, | ||
| known_quotes, | ||
| sortMode, | ||
| offersPerPage, | ||
| ), | ||
| [pendingApprovals, known_quotes, sortMode, offersPerPage], | ||
| ); | ||
|
|
||
| const liveOffersRef = useRef<SortedMakerEntry[]>(liveOffers); | ||
| liveOffersRef.current = liveOffers; | ||
|
|
||
| const [snapshot, setSnapshot] = useState<SortedMakerEntry[]>(liveOffers); | ||
|
|
||
| useEffect(() => { | ||
| setSnapshot(liveOffersRef.current); | ||
| }, [sortMode, bitcoinBalance]); | ||
|
|
||
| useEffect(() => { | ||
| const snapshotPeers = new Set( | ||
| snapshot.map((o) => o.quote_with_address.peer_id), | ||
| ); | ||
| const hasNewPeer = liveOffers.some( | ||
| (o) => !snapshotPeers.has(o.quote_with_address.peer_id), | ||
| ); | ||
| if (hasNewPeer) setSnapshot(liveOffers); | ||
| }, [snapshot, liveOffers]); | ||
|
|
||
| useEffect(() => { | ||
| const id = window.setInterval(() => { | ||
| setSnapshot((prev) => | ||
| _.isEqual(prev, liveOffersRef.current) ? prev : liveOffersRef.current, | ||
| ); | ||
| }, REFRESH_INTERVAL_MS); | ||
| return () => window.clearInterval(id); | ||
| }, []); | ||
|
|
||
| return snapshot; | ||
| } | ||
68 changes: 68 additions & 0 deletions
68
...components/pages/swap/swap/init/deposit_and_choose_offer/useResolveSelectMakerApproval.ts
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,68 @@ | ||
| import { useCallback } from "react"; | ||
| import { store, type RootState } from "renderer/store/storeRenderer"; | ||
| import { resolveApproval } from "renderer/rpc"; | ||
| import { isPendingSelectMakerApprovalEvent } from "models/tauriModelExt"; | ||
|
|
||
| const WAIT_FOR_APPROVAL_MS = 5_000; | ||
|
|
||
| function findPendingApprovalForPeer( | ||
| state: RootState, | ||
| peerId: string, | ||
| ): string | null { | ||
| const requests = Object.values(state.rpc.state.approvalRequests); | ||
| for (const req of requests) { | ||
| if (req.request_status.state !== "Pending") continue; | ||
| if (!isPendingSelectMakerApprovalEvent(req)) continue; | ||
| if (req.request.content.maker.peer_id === peerId) return req.request_id; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * The snapshot list shown to the user can hold a request id that has already | ||
| * expired by the time they click Select. In that case we wait briefly for the | ||
| * backend to emit a fresh pending approval for the same peer and resolve that | ||
| * one instead, so a stale snapshot doesn't surface as a confusing error. | ||
| */ | ||
| export function useResolveSelectMakerApproval() { | ||
| return useCallback( | ||
| async (peerId: string, snapshotRequestId: string | undefined) => { | ||
| const tryResolve = (id: string) => | ||
| resolveApproval(id, true as unknown as object); | ||
|
|
||
| if (snapshotRequestId) { | ||
| const stillPending = | ||
| store.getState().rpc.state.approvalRequests[snapshotRequestId] | ||
| ?.request_status.state === "Pending"; | ||
| if (stillPending) return tryResolve(snapshotRequestId); | ||
| } | ||
|
|
||
| const immediate = findPendingApprovalForPeer(store.getState(), peerId); | ||
| if (immediate) return tryResolve(immediate); | ||
|
|
||
| const freshId = await new Promise<string | null>((resolve) => { | ||
| const timeout = window.setTimeout(() => { | ||
| unsubscribe(); | ||
| resolve(null); | ||
| }, WAIT_FOR_APPROVAL_MS); | ||
|
|
||
| const unsubscribe = store.subscribe(() => { | ||
| const found = findPendingApprovalForPeer(store.getState(), peerId); | ||
| if (found) { | ||
| window.clearTimeout(timeout); | ||
| unsubscribe(); | ||
| resolve(found); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| if (!freshId) { | ||
| throw new Error( | ||
| "This offer is no longer available. Please pick another maker.", | ||
| ); | ||
| } | ||
| return tryResolve(freshId); | ||
| }, | ||
| [], | ||
| ); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.