-
Notifications
You must be signed in to change notification settings - Fork 531
fix: default closed stock markets to token price |OK-61733 #13222
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
102 changes: 102 additions & 0 deletions
102
packages/kit/src/views/Market/MarketDetailV2/hooks/useStockPriceSource.test.tsx
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,102 @@ | ||
| /** @jest-environment jsdom */ | ||
|
|
||
| import { act, renderHook } from '@testing-library/react'; | ||
|
|
||
| import type { IMarketPriceSource } from '@onekeyhq/kit-bg/src/states/jotai/atoms'; | ||
|
|
||
| import { useStockPriceSource } from './useStockPriceSource'; | ||
|
|
||
| let mockStock: { | ||
| stockId: string; | ||
| stockDetail?: { marketStatus?: { isOpen: boolean } }; | ||
| }; | ||
|
|
||
| jest.mock('./StockDetailContext', () => ({ | ||
| useStockDetail: () => mockStock, | ||
| })); | ||
|
|
||
| jest.mock('@onekeyhq/kit-bg/src/states/jotai/atoms', () => ({ | ||
| useMarketPriceSourceAtom: () => { | ||
| const { useState } = jest.requireActual<typeof import('react')>('react'); | ||
| return useState<{ source: IMarketPriceSource }>({ source: 'share' }); | ||
| }, | ||
| })); | ||
|
|
||
| describe('useStockPriceSource', () => { | ||
| beforeEach(() => { | ||
| mockStock = { stockId: 'AAPL' }; | ||
| }); | ||
|
|
||
| it.each([ | ||
| [false, 'token'], | ||
| [true, 'share'], | ||
| ] as const)('defaults isOpen=%s to %s after loading', (isOpen, source) => { | ||
| const { result, rerender } = renderHook(() => useStockPriceSource()); | ||
| expect(result.current.priceMode).toBe('share'); | ||
|
|
||
| mockStock.stockDetail = { marketStatus: { isOpen } }; | ||
| rerender(); | ||
|
|
||
| expect(result.current.priceMode).toBe(source); | ||
| }); | ||
|
|
||
| it('uses an already loaded closed-market status on mount', () => { | ||
| mockStock.stockDetail = { marketStatus: { isOpen: false } }; | ||
| const { result } = renderHook(() => useStockPriceSource()); | ||
| expect(result.current.priceMode).toBe('token'); | ||
| }); | ||
|
|
||
| it('keeps Share Price when the market status is missing', () => { | ||
| mockStock.stockDetail = {}; | ||
| const { result } = renderHook(() => useStockPriceSource()); | ||
| expect(result.current.priceMode).toBe('share'); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [true, 'token'], | ||
| [false, 'share'], | ||
| ] as const)( | ||
| 'preserves manual %s -> %s through polling', | ||
| (isInitiallyOpen, source) => { | ||
| mockStock.stockDetail = { marketStatus: { isOpen: isInitiallyOpen } }; | ||
| const { result, rerender } = renderHook(() => useStockPriceSource()); | ||
| act(() => result.current.handlePriceModeChange(source)); | ||
|
|
||
| for (const isOpen of [true, false, true]) { | ||
| mockStock.stockDetail = { marketStatus: { isOpen } }; | ||
| rerender(); | ||
| expect(result.current.priceMode).toBe(source); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| it('preserves a manual Share Price choice before the response arrives', () => { | ||
| const { result, rerender } = renderHook(() => useStockPriceSource()); | ||
| act(() => result.current.handlePriceModeChange('share')); | ||
|
|
||
| mockStock.stockDetail = { marketStatus: { isOpen: false } }; | ||
| rerender(); | ||
| expect(result.current.priceMode).toBe('share'); | ||
| }); | ||
|
|
||
| it('reinitializes on stock changes, including returning to a previous stock', () => { | ||
| mockStock.stockDetail = { marketStatus: { isOpen: false } }; | ||
| const { result, rerender } = renderHook(() => useStockPriceSource()); | ||
| expect(result.current.priceMode).toBe('token'); | ||
| act(() => result.current.handlePriceModeChange('share')); | ||
|
|
||
| mockStock = { stockId: 'MSFT' }; | ||
| rerender(); | ||
| expect(result.current.priceMode).toBe('share'); | ||
| mockStock.stockDetail = { marketStatus: { isOpen: true } }; | ||
| rerender(); | ||
| expect(result.current.priceMode).toBe('share'); | ||
|
|
||
| mockStock = { | ||
| stockId: 'AAPL', | ||
| stockDetail: { marketStatus: { isOpen: false } }, | ||
| }; | ||
| rerender(); | ||
| expect(result.current.priceMode).toBe('token'); | ||
| }); | ||
| }); |
44 changes: 44 additions & 0 deletions
44
packages/kit/src/views/Market/MarketDetailV2/hooks/useStockPriceSource.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,44 @@ | ||
| import { useCallback, useEffect, useRef } from 'react'; | ||
|
|
||
| import { | ||
| type IMarketPriceSource, | ||
| useMarketPriceSourceAtom, | ||
| } from '@onekeyhq/kit-bg/src/states/jotai/atoms'; | ||
|
|
||
| import { useStockDetail } from './StockDetailContext'; | ||
|
|
||
| export function useStockPriceSource() { | ||
| const { stockId, stockDetail } = useStockDetail(); | ||
| const isOpen = stockDetail?.marketStatus?.isOpen; | ||
| const [{ source: priceMode }, setPriceSource] = useMarketPriceSourceAtom(); | ||
| const initializedRef = useRef(false); | ||
|
|
||
| useEffect(() => { | ||
| initializedRef.current = false; | ||
| setPriceSource((prev) => | ||
| prev.source === 'share' ? prev : { source: 'share' }, | ||
| ); | ||
| }, [stockId, setPriceSource]); | ||
|
|
||
| useEffect(() => { | ||
| // Wait for this stock's market status, then apply its default only once. | ||
| // Quote polling must not replace the user's choice on the same stock. | ||
| if (!stockId || initializedRef.current || typeof isOpen !== 'boolean') { | ||
| return; | ||
| } | ||
| initializedRef.current = true; | ||
| const source = isOpen ? 'share' : 'token'; | ||
| setPriceSource((prev) => (prev.source === source ? prev : { source })); | ||
| }, [isOpen, stockId, setPriceSource]); | ||
|
|
||
| const handlePriceModeChange = useCallback( | ||
| (source: IMarketPriceSource) => { | ||
| // A manual choice made before the first response also takes precedence. | ||
| initializedRef.current = true; | ||
| setPriceSource({ source }); | ||
| }, | ||
| [setPriceSource], | ||
| ); | ||
|
|
||
| return { priceMode, handlePriceModeChange }; | ||
| } | ||
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.
P1: [Returning to a stacked stock page skips reinitialization]
When a desktop user opens stock A, navigates to stock B, and then pops back, A can keep B’s price source. Desktop detail screens remain mounted in the navigation stack, so A’s hook instance still has
initializedRef.current === true, while B has written to the shared global price-source atom.Because returning to A changes neither A’s
stockIdnor itsisOpenvalue, these effects do not reinitialize it. A closed stock can therefore come back showing the stale Share Price header and chart instead of Token Price.Please make route focus the ownership boundary: reset and apply the focused stock’s current default on each focus epoch, while preventing blurred route instances from writing the global atom. A regression test should mount two route instances sharing the atom and switch focus between them.