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/mobile-swap-status-infra.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": minor
---

Add mobile swap transaction status drawer infrastructure.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ apps/ledger-live-mobile/src/components/Stake/ @ledge
apps/ledger-live-mobile/src/components/WebPTXPlayer/ @ledgerhq/ptx
apps/ledger-live-mobile/src/reducers/earn.ts @ledgerhq/ptx
apps/ledger-live-mobile/src/reducers/swap.ts @ledgerhq/ptx
apps/ledger-live-mobile/src/reducers/swapTransactionStatusDrawer.ts @ledgerhq/ptx
apps/ledger-live-mobile/src/screens/Exchange/ @ledgerhq/ptx
apps/ledger-live-mobile/src/screens/PTX/ @ledgerhq/ptx
apps/ledger-live-mobile/src/screens/Swap/ @ledgerhq/ptx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const QueuedDrawerBottomSheet = ({
bottomSheetRef,
areDrawersLocked,
handleBackdropPress,
handleHeaderClosePressed,
handleDismiss,
handleCloseAnimationStart,
onBack: hookOnBack,
Expand All @@ -91,6 +92,7 @@ const QueuedDrawerBottomSheet = ({
isForcingToBeOpened,
onClose,
onBack,
onHeaderClosePressed,
onBackdropPress,
onModalHide,
preventBackdropClick,
Expand All @@ -109,9 +111,9 @@ const QueuedDrawerBottomSheet = ({
hideCloseButton={noCloseButton || areDrawersLocked}
hideHandle={hideHandle}
onBack={hasBackButton ? hookOnBack : undefined}
onHeaderClosePressed={handleHeaderClosePressed}
onAnimate={handleCloseAnimationStart}
onDismiss={handleDismiss}
onHeaderClosePressed={onHeaderClosePressed}
backdropPressBehavior={preventBackdropClick || areDrawersLocked ? "none" : "close"}
onBackdropPress={handleBackdropPress}
backgroundComponent={backgroundComponent}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,37 @@ describe("useQueuedDrawerBottomSheet", () => {
expect(onClose).toHaveBeenCalledTimes(1);
});

it("calls header close callbacks once and ignores later close animation and dismiss callbacks", () => {
const onClose = jest.fn();
const onHeaderClosePressed = jest.fn();
const { signal } = setupDrawerStateCapture();

const { result } = renderHook(() =>
useQueuedDrawerBottomSheet({
isRequestingToBeOpened: true,
onClose,
onHeaderClosePressed,
}),
);

signal(true);

act(() => {
result.current.handleHeaderClosePressed();
});

expect(onHeaderClosePressed).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledTimes(1);

act(() => {
result.current.handleCloseAnimationStart(0, -1);
result.current.handleDismiss();
});

expect(onHeaderClosePressed).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledTimes(1);
});

it("does not clear consumer state on open or snap-point animations", () => {
const onClose = jest.fn();
const { signal } = setupDrawerStateCapture();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface UseQueuedDrawerBottomSheetProps {
isForcingToBeOpened?: boolean;
onClose?: () => void;
onBack?: () => void;
onHeaderClosePressed?: () => void;
onBackdropPress?: () => void;
onModalHide?: () => void;
preventBackdropClick?: boolean;
Expand All @@ -26,6 +27,7 @@ const useQueuedDrawerBottomSheet = ({
isForcingToBeOpened = false,
onClose,
onBack,
onHeaderClosePressed,
onBackdropPress,
onModalHide,
preventBackdropClick,
Expand All @@ -43,6 +45,9 @@ const useQueuedDrawerBottomSheet = ({
const onCloseRef = useRef(onClose);
onCloseRef.current = onClose;

const onHeaderClosePressedRef = useRef(onHeaderClosePressed);
onHeaderClosePressedRef.current = onHeaderClosePressed;

const onBackdropPressRef = useRef(onBackdropPress);
onBackdropPressRef.current = onBackdropPress;

Expand Down Expand Up @@ -118,6 +123,15 @@ const useQueuedDrawerBottomSheet = ({
handleUserClose();
}, [handleUserClose]);

const handleHeaderClosePressed = useCallback(() => {
if (stateRef.current === "dismissing") return;

logDrawer("Header close pressed");
stateRef.current = "dismissing";
onHeaderClosePressedRef.current?.();
onCloseRef.current?.();
}, []);

// Fired at the START of an animation. A close animation targets index -1, so this is the
// earliest deterministic signal that the sheet is closing — for the X (close) button, the
// backdrop and the pan-down gesture alike. We clear consumer state here rather than waiting for
Expand Down Expand Up @@ -192,6 +206,7 @@ const useQueuedDrawerBottomSheet = ({
areDrawersLocked,
handleUserClose,
handleBackdropPress,
handleHeaderClosePressed,
handleDismiss,
handleCloseAnimationStart,
onBack,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import type { SwapTransactionStatusParams } from "@ledgerhq/live-common/exchange/swapTransactionStatus/index";
import reducer, {
INITIAL_STATE,
closeSwapTransactionStatusDrawer,
openSwapTransactionStatusDrawer,
selectIsSwapTransactionStatusDrawerOpen,
selectSwapTransactionStatusDrawerParams,
swapTransactionStatusDrawerSelector,
} from "../swapTransactionStatusDrawer";
import type { State } from "../types";

const swapTransactionStatusParams: SwapTransactionStatusParams = {
swapId: "swap-id",
provider: "lifi",
redirectUrl: "ledgerlive://swap/status",
};

const buildState = (swapTransactionStatusDrawer = INITIAL_STATE): State => ({
...({} as State),
swapTransactionStatusDrawer,
});

describe("swapTransactionStatusDrawer reducer", () => {
it("should expose a closed initial state", () => {
expect(INITIAL_STATE).toEqual({
isOpen: false,
params: null,
});
});

it("should open the drawer with transaction status params", () => {
const state = reducer(
INITIAL_STATE,
openSwapTransactionStatusDrawer(swapTransactionStatusParams),
);

expect(state).toEqual({
isOpen: true,
params: swapTransactionStatusParams,
});
});

it("should replace params when opening an already open drawer", () => {
const initialParams: SwapTransactionStatusParams = {
swapId: "previous-swap-id",
provider: "changelly",
};
const openState = reducer(INITIAL_STATE, openSwapTransactionStatusDrawer(initialParams));

const state = reducer(openState, openSwapTransactionStatusDrawer(swapTransactionStatusParams));

expect(state).toEqual({
isOpen: true,
params: swapTransactionStatusParams,
});
});

it("should close the drawer and clear params", () => {
const openState = reducer(
INITIAL_STATE,
openSwapTransactionStatusDrawer(swapTransactionStatusParams),
);

const state = reducer(openState, closeSwapTransactionStatusDrawer());

expect(state).toEqual(INITIAL_STATE);
});
});

describe("swapTransactionStatusDrawer selectors", () => {
it("should return the drawer slice from state", () => {
const drawerState = {
isOpen: true,
params: swapTransactionStatusParams,
};

expect(swapTransactionStatusDrawerSelector(buildState(drawerState))).toEqual(drawerState);
});

it("should return whether the drawer is open", () => {
expect(
selectIsSwapTransactionStatusDrawerOpen(
buildState({
isOpen: true,
params: swapTransactionStatusParams,
}),
),
).toBe(true);
});

it("should return the current drawer params", () => {
expect(
selectSwapTransactionStatusDrawerParams(
buildState({
isOpen: true,
params: swapTransactionStatusParams,
}),
),
).toEqual(swapTransactionStatusParams);
});
});
2 changes: 2 additions & 0 deletions apps/ledger-live-mobile/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import modularDrawer from "./modularDrawer";
import receiveOptionsDrawer from "./receiveOptionsDrawer";
import rebornBuyDeviceDrawer from "./rebornBuyDeviceDrawer";
import transferDrawer from "./transferDrawer";
import swapTransactionStatusDrawer from "./swapTransactionStatusDrawer";
import notifications from "./notifications";
import protect from "./protect";
import ratings from "./ratings";
Expand Down Expand Up @@ -69,6 +70,7 @@ const appReducer = combineReducers({
receiveOptionsDrawer,
rebornBuyDeviceDrawer,
transferDrawer,
swapTransactionStatusDrawer,
notifications,
postOnboarding,
postOnboardingHubDrawer,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import type { SwapTransactionStatusParams } from "@ledgerhq/live-common/exchange/swapTransactionStatus/index";
import type { State } from "~/reducers/types";

export type SwapTransactionStatusDrawerState = {
isOpen: boolean;
params: SwapTransactionStatusParams | null;
};

export const INITIAL_STATE: SwapTransactionStatusDrawerState = {
isOpen: false,
params: null,
};

export const swapTransactionStatusDrawerSelector = (state: State) =>
state.swapTransactionStatusDrawer;
export const selectIsSwapTransactionStatusDrawerOpen = (state: State) =>
state.swapTransactionStatusDrawer.isOpen;
export const selectSwapTransactionStatusDrawerParams = (state: State) =>
state.swapTransactionStatusDrawer.params;

const swapTransactionStatusDrawerSlice = createSlice({
name: "swapTransactionStatusDrawer",
initialState: INITIAL_STATE,
reducers: {
openSwapTransactionStatusDrawer: (
state,
action: PayloadAction<SwapTransactionStatusParams>,
) => {
state.isOpen = true;
state.params = action.payload;
},
closeSwapTransactionStatusDrawer: state => {
state.isOpen = false;
state.params = null;
},
},
});

export const { openSwapTransactionStatusDrawer, closeSwapTransactionStatusDrawer } =
swapTransactionStatusDrawerSlice.actions;

export default swapTransactionStatusDrawerSlice.reducer;
2 changes: 2 additions & 0 deletions apps/ledger-live-mobile/src/reducers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type { ModularDrawerState } from "./modularDrawer";
import type { LLMRTKApiState } from "~/context/rtkQueryApi";
import type { ReceiveOptionsDrawerState } from "./receiveOptionsDrawer";
import type { TransferDrawerState } from "./transferDrawer";
import type { SwapTransactionStatusDrawerState } from "./swapTransactionStatusDrawer";
import type { PostOnboardingHubDrawerState } from "./postOnboardingHubDrawer";
import type { SendFlowState } from "./sendFlow";
import { IdentitiesState } from "@ledgerhq/client-ids/store";
Expand Down Expand Up @@ -457,6 +458,7 @@ export type State = LLMRTKApiState & {
receiveOptionsDrawer: ReceiveOptionsDrawerState;
rebornBuyDeviceDrawer: RebornBuyDeviceDrawerState;
transferDrawer: TransferDrawerState;
swapTransactionStatusDrawer: SwapTransactionStatusDrawerState;
notifications: NotificationsState;
postOnboarding: PostOnboardingState;
postOnboardingHubDrawer: PostOnboardingHubDrawerState;
Expand Down
Loading