Skip to content

Commit 9e15c45

Browse files
author
Nader Chehab
committed
App Switch in New Tab
1 parent 7ef1eae commit 9e15c45

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

dist/button.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/test/button.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ui/buttons/props.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,9 @@ type ThrowErrorForInvalidButtonColorArgs = {|
567567

568568
export type ButtonProps = {|
569569
// app switch properties
570-
appSwitchWhenAvailable: boolean,
570+
appSwitchWhenAvailable:
571+
| boolean
572+
| {| enabled?: boolean, mode?: "sameTab" | "newTab" |},
571573
listenForHashChanges: () => void,
572574
removeListenerForHashChanges: () => void,
573575
// Not passed to child iframe

src/zoid/buttons/component.jsx

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,44 @@ export const getButtonsComponent: () => ButtonsComponent = memoize(() => {
325325
},
326326

327327
props: {
328+
// New nested shape with backward compatibility.
329+
// Accepts boolean (legacy) OR object { enabled, mode }
328330
appSwitchWhenAvailable: {
329-
type: "boolean",
331+
type: "object",
330332
queryParam: true,
331333
required: false,
334+
validate: ({ value }) => {
335+
if (
336+
typeof value === "boolean" ||
337+
value === null ||
338+
value === undefined ||
339+
typeof value === "string"
340+
) {
341+
return;
342+
}
343+
if (typeof value === "object") {
344+
// no-op: checked in value()
345+
return;
346+
}
347+
throw new Error("Invalid appSwitchWhenAvailable value");
348+
},
349+
value: ({ props }) => {
350+
// Backwards compatibility: if merchant passed a boolean or string,
351+
// convert to nested shape with default mode 'sameTab'.
352+
const { appSwitchWhenAvailable } = props;
353+
if (
354+
typeof appSwitchWhenAvailable === "boolean" ||
355+
typeof appSwitchWhenAvailable === "string"
356+
) {
357+
const enabled =
358+
String(appSwitchWhenAvailable) === "true" ||
359+
appSwitchWhenAvailable === true;
360+
return { enabled, mode: "sameTab" };
361+
}
362+
// If already an object, pass through
363+
return appSwitchWhenAvailable;
364+
},
365+
serialization: "json",
332366
},
333367

334368
showPayPalAppSwitchOverlay: {
@@ -639,8 +673,15 @@ export const getButtonsComponent: () => ButtonsComponent = memoize(() => {
639673
eagerOrderCreation: {
640674
type: "boolean",
641675
queryParam: true,
642-
value: ({ props }) =>
643-
isEagerOrderCreationEnabled(props.appSwitchWhenAvailable),
676+
value: ({ props }) => {
677+
const enabled = Boolean(
678+
props?.appSwitchWhenAvailable &&
679+
(typeof props.appSwitchWhenAvailable === "object"
680+
? props.appSwitchWhenAvailable.enabled
681+
: props.appSwitchWhenAvailable)
682+
);
683+
return isEagerOrderCreationEnabled(enabled);
684+
},
644685
},
645686

646687
enableFunding: {

src/zoid/buttons/prerender.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ export function PrerenderedButtons({
3939
props,
4040
}: PrerenderedButtonsProps): ChildType {
4141
const eagerOrderCreation = isEagerOrderCreationEnabled(
42-
props.appSwitchWhenAvailable
42+
// appSwitchWhenAvailable is now an object
43+
// @ts-ignore Flow types: treat object as truthy
44+
Boolean(
45+
props?.appSwitchWhenAvailable && props.appSwitchWhenAvailable.enabled
46+
)
4347
);
4448
let win;
4549
const handleClick = (

0 commit comments

Comments
 (0)