@getpara/graz-integration 2.0.0-alpha.50
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/dist/chunk-IV3L3JVM.js +46 -0
- package/dist/connector.d.ts +18 -0
- package/dist/connector.js +72 -0
- package/dist/connectorModal.d.ts +4 -0
- package/dist/connectorModal.js +115 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -0
- package/dist/package.json +4 -0
- package/package.json +30 -0
- package/scripts/build.mjs +36 -0
- package/src/connector.ts +88 -0
- package/src/connectorModal.tsx +120 -0
- package/src/index.ts +2 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp.call(b, prop))
|
|
12
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols)
|
|
14
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
+
if (__propIsEnum.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
20
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
|
+
var __async = (__this, __arguments, generator) => {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
var fulfilled = (value) => {
|
|
24
|
+
try {
|
|
25
|
+
step(generator.next(value));
|
|
26
|
+
} catch (e) {
|
|
27
|
+
reject(e);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var rejected = (value) => {
|
|
31
|
+
try {
|
|
32
|
+
step(generator.throw(value));
|
|
33
|
+
} catch (e) {
|
|
34
|
+
reject(e);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
38
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export {
|
|
43
|
+
__spreadValues,
|
|
44
|
+
__spreadProps,
|
|
45
|
+
__async
|
|
46
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ChainInfo } from '@keplr-wallet/types';
|
|
2
|
+
import { ParaGrazConnector as CoreParaGrazConnector, ParaGrazConfig as CoreParaGrazConfig } from '@getpara/graz-connector';
|
|
3
|
+
import { ParaModalProps as CoreParaModalProps } from '@getpara/react-sdk-lite';
|
|
4
|
+
import { QueryClient } from '@tanstack/react-query';
|
|
5
|
+
export type ParaModalProps = Omit<CoreParaModalProps, 'isOpen' | 'para'> & {
|
|
6
|
+
appName: string;
|
|
7
|
+
};
|
|
8
|
+
export interface ParaGrazConfig extends CoreParaGrazConfig {
|
|
9
|
+
modalProps?: ParaModalProps;
|
|
10
|
+
queryClient?: QueryClient;
|
|
11
|
+
}
|
|
12
|
+
export declare class ParaGrazConnector extends CoreParaGrazConnector {
|
|
13
|
+
protected isModalClosed: boolean;
|
|
14
|
+
protected config: ParaGrazConfig;
|
|
15
|
+
constructor(config: ParaGrazConfig, chains?: ChainInfo[] | null);
|
|
16
|
+
protected waitForLogin(timeoutMs?: number): Promise<void>;
|
|
17
|
+
enable(chainIdsInput: string | string[]): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__async
|
|
4
|
+
} from "./chunk-IV3L3JVM.js";
|
|
5
|
+
import {
|
|
6
|
+
ParaGrazConnector as CoreParaGrazConnector,
|
|
7
|
+
toArray
|
|
8
|
+
} from "@getpara/graz-connector";
|
|
9
|
+
import { renderModal } from "./connectorModal";
|
|
10
|
+
class ParaGrazConnector extends CoreParaGrazConnector {
|
|
11
|
+
constructor(config, chains) {
|
|
12
|
+
super(config, chains);
|
|
13
|
+
this.isModalClosed = true;
|
|
14
|
+
}
|
|
15
|
+
waitForLogin(timeoutMs = 6e4) {
|
|
16
|
+
return __async(this, null, function* () {
|
|
17
|
+
const deadline = Date.now() + timeoutMs;
|
|
18
|
+
let delay = 500;
|
|
19
|
+
const MAX_DELAY = 5e3;
|
|
20
|
+
while (true) {
|
|
21
|
+
if (yield this.paraWebClient.isFullyLoggedIn()) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (this.isModalClosed) {
|
|
25
|
+
throw new Error("Login modal closed by user");
|
|
26
|
+
}
|
|
27
|
+
if (Date.now() >= deadline) {
|
|
28
|
+
throw new Error(`Login timeout after ${timeoutMs / 1e3}s`);
|
|
29
|
+
}
|
|
30
|
+
yield new Promise((r) => setTimeout(r, delay));
|
|
31
|
+
delay = Math.min(delay * 1.5, MAX_DELAY);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
enable(chainIdsInput) {
|
|
36
|
+
return __async(this, null, function* () {
|
|
37
|
+
var _a, _b, _c, _d, _e;
|
|
38
|
+
const chainIds = toArray(chainIdsInput);
|
|
39
|
+
const previousEnabled = new Set(this.enabledChainIds);
|
|
40
|
+
try {
|
|
41
|
+
chainIds.forEach((id) => this.enabledChainIds.add(id));
|
|
42
|
+
if (yield this.hasCosmosWallet()) {
|
|
43
|
+
(_b = (_a = this.events) == null ? void 0 : _a.onEnabled) == null ? void 0 : _b.call(_a, chainIds, this);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
this.isModalClosed = false;
|
|
47
|
+
yield renderModal(
|
|
48
|
+
this.paraWebClient,
|
|
49
|
+
(_c = this.config.modalProps) != null ? _c : {},
|
|
50
|
+
() => {
|
|
51
|
+
this.isModalClosed = true;
|
|
52
|
+
},
|
|
53
|
+
this.config.queryClient
|
|
54
|
+
);
|
|
55
|
+
yield this.waitForLogin();
|
|
56
|
+
yield this.waitForAccounts();
|
|
57
|
+
(_e = (_d = this.events) == null ? void 0 : _d.onEnabled) == null ? void 0 : _e.call(_d, chainIds, this);
|
|
58
|
+
} catch (err) {
|
|
59
|
+
this.enabledChainIds = previousEnabled;
|
|
60
|
+
if (err instanceof Error) {
|
|
61
|
+
throw err;
|
|
62
|
+
}
|
|
63
|
+
throw new Error("Failed to enable Para wallet with modal");
|
|
64
|
+
} finally {
|
|
65
|
+
this.isModalClosed = true;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
ParaGrazConnector
|
|
72
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import ParaWeb from '@getpara/react-sdk-lite';
|
|
2
|
+
import { type QueryClient } from '@tanstack/react-query';
|
|
3
|
+
import { ParaModalProps } from './connector';
|
|
4
|
+
export declare function renderModal(para: ParaWeb, modalProps: Partial<ParaModalProps> | undefined, onCloseArg: () => void, queryClient: QueryClient): Promise<void>;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__async,
|
|
4
|
+
__spreadProps,
|
|
5
|
+
__spreadValues
|
|
6
|
+
} from "./chunk-IV3L3JVM.js";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
import { setIsOpen } from "@getpara/react-sdk-lite";
|
|
9
|
+
let rendererPromise = null;
|
|
10
|
+
function getRenderer() {
|
|
11
|
+
console.log("[DEBUG] getRenderer called.");
|
|
12
|
+
if (!rendererPromise) {
|
|
13
|
+
rendererPromise = (() => __async(this, null, function* () {
|
|
14
|
+
try {
|
|
15
|
+
const { createRoot } = yield import("react-dom/client");
|
|
16
|
+
console.log("[DEBUG] Imported createRoot from react-dom/client.");
|
|
17
|
+
return { createRoot: (el) => createRoot(el) };
|
|
18
|
+
} catch (err) {
|
|
19
|
+
console.log(
|
|
20
|
+
"[DEBUG] Failed to import react-dom/client; falling back to legacy ReactDOM. Error:",
|
|
21
|
+
err.message
|
|
22
|
+
);
|
|
23
|
+
const ReactDOM = yield import("react-dom");
|
|
24
|
+
console.log("[DEBUG] Imported legacy ReactDOM.");
|
|
25
|
+
return {
|
|
26
|
+
legacyRender: ReactDOM.render,
|
|
27
|
+
legacyUnmount: ReactDOM.unmountComponentAtNode
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}))();
|
|
31
|
+
}
|
|
32
|
+
return rendererPromise;
|
|
33
|
+
}
|
|
34
|
+
const wrap = (ctx, err, extraInfo) => {
|
|
35
|
+
const baseMsg = `connectorModal error in ${ctx}: ${err.message}`;
|
|
36
|
+
const fullMsg = extraInfo ? `${baseMsg}. Additional details: ${extraInfo}` : baseMsg;
|
|
37
|
+
throw new Error(fullMsg);
|
|
38
|
+
};
|
|
39
|
+
function renderModal(para, modalProps, onCloseArg, queryClient) {
|
|
40
|
+
return __async(this, null, function* () {
|
|
41
|
+
var _a, _b, _c;
|
|
42
|
+
console.log("[DEBUG] renderModal called with para:", para, "modalProps:", modalProps, "queryClient:", queryClient);
|
|
43
|
+
if (typeof window === "undefined") {
|
|
44
|
+
console.log("[DEBUG] renderModal: window undefined, returning early.");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const props = modalProps != null ? modalProps : {};
|
|
48
|
+
console.log("[DEBUG] Using modal props:", props);
|
|
49
|
+
const containerId = "para-modal";
|
|
50
|
+
let container = document.getElementById(containerId);
|
|
51
|
+
console.log("[DEBUG] Existing container:", container);
|
|
52
|
+
if (!container) {
|
|
53
|
+
container = document.createElement("div");
|
|
54
|
+
container.id = containerId;
|
|
55
|
+
document.body.appendChild(container);
|
|
56
|
+
console.log("[DEBUG] Created new container:", container);
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const renderer = yield getRenderer();
|
|
60
|
+
console.log("[DEBUG] Retrieved renderer:", renderer);
|
|
61
|
+
let root = (_a = container.__paraRoot) != null ? _a : null;
|
|
62
|
+
console.log("[DEBUG] Existing root:", root);
|
|
63
|
+
if (!root && renderer.createRoot) {
|
|
64
|
+
root = renderer.createRoot(container);
|
|
65
|
+
container.__paraRoot = root;
|
|
66
|
+
console.log("[DEBUG] Created new root with createRoot.");
|
|
67
|
+
}
|
|
68
|
+
const { ParaProvider } = yield import("@getpara/react-sdk-lite");
|
|
69
|
+
console.log("[DEBUG] Imported ParaProvider from @getpara/react-sdk-lite.");
|
|
70
|
+
const { QueryClientProvider } = yield import("@tanstack/react-query");
|
|
71
|
+
console.log("[DEBUG] Imported QueryClientProvider from @tanstack/react-query.");
|
|
72
|
+
const handleClose = () => {
|
|
73
|
+
var _a2, _b2;
|
|
74
|
+
console.log("[DEBUG] handleClose triggered.");
|
|
75
|
+
onCloseArg();
|
|
76
|
+
(_a2 = props.onClose) == null ? void 0 : _a2.call(props);
|
|
77
|
+
setIsOpen(false);
|
|
78
|
+
if (root) {
|
|
79
|
+
root.unmount();
|
|
80
|
+
console.log("[DEBUG] Unmounted using root.unmount.");
|
|
81
|
+
} else {
|
|
82
|
+
(_b2 = renderer.legacyUnmount) == null ? void 0 : _b2.call(renderer, container);
|
|
83
|
+
console.log("[DEBUG] Unmounted using legacyUnmount.");
|
|
84
|
+
}
|
|
85
|
+
container == null ? void 0 : container.remove();
|
|
86
|
+
console.log("[DEBUG] Removed container.");
|
|
87
|
+
};
|
|
88
|
+
const element = /* @__PURE__ */ jsx(QueryClientProvider, { client: queryClient, children: /* @__PURE__ */ jsx(
|
|
89
|
+
ParaProvider,
|
|
90
|
+
{
|
|
91
|
+
paraClientConfig: para,
|
|
92
|
+
config: { appName: (_b = props.appName) != null ? _b : "" },
|
|
93
|
+
paraModalConfig: __spreadProps(__spreadValues({}, props), { onClose: handleClose })
|
|
94
|
+
}
|
|
95
|
+
) });
|
|
96
|
+
console.log("[DEBUG] Created render element.");
|
|
97
|
+
if (root) {
|
|
98
|
+
root.render(element);
|
|
99
|
+
console.log("[DEBUG] Rendered using root.render.");
|
|
100
|
+
} else {
|
|
101
|
+
(_c = renderer.legacyRender) == null ? void 0 : _c.call(renderer, element, container);
|
|
102
|
+
console.log("[DEBUG] Rendered using legacyRender.");
|
|
103
|
+
}
|
|
104
|
+
setIsOpen(true);
|
|
105
|
+
console.log("[DEBUG] Set modal isOpen to true.");
|
|
106
|
+
} catch (err) {
|
|
107
|
+
console.log("[DEBUG] Error in renderModal; cleaning up container.");
|
|
108
|
+
container == null ? void 0 : container.remove();
|
|
109
|
+
throw wrap("renderModal", err, `containerId: ${containerId}, modalProps: ${JSON.stringify(props)}`);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
export {
|
|
114
|
+
renderModal
|
|
115
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@getpara/graz-integration",
|
|
3
|
+
"version": "2.0.0-alpha.50",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"typegen": "tsc --emitDeclarationOnly",
|
|
10
|
+
"build": "rm -rf dist && yarn typegen && node ./scripts/build.mjs"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@getpara/graz-connector": "2.0.0-alpha.50"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@getpara/react-sdk-lite": "2.0.0-alpha.50",
|
|
17
|
+
"@tanstack/react-query": "^5.74.0",
|
|
18
|
+
"graz": "^0.3.3",
|
|
19
|
+
"react": "^18.2.0",
|
|
20
|
+
"react-dom": "^18.2.0",
|
|
21
|
+
"typescript": "5.1.6"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@getpara/react-sdk-lite": "2.0.0-alpha.50",
|
|
25
|
+
"@tanstack/react-query": "^5.74.0",
|
|
26
|
+
"graz": "^0.3.3",
|
|
27
|
+
"react": ">=18.0.0",
|
|
28
|
+
"react-dom": ">=18.0.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as esbuild from 'esbuild';
|
|
2
|
+
import * as fs from 'fs/promises';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { dirname, resolve } from 'path';
|
|
5
|
+
import { glob } from 'glob';
|
|
6
|
+
|
|
7
|
+
const entryPoints = await glob('src/**/*.{ts,tsx,js,jsx}');
|
|
8
|
+
|
|
9
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const distDir = resolve(__dirname, '../dist');
|
|
11
|
+
|
|
12
|
+
await fs.mkdir(distDir, { recursive: true });
|
|
13
|
+
await fs.writeFile(`${distDir}/package.json`, JSON.stringify({ type: 'module', sideEffects: false }, null, 2));
|
|
14
|
+
|
|
15
|
+
/** @type {import('esbuild').BuildOptions} */
|
|
16
|
+
await esbuild.build({
|
|
17
|
+
banner: {
|
|
18
|
+
js: '"use client";', // Required for Next 13 App Router
|
|
19
|
+
},
|
|
20
|
+
bundle: false,
|
|
21
|
+
write: true,
|
|
22
|
+
format: 'esm',
|
|
23
|
+
loader: {
|
|
24
|
+
'.png': 'dataurl',
|
|
25
|
+
'.svg': 'dataurl',
|
|
26
|
+
'.json': 'text',
|
|
27
|
+
},
|
|
28
|
+
platform: 'browser',
|
|
29
|
+
entryPoints,
|
|
30
|
+
outdir: distDir,
|
|
31
|
+
allowOverwrite: true,
|
|
32
|
+
splitting: true, // Required for tree shaking
|
|
33
|
+
minify: false,
|
|
34
|
+
target: ['es2015'],
|
|
35
|
+
packages: 'external',
|
|
36
|
+
});
|
package/src/connector.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ChainInfo } from '@keplr-wallet/types';
|
|
2
|
+
import {
|
|
3
|
+
ParaGrazConnector as CoreParaGrazConnector,
|
|
4
|
+
ParaGrazConfig as CoreParaGrazConfig,
|
|
5
|
+
toArray,
|
|
6
|
+
} from '@getpara/graz-connector';
|
|
7
|
+
import { ParaModalProps as CoreParaModalProps } from '@getpara/react-sdk-lite';
|
|
8
|
+
import { renderModal } from './connectorModal';
|
|
9
|
+
import { QueryClient } from '@tanstack/react-query';
|
|
10
|
+
|
|
11
|
+
export type ParaModalProps = Omit<CoreParaModalProps, 'isOpen' | 'para'> & { appName: string };
|
|
12
|
+
|
|
13
|
+
export interface ParaGrazConfig extends CoreParaGrazConfig {
|
|
14
|
+
modalProps?: ParaModalProps;
|
|
15
|
+
queryClient?: QueryClient;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class ParaGrazConnector extends CoreParaGrazConnector {
|
|
19
|
+
protected isModalClosed = true;
|
|
20
|
+
declare protected config: ParaGrazConfig;
|
|
21
|
+
|
|
22
|
+
constructor(config: ParaGrazConfig, chains?: ChainInfo[] | null) {
|
|
23
|
+
super(config, chains);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected async waitForLogin(timeoutMs = 60_000): Promise<void> {
|
|
27
|
+
const deadline = Date.now() + timeoutMs;
|
|
28
|
+
let delay = 500;
|
|
29
|
+
const MAX_DELAY = 5_000;
|
|
30
|
+
|
|
31
|
+
while (true) {
|
|
32
|
+
if (await this.paraWebClient.isFullyLoggedIn()) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (this.isModalClosed) {
|
|
37
|
+
throw new Error('Login modal closed by user');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (Date.now() >= deadline) {
|
|
41
|
+
throw new Error(`Login timeout after ${timeoutMs / 1000}s`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
await new Promise(r => setTimeout(r, delay));
|
|
45
|
+
delay = Math.min(delay * 1.5, MAX_DELAY);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async enable(chainIdsInput: string | string[]): Promise<void> {
|
|
50
|
+
const chainIds = toArray(chainIdsInput);
|
|
51
|
+
const previousEnabled = new Set(this.enabledChainIds);
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
chainIds.forEach(id => this.enabledChainIds.add(id));
|
|
55
|
+
|
|
56
|
+
if (await this.hasCosmosWallet()) {
|
|
57
|
+
this.events?.onEnabled?.(chainIds, this);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
this.isModalClosed = false;
|
|
62
|
+
|
|
63
|
+
await renderModal(
|
|
64
|
+
this.paraWebClient,
|
|
65
|
+
this.config.modalProps ?? {},
|
|
66
|
+
() => {
|
|
67
|
+
this.isModalClosed = true;
|
|
68
|
+
},
|
|
69
|
+
this.config.queryClient!,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
await this.waitForLogin();
|
|
73
|
+
await this.waitForAccounts();
|
|
74
|
+
|
|
75
|
+
this.events?.onEnabled?.(chainIds, this);
|
|
76
|
+
} catch (err) {
|
|
77
|
+
this.enabledChainIds = previousEnabled;
|
|
78
|
+
|
|
79
|
+
if (err instanceof Error) {
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
throw new Error('Failed to enable Para wallet with modal');
|
|
84
|
+
} finally {
|
|
85
|
+
this.isModalClosed = true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import ParaWeb, { setIsOpen } from '@getpara/react-sdk-lite';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { type QueryClient } from '@tanstack/react-query';
|
|
4
|
+
import { ParaModalProps } from './connector';
|
|
5
|
+
|
|
6
|
+
type Root = { render(node: React.ReactNode): void; unmount(): void };
|
|
7
|
+
|
|
8
|
+
let rendererPromise: Promise<{
|
|
9
|
+
createRoot?: (el: HTMLElement) => Root;
|
|
10
|
+
legacyRender?: (node: React.ReactNode, el: HTMLElement) => void;
|
|
11
|
+
legacyUnmount?: (el: HTMLElement) => void;
|
|
12
|
+
}> | null = null;
|
|
13
|
+
|
|
14
|
+
function getRenderer() {
|
|
15
|
+
console.log('[DEBUG] getRenderer called.');
|
|
16
|
+
if (!rendererPromise) {
|
|
17
|
+
rendererPromise = (async () => {
|
|
18
|
+
try {
|
|
19
|
+
const { createRoot } = await import('react-dom/client');
|
|
20
|
+
console.log('[DEBUG] Imported createRoot from react-dom/client.');
|
|
21
|
+
return { createRoot: (el: HTMLElement) => createRoot(el) };
|
|
22
|
+
} catch (err) {
|
|
23
|
+
console.log(
|
|
24
|
+
'[DEBUG] Failed to import react-dom/client; falling back to legacy ReactDOM. Error:',
|
|
25
|
+
(err as Error).message,
|
|
26
|
+
);
|
|
27
|
+
const ReactDOM = await import('react-dom');
|
|
28
|
+
console.log('[DEBUG] Imported legacy ReactDOM.');
|
|
29
|
+
return {
|
|
30
|
+
legacyRender: ReactDOM.render,
|
|
31
|
+
legacyUnmount: ReactDOM.unmountComponentAtNode,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
})();
|
|
35
|
+
}
|
|
36
|
+
return rendererPromise;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const wrap = (ctx: string, err: unknown, extraInfo?: string): never => {
|
|
40
|
+
const baseMsg = `connectorModal error in ${ctx}: ${(err as Error).message}`;
|
|
41
|
+
const fullMsg = extraInfo ? `${baseMsg}. Additional details: ${extraInfo}` : baseMsg;
|
|
42
|
+
throw new Error(fullMsg);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export async function renderModal(
|
|
46
|
+
para: ParaWeb,
|
|
47
|
+
modalProps: Partial<ParaModalProps> | undefined,
|
|
48
|
+
onCloseArg: () => void,
|
|
49
|
+
queryClient: QueryClient,
|
|
50
|
+
): Promise<void> {
|
|
51
|
+
console.log('[DEBUG] renderModal called with para:', para, 'modalProps:', modalProps, 'queryClient:', queryClient);
|
|
52
|
+
if (typeof window === 'undefined') {
|
|
53
|
+
console.log('[DEBUG] renderModal: window undefined, returning early.');
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const props = modalProps ?? {};
|
|
57
|
+
console.log('[DEBUG] Using modal props:', props);
|
|
58
|
+
const containerId = 'para-modal';
|
|
59
|
+
let container = document.getElementById(containerId) as HTMLElement | null;
|
|
60
|
+
console.log('[DEBUG] Existing container:', container);
|
|
61
|
+
if (!container) {
|
|
62
|
+
container = document.createElement('div');
|
|
63
|
+
container.id = containerId;
|
|
64
|
+
document.body.appendChild(container);
|
|
65
|
+
console.log('[DEBUG] Created new container:', container);
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
const renderer = await getRenderer();
|
|
69
|
+
console.log('[DEBUG] Retrieved renderer:', renderer);
|
|
70
|
+
let root: Root | null = (container as any).__paraRoot ?? null;
|
|
71
|
+
console.log('[DEBUG] Existing root:', root);
|
|
72
|
+
if (!root && renderer.createRoot) {
|
|
73
|
+
root = renderer.createRoot(container);
|
|
74
|
+
(container as any).__paraRoot = root;
|
|
75
|
+
console.log('[DEBUG] Created new root with createRoot.');
|
|
76
|
+
}
|
|
77
|
+
const { ParaProvider } = await import('@getpara/react-sdk-lite');
|
|
78
|
+
console.log('[DEBUG] Imported ParaProvider from @getpara/react-sdk-lite.');
|
|
79
|
+
const { QueryClientProvider } = await import('@tanstack/react-query');
|
|
80
|
+
console.log('[DEBUG] Imported QueryClientProvider from @tanstack/react-query.');
|
|
81
|
+
const handleClose = () => {
|
|
82
|
+
console.log('[DEBUG] handleClose triggered.');
|
|
83
|
+
onCloseArg();
|
|
84
|
+
props.onClose?.();
|
|
85
|
+
setIsOpen(false);
|
|
86
|
+
if (root) {
|
|
87
|
+
root.unmount();
|
|
88
|
+
console.log('[DEBUG] Unmounted using root.unmount.');
|
|
89
|
+
} else {
|
|
90
|
+
renderer.legacyUnmount?.(container!);
|
|
91
|
+
console.log('[DEBUG] Unmounted using legacyUnmount.');
|
|
92
|
+
}
|
|
93
|
+
container?.remove();
|
|
94
|
+
console.log('[DEBUG] Removed container.');
|
|
95
|
+
};
|
|
96
|
+
const element = (
|
|
97
|
+
<QueryClientProvider client={queryClient}>
|
|
98
|
+
<ParaProvider
|
|
99
|
+
paraClientConfig={para}
|
|
100
|
+
config={{ appName: props.appName ?? '' }}
|
|
101
|
+
paraModalConfig={{ ...props, onClose: handleClose }}
|
|
102
|
+
/>
|
|
103
|
+
</QueryClientProvider>
|
|
104
|
+
);
|
|
105
|
+
console.log('[DEBUG] Created render element.');
|
|
106
|
+
if (root) {
|
|
107
|
+
root.render(element);
|
|
108
|
+
console.log('[DEBUG] Rendered using root.render.');
|
|
109
|
+
} else {
|
|
110
|
+
renderer.legacyRender?.(element, container!);
|
|
111
|
+
console.log('[DEBUG] Rendered using legacyRender.');
|
|
112
|
+
}
|
|
113
|
+
setIsOpen(true);
|
|
114
|
+
console.log('[DEBUG] Set modal isOpen to true.');
|
|
115
|
+
} catch (err) {
|
|
116
|
+
console.log('[DEBUG] Error in renderModal; cleaning up container.');
|
|
117
|
+
container?.remove();
|
|
118
|
+
throw wrap('renderModal', err, `containerId: ${containerId}, modalProps: ${JSON.stringify(props)}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"lib": ["dom", "dom.iterable", "esnext"],
|
|
6
|
+
"jsx": "react-jsx",
|
|
7
|
+
"module": "ESNext",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationDir": "./dist"
|
|
10
|
+
},
|
|
11
|
+
"include": ["src/**/*.ts", "src/**/*.tsx"],
|
|
12
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.test.tsx", "__tests__"]
|
|
13
|
+
}
|