@geophrase/react 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/package.json +13 -0
- package/src/index.js +3 -0
- package/src/useGeophrase.js +70 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@geophrase/react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React and Next.js hooks for the Geophrase Connect SDK",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"react": ">=16.8.0"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@geophrase/core": "*"
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef, useCallback } from 'react';
|
|
4
|
+
import Geophrase from '@geophrase/core';
|
|
5
|
+
|
|
6
|
+
export const useGeophrase = (options = {}) => {
|
|
7
|
+
const geoInstance = useRef(null);
|
|
8
|
+
|
|
9
|
+
// 1. Keep a mutable reference to the latest callbacks
|
|
10
|
+
// This prevents React from destroying/recreating the iframe just
|
|
11
|
+
// because the parent component re-rendered and created a new function.
|
|
12
|
+
const savedCallbacks = useRef({
|
|
13
|
+
onSuccess: options.onSuccess,
|
|
14
|
+
onError: options.onError,
|
|
15
|
+
onClose: options.onClose
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// 2. Update the callbacks on every render without triggering the useEffect
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
savedCallbacks.current = {
|
|
21
|
+
onSuccess: options.onSuccess,
|
|
22
|
+
onError: options.onError,
|
|
23
|
+
onClose: options.onClose
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// 3. Manage the Vanilla JS Lifecycle safely
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (typeof window === 'undefined') return;
|
|
30
|
+
|
|
31
|
+
// Instantiate the vanilla class EXACTLY once per mount
|
|
32
|
+
geoInstance.current = new Geophrase({
|
|
33
|
+
key: options.key,
|
|
34
|
+
order_id: options.order_id,
|
|
35
|
+
phone: options.phone,
|
|
36
|
+
// Pass wrapper functions to always execute the latest React state
|
|
37
|
+
onSuccess: (data) => {
|
|
38
|
+
if (savedCallbacks.current.onSuccess) savedCallbacks.current.onSuccess(data);
|
|
39
|
+
},
|
|
40
|
+
onError: (error) => {
|
|
41
|
+
if (savedCallbacks.current.onError) savedCallbacks.current.onError(error);
|
|
42
|
+
},
|
|
43
|
+
onClose: () => {
|
|
44
|
+
if (savedCallbacks.current.onClose) savedCallbacks.current.onClose();
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Cleanup: Destroy the DOM elements if the React component unmounts
|
|
49
|
+
// This perfectly handles React 18's Strict Mode double-mount behavior
|
|
50
|
+
return () => {
|
|
51
|
+
if (geoInstance.current) {
|
|
52
|
+
geoInstance.current.destroy();
|
|
53
|
+
geoInstance.current = null;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// We only re-run this effect if the CORE configuration changes
|
|
58
|
+
}, [options.key, options.order_id, options.phone]);
|
|
59
|
+
|
|
60
|
+
// 4. Provide a stable reference to the open method
|
|
61
|
+
const open = useCallback(() => {
|
|
62
|
+
if (geoInstance.current) {
|
|
63
|
+
geoInstance.current.open();
|
|
64
|
+
} else {
|
|
65
|
+
console.warn("Geophrase widget is not ready yet.");
|
|
66
|
+
}
|
|
67
|
+
}, []);
|
|
68
|
+
|
|
69
|
+
return { open };
|
|
70
|
+
};
|