@hygraph/app-sdk-react 0.0.2

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 ADDED
@@ -0,0 +1,15 @@
1
+ # @hygraph/app-sdk-react
2
+
3
+ React SDK for building Apps for Hygraph.
4
+
5
+ ## ⚡ Usage
6
+
7
+ For docs, guides and examples, please visit [our official documentation](https://hygraph.com/docs/).
8
+
9
+ ## 📝 License
10
+
11
+ Licensed under the MIT License.
12
+
13
+ ---
14
+
15
+ Made with 💜 by Hygraph 👋 [join our community](https://slack.hygraph.com/)!
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import { AppProps } from '@hygraph/app-sdk';
3
+ import type { ExtensionDialogProps, FormSidebarExtensionProps } from '@hygraph/app-sdk';
4
+ import type { FieldExtensionProps } from './hooks';
5
+ type WrapperContext = AppProps | FieldExtensionProps | FormSidebarExtensionProps | (ExtensionDialogProps & Record<string, unknown>);
6
+ type SDKState = 'init' | 'error' | 'ok' | 'validation';
7
+ declare const WrapperContext: React.Context<WrapperContext>;
8
+ type WrapperProps = {
9
+ uid?: string;
10
+ fallback?: React.FC<{
11
+ state: SDKState;
12
+ }>;
13
+ debug?: boolean;
14
+ } & {
15
+ children: React.ReactNode;
16
+ };
17
+ export declare function Wrapper(props: WrapperProps): JSX.Element;
18
+ export declare function useWrapperContext(): WrapperContext;
19
+ export {};
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import { AppProps } from '@hygraph/app-sdk';
3
+ import type { ExtensionDialogProps, FormSidebarExtensionProps } from '@hygraph/app-sdk';
4
+ import type { FieldExtensionProps } from './hooks';
5
+ type WrapperContext = AppProps | FieldExtensionProps | FormSidebarExtensionProps | (ExtensionDialogProps & Record<string, unknown>);
6
+ type SDKState = 'init' | 'error' | 'ok' | 'validation';
7
+ declare const WrapperContext: React.Context<WrapperContext>;
8
+ type WrapperProps = {
9
+ uid?: string;
10
+ fallback?: React.FC<{
11
+ state: SDKState;
12
+ }>;
13
+ debug?: boolean;
14
+ } & {
15
+ children: React.ReactNode;
16
+ };
17
+ export declare function Wrapper(props: WrapperProps): JSX.Element;
18
+ export declare function useWrapperContext(): WrapperContext;
19
+ export {};
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __read = (this && this.__read) || function (o, n) {
39
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
40
+ if (!m) return o;
41
+ var i = m.call(o), r, ar = [], e;
42
+ try {
43
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
+ }
45
+ catch (error) { e = { error: error }; }
46
+ finally {
47
+ try {
48
+ if (r && !r.done && (m = i["return"])) m.call(i);
49
+ }
50
+ finally { if (e) throw e.error; }
51
+ }
52
+ return ar;
53
+ };
54
+ var __importDefault = (this && this.__importDefault) || function (mod) {
55
+ return (mod && mod.__esModule) ? mod : { "default": mod };
56
+ };
57
+ Object.defineProperty(exports, "__esModule", { value: true });
58
+ exports.useWrapperContext = exports.Wrapper = void 0;
59
+ var react_1 = __importDefault(require("react"));
60
+ var app_sdk_1 = require("@hygraph/app-sdk");
61
+ var WrapperContext = react_1.default.createContext({});
62
+ function useInitSDK(uid, debug) {
63
+ if (debug === void 0) { debug = false; }
64
+ var _a = __read(react_1.default.useState(), 2), sdkProps = _a[0], setSdkProps = _a[1];
65
+ var _b = __read(react_1.default.useState('init'), 2), extensionState = _b[0], setExtensionState = _b[1];
66
+ react_1.default.useEffect(function () {
67
+ function initSdk() {
68
+ return __awaiter(this, void 0, void 0, function () {
69
+ var initialState, error_1;
70
+ return __generator(this, function (_a) {
71
+ switch (_a.label) {
72
+ case 0:
73
+ _a.trys.push([0, 2, , 3]);
74
+ return [4 /*yield*/, (0, app_sdk_1.init)({
75
+ uid: uid,
76
+ debug: debug,
77
+ onProps: setSdkProps,
78
+ })];
79
+ case 1:
80
+ initialState = _a.sent();
81
+ if (initialState.status === 'ok') {
82
+ setSdkProps(initialState.props);
83
+ }
84
+ setExtensionState(initialState.status);
85
+ return [3 /*break*/, 3];
86
+ case 2:
87
+ error_1 = _a.sent();
88
+ setExtensionState('error');
89
+ console.error(error_1);
90
+ return [3 /*break*/, 3];
91
+ case 3: return [2 /*return*/];
92
+ }
93
+ });
94
+ });
95
+ }
96
+ initSdk();
97
+ }, [uid, debug]);
98
+ return { sdkProps: sdkProps, extensionState: extensionState };
99
+ }
100
+ function Wrapper(props) {
101
+ var children = props.children, uid = props.uid, _a = props.fallback, FallbackComponent = _a === void 0 ? DefaultFallbackComponent : _a, debug = props.debug;
102
+ var _b = useInitSDK(uid, debug), sdkProps = _b.sdkProps, extensionState = _b.extensionState;
103
+ if (extensionState != 'ok' || !sdkProps)
104
+ return react_1.default.createElement(FallbackComponent, { state: extensionState });
105
+ return react_1.default.createElement(WrapperContext.Provider, { value: sdkProps, children: children });
106
+ }
107
+ exports.Wrapper = Wrapper;
108
+ function useWrapperContext() {
109
+ return react_1.default.useContext(WrapperContext);
110
+ }
111
+ exports.useWrapperContext = useWrapperContext;
112
+ function DefaultFallbackComponent(_a) {
113
+ var state = _a.state;
114
+ return (react_1.default.createElement("p", null, state === 'error'
115
+ ? 'SDK connection error, check logs'
116
+ : 'Waiting for connection'));
117
+ }
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import type { FormSidebarExtensionProps, FieldExtensionProps as FieldExtensionPropsBase, AppInstallation } from '@hygraph/app-sdk';
3
+ export declare function useFieldExtension(): FieldExtensionProps;
4
+ export declare function useFormSidebarExtension(): FormSidebarExtensionProps;
5
+ export declare function useUiExtensionDialog<DialogReturn = any, DialogProps = Record<string, unknown>>(): {
6
+ onCloseDialog: (value?: DialogReturn | null) => void;
7
+ } & DialogProps;
8
+ export declare function useApp(): {
9
+ readonly useUpdateInstallation: () => readonly [(arg: Partial<Pick<AppInstallation, "config" | "status">>) => Promise<AppInstallation>, {
10
+ readonly data: AppInstallation | undefined;
11
+ readonly error: string;
12
+ readonly loading: boolean;
13
+ }];
14
+ readonly updateInstallation: (arg: Partial<Pick<AppInstallation, "config" | "status">>) => Promise<AppInstallation>;
15
+ readonly installation: AppInstallation;
16
+ readonly context: import("@hygraph/app-sdk").Context;
17
+ readonly openDialog: import("@hygraph/app-sdk/dist/type-helpers/dialog").OpenDialog;
18
+ readonly showToast: import("@hygraph/app-sdk/dist/type-helpers/toast").ShowToast;
19
+ readonly openAssetPicker: import("@hygraph/app-sdk/dist/type-helpers/openAssetPicker").OpenAssetPicker;
20
+ readonly redirectParent: (location: string | Location) => Promise<void>;
21
+ readonly historyReplace: (url: string | ((href: string) => string)) => Promise<void>;
22
+ readonly historyPush: (url: string | ((href: string) => string)) => Promise<void>;
23
+ };
24
+ export interface FieldExtensionProps extends Omit<FieldExtensionPropsBase, 'onBlur' | 'onChange' | 'onFocus'> {
25
+ onBlur: <T extends HTMLElement = HTMLElement>(event?: React.FocusEvent<T>) => Promise<void>;
26
+ onChange: <T extends HTMLElement = HTMLElement>(event: React.ChangeEvent<T> | any) => Promise<void>;
27
+ onFocus: <T extends HTMLElement = HTMLElement>(event?: React.FocusEvent<T>) => Promise<void>;
28
+ }
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
29
+ var __importDefault = (this && this.__importDefault) || function (mod) {
30
+ return (mod && mod.__esModule) ? mod : { "default": mod };
31
+ };
32
+ Object.defineProperty(exports, "__esModule", { value: true });
33
+ exports.useApp = exports.useUiExtensionDialog = exports.useFormSidebarExtension = exports.useFieldExtension = void 0;
34
+ var react_1 = __importDefault(require("react"));
35
+ var Wrapper_1 = require("./Wrapper");
36
+ function useFieldExtension() {
37
+ return (0, Wrapper_1.useWrapperContext)();
38
+ }
39
+ exports.useFieldExtension = useFieldExtension;
40
+ function useFormSidebarExtension() {
41
+ return (0, Wrapper_1.useWrapperContext)();
42
+ }
43
+ exports.useFormSidebarExtension = useFormSidebarExtension;
44
+ function useUiExtensionDialog() {
45
+ return (0, Wrapper_1.useWrapperContext)();
46
+ }
47
+ exports.useUiExtensionDialog = useUiExtensionDialog;
48
+ function useApp() {
49
+ var appProps = (0, Wrapper_1.useWrapperContext)();
50
+ var installProps = useUpdateInstallation(appProps === null || appProps === void 0 ? void 0 : appProps.updateInstallation);
51
+ return __assign(__assign({}, appProps), { useUpdateInstallation: function () { return installProps; } });
52
+ }
53
+ exports.useApp = useApp;
54
+ function useUpdateInstallation(installApp) {
55
+ var _a = __read(react_1.default.useState(false), 2), loading = _a[0], setLoading = _a[1];
56
+ var _b = __read(react_1.default.useState(''), 2), error = _b[0], setError = _b[1];
57
+ var _c = __read(react_1.default.useState(), 2), data = _c[0], setData = _c[1];
58
+ var executeInstall = function (args) {
59
+ setLoading(true);
60
+ setError('');
61
+ return installApp(args)
62
+ .then(function (installation) {
63
+ setData(installation);
64
+ setLoading(false);
65
+ return installation;
66
+ })
67
+ .catch(function (error) {
68
+ setError(error);
69
+ setLoading(false);
70
+ return error;
71
+ });
72
+ };
73
+ return [executeInstall, { data: data, error: error, loading: loading }];
74
+ }
@@ -0,0 +1,3 @@
1
+ export * from '@hygraph/app-sdk';
2
+ export { Wrapper } from './Wrapper';
3
+ export { useApp, useFieldExtension, useFormSidebarExtension, useUiExtensionDialog, } from './hooks';
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.useUiExtensionDialog = exports.useFormSidebarExtension = exports.useFieldExtension = exports.useApp = exports.Wrapper = void 0;
18
+ __exportStar(require("@hygraph/app-sdk"), exports);
19
+ var Wrapper_1 = require("./Wrapper");
20
+ Object.defineProperty(exports, "Wrapper", { enumerable: true, get: function () { return Wrapper_1.Wrapper; } });
21
+ var hooks_1 = require("./hooks");
22
+ Object.defineProperty(exports, "useApp", { enumerable: true, get: function () { return hooks_1.useApp; } });
23
+ Object.defineProperty(exports, "useFieldExtension", { enumerable: true, get: function () { return hooks_1.useFieldExtension; } });
24
+ Object.defineProperty(exports, "useFormSidebarExtension", { enumerable: true, get: function () { return hooks_1.useFormSidebarExtension; } });
25
+ Object.defineProperty(exports, "useUiExtensionDialog", { enumerable: true, get: function () { return hooks_1.useUiExtensionDialog; } });
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import { AppProps } from '@hygraph/app-sdk';
3
+ import type { ExtensionDialogProps, FormSidebarExtensionProps } from '@hygraph/app-sdk';
4
+ import type { FieldExtensionProps } from './hooks';
5
+ type WrapperContext = AppProps | FieldExtensionProps | FormSidebarExtensionProps | (ExtensionDialogProps & Record<string, unknown>);
6
+ type SDKState = 'init' | 'error' | 'ok' | 'validation';
7
+ declare const WrapperContext: React.Context<WrapperContext>;
8
+ type WrapperProps = {
9
+ uid?: string;
10
+ fallback?: React.FC<{
11
+ state: SDKState;
12
+ }>;
13
+ debug?: boolean;
14
+ } & {
15
+ children: React.ReactNode;
16
+ };
17
+ export declare function Wrapper(props: WrapperProps): JSX.Element;
18
+ export declare function useWrapperContext(): WrapperContext;
19
+ export {};
@@ -0,0 +1,109 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ var __read = (this && this.__read) || function (o, n) {
38
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
39
+ if (!m) return o;
40
+ var i = m.call(o), r, ar = [], e;
41
+ try {
42
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
43
+ }
44
+ catch (error) { e = { error: error }; }
45
+ finally {
46
+ try {
47
+ if (r && !r.done && (m = i["return"])) m.call(i);
48
+ }
49
+ finally { if (e) throw e.error; }
50
+ }
51
+ return ar;
52
+ };
53
+ import React from 'react';
54
+ import { init } from '@hygraph/app-sdk';
55
+ var WrapperContext = React.createContext({});
56
+ function useInitSDK(uid, debug) {
57
+ if (debug === void 0) { debug = false; }
58
+ var _a = __read(React.useState(), 2), sdkProps = _a[0], setSdkProps = _a[1];
59
+ var _b = __read(React.useState('init'), 2), extensionState = _b[0], setExtensionState = _b[1];
60
+ React.useEffect(function () {
61
+ function initSdk() {
62
+ return __awaiter(this, void 0, void 0, function () {
63
+ var initialState, error_1;
64
+ return __generator(this, function (_a) {
65
+ switch (_a.label) {
66
+ case 0:
67
+ _a.trys.push([0, 2, , 3]);
68
+ return [4 /*yield*/, init({
69
+ uid: uid,
70
+ debug: debug,
71
+ onProps: setSdkProps,
72
+ })];
73
+ case 1:
74
+ initialState = _a.sent();
75
+ if (initialState.status === 'ok') {
76
+ setSdkProps(initialState.props);
77
+ }
78
+ setExtensionState(initialState.status);
79
+ return [3 /*break*/, 3];
80
+ case 2:
81
+ error_1 = _a.sent();
82
+ setExtensionState('error');
83
+ console.error(error_1);
84
+ return [3 /*break*/, 3];
85
+ case 3: return [2 /*return*/];
86
+ }
87
+ });
88
+ });
89
+ }
90
+ initSdk();
91
+ }, [uid, debug]);
92
+ return { sdkProps: sdkProps, extensionState: extensionState };
93
+ }
94
+ export function Wrapper(props) {
95
+ var children = props.children, uid = props.uid, _a = props.fallback, FallbackComponent = _a === void 0 ? DefaultFallbackComponent : _a, debug = props.debug;
96
+ var _b = useInitSDK(uid, debug), sdkProps = _b.sdkProps, extensionState = _b.extensionState;
97
+ if (extensionState != 'ok' || !sdkProps)
98
+ return React.createElement(FallbackComponent, { state: extensionState });
99
+ return React.createElement(WrapperContext.Provider, { value: sdkProps, children: children });
100
+ }
101
+ export function useWrapperContext() {
102
+ return React.useContext(WrapperContext);
103
+ }
104
+ function DefaultFallbackComponent(_a) {
105
+ var state = _a.state;
106
+ return (React.createElement("p", null, state === 'error'
107
+ ? 'SDK connection error, check logs'
108
+ : 'Waiting for connection'));
109
+ }
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import type { FormSidebarExtensionProps, FieldExtensionProps as FieldExtensionPropsBase, AppInstallation } from '@hygraph/app-sdk';
3
+ export declare function useFieldExtension(): FieldExtensionProps;
4
+ export declare function useFormSidebarExtension(): FormSidebarExtensionProps;
5
+ export declare function useUiExtensionDialog<DialogReturn = any, DialogProps = Record<string, unknown>>(): {
6
+ onCloseDialog: (value?: DialogReturn | null) => void;
7
+ } & DialogProps;
8
+ export declare function useApp(): {
9
+ readonly useUpdateInstallation: () => readonly [(arg: Partial<Pick<AppInstallation, "config" | "status">>) => Promise<AppInstallation>, {
10
+ readonly data: AppInstallation | undefined;
11
+ readonly error: string;
12
+ readonly loading: boolean;
13
+ }];
14
+ readonly updateInstallation: (arg: Partial<Pick<AppInstallation, "config" | "status">>) => Promise<AppInstallation>;
15
+ readonly installation: AppInstallation;
16
+ readonly context: import("@hygraph/app-sdk").Context;
17
+ readonly openDialog: import("@hygraph/app-sdk/dist/type-helpers/dialog").OpenDialog;
18
+ readonly showToast: import("@hygraph/app-sdk/dist/type-helpers/toast").ShowToast;
19
+ readonly openAssetPicker: import("@hygraph/app-sdk/dist/type-helpers/openAssetPicker").OpenAssetPicker;
20
+ readonly redirectParent: (location: string | Location) => Promise<void>;
21
+ readonly historyReplace: (url: string | ((href: string) => string)) => Promise<void>;
22
+ readonly historyPush: (url: string | ((href: string) => string)) => Promise<void>;
23
+ };
24
+ export interface FieldExtensionProps extends Omit<FieldExtensionPropsBase, 'onBlur' | 'onChange' | 'onFocus'> {
25
+ onBlur: <T extends HTMLElement = HTMLElement>(event?: React.FocusEvent<T>) => Promise<void>;
26
+ onChange: <T extends HTMLElement = HTMLElement>(event: React.ChangeEvent<T> | any) => Promise<void>;
27
+ onFocus: <T extends HTMLElement = HTMLElement>(event?: React.FocusEvent<T>) => Promise<void>;
28
+ }
@@ -0,0 +1,64 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __read = (this && this.__read) || function (o, n) {
13
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
14
+ if (!m) return o;
15
+ var i = m.call(o), r, ar = [], e;
16
+ try {
17
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
18
+ }
19
+ catch (error) { e = { error: error }; }
20
+ finally {
21
+ try {
22
+ if (r && !r.done && (m = i["return"])) m.call(i);
23
+ }
24
+ finally { if (e) throw e.error; }
25
+ }
26
+ return ar;
27
+ };
28
+ import React from 'react';
29
+ import { useWrapperContext } from './Wrapper';
30
+ export function useFieldExtension() {
31
+ return useWrapperContext();
32
+ }
33
+ export function useFormSidebarExtension() {
34
+ return useWrapperContext();
35
+ }
36
+ export function useUiExtensionDialog() {
37
+ return useWrapperContext();
38
+ }
39
+ export function useApp() {
40
+ var appProps = useWrapperContext();
41
+ var installProps = useUpdateInstallation(appProps === null || appProps === void 0 ? void 0 : appProps.updateInstallation);
42
+ return __assign(__assign({}, appProps), { useUpdateInstallation: function () { return installProps; } });
43
+ }
44
+ function useUpdateInstallation(installApp) {
45
+ var _a = __read(React.useState(false), 2), loading = _a[0], setLoading = _a[1];
46
+ var _b = __read(React.useState(''), 2), error = _b[0], setError = _b[1];
47
+ var _c = __read(React.useState(), 2), data = _c[0], setData = _c[1];
48
+ var executeInstall = function (args) {
49
+ setLoading(true);
50
+ setError('');
51
+ return installApp(args)
52
+ .then(function (installation) {
53
+ setData(installation);
54
+ setLoading(false);
55
+ return installation;
56
+ })
57
+ .catch(function (error) {
58
+ setError(error);
59
+ setLoading(false);
60
+ return error;
61
+ });
62
+ };
63
+ return [executeInstall, { data: data, error: error, loading: loading }];
64
+ }
@@ -0,0 +1,3 @@
1
+ export * from '@hygraph/app-sdk';
2
+ export { Wrapper } from './Wrapper';
3
+ export { useApp, useFieldExtension, useFormSidebarExtension, useUiExtensionDialog, } from './hooks';
@@ -0,0 +1,3 @@
1
+ export * from '@hygraph/app-sdk';
2
+ export { Wrapper } from './Wrapper';
3
+ export { useApp, useFieldExtension, useFormSidebarExtension, useUiExtensionDialog, } from './hooks';
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import type { FormSidebarExtensionProps, FieldExtensionProps as FieldExtensionPropsBase, AppInstallation } from '@hygraph/app-sdk';
3
+ export declare function useFieldExtension(): FieldExtensionProps;
4
+ export declare function useFormSidebarExtension(): FormSidebarExtensionProps;
5
+ export declare function useUiExtensionDialog<DialogReturn = any, DialogProps = Record<string, unknown>>(): {
6
+ onCloseDialog: (value?: DialogReturn | null) => void;
7
+ } & DialogProps;
8
+ export declare function useApp(): {
9
+ readonly useUpdateInstallation: () => readonly [(arg: Partial<Pick<AppInstallation, "config" | "status">>) => Promise<AppInstallation>, {
10
+ readonly data: AppInstallation | undefined;
11
+ readonly error: string;
12
+ readonly loading: boolean;
13
+ }];
14
+ readonly updateInstallation: (arg: Partial<Pick<AppInstallation, "config" | "status">>) => Promise<AppInstallation>;
15
+ readonly installation: AppInstallation;
16
+ readonly context: import("@hygraph/app-sdk").Context;
17
+ readonly openDialog: import("@hygraph/app-sdk/dist/type-helpers/dialog").OpenDialog;
18
+ readonly showToast: import("@hygraph/app-sdk/dist/type-helpers/toast").ShowToast;
19
+ readonly openAssetPicker: import("@hygraph/app-sdk/dist/type-helpers/openAssetPicker").OpenAssetPicker;
20
+ readonly redirectParent: (location: string | Location) => Promise<void>;
21
+ readonly historyReplace: (url: string | ((href: string) => string)) => Promise<void>;
22
+ readonly historyPush: (url: string | ((href: string) => string)) => Promise<void>;
23
+ };
24
+ export interface FieldExtensionProps extends Omit<FieldExtensionPropsBase, 'onBlur' | 'onChange' | 'onFocus'> {
25
+ onBlur: <T extends HTMLElement = HTMLElement>(event?: React.FocusEvent<T>) => Promise<void>;
26
+ onChange: <T extends HTMLElement = HTMLElement>(event: React.ChangeEvent<T> | any) => Promise<void>;
27
+ onFocus: <T extends HTMLElement = HTMLElement>(event?: React.FocusEvent<T>) => Promise<void>;
28
+ }
@@ -0,0 +1,3 @@
1
+ export * from '@hygraph/app-sdk';
2
+ export { Wrapper } from './Wrapper';
3
+ export { useApp, useFieldExtension, useFormSidebarExtension, useUiExtensionDialog, } from './hooks';
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@hygraph/app-sdk-react",
3
+ "license": "MIT",
4
+ "version": "0.0.2",
5
+ "author": "Hygraph",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "main": "dist/cjs/index.js",
10
+ "module": "dist/esm/index.js",
11
+ "types": "dist/index.d.ts",
12
+ "scripts": {
13
+ "build": "npm run build:types && npm run build:esm && npm run build:cjs",
14
+ "build:types": "tsc --emitDeclarationOnly",
15
+ "build:esm": "tsc --target es5 --module es2015 --outDir dist/esm",
16
+ "build:cjs": "tsc --target es5 --module commonjs --outDir dist/cjs",
17
+ "prepare": "npm run build",
18
+ "test": "echo \"Error: no test specified\" && exit 1"
19
+ },
20
+ "dependencies": {
21
+ "@graphcms/zoid": "^9.0.64-alpha.3",
22
+ "@hygraph/app-sdk": "0.0.2"
23
+ },
24
+ "devDependencies": {
25
+ "@types/react": "^17.0.2",
26
+ "react": "^17.0.1",
27
+ "typescript": "^4.1.5"
28
+ },
29
+ "gitHead": "77d8975601e7f944471fede0c200ffd7779dda05"
30
+ }
@@ -0,0 +1,83 @@
1
+ import React from 'react';
2
+ import { init, AppProps } from '@hygraph/app-sdk';
3
+ import type {
4
+ ExtensionDialogProps,
5
+ FormSidebarExtensionProps,
6
+ } from '@hygraph/app-sdk';
7
+ import type { FieldExtensionProps } from './hooks';
8
+
9
+ type WrapperContext =
10
+ | AppProps
11
+ | FieldExtensionProps
12
+ | FormSidebarExtensionProps
13
+ | (ExtensionDialogProps & Record<string, unknown>);
14
+
15
+ type SDKState = 'init' | 'error' | 'ok' | 'validation';
16
+
17
+ const WrapperContext = React.createContext<WrapperContext>(
18
+ {} as WrapperContext
19
+ );
20
+
21
+ type WrapperProps = {
22
+ uid?: string;
23
+ fallback?: React.FC<{ state: SDKState }>;
24
+ debug?: boolean;
25
+ } & { children: React.ReactNode };
26
+
27
+ function useInitSDK<T>(uid?: string, debug: boolean = false) {
28
+ const [sdkProps, setSdkProps] = React.useState<T>();
29
+ const [extensionState, setExtensionState] = React.useState<SDKState>('init');
30
+
31
+ React.useEffect(() => {
32
+ async function initSdk() {
33
+ try {
34
+ const initialState = await init({
35
+ uid,
36
+ debug,
37
+ onProps: setSdkProps,
38
+ });
39
+ if (initialState.status === 'ok') {
40
+ setSdkProps(initialState.props);
41
+ }
42
+ setExtensionState(initialState.status);
43
+ } catch (error) {
44
+ setExtensionState('error');
45
+ console.error(error);
46
+ }
47
+ }
48
+
49
+ initSdk();
50
+ }, [uid, debug]);
51
+
52
+ return { sdkProps, extensionState };
53
+ }
54
+
55
+ export function Wrapper(props: WrapperProps) {
56
+ const {
57
+ children,
58
+ uid,
59
+ fallback: FallbackComponent = DefaultFallbackComponent,
60
+ debug,
61
+ } = props;
62
+
63
+ const { sdkProps, extensionState } = useInitSDK<WrapperContext>(uid, debug);
64
+
65
+ if (extensionState != 'ok' || !sdkProps)
66
+ return <FallbackComponent state={extensionState} />;
67
+
68
+ return <WrapperContext.Provider value={sdkProps} children={children} />;
69
+ }
70
+
71
+ export function useWrapperContext() {
72
+ return React.useContext(WrapperContext);
73
+ }
74
+
75
+ function DefaultFallbackComponent({ state }: { state: SDKState }) {
76
+ return (
77
+ <p>
78
+ {state === 'error'
79
+ ? 'SDK connection error, check logs'
80
+ : 'Waiting for connection'}
81
+ </p>
82
+ );
83
+ }
@@ -0,0 +1 @@
1
+ declare module "@graphcms/zoid";
package/src/hooks.ts ADDED
@@ -0,0 +1,72 @@
1
+ import React from 'react';
2
+ import { useWrapperContext } from './Wrapper';
3
+ import type {
4
+ FormSidebarExtensionProps,
5
+ AppProps,
6
+ FieldExtensionProps as FieldExtensionPropsBase,
7
+ AppInstallation,
8
+ } from '@hygraph/app-sdk';
9
+
10
+ export function useFieldExtension() {
11
+ return useWrapperContext() as FieldExtensionProps;
12
+ }
13
+
14
+ export function useFormSidebarExtension() {
15
+ return useWrapperContext() as FormSidebarExtensionProps;
16
+ }
17
+
18
+ export function useUiExtensionDialog<
19
+ DialogReturn = any,
20
+ DialogProps = Record<string, unknown>
21
+ >() {
22
+ return useWrapperContext() as {
23
+ onCloseDialog: (value?: DialogReturn | null) => void;
24
+ } & DialogProps;
25
+ }
26
+
27
+ export function useApp() {
28
+ const appProps = useWrapperContext() as AppProps;
29
+
30
+ const installProps = useUpdateInstallation(appProps?.updateInstallation);
31
+ return { ...appProps, useUpdateInstallation: () => installProps } as const;
32
+ }
33
+
34
+ function useUpdateInstallation(installApp: AppProps['updateInstallation']) {
35
+ const [loading, setLoading] = React.useState(false);
36
+ const [error, setError] = React.useState('');
37
+ const [data, setData] = React.useState<AppInstallation>();
38
+
39
+ const executeInstall: AppProps['updateInstallation'] = args => {
40
+ setLoading(true);
41
+ setError('');
42
+
43
+ return installApp(args)
44
+ .then(installation => {
45
+ setData(installation);
46
+ setLoading(false);
47
+ return installation;
48
+ })
49
+ .catch(error => {
50
+ setError(error);
51
+ setLoading(false);
52
+ return error;
53
+ });
54
+ };
55
+
56
+ return [executeInstall, { data, error, loading }] as const;
57
+ }
58
+
59
+ export interface FieldExtensionProps
60
+ extends Omit<FieldExtensionPropsBase, 'onBlur' | 'onChange' | 'onFocus'> {
61
+ onBlur: <T extends HTMLElement = HTMLElement>(
62
+ event?: React.FocusEvent<T>
63
+ ) => Promise<void>;
64
+
65
+ onChange: <T extends HTMLElement = HTMLElement>(
66
+ event: React.ChangeEvent<T> | any
67
+ ) => Promise<void>;
68
+
69
+ onFocus: <T extends HTMLElement = HTMLElement>(
70
+ event?: React.FocusEvent<T>
71
+ ) => Promise<void>;
72
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,8 @@
1
+ export * from '@hygraph/app-sdk';
2
+ export { Wrapper } from './Wrapper';
3
+ export {
4
+ useApp,
5
+ useFieldExtension,
6
+ useFormSidebarExtension,
7
+ useUiExtensionDialog,
8
+ } from './hooks';
package/tsconfig.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
+
5
+ /* Basic Options */
6
+ // "incremental": true, /* Enable incremental compilation */
7
+ "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
8
+ "module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
9
+ "lib": ["esnext", "dom"], /* Specify library files to be included in the compilation. */
10
+ "allowJs": true, /* Allow javascript files to be compiled. */
11
+ // "checkJs": true, /* Report errors in .js files. */
12
+ "jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
13
+ "declaration": true, /* Generates corresponding '.d.ts' file. */
14
+ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15
+ // "sourceMap": true, /* Generates corresponding '.map' file. */
16
+ // "outFile": "./", /* Concatenate and emit output to single file. */
17
+ "outDir": "./dist", /* Redirect output structure to the directory. */
18
+ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
19
+ // "composite": true, /* Enable project compilation */
20
+ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
21
+ // "removeComments": true, /* Do not emit comments to output. */
22
+ // "noEmit": true, /* Do not emit outputs. */
23
+ // "importHelpers": true, /* Import emit helpers from 'tslib'. */
24
+ "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
25
+ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
26
+
27
+ /* Strict Type-Checking Options */
28
+ "strict": true, /* Enable all strict type-checking options. */
29
+ "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
30
+ // "strictNullChecks": true, /* Enable strict null checks. */
31
+ // "strictFunctionTypes": true, /* Enable strict checking of function types. */
32
+ // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
33
+ // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
34
+ // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
35
+ // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
36
+
37
+ /* Additional Checks */
38
+ // "noUnusedLocals": true, /* Report errors on unused locals. */
39
+ // "noUnusedParameters": true, /* Report errors on unused parameters. */
40
+ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
41
+ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
42
+ // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
43
+
44
+ /* Module Resolution Options */
45
+ "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
46
+ // "baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
47
+ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
48
+ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
49
+ // "typeRoots": [], /* List of folders to include type definitions from. */
50
+ // "types": [], /* Type declaration files to be included in compilation. */
51
+ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
52
+ "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
53
+ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
54
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
55
+
56
+ /* Source Map Options */
57
+ // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
58
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
59
+ // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
60
+ // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
61
+
62
+ /* Experimental Options */
63
+ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
64
+ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
65
+
66
+ /* Advanced Options */
67
+ "skipLibCheck": true, /* Skip type checking of declaration files. */
68
+ "forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
69
+ "importsNotUsedAsValues": "error"
70
+ },
71
+ "include": ["./src"],
72
+ "exclude": ["node_modules", "dist"]
73
+ }