jazz-react-native 0.8.51 → 0.9.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +19 -0
- package/README.md +1 -1
- package/dist/auth/DemoAuthMethod.d.ts +1 -0
- package/dist/auth/DemoAuthMethod.js +17 -4
- package/dist/auth/DemoAuthMethod.js.map +1 -1
- package/dist/auth/DemoAuthUI.js +11 -6
- package/dist/auth/DemoAuthUI.js.map +1 -1
- package/dist/hooks.d.ts +10056 -0
- package/dist/hooks.js +36 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +2 -49
- package/dist/index.js +2 -128
- package/dist/index.js.map +1 -1
- package/dist/platform.d.ts +51 -0
- package/dist/platform.js +100 -0
- package/dist/platform.js.map +1 -0
- package/dist/provider.d.ts +13 -45
- package/dist/provider.js +52 -122
- package/dist/provider.js.map +1 -1
- package/dist/storage/kv-store-context.d.ts +1 -0
- package/dist/storage/kv-store-context.js +3 -0
- package/dist/storage/kv-store-context.js.map +1 -1
- package/dist/testing.d.ts +1 -0
- package/dist/testing.js +2 -0
- package/dist/testing.js.map +1 -0
- package/dist/tests/DemoAuthMethod.test.js +28 -7
- package/dist/tests/DemoAuthMethod.test.js.map +1 -1
- package/package.json +10 -5
- package/src/auth/DemoAuthMethod.ts +24 -4
- package/src/auth/DemoAuthUI.tsx +13 -6
- package/src/hooks.tsx +60 -0
- package/src/index.ts +3 -240
- package/src/platform.ts +205 -0
- package/src/provider.tsx +77 -282
- package/src/storage/kv-store-context.ts +4 -0
- package/src/testing.tsx +1 -0
- package/src/tests/DemoAuthMethod.test.ts +34 -7
package/dist/hooks.js
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
import { useEffect } from "react";
|
2
|
+
import { createUseAccountHooks, useJazzContext } from "jazz-react-core";
|
3
|
+
import { Linking } from "react-native";
|
4
|
+
import { parseInviteLink } from "./platform.js";
|
5
|
+
export { useCoState, experimental_useInboxSender } from "jazz-react-core";
|
6
|
+
export const { useAccount, useAccountOrGuest } = createUseAccountHooks();
|
7
|
+
export function useAcceptInvite({ invitedObjectSchema, onAccept, forValueHint, }) {
|
8
|
+
const context = useJazzContext();
|
9
|
+
if (!("me" in context)) {
|
10
|
+
throw new Error("useAcceptInvite can't be used in a JazzProvider with auth === 'guest'.");
|
11
|
+
}
|
12
|
+
useEffect(() => {
|
13
|
+
const handleDeepLink = ({ url }) => {
|
14
|
+
const result = parseInviteLink(url);
|
15
|
+
if (result && result.valueHint === forValueHint) {
|
16
|
+
context.me
|
17
|
+
.acceptInvite(result.valueID, result.inviteSecret, invitedObjectSchema)
|
18
|
+
.then(() => {
|
19
|
+
onAccept(result.valueID);
|
20
|
+
})
|
21
|
+
.catch((e) => {
|
22
|
+
console.error("Failed to accept invite", e);
|
23
|
+
});
|
24
|
+
}
|
25
|
+
};
|
26
|
+
const linkingListener = Linking.addEventListener("url", handleDeepLink);
|
27
|
+
void Linking.getInitialURL().then((url) => {
|
28
|
+
if (url)
|
29
|
+
handleDeepLink({ url });
|
30
|
+
});
|
31
|
+
return () => {
|
32
|
+
linkingListener.remove();
|
33
|
+
};
|
34
|
+
}, [context, onAccept, invitedObjectSchema, forValueHint]);
|
35
|
+
}
|
36
|
+
//# sourceMappingURL=hooks.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAElC,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAExE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAGhD,OAAO,EAAE,UAAU,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AAE1E,MAAM,CAAC,MAAM,EAAE,UAAU,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,EAAE,mBAAmB,EAAE,YAAY,CAAC,CAAC,CAAC;AAC7D,CAAC"}
|
package/dist/index.d.ts
CHANGED
@@ -1,52 +1,5 @@
|
|
1
|
-
import { Account, AgentID, AnonymousJazzAgent, AuthMethod, CoValue, CoValueClass, CryptoProvider, ID, InviteSecret, SessionID } from "jazz-tools";
|
2
|
-
import { RawAccountID } from "cojson";
|
3
|
-
export { RNDemoAuth } from "./auth/DemoAuthMethod.js";
|
4
|
-
import { PureJSCrypto } from "cojson/native";
|
5
|
-
import type { RNQuickCrypto } from "./crypto/RNQuickCrypto.js";
|
6
|
-
/** @category Context Creation */
|
7
|
-
export type ReactNativeContext<Acc extends Account> = {
|
8
|
-
me: Acc;
|
9
|
-
logOut: () => void;
|
10
|
-
done: () => void;
|
11
|
-
};
|
12
|
-
export type ReactNativeGuestContext = {
|
13
|
-
guest: AnonymousJazzAgent;
|
14
|
-
logOut: () => void;
|
15
|
-
done: () => void;
|
16
|
-
};
|
17
|
-
export type ReactNativeContextOptions<Acc extends Account> = {
|
18
|
-
auth: AuthMethod;
|
19
|
-
AccountSchema: CoValueClass<Acc> & {
|
20
|
-
fromNode: (typeof Account)["fromNode"];
|
21
|
-
};
|
22
|
-
} & BaseReactNativeContextOptions;
|
23
|
-
export type BaseReactNativeContextOptions = {
|
24
|
-
peer: `wss://${string}` | `ws://${string}`;
|
25
|
-
reconnectionTimeout?: number;
|
26
|
-
storage?: "indexedDB" | "singleTabOPFS";
|
27
|
-
CryptoProvider?: typeof PureJSCrypto | typeof RNQuickCrypto;
|
28
|
-
};
|
29
|
-
/** @category Context Creation */
|
30
|
-
export declare function createJazzRNContext<Acc extends Account>(options: ReactNativeContextOptions<Acc>): Promise<ReactNativeContext<Acc>>;
|
31
|
-
export declare function createJazzRNContext(options: BaseReactNativeContextOptions): Promise<ReactNativeGuestContext>;
|
32
|
-
export declare function createJazzRNContext<Acc extends Account>(options: ReactNativeContextOptions<Acc> | BaseReactNativeContextOptions): Promise<ReactNativeContext<Acc> | ReactNativeGuestContext>;
|
33
|
-
/** @category Auth Providers */
|
34
|
-
export type SessionProvider = (accountID: ID<Account> | AgentID) => Promise<SessionID>;
|
35
|
-
export declare function provideLockSession(accountID: ID<Account> | AgentID, crypto: CryptoProvider): Promise<{
|
36
|
-
sessionID: `${RawAccountID}_session_z${string}` | `sealer_z${string}/signer_z${string}_session_z${string}`;
|
37
|
-
sessionDone: () => void;
|
38
|
-
}>;
|
39
|
-
/** @category Invite Links */
|
40
|
-
export declare function createInviteLink<C extends CoValue>(value: C, role: "reader" | "writer" | "admin", { baseURL, valueHint }?: {
|
41
|
-
baseURL?: string;
|
42
|
-
valueHint?: string;
|
43
|
-
}): string;
|
44
|
-
/** @category Invite Links */
|
45
|
-
export declare function parseInviteLink<C extends CoValue>(inviteURL: string): {
|
46
|
-
valueID: ID<C>;
|
47
|
-
valueHint?: string;
|
48
|
-
inviteSecret: InviteSecret;
|
49
|
-
} | undefined;
|
50
1
|
export * from "./provider.js";
|
51
2
|
export * from "./auth/auth.js";
|
52
3
|
export * from "./storage/kv-store-context.js";
|
4
|
+
export * from "./hooks.js";
|
5
|
+
export { parseInviteLink, createInviteLink, setupKvStore } from "./platform.js";
|
package/dist/index.js
CHANGED
@@ -1,132 +1,6 @@
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
3
|
-
import { cojsonInternals, createJazzContext, } from "jazz-tools";
|
4
|
-
export { RNDemoAuth } from "./auth/DemoAuthMethod.js";
|
5
|
-
import { PureJSCrypto } from "cojson/native";
|
6
|
-
import { createWebSocketPeerWithReconnection } from "./createWebSocketPeerWithReconnection.js";
|
7
|
-
import { KvStoreContext } from "./storage/kv-store-context.js";
|
8
|
-
export async function createJazzRNContext(options) {
|
9
|
-
const websocketPeer = createWebSocketPeerWithReconnection(options.peer, options.reconnectionTimeout, (peer) => {
|
10
|
-
node.syncManager.addPeer(peer);
|
11
|
-
});
|
12
|
-
const CryptoProvider = options.CryptoProvider || PureJSCrypto;
|
13
|
-
const context = "auth" in options
|
14
|
-
? await createJazzContext({
|
15
|
-
AccountSchema: options.AccountSchema,
|
16
|
-
auth: options.auth,
|
17
|
-
crypto: await CryptoProvider.create(),
|
18
|
-
peersToLoadFrom: [websocketPeer.peer],
|
19
|
-
sessionProvider: provideLockSession,
|
20
|
-
})
|
21
|
-
: await createJazzContext({
|
22
|
-
crypto: await CryptoProvider.create(),
|
23
|
-
peersToLoadFrom: [websocketPeer.peer],
|
24
|
-
});
|
25
|
-
const node = "account" in context ? context.account._raw.core.node : context.agent.node;
|
26
|
-
return "account" in context
|
27
|
-
? {
|
28
|
-
me: context.account,
|
29
|
-
done: () => {
|
30
|
-
websocketPeer.done();
|
31
|
-
context.done();
|
32
|
-
},
|
33
|
-
logOut: () => {
|
34
|
-
context.logOut();
|
35
|
-
},
|
36
|
-
}
|
37
|
-
: {
|
38
|
-
guest: context.agent,
|
39
|
-
done: () => {
|
40
|
-
websocketPeer.done();
|
41
|
-
context.done();
|
42
|
-
},
|
43
|
-
logOut: () => {
|
44
|
-
context.logOut();
|
45
|
-
},
|
46
|
-
};
|
47
|
-
}
|
48
|
-
export async function provideLockSession(accountID, crypto) {
|
49
|
-
const sessionDone = () => { };
|
50
|
-
const kvStore = KvStoreContext.getInstance().getStorage();
|
51
|
-
const sessionID = (await kvStore.get(accountID)) ||
|
52
|
-
crypto.newRandomSessionID(accountID);
|
53
|
-
await kvStore.set(accountID, sessionID);
|
54
|
-
return Promise.resolve({
|
55
|
-
sessionID,
|
56
|
-
sessionDone,
|
57
|
-
});
|
58
|
-
}
|
59
|
-
/** @category Invite Links */
|
60
|
-
export function createInviteLink(value, role, { baseURL, valueHint } = {}) {
|
61
|
-
const coValueCore = value._raw.core;
|
62
|
-
let currentCoValue = coValueCore;
|
63
|
-
while (currentCoValue.header.ruleset.type === "ownedByGroup") {
|
64
|
-
currentCoValue = currentCoValue.getGroup().core;
|
65
|
-
}
|
66
|
-
if (currentCoValue.header.ruleset.type !== "group") {
|
67
|
-
throw new Error("Can't create invite link for object without group");
|
68
|
-
}
|
69
|
-
const group = cojsonInternals.expectGroup(currentCoValue.getCurrentContent());
|
70
|
-
const inviteSecret = group.createInvite(role);
|
71
|
-
return `${baseURL}/invite/${valueHint ? valueHint + "/" : ""}${value.id}/${inviteSecret}`;
|
72
|
-
}
|
73
|
-
/** @category Invite Links */
|
74
|
-
// TODO: copied from jazz-browser, should be shared
|
75
|
-
export function parseInviteLink(inviteURL) {
|
76
|
-
const url = new URL(inviteURL);
|
77
|
-
const parts = url.hash.split("/");
|
78
|
-
let valueHint;
|
79
|
-
let valueID;
|
80
|
-
let inviteSecret;
|
81
|
-
if (parts[0] === "#" && parts[1] === "invite") {
|
82
|
-
if (parts.length === 5) {
|
83
|
-
valueHint = parts[2];
|
84
|
-
valueID = parts[3];
|
85
|
-
inviteSecret = parts[4];
|
86
|
-
}
|
87
|
-
else if (parts.length === 4) {
|
88
|
-
valueID = parts[2];
|
89
|
-
inviteSecret = parts[3];
|
90
|
-
}
|
91
|
-
if (!valueID || !inviteSecret) {
|
92
|
-
return undefined;
|
93
|
-
}
|
94
|
-
return { valueID, inviteSecret, valueHint };
|
95
|
-
}
|
96
|
-
}
|
97
|
-
// getting out of the `expo` business 🤞
|
98
|
-
// export function parseInviteLink<C extends CoValue>(
|
99
|
-
// inviteURL: string,
|
100
|
-
// ):
|
101
|
-
// | {
|
102
|
-
// valueID: ID<C>;
|
103
|
-
// valueHint?: string;
|
104
|
-
// inviteSecret: InviteSecret;
|
105
|
-
// }
|
106
|
-
// | undefined {
|
107
|
-
// const url = Linking.parse(inviteURL);
|
108
|
-
// const parts = url.path?.split("/");
|
109
|
-
// if (!parts || parts[0] !== "invite") {
|
110
|
-
// return undefined;
|
111
|
-
// }
|
112
|
-
// let valueHint: string | undefined;
|
113
|
-
// let valueID: ID<C> | undefined;
|
114
|
-
// let inviteSecret: InviteSecret | undefined;
|
115
|
-
// if (parts.length === 4) {
|
116
|
-
// valueHint = parts[1];
|
117
|
-
// valueID = parts[2] as ID<C>;
|
118
|
-
// inviteSecret = parts[3] as InviteSecret;
|
119
|
-
// } else if (parts.length === 3) {
|
120
|
-
// valueID = parts[1] as ID<C>;
|
121
|
-
// inviteSecret = parts[2] as InviteSecret;
|
122
|
-
// }
|
123
|
-
// if (!valueID || !inviteSecret) {
|
124
|
-
// return undefined;
|
125
|
-
// }
|
126
|
-
// return { valueID, inviteSecret, valueHint };
|
127
|
-
// }
|
128
|
-
/////////
|
129
1
|
export * from "./provider.js";
|
130
2
|
export * from "./auth/auth.js";
|
131
3
|
export * from "./storage/kv-store-context.js";
|
4
|
+
export * from "./hooks.js";
|
5
|
+
export { parseInviteLink, createInviteLink, setupKvStore } from "./platform.js";
|
132
6
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,YAAY,CAAC;AAE3B,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC"}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
import { Account, AgentID, AnonymousJazzAgent, AuthMethod, CoValue, CoValueClass, CryptoProvider, ID, InviteSecret, SessionID } from "jazz-tools";
|
2
|
+
import { RawAccountID } from "cojson";
|
3
|
+
export { RNDemoAuth } from "./auth/DemoAuthMethod.js";
|
4
|
+
import { PureJSCrypto } from "cojson/native";
|
5
|
+
import type { RNQuickCrypto } from "./crypto/RNQuickCrypto.js";
|
6
|
+
import { ExpoSecureStoreAdapter } from "./storage/expo-secure-store-adapter.js";
|
7
|
+
/** @category Context Creation */
|
8
|
+
export type ReactNativeContext<Acc extends Account> = {
|
9
|
+
me: Acc;
|
10
|
+
logOut: () => void;
|
11
|
+
done: () => void;
|
12
|
+
};
|
13
|
+
export type ReactNativeGuestContext = {
|
14
|
+
guest: AnonymousJazzAgent;
|
15
|
+
logOut: () => void;
|
16
|
+
done: () => void;
|
17
|
+
};
|
18
|
+
export type ReactNativeContextOptions<Acc extends Account> = {
|
19
|
+
auth: AuthMethod;
|
20
|
+
AccountSchema: CoValueClass<Acc> & {
|
21
|
+
fromNode: (typeof Account)["fromNode"];
|
22
|
+
};
|
23
|
+
} & BaseReactNativeContextOptions;
|
24
|
+
export type BaseReactNativeContextOptions = {
|
25
|
+
peer: `wss://${string}` | `ws://${string}`;
|
26
|
+
reconnectionTimeout?: number;
|
27
|
+
storage?: "indexedDB" | "singleTabOPFS";
|
28
|
+
CryptoProvider?: typeof PureJSCrypto | typeof RNQuickCrypto;
|
29
|
+
};
|
30
|
+
/** @category Context Creation */
|
31
|
+
export declare function createJazzRNContext<Acc extends Account>(options: ReactNativeContextOptions<Acc>): Promise<ReactNativeContext<Acc>>;
|
32
|
+
export declare function createJazzRNContext(options: BaseReactNativeContextOptions): Promise<ReactNativeGuestContext>;
|
33
|
+
export declare function createJazzRNContext<Acc extends Account>(options: ReactNativeContextOptions<Acc> | BaseReactNativeContextOptions): Promise<ReactNativeContext<Acc> | ReactNativeGuestContext>;
|
34
|
+
/** @category Auth Providers */
|
35
|
+
export type SessionProvider = (accountID: ID<Account> | AgentID) => Promise<SessionID>;
|
36
|
+
export declare function provideLockSession(accountID: ID<Account> | AgentID, crypto: CryptoProvider): Promise<{
|
37
|
+
sessionID: `${RawAccountID}_session_z${string}` | `sealer_z${string}/signer_z${string}_session_z${string}`;
|
38
|
+
sessionDone: () => void;
|
39
|
+
}>;
|
40
|
+
/** @category Invite Links */
|
41
|
+
export declare function createInviteLink<C extends CoValue>(value: C, role: "reader" | "writer" | "admin", { baseURL, valueHint }?: {
|
42
|
+
baseURL?: string;
|
43
|
+
valueHint?: string;
|
44
|
+
}): string;
|
45
|
+
/** @category Invite Links */
|
46
|
+
export declare function parseInviteLink<C extends CoValue>(inviteURL: string): {
|
47
|
+
valueID: ID<C>;
|
48
|
+
valueHint?: string;
|
49
|
+
inviteSecret: InviteSecret;
|
50
|
+
} | undefined;
|
51
|
+
export declare function setupKvStore(kvStore?: ExpoSecureStoreAdapter): ExpoSecureStoreAdapter;
|
package/dist/platform.js
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
import { cojsonInternals, createJazzContext, } from "jazz-tools";
|
2
|
+
export { RNDemoAuth } from "./auth/DemoAuthMethod.js";
|
3
|
+
import { PureJSCrypto } from "cojson/native";
|
4
|
+
import { createWebSocketPeerWithReconnection } from "./createWebSocketPeerWithReconnection.js";
|
5
|
+
import { ExpoSecureStoreAdapter } from "./storage/expo-secure-store-adapter.js";
|
6
|
+
import { KvStoreContext } from "./storage/kv-store-context.js";
|
7
|
+
export async function createJazzRNContext(options) {
|
8
|
+
const websocketPeer = createWebSocketPeerWithReconnection(options.peer, options.reconnectionTimeout, (peer) => {
|
9
|
+
node.syncManager.addPeer(peer);
|
10
|
+
});
|
11
|
+
const CryptoProvider = options.CryptoProvider || PureJSCrypto;
|
12
|
+
const context = "auth" in options
|
13
|
+
? await createJazzContext({
|
14
|
+
AccountSchema: options.AccountSchema,
|
15
|
+
auth: options.auth,
|
16
|
+
crypto: await CryptoProvider.create(),
|
17
|
+
peersToLoadFrom: [websocketPeer.peer],
|
18
|
+
sessionProvider: provideLockSession,
|
19
|
+
})
|
20
|
+
: await createJazzContext({
|
21
|
+
crypto: await CryptoProvider.create(),
|
22
|
+
peersToLoadFrom: [websocketPeer.peer],
|
23
|
+
});
|
24
|
+
const node = "account" in context ? context.account._raw.core.node : context.agent.node;
|
25
|
+
return "account" in context
|
26
|
+
? {
|
27
|
+
me: context.account,
|
28
|
+
done: () => {
|
29
|
+
websocketPeer.done();
|
30
|
+
context.done();
|
31
|
+
},
|
32
|
+
logOut: () => {
|
33
|
+
context.logOut();
|
34
|
+
},
|
35
|
+
}
|
36
|
+
: {
|
37
|
+
guest: context.agent,
|
38
|
+
done: () => {
|
39
|
+
websocketPeer.done();
|
40
|
+
context.done();
|
41
|
+
},
|
42
|
+
logOut: () => {
|
43
|
+
context.logOut();
|
44
|
+
},
|
45
|
+
};
|
46
|
+
}
|
47
|
+
export async function provideLockSession(accountID, crypto) {
|
48
|
+
const sessionDone = () => { };
|
49
|
+
const kvStore = KvStoreContext.getInstance().getStorage();
|
50
|
+
const sessionID = (await kvStore.get(accountID)) ||
|
51
|
+
crypto.newRandomSessionID(accountID);
|
52
|
+
await kvStore.set(accountID, sessionID);
|
53
|
+
return Promise.resolve({
|
54
|
+
sessionID,
|
55
|
+
sessionDone,
|
56
|
+
});
|
57
|
+
}
|
58
|
+
/** @category Invite Links */
|
59
|
+
export function createInviteLink(value, role, { baseURL, valueHint } = {}) {
|
60
|
+
const coValueCore = value._raw.core;
|
61
|
+
let currentCoValue = coValueCore;
|
62
|
+
while (currentCoValue.header.ruleset.type === "ownedByGroup") {
|
63
|
+
currentCoValue = currentCoValue.getGroup().core;
|
64
|
+
}
|
65
|
+
if (currentCoValue.header.ruleset.type !== "group") {
|
66
|
+
throw new Error("Can't create invite link for object without group");
|
67
|
+
}
|
68
|
+
const group = cojsonInternals.expectGroup(currentCoValue.getCurrentContent());
|
69
|
+
const inviteSecret = group.createInvite(role);
|
70
|
+
return `${baseURL}/invite/${valueHint ? valueHint + "/" : ""}${value.id}/${inviteSecret}`;
|
71
|
+
}
|
72
|
+
/** @category Invite Links */
|
73
|
+
// TODO: copied from jazz-browser, should be shared
|
74
|
+
export function parseInviteLink(inviteURL) {
|
75
|
+
const url = new URL(inviteURL);
|
76
|
+
const parts = url.hash.split("/");
|
77
|
+
let valueHint;
|
78
|
+
let valueID;
|
79
|
+
let inviteSecret;
|
80
|
+
if (parts[0] === "#" && parts[1] === "invite") {
|
81
|
+
if (parts.length === 5) {
|
82
|
+
valueHint = parts[2];
|
83
|
+
valueID = parts[3];
|
84
|
+
inviteSecret = parts[4];
|
85
|
+
}
|
86
|
+
else if (parts.length === 4) {
|
87
|
+
valueID = parts[2];
|
88
|
+
inviteSecret = parts[3];
|
89
|
+
}
|
90
|
+
if (!valueID || !inviteSecret) {
|
91
|
+
return undefined;
|
92
|
+
}
|
93
|
+
return { valueID, inviteSecret, valueHint };
|
94
|
+
}
|
95
|
+
}
|
96
|
+
export function setupKvStore(kvStore = new ExpoSecureStoreAdapter()) {
|
97
|
+
KvStoreContext.getInstance().initialize(kvStore);
|
98
|
+
return kvStore;
|
99
|
+
}
|
100
|
+
//# sourceMappingURL=platform.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"platform.js","sourceRoot":"","sources":["../src/platform.ts"],"names":[],"mappings":"AAAA,OAAO,EAWL,eAAe,EACf,iBAAiB,GAClB,MAAM,YAAY,CAAC;AAIpB,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,mCAAmC,EAAE,MAAM,0CAA0C,CAAC;AAE/F,OAAO,EAAE,sBAAsB,EAAE,MAAM,wCAAwC,CAAC;AAChF,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAwC/D,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,OAAuE;IAEvE,MAAM,aAAa,GAAG,mCAAmC,CACvD,OAAO,CAAC,IAAI,EACZ,OAAO,CAAC,mBAAmB,EAC3B,CAAC,IAAI,EAAE,EAAE;QACP,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC,CACF,CAAC;IAEF,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,YAAY,CAAC;IAE9D,MAAM,OAAO,GACX,MAAM,IAAI,OAAO;QACf,CAAC,CAAC,MAAM,iBAAiB,CAAC;YACtB,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AAC,SAAS,CAAC,CAAC;IAC/B,MAAM,KAAK,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAElC,IAAI,SAA6B,CAAC;IAClC,IAAI,OAA0B,CAAC;IAC/B,IAAI,YAAsC,CAAC;IAE3C,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC9C,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,SAAS,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YACrB,OAAO,GAAG,KAAK,CAAC,CAAC,CAAU,CAAC;YAC5B,YAAY,GAAG,KAAK,CAAC,CAAC,CAAiB,CAAC;QAC1C,CAAC;aAAM,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,GAAG,KAAK,CAAC,CAAC,CAAU,CAAC;YAC5B,YAAY,GAAG,KAAK,CAAC,CAAC,CAAiB,CAAC;QAC1C,CAAC;QAED,IAAI,CAAC,OAAO,IAAI,CAAC,YAAY,EAAE,CAAC;YAC9B,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,CAAC;IAC9C,CAAC;AACH,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,OAAO,GAAG,IAAI,sBAAsB,EAAE;IACjE,cAAc,CAAC,WAAW,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAEjD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/provider.d.ts
CHANGED
@@ -1,48 +1,16 @@
|
|
1
|
-
import
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
5
|
-
export
|
6
|
-
|
1
|
+
import { Account, AccountClass, AuthMethod } from "jazz-tools";
|
2
|
+
import { BaseReactNativeContextOptions } from "./platform.js";
|
3
|
+
export interface Register {
|
4
|
+
}
|
5
|
+
export type RegisteredAccount = Register extends {
|
6
|
+
Account: infer Acc;
|
7
|
+
} ? Acc : Account;
|
8
|
+
export type JazzProviderProps<Acc extends Account = RegisteredAccount> = {
|
9
|
+
children: React.ReactNode;
|
10
|
+
auth: AuthMethod | "guest";
|
11
|
+
peer: `wss://${string}` | `ws://${string}`;
|
7
12
|
AccountSchema?: AccountClass<Acc>;
|
8
13
|
CryptoProvider?: BaseReactNativeContextOptions["CryptoProvider"];
|
9
|
-
}
|
14
|
+
};
|
10
15
|
/** @category Context & Hooks */
|
11
|
-
export
|
12
|
-
/** @category Provider Component */
|
13
|
-
Provider: React.FC<{
|
14
|
-
children: React.ReactNode;
|
15
|
-
auth: AuthMethod | "guest";
|
16
|
-
peer: `wss://${string}` | `ws://${string}`;
|
17
|
-
storage?: "indexedDB" | "singleTabOPFS";
|
18
|
-
}>;
|
19
|
-
/** @category Hooks */
|
20
|
-
useAccount(): {
|
21
|
-
me: Acc;
|
22
|
-
logOut: () => void;
|
23
|
-
};
|
24
|
-
/** @category Hooks */
|
25
|
-
useAccount<D extends DepthsIn<Acc>>(depth: D): {
|
26
|
-
me: DeeplyLoaded<Acc, D> | undefined;
|
27
|
-
logOut: () => void;
|
28
|
-
};
|
29
|
-
/** @category Hooks */
|
30
|
-
useAccountOrGuest(): {
|
31
|
-
me: Acc | AnonymousJazzAgent;
|
32
|
-
};
|
33
|
-
useAccountOrGuest<D extends DepthsIn<Acc>>(depth: D): {
|
34
|
-
me: DeeplyLoaded<Acc, D> | undefined | AnonymousJazzAgent;
|
35
|
-
};
|
36
|
-
/** @category Hooks */
|
37
|
-
useCoState<V extends CoValue, D>(Schema: {
|
38
|
-
new (...args: any[]): V;
|
39
|
-
} & CoValueClass, id: ID<V> | undefined, depth?: D & DepthsIn<V>): DeeplyLoaded<V, D> | undefined;
|
40
|
-
/** @category Hooks */
|
41
|
-
useAcceptInvite<V extends CoValue>({ invitedObjectSchema, onAccept, forValueHint, }: {
|
42
|
-
invitedObjectSchema: CoValueClass<V>;
|
43
|
-
onAccept: (projectID: ID<V>) => void;
|
44
|
-
forValueHint?: string;
|
45
|
-
}): void;
|
46
|
-
kvStore: KvStore;
|
47
|
-
}
|
48
|
-
export * from "./media.js";
|
16
|
+
export declare function JazzProvider<Acc extends Account = RegisteredAccount>({ children, auth, peer, AccountSchema, CryptoProvider, }: JazzProviderProps<Acc>): import("react").JSX.Element;
|
package/dist/provider.js
CHANGED
@@ -1,139 +1,69 @@
|
|
1
|
-
import
|
2
|
-
import { Account
|
3
|
-
import {
|
4
|
-
import {
|
5
|
-
import {
|
1
|
+
import { JazzContext } from "jazz-react-core";
|
2
|
+
import { Account } from "jazz-tools";
|
3
|
+
import { useState } from "react";
|
4
|
+
import { useEffect, useRef } from "react";
|
5
|
+
import { createJazzRNContext, } from "./platform.js";
|
6
6
|
/** @category Context & Hooks */
|
7
|
-
export function
|
8
|
-
const
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
7
|
+
export function JazzProvider({ children, auth, peer, AccountSchema = Account, CryptoProvider, }) {
|
8
|
+
const [ctx, setCtx] = useState();
|
9
|
+
const [sessionCount, setSessionCount] = useState(0);
|
10
|
+
const effectExecuted = useRef(false);
|
11
|
+
effectExecuted.current = false;
|
12
|
+
useEffect(() => {
|
13
|
+
// Avoid double execution of the effect in development mode for easier debugging.
|
14
|
+
if (process.env.NODE_ENV === "development") {
|
15
|
+
if (effectExecuted.current) {
|
16
|
+
return;
|
17
|
+
}
|
18
|
+
effectExecuted.current = true;
|
19
|
+
// In development mode we don't return a cleanup function because otherwise
|
20
|
+
// the double effect execution would mark the context as done immediately.
|
21
|
+
//
|
22
|
+
// So we mark it as done in the subsequent execution.
|
23
|
+
const previousContext = ctx;
|
24
|
+
if (previousContext) {
|
25
|
+
previousContext.done();
|
26
|
+
}
|
27
|
+
}
|
28
|
+
async function createContext() {
|
29
|
+
const currentContext = await createJazzRNContext(auth === "guest"
|
18
30
|
? {
|
19
31
|
peer,
|
20
|
-
storage,
|
21
32
|
CryptoProvider,
|
22
33
|
}
|
23
34
|
: {
|
24
35
|
AccountSchema,
|
25
36
|
auth: auth,
|
26
37
|
peer,
|
27
|
-
storage,
|
28
38
|
CryptoProvider,
|
29
|
-
}).then((context) => {
|
30
|
-
setCtx({
|
31
|
-
...context,
|
32
|
-
logOut: () => {
|
33
|
-
context.logOut();
|
34
|
-
setCtx(undefined);
|
35
|
-
setSessionCount(sessionCount + 1);
|
36
|
-
},
|
37
39
|
});
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
}, [AccountSchema, auth, peer, storage, sessionCount]);
|
48
|
-
return (<JazzContext.Provider value={ctx}>{ctx && children}</JazzContext.Provider>);
|
49
|
-
}
|
50
|
-
function useAccount(depth) {
|
51
|
-
const context = React.useContext(JazzContext);
|
52
|
-
if (!context) {
|
53
|
-
throw new Error("useAccount must be used within a JazzProvider");
|
54
|
-
}
|
55
|
-
if (!("me" in context)) {
|
56
|
-
throw new Error("useAccount can't be used in a JazzProvider with auth === 'guest' - consider using useAccountOrGuest()");
|
57
|
-
}
|
58
|
-
const me = useCoState(context?.me.constructor, context?.me.id, depth);
|
59
|
-
return {
|
60
|
-
me: depth === undefined ? me || context.me : me,
|
61
|
-
logOut: context.logOut,
|
62
|
-
};
|
63
|
-
}
|
64
|
-
function useAccountOrGuest(depth) {
|
65
|
-
const context = React.useContext(JazzContext);
|
66
|
-
if (!context) {
|
67
|
-
throw new Error("useAccountOrGuest must be used within a JazzProvider");
|
68
|
-
}
|
69
|
-
const contextMe = "me" in context ? context.me : undefined;
|
70
|
-
const me = useCoState(contextMe?.constructor, contextMe?.id, depth);
|
71
|
-
if ("me" in context) {
|
72
|
-
return {
|
73
|
-
me: depth === undefined ? me || context.me : me,
|
40
|
+
const logOut = () => {
|
41
|
+
currentContext.logOut();
|
42
|
+
setCtx(undefined);
|
43
|
+
setSessionCount(sessionCount + 1);
|
44
|
+
if (process.env.NODE_ENV === "development") {
|
45
|
+
// In development mode we don't return a cleanup function
|
46
|
+
// so we mark the context as done here.
|
47
|
+
currentContext.done();
|
48
|
+
}
|
74
49
|
};
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
}
|
80
|
-
function useCoState(
|
81
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
82
|
-
Schema, id, depth = []) {
|
83
|
-
const [state, setState] = useState({ value: undefined });
|
84
|
-
const context = React.useContext(JazzContext);
|
85
|
-
if (!context) {
|
86
|
-
throw new Error("useCoState must be used within a JazzProvider");
|
87
|
-
}
|
88
|
-
useEffect(() => {
|
89
|
-
if (!id)
|
90
|
-
return;
|
91
|
-
return subscribeToCoValue(Schema, id, "me" in context ? context.me : context.guest, depth, (value) => {
|
92
|
-
setState({ value });
|
50
|
+
setCtx({
|
51
|
+
...currentContext,
|
52
|
+
AccountSchema,
|
53
|
+
logOut,
|
93
54
|
});
|
94
|
-
|
95
|
-
return state.value;
|
96
|
-
}
|
97
|
-
function useAcceptInvite({ invitedObjectSchema, onAccept, forValueHint, }) {
|
98
|
-
const context = React.useContext(JazzContext);
|
99
|
-
if (!context) {
|
100
|
-
throw new Error("useAcceptInvite must be used within a JazzProvider");
|
55
|
+
return currentContext;
|
101
56
|
}
|
102
|
-
|
103
|
-
|
57
|
+
const promise = createContext();
|
58
|
+
// In development mode we don't return a cleanup function because otherwise
|
59
|
+
// the double effect execution would mark the context as done immediately.
|
60
|
+
if (process.env.NODE_ENV === "development") {
|
61
|
+
return;
|
104
62
|
}
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
.acceptInvite(result.valueID, result.inviteSecret, invitedObjectSchema)
|
111
|
-
.then(() => {
|
112
|
-
onAccept(result.valueID);
|
113
|
-
})
|
114
|
-
.catch((e) => {
|
115
|
-
console.error("Failed to accept invite", e);
|
116
|
-
});
|
117
|
-
}
|
118
|
-
};
|
119
|
-
const linkingListener = Linking.addEventListener("url", handleDeepLink);
|
120
|
-
void Linking.getInitialURL().then((url) => {
|
121
|
-
if (url)
|
122
|
-
handleDeepLink({ url });
|
123
|
-
});
|
124
|
-
return () => {
|
125
|
-
linkingListener.remove();
|
126
|
-
};
|
127
|
-
}, [context, onAccept, invitedObjectSchema, forValueHint]);
|
128
|
-
}
|
129
|
-
return {
|
130
|
-
Provider,
|
131
|
-
useAccount,
|
132
|
-
useAccountOrGuest,
|
133
|
-
useCoState,
|
134
|
-
useAcceptInvite,
|
135
|
-
kvStore,
|
136
|
-
};
|
63
|
+
return () => {
|
64
|
+
void promise.then((context) => context.done());
|
65
|
+
};
|
66
|
+
}, [AccountSchema, auth, peer, sessionCount]);
|
67
|
+
return (<JazzContext.Provider value={ctx}>{ctx && children}</JazzContext.Provider>);
|
137
68
|
}
|
138
|
-
export * from "./media.js";
|
139
69
|
//# sourceMappingURL=provider.js.map
|