jazz-vue 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright 2024, Garden Computing, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # `jazz-react`
2
+
3
+ React bindings for Jazz (see [jazz.tools](https://jazz.tools)), a framework for distributed state.
@@ -0,0 +1,27 @@
1
+ import { ImageDefinition } from 'jazz-tools';
2
+ declare function __VLS_template(): {
3
+ slots: {
4
+ default?(_: {
5
+ src: string | undefined;
6
+ res: `${number}x${number}` | "placeholder" | undefined;
7
+ originalSize: readonly [number, number] | undefined;
8
+ }): any;
9
+ };
10
+ refs: {};
11
+ attrs: Partial<{}>;
12
+ };
13
+ type __VLS_TemplateResult = ReturnType<typeof __VLS_template>;
14
+ declare const __VLS_component: import('vue').DefineComponent<{
15
+ image: ImageDefinition | null | undefined;
16
+ maxWidth?: number;
17
+ }, {}, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {}, string, import('vue').PublicProps, Readonly<{
18
+ image: ImageDefinition | null | undefined;
19
+ maxWidth?: number;
20
+ }> & Readonly<{}>, {}, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {}, any>;
21
+ declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_TemplateResult["slots"]>;
22
+ export default _default;
23
+ type __VLS_WithTemplateSlots<T, S> = T & {
24
+ new (): {
25
+ $slots: S;
26
+ };
27
+ };
@@ -0,0 +1,9 @@
1
+ import { DemoAuthState } from './useDemoAuth.js';
2
+ declare const _default: import('vue').DefineComponent<{
3
+ appName: string;
4
+ state: DemoAuthState;
5
+ }, {}, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {}, string, import('vue').PublicProps, Readonly<{
6
+ appName: string;
7
+ state: DemoAuthState;
8
+ }> & Readonly<{}>, {}, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {}, any>;
9
+ export default _default;
@@ -0,0 +1,56 @@
1
+ import { AgentSecret } from 'cojson';
2
+ import { BrowserDemoAuth } from 'jazz-browser';
3
+ import { Account, ID } from 'jazz-tools';
4
+ export type DemoAuthState = ({
5
+ state: "uninitialized";
6
+ } | {
7
+ state: "loading";
8
+ } | {
9
+ state: "ready";
10
+ existingUsers: string[];
11
+ signUp: (username: string) => void;
12
+ logInAs: (existingUser: string) => void;
13
+ } | {
14
+ state: "signedIn";
15
+ logOut: () => void;
16
+ }) & {
17
+ errors: string[];
18
+ };
19
+ /** @category Auth Providers */
20
+ export declare function useDemoAuth({ seedAccounts, }?: {
21
+ seedAccounts?: {
22
+ [name: string]: {
23
+ accountID: ID<Account>;
24
+ accountSecret: AgentSecret;
25
+ };
26
+ };
27
+ }): {
28
+ authMethod: import('vue').Ref<{
29
+ driver: {
30
+ onReady: (next: {
31
+ signUp: (username: string) => Promise<void>;
32
+ existingUsers: string[];
33
+ logInAs: (existingUser: string) => Promise<void>;
34
+ }) => void;
35
+ onSignedIn: (next: {
36
+ logOut: () => void;
37
+ }) => void;
38
+ onError: (error: string | Error) => void;
39
+ };
40
+ start: () => Promise<import('jazz-tools').AuthResult>;
41
+ }, BrowserDemoAuth | {
42
+ driver: {
43
+ onReady: (next: {
44
+ signUp: (username: string) => Promise<void>;
45
+ existingUsers: string[];
46
+ logInAs: (existingUser: string) => Promise<void>;
47
+ }) => void;
48
+ onSignedIn: (next: {
49
+ logOut: () => void;
50
+ }) => void;
51
+ onError: (error: string | Error) => void;
52
+ };
53
+ start: () => Promise<import('jazz-tools').AuthResult>;
54
+ }>;
55
+ state: import('vue').Reactive<DemoAuthState>;
56
+ };
@@ -0,0 +1,29 @@
1
+ import { Account, AnonymousJazzAgent, CoValue, CoValueClass, DeeplyLoaded, DepthsIn, ID } from 'jazz-tools';
2
+ import { Component, ComputedRef, Ref } from 'vue';
3
+ export declare const logoutHandler: Ref<(() => void) | undefined, (() => void) | undefined>;
4
+ export interface JazzVueApp<Acc extends Account> {
5
+ JazzProvider: Component;
6
+ useAccount(): {
7
+ me: ComputedRef<Acc>;
8
+ logOut: () => void;
9
+ };
10
+ useAccount<D extends DepthsIn<Acc>>(depth: D): {
11
+ me: ComputedRef<DeeplyLoaded<Acc, D> | undefined>;
12
+ logOut: () => void;
13
+ };
14
+ useAccountOrGuest(): {
15
+ me: ComputedRef<Acc | AnonymousJazzAgent>;
16
+ };
17
+ useAccountOrGuest<D extends DepthsIn<Acc>>(depth: D): {
18
+ me: ComputedRef<DeeplyLoaded<Acc, D> | undefined | AnonymousJazzAgent>;
19
+ };
20
+ useCoState<V extends CoValue, D>(Schema: CoValueClass<V>, id: ID<V> | undefined, depth?: D & DepthsIn<V>): Ref<DeeplyLoaded<V, D> | undefined>;
21
+ useAcceptInvite<V extends CoValue>(args: {
22
+ invitedObjectSchema: CoValueClass<V>;
23
+ onAccept: (projectID: ID<V>) => void;
24
+ forValueHint?: string;
25
+ }): void;
26
+ }
27
+ export declare function createJazzVueApp<Acc extends Account>({ AccountSchema, }?: {
28
+ AccountSchema?: any;
29
+ }): JazzVueApp<Acc>;
@@ -0,0 +1,4 @@
1
+ export * from './createJazzVueApp.js';
2
+ export * from './auth/useDemoAuth.js';
3
+ export { default as DemoAuthBasicUI } from './auth/DemoAuthBasicUI.vue';
4
+ export { default as ProgressiveImg } from './ProgressiveImg.vue';
package/dist/index.js ADDED
@@ -0,0 +1,361 @@
1
+ import { createJazzBrowserContext as D, consumeInviteLinkFromWindowLocation as _, BrowserDemoAuth as B } from "jazz-browser";
2
+ import { Account as j, subscribeToCoValue as T } from "jazz-tools";
3
+ import { ref as y, defineComponent as R, provide as V, onMounted as P, watch as A, onUnmounted as I, computed as v, toRaw as b, shallowRef as M, inject as N, reactive as $, openBlock as g, createElementBlock as f, normalizeStyle as w, Fragment as k, createElementVNode as x, toDisplayString as C, renderList as E, withModifiers as F, withDirectives as G, vModelText as W, createCommentVNode as L, toRef as q, renderSlot as H, unref as J } from "vue";
4
+ const S = y(), O = Symbol("JazzContext");
5
+ function te({
6
+ AccountSchema: p = j
7
+ } = {}) {
8
+ const i = R({
9
+ name: "JazzProvider",
10
+ props: {
11
+ auth: {
12
+ type: [String, Object],
13
+ required: !0
14
+ },
15
+ peer: {
16
+ type: String,
17
+ required: !0
18
+ },
19
+ storage: {
20
+ type: String,
21
+ default: void 0
22
+ }
23
+ },
24
+ setup(r, { slots: o }) {
25
+ const t = y(
26
+ void 0
27
+ ), n = y(0);
28
+ V(O, t);
29
+ const s = async () => {
30
+ var e, u;
31
+ t.value && ((u = (e = t.value).done) == null || u.call(e), t.value = void 0);
32
+ try {
33
+ const m = await D(
34
+ r.auth === "guest" ? { peer: r.peer, storage: r.storage } : {
35
+ AccountSchema: p,
36
+ auth: r.auth,
37
+ peer: r.peer,
38
+ storage: r.storage
39
+ }
40
+ );
41
+ t.value = {
42
+ ...m,
43
+ logOut: () => {
44
+ var z;
45
+ (z = S.value) == null || z.call(S), n.value += 1;
46
+ }
47
+ };
48
+ } catch (m) {
49
+ console.error("Error creating Jazz browser context:", m);
50
+ }
51
+ };
52
+ return P(() => {
53
+ s();
54
+ }), A(
55
+ () => n.value,
56
+ async () => {
57
+ await s();
58
+ }
59
+ ), I(() => {
60
+ var e, u;
61
+ t.value && ((u = (e = t.value).done) == null || u.call(e));
62
+ }), () => {
63
+ var e;
64
+ return t.value ? (e = o.default) == null ? void 0 : e.call(o) : null;
65
+ };
66
+ }
67
+ });
68
+ function l() {
69
+ const r = N(O);
70
+ if (!r)
71
+ throw new Error("useJazzContext must be used within a JazzProvider");
72
+ return r;
73
+ }
74
+ function a(r) {
75
+ const o = l();
76
+ if (!o.value)
77
+ throw new Error("useAccount must be used within a JazzProvider");
78
+ if (!("me" in o.value))
79
+ throw new Error(
80
+ "useAccount can't be used in a JazzProvider with auth === 'guest' - consider using useAccountOrGuest()"
81
+ );
82
+ const t = d(
83
+ o.value.me.constructor,
84
+ o.value.me.id,
85
+ r
86
+ );
87
+ return {
88
+ me: v(() => {
89
+ const n = r === void 0 ? t.value || b(o.value.me) : t.value;
90
+ return n && b(n);
91
+ }),
92
+ logOut: o.value.logOut
93
+ };
94
+ }
95
+ function c(r) {
96
+ const o = l();
97
+ if (!o.value)
98
+ throw new Error("useAccountOrGuest must be used within a JazzProvider");
99
+ const t = "me" in o.value ? o.value.me : void 0, n = d(
100
+ t == null ? void 0 : t.constructor,
101
+ t == null ? void 0 : t.id,
102
+ r
103
+ );
104
+ return "me" in o.value ? {
105
+ me: v(
106
+ () => r === void 0 ? n.value || b(o.value.me) : n.value
107
+ )
108
+ } : {
109
+ me: v(() => b(o.value.guest))
110
+ };
111
+ }
112
+ function d(r, o, t = []) {
113
+ const n = M(void 0), s = l();
114
+ if (!s.value)
115
+ throw new Error("useCoState must be used within a JazzProvider");
116
+ let e;
117
+ return A(
118
+ [() => o, () => s, () => r, () => t],
119
+ () => {
120
+ e && e(), o && (e = T(
121
+ r,
122
+ o,
123
+ "me" in s.value ? b(s.value.me) : b(s.value.guest),
124
+ t,
125
+ (m) => {
126
+ n.value = m;
127
+ }
128
+ ));
129
+ },
130
+ { immediate: !0 }
131
+ ), I(() => {
132
+ e && e();
133
+ }), v(() => n.value);
134
+ }
135
+ function h({
136
+ invitedObjectSchema: r,
137
+ onAccept: o,
138
+ forValueHint: t
139
+ }) {
140
+ const n = l();
141
+ if (!n.value)
142
+ throw new Error("useAcceptInvite must be used within a JazzProvider");
143
+ if (!("me" in n.value))
144
+ throw new Error(
145
+ "useAcceptInvite can't be used in a JazzProvider with auth === 'guest'."
146
+ );
147
+ const s = () => {
148
+ _({
149
+ as: b(n.value.me),
150
+ invitedObjectSchema: r,
151
+ forValueHint: t
152
+ }).then((u) => u && o(u.valueID)).catch((u) => {
153
+ console.error("Failed to accept invite", u);
154
+ });
155
+ };
156
+ P(() => {
157
+ s();
158
+ }), A(
159
+ () => o,
160
+ (e, u) => {
161
+ e !== u && s();
162
+ }
163
+ );
164
+ }
165
+ return {
166
+ JazzProvider: i,
167
+ useAccount: a,
168
+ useAccountOrGuest: c,
169
+ useCoState: d,
170
+ useAcceptInvite: h
171
+ };
172
+ }
173
+ function re({
174
+ seedAccounts: p
175
+ } = {}) {
176
+ const i = $({
177
+ state: "loading",
178
+ errors: []
179
+ }), l = y(
180
+ new B(
181
+ {
182
+ onReady: ({ signUp: a, existingUsers: c, logInAs: d }) => {
183
+ i.state = "ready", i.signUp = a, i.existingUsers = c, i.logInAs = d, i.errors = [];
184
+ },
185
+ onSignedIn: ({ logOut: a }) => {
186
+ i.state = "signedIn", i.logOut = () => {
187
+ a(), i.state = "ready", i.errors = [];
188
+ }, i.errors = [], S.value = i.logOut;
189
+ },
190
+ onError: (a) => {
191
+ i.errors.push(a.toString());
192
+ }
193
+ },
194
+ p
195
+ )
196
+ );
197
+ return I(() => {
198
+ i.state === "signedIn" && (S.value = void 0);
199
+ }), { authMethod: l, state: i };
200
+ }
201
+ const K = { key: 0 }, Q = {
202
+ key: 0,
203
+ style: { display: "flex", "flex-direction": "column", gap: "0.5rem" }
204
+ }, X = ["onClick", "aria-label"], oe = /* @__PURE__ */ R({
205
+ __name: "DemoAuthBasicUI",
206
+ props: {
207
+ appName: {},
208
+ state: {}
209
+ },
210
+ setup(p) {
211
+ const i = p, l = y(""), a = v(
212
+ () => window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches
213
+ ), c = v(() => ({
214
+ minHeight: "100vh",
215
+ display: "flex",
216
+ flexDirection: "column",
217
+ justifyContent: "center",
218
+ alignItems: "center",
219
+ width: "100%",
220
+ padding: "1rem",
221
+ maxWidth: "100vw",
222
+ gap: "2rem",
223
+ margin: "0",
224
+ ...a.value ? { background: "#000" } : {}
225
+ })), d = v(() => ({
226
+ border: a.value ? "2px solid #444" : "2px solid #ddd",
227
+ padding: "11px 8px",
228
+ borderRadius: "6px",
229
+ background: a.value ? "#000" : "#fff",
230
+ color: a.value ? "#fff" : "#000"
231
+ })), h = v(() => ({
232
+ padding: "13px 5px",
233
+ border: "none",
234
+ borderRadius: "6px",
235
+ cursor: "pointer",
236
+ background: a.value ? "#444" : "#ddd",
237
+ color: a.value ? "#fff" : "#000"
238
+ })), r = v(() => ({
239
+ background: a.value ? "#0d0d0d" : "#eee",
240
+ color: a.value ? "#fff" : "#000",
241
+ padding: "0.5rem",
242
+ border: "none",
243
+ borderRadius: "6px"
244
+ })), o = () => {
245
+ i.state.signUp(l.value), l.value = "";
246
+ }, t = (n) => {
247
+ i.state.logInAs(n);
248
+ };
249
+ return (n, s) => (g(), f("div", {
250
+ style: w(c.value)
251
+ }, [
252
+ n.state.state === "loading" ? (g(), f("div", K, "Loading...")) : n.state.state === "ready" ? (g(), f(k, { key: 1 }, [
253
+ x("h1", {
254
+ style: w({ color: a.value ? "#fff" : "#000", textAlign: "center" })
255
+ }, C(n.appName), 5),
256
+ (g(!0), f(k, null, E(n.state.errors, (e) => (g(), f("div", {
257
+ key: e,
258
+ style: { color: "red" }
259
+ }, C(e), 1))), 128)),
260
+ x("form", {
261
+ onSubmit: F(o, ["prevent"]),
262
+ style: { display: "flex", "flex-direction": "column", gap: "0.5rem" }
263
+ }, [
264
+ G(x("input", {
265
+ "onUpdate:modelValue": s[0] || (s[0] = (e) => l.value = e),
266
+ placeholder: "Display name",
267
+ autoComplete: "webauthn",
268
+ style: w(d.value)
269
+ }, null, 4), [
270
+ [W, l.value]
271
+ ]),
272
+ x("input", {
273
+ type: "submit",
274
+ value: "Sign up",
275
+ style: w(h.value)
276
+ }, null, 4)
277
+ ], 32),
278
+ n.state.existingUsers.length > 0 ? (g(), f("div", Q, [
279
+ x("p", {
280
+ style: w({
281
+ color: a.value ? "#e2e2e2" : "#000",
282
+ textAlign: "center",
283
+ paddingTop: "0.5rem",
284
+ borderTop: "1px solid",
285
+ borderColor: a.value ? "#111" : "#e2e2e2"
286
+ })
287
+ }, " Log in as ", 4),
288
+ (g(!0), f(k, null, E(n.state.existingUsers, (e) => (g(), f("button", {
289
+ key: e,
290
+ onClick: (u) => t(e),
291
+ type: "button",
292
+ "aria-label": `Log in as ${e}`,
293
+ style: w(r.value)
294
+ }, C(e), 13, X))), 128))
295
+ ])) : L("", !0)
296
+ ], 64)) : L("", !0)
297
+ ], 4));
298
+ }
299
+ }), ne = /* @__PURE__ */ R({
300
+ __name: "ProgressiveImg",
301
+ props: {
302
+ image: {},
303
+ maxWidth: {}
304
+ },
305
+ setup(p) {
306
+ function i(c, d) {
307
+ const h = y({});
308
+ let r;
309
+ const o = A(
310
+ () => {
311
+ var t;
312
+ return [(t = c.value) == null ? void 0 : t.id, d];
313
+ },
314
+ () => {
315
+ let t;
316
+ return c.value ? c.value.subscribe({}, (s) => {
317
+ var u, m;
318
+ const e = s == null ? void 0 : s.highestResAvailable({ maxWidth: d });
319
+ if (e) {
320
+ if (e.res !== t) {
321
+ t = e.res;
322
+ const z = e.stream.toBlob();
323
+ if (z) {
324
+ const U = URL.createObjectURL(z);
325
+ h.value = {
326
+ src: U,
327
+ res: e.res,
328
+ originalSize: (u = c.value) == null ? void 0 : u.originalSize
329
+ }, r && r(), r = () => {
330
+ setTimeout(() => URL.revokeObjectURL(U), 200);
331
+ };
332
+ }
333
+ }
334
+ } else
335
+ h.value = {
336
+ src: s == null ? void 0 : s.placeholderDataURL,
337
+ res: "placeholder",
338
+ originalSize: (m = c.value) == null ? void 0 : m.originalSize
339
+ };
340
+ }) : void 0;
341
+ }
342
+ );
343
+ return I(() => {
344
+ o(), r && r();
345
+ }), h;
346
+ }
347
+ const l = p, a = i(q(l, "image"), l.maxWidth);
348
+ return (c, d) => H(c.$slots, "default", {
349
+ src: J(a).src,
350
+ res: J(a).res,
351
+ originalSize: J(a).originalSize
352
+ });
353
+ }
354
+ });
355
+ export {
356
+ oe as DemoAuthBasicUI,
357
+ ne as ProgressiveImg,
358
+ te as createJazzVueApp,
359
+ S as logoutHandler,
360
+ re as useDemoAuth
361
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "jazz-vue",
3
+ "version": "0.8.6",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "src/index.ts",
7
+ "license": "MIT",
8
+ "dependencies": {
9
+ "@scure/bip39": "^1.3.0",
10
+ "cojson": "0.8.16",
11
+ "jazz-browser": "0.8.16",
12
+ "jazz-tools": "0.8.16"
13
+ },
14
+ "devDependencies": {
15
+ "@vitejs/plugin-vue": "^5.1.4",
16
+ "typescript": "^5.3.3",
17
+ "vite": "^5.0.10",
18
+ "vite-plugin-dts": "^4.2.4",
19
+ "vue": "^3.5.11",
20
+ "vue-tsc": "^2.1.6"
21
+ },
22
+ "peerDependencies": {
23
+ "vue": "^3.5.11"
24
+ },
25
+ "scripts": {
26
+ "dev": "vite",
27
+ "format-and-lint": "biome check .",
28
+ "format-and-lint:fix": "biome check . --write",
29
+ "build": "rm -rf ./dist && vite build"
30
+ }
31
+ }
@@ -0,0 +1,82 @@
1
+ <script setup lang="ts">
2
+ import type { ImageDefinition } from "jazz-tools";
3
+ import { type Ref, onUnmounted, ref, toRef, watch } from "vue";
4
+
5
+ interface ImageState {
6
+ src?: string;
7
+ res?: `${number}x${number}` | "placeholder";
8
+ originalSize?: readonly [number, number];
9
+ }
10
+
11
+ function useProgressiveImg(
12
+ image: Ref<ImageDefinition | null | undefined>,
13
+ maxWidth?: number,
14
+ ) {
15
+ const current = ref<ImageState>({});
16
+
17
+ let cleanup: (() => void) | undefined;
18
+ const unsubscribe = watch(
19
+ () => [image.value?.id, maxWidth],
20
+ () => {
21
+ let lastHighestRes: string | undefined;
22
+
23
+ if (!image.value) return;
24
+
25
+ const unsub = image.value.subscribe({}, (update) => {
26
+ const highestRes = update?.highestResAvailable({ maxWidth });
27
+
28
+ if (highestRes) {
29
+ if (highestRes.res !== lastHighestRes) {
30
+ lastHighestRes = highestRes.res;
31
+ const blob = highestRes.stream.toBlob();
32
+
33
+ if (blob) {
34
+ const blobURI = URL.createObjectURL(blob);
35
+ current.value = {
36
+ src: blobURI,
37
+ res: highestRes.res,
38
+ originalSize: image.value?.originalSize,
39
+ };
40
+
41
+ if (cleanup) cleanup();
42
+ cleanup = () => {
43
+ setTimeout(() => URL.revokeObjectURL(blobURI), 200);
44
+ };
45
+ }
46
+ }
47
+ } else {
48
+ current.value = {
49
+ src: update?.placeholderDataURL,
50
+ res: "placeholder",
51
+ originalSize: image.value?.originalSize,
52
+ };
53
+ }
54
+ });
55
+
56
+ return unsub;
57
+ },
58
+ );
59
+
60
+ onUnmounted(() => {
61
+ unsubscribe();
62
+ if (cleanup) cleanup();
63
+ });
64
+
65
+ return current;
66
+ }
67
+
68
+ const props = defineProps<{
69
+ image: ImageDefinition | null | undefined;
70
+ maxWidth?: number;
71
+ }>();
72
+
73
+ const current = useProgressiveImg(toRef(props, "image"), props.maxWidth);
74
+ </script>
75
+
76
+ <template>
77
+ <slot
78
+ :src="current.src"
79
+ :res="current.res"
80
+ :original-size="current.originalSize"
81
+ />
82
+ </template>