@@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222*/
2323
24- import React , { useCallback , useMemo , useRef } from 'react' ;
24+ import React , { useCallback , useMemo , useRef , useEffect , useState } from 'react' ;
2525import type { PropsWithChildren } from 'react' ;
2626import { type EmitterSubscription } from 'react-native' ;
2727import { ShopifyCheckoutSheet } from './index' ;
@@ -31,12 +31,12 @@ import type {
3131 RemoveEventListeners ,
3232 CheckoutEvent ,
3333 Configuration ,
34- AcceleratedCheckoutConfiguration ,
3534} from './index.d' ;
3635
3736type Maybe < T > = T | undefined ;
3837
3938interface Context {
39+ acceleratedCheckoutsAvailable : boolean ;
4040 addEventListener : AddEventListener ;
4141 getConfig : ( ) => Promise < Configuration | undefined > ;
4242 setConfig : ( config : Configuration ) => void ;
@@ -46,19 +46,12 @@ interface Context {
4646 dismiss : ( ) => void ;
4747 invalidate : ( ) => void ;
4848 version : Maybe < string > ;
49- configureAcceleratedCheckouts : (
50- config : AcceleratedCheckoutConfiguration ,
51- ) => void ;
52- isAcceleratedCheckoutAvailable : ( options : {
53- cartId ?: string ;
54- variantId ?: string ;
55- quantity ?: number ;
56- } ) => Promise < boolean > ;
5749}
5850
5951const noop = ( ) => undefined ;
6052
6153const ShopifyCheckoutSheetContext = React . createContext < Context > ( {
54+ acceleratedCheckoutsAvailable : false ,
6255 addEventListener : noop ,
6356 removeEventListeners : noop ,
6457 setConfig : noop ,
@@ -68,8 +61,6 @@ const ShopifyCheckoutSheetContext = React.createContext<Context>({
6861 invalidate : noop ,
6962 dismiss : noop ,
7063 version : undefined ,
71- configureAcceleratedCheckouts : noop ,
72- isAcceleratedCheckoutAvailable : async ( ) => false ,
7364} ) ;
7465
7566interface Props {
@@ -82,12 +73,32 @@ export function ShopifyCheckoutSheetProvider({
8273 configuration,
8374 children,
8475} : PropsWithChildren < Props > ) {
76+ const [ acceleratedCheckoutsAvailable , setAcceleratedCheckoutsAvailable ] =
77+ useState ( false ) ;
8578 const instance = useRef < ShopifyCheckoutSheet | null > ( null ) ;
8679
8780 if ( ! instance . current ) {
8881 instance . current = new ShopifyCheckoutSheet ( configuration , features ) ;
8982 }
9083
84+ useEffect ( ( ) => {
85+ async function configureAcceleratedCheckouts ( ) {
86+ if ( ! instance . current || ! configuration ) {
87+ return ;
88+ }
89+
90+ if ( configuration . acceleratedCheckouts ) {
91+ setAcceleratedCheckoutsAvailable (
92+ await instance . current ?. configureAcceleratedCheckouts (
93+ configuration . acceleratedCheckouts ,
94+ ) ,
95+ ) ;
96+ }
97+ }
98+
99+ configureAcceleratedCheckouts ( ) ;
100+ } , [ configuration ] ) ;
101+
91102 const addEventListener : AddEventListener = useCallback (
92103 ( eventName , callback ) : EmitterSubscription | undefined => {
93104 return instance . current ?. addEventListener ( eventName , callback ) ;
@@ -127,29 +138,9 @@ export function ShopifyCheckoutSheetProvider({
127138 return instance . current ?. getConfig ( ) ;
128139 } , [ ] ) ;
129140
130- const configureAcceleratedCheckouts = useCallback (
131- ( config : AcceleratedCheckoutConfiguration ) => {
132- instance . current ?. configureAcceleratedCheckouts ( config ) ;
133- } ,
134- [ ] ,
135- ) ;
136-
137- const isAcceleratedCheckoutAvailable = useCallback (
138- async ( options : {
139- cartId ?: string ;
140- variantId ?: string ;
141- quantity ?: number ;
142- } ) => {
143- return (
144- ( await instance . current ?. isAcceleratedCheckoutAvailable ( options ) ) ??
145- false
146- ) ;
147- } ,
148- [ ] ,
149- ) ;
150-
151141 const context = useMemo ( ( ) : Context => {
152142 return {
143+ acceleratedCheckoutsAvailable,
153144 addEventListener,
154145 dismiss,
155146 setConfig,
@@ -159,10 +150,9 @@ export function ShopifyCheckoutSheetProvider({
159150 invalidate,
160151 removeEventListeners,
161152 version : instance . current ?. version ,
162- configureAcceleratedCheckouts,
163- isAcceleratedCheckoutAvailable,
164153 } ;
165154 } , [
155+ acceleratedCheckoutsAvailable ,
166156 addEventListener ,
167157 dismiss ,
168158 removeEventListeners ,
@@ -171,8 +161,6 @@ export function ShopifyCheckoutSheetProvider({
171161 preload ,
172162 present ,
173163 invalidate ,
174- configureAcceleratedCheckouts ,
175- isAcceleratedCheckoutAvailable ,
176164 ] ) ;
177165
178166 return (
0 commit comments