@vennbase/react 0.1.8
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/LICENSE +183 -0
- package/README.md +365 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +694 -0
- package/dist/index.js.map +1 -0
- package/dist/polling.d.ts +14 -0
- package/dist/polling.js +150 -0
- package/dist/polling.js.map +1 -0
- package/dist/runtime.d.ts +98 -0
- package/dist/runtime.js +383 -0
- package/dist/runtime.js.map +1 -0
- package/dist/type-assertions.d.ts +1 -0
- package/dist/type-assertions.js +50 -0
- package/dist/type-assertions.js.map +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made
|
|
34
|
+
available under the License, as indicated by a copyright notice that is included
|
|
35
|
+
in or attached to the work (an example is provided in the Appendix below).
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
|
47
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
|
48
|
+
on behalf of the copyright owner. For the purposes of this definition,
|
|
49
|
+
"submitted" means any form of electronic, verbal, or written communication sent
|
|
50
|
+
to the Licensor or its representatives, including but not limited to
|
|
51
|
+
communication on electronic mailing lists, source code control systems, and
|
|
52
|
+
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
|
53
|
+
the purpose of discussing and improving the Work, but excluding communication
|
|
54
|
+
that is conspicuously marked or otherwise designated in writing by the copyright
|
|
55
|
+
owner as "Not a Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
66
|
+
Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
71
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
72
|
+
transfer the Work, where such license applies only to those patent claims
|
|
73
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
74
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work
|
|
75
|
+
to which such Contribution(s) was submitted. If You institute patent litigation
|
|
76
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
77
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
78
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
79
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
80
|
+
such litigation is filed.
|
|
81
|
+
|
|
82
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
83
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
84
|
+
Source or Object form, provided that You meet the following conditions:
|
|
85
|
+
|
|
86
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
87
|
+
this License; and
|
|
88
|
+
|
|
89
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
90
|
+
You changed the files; and
|
|
91
|
+
|
|
92
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
93
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
94
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
95
|
+
of the Derivative Works; and
|
|
96
|
+
|
|
97
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
98
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
99
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
100
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
101
|
+
following places: within a NOTICE text file distributed as part of the
|
|
102
|
+
Derivative Works; within the Source form or documentation, if provided along
|
|
103
|
+
with the Derivative Works; or, within a display generated by the Derivative
|
|
104
|
+
Works, if and wherever such third-party notices normally appear. The contents of
|
|
105
|
+
the NOTICE file are for informational purposes only and do not modify the
|
|
106
|
+
License. You may add Your own attribution notices within Derivative Works that
|
|
107
|
+
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
|
108
|
+
provided that such additional attribution notices cannot be construed as
|
|
109
|
+
modifying the License.
|
|
110
|
+
|
|
111
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
112
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
113
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
114
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
115
|
+
with the conditions stated in this License.
|
|
116
|
+
|
|
117
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
118
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
119
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
120
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
121
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
122
|
+
executed with Licensor regarding such Contributions.
|
|
123
|
+
|
|
124
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
125
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
126
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
127
|
+
reproducing the content of the NOTICE file.
|
|
128
|
+
|
|
129
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
130
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
131
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
132
|
+
KIND, either express or implied, including, without limitation, any warranties
|
|
133
|
+
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
134
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
135
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
136
|
+
associated with Your exercise of permissions under this License.
|
|
137
|
+
|
|
138
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
139
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
140
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
141
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
142
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
143
|
+
arising as a result of this License or out of the use or inability to use the
|
|
144
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
145
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
146
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
147
|
+
damages.
|
|
148
|
+
|
|
149
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
150
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
151
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
152
|
+
and/or rights consistent with this License. However, in accepting such
|
|
153
|
+
obligations, You may act only on Your own behalf and on Your sole responsibility,
|
|
154
|
+
not on behalf of any other Contributor, and only if You agree to indemnify,
|
|
155
|
+
defend, and hold each Contributor harmless for any liability incurred by, or
|
|
156
|
+
claims asserted against, such Contributor by reason of your accepting any such
|
|
157
|
+
warranty or additional liability.
|
|
158
|
+
|
|
159
|
+
END OF TERMS AND CONDITIONS
|
|
160
|
+
|
|
161
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
162
|
+
|
|
163
|
+
To apply the Apache License to your work, attach the following boilerplate
|
|
164
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
|
165
|
+
identifying information. (Don't include the brackets!) The text should be
|
|
166
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
|
167
|
+
recommend that a file or class name and description of purpose be included on the
|
|
168
|
+
same "printed page" as the copyright notice for easier identification within
|
|
169
|
+
third-party archives.
|
|
170
|
+
|
|
171
|
+
Copyright [yyyy] [name of copyright owner]
|
|
172
|
+
|
|
173
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
174
|
+
you may not use this file except in compliance with the License.
|
|
175
|
+
You may obtain a copy of the License at
|
|
176
|
+
|
|
177
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
178
|
+
|
|
179
|
+
Unless required by applicable law or agreed to in writing, software
|
|
180
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
181
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
182
|
+
See the License for the specific language governing permissions and
|
|
183
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
# @vennbase/react
|
|
2
|
+
|
|
3
|
+
React hooks and provider for [Vennbase](https://www.npmjs.com/package/@vennbase/core) — a multi-user database for apps that have no backend. Users sign in with their [Puter](https://puter.com) accounts. Their data lives in their Puter storage.
|
|
4
|
+
|
|
5
|
+
See [`@vennbase/core`](https://www.npmjs.com/package/@vennbase/core) for the full API and schema documentation.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @vennbase/react @vennbase/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
Create one `Vennbase` instance for your app.
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { Vennbase } from "@vennbase/core";
|
|
19
|
+
import { schema } from "./schema";
|
|
20
|
+
|
|
21
|
+
const db = new Vennbase({ schema, appBaseUrl: window.location.origin });
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
If you want to read the Vennbase instance from React context once, wrap your app in `<VennbaseProvider>` and call `useVennbase()` where needed:
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { VennbaseProvider, useVennbase, useSession } from "@vennbase/react";
|
|
28
|
+
|
|
29
|
+
export function App() {
|
|
30
|
+
return (
|
|
31
|
+
<VennbaseProvider db={db}>
|
|
32
|
+
<AppShell />
|
|
33
|
+
</VennbaseProvider>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function AppShell() {
|
|
38
|
+
const db = useVennbase<Schema>();
|
|
39
|
+
const session = useSession(db);
|
|
40
|
+
return <Main session={session} />;
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Auth
|
|
45
|
+
|
|
46
|
+
Use `useSession` to gate your UI on the auth state:
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
import { useSession } from "@vennbase/react";
|
|
50
|
+
import { db } from "./db";
|
|
51
|
+
|
|
52
|
+
function AppShell() {
|
|
53
|
+
const session = useSession(db);
|
|
54
|
+
|
|
55
|
+
if (session.status === "loading") return <p>Checking session…</p>;
|
|
56
|
+
|
|
57
|
+
if (!session.session?.signedIn) {
|
|
58
|
+
return <button onClick={() => void session.signIn()}>Log in with Puter</button>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return <Main />;
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Querying
|
|
66
|
+
|
|
67
|
+
`useQuery` polls for changes and re-renders automatically. `rows` is always a typed array — never `undefined`.
|
|
68
|
+
|
|
69
|
+
`useQuery(db, "games", ...)` never means "all accessible games". If a collection is not declared as `in: ["user"]`, omitting `in` is an error.
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
import { useQuery } from "@vennbase/react";
|
|
73
|
+
import { db } from "./db";
|
|
74
|
+
|
|
75
|
+
function CardList({ board }: { board: BoardHandle }) {
|
|
76
|
+
const { rows: cards } = useQuery<Schema, "cards">(db, "cards", {
|
|
77
|
+
in: board,
|
|
78
|
+
index: "byCreatedAt",
|
|
79
|
+
order: "asc",
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<ul>
|
|
84
|
+
{cards.map((card) => (
|
|
85
|
+
<li key={card.id}>{card.fields.text}</li>
|
|
86
|
+
))}
|
|
87
|
+
</ul>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Collections declared as `in: ["user"]` keep the same ergonomics in React: if you omit `in`, Vennbase queries inside the current signed-in user's built-in `user` row.
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
function RecentBoards() {
|
|
96
|
+
const { rows: recentBoards } = useQuery<Schema, "recentBoards">(db, "recentBoards", {
|
|
97
|
+
index: "byOpenedAt",
|
|
98
|
+
order: "desc",
|
|
99
|
+
limit: 10,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
<ul>
|
|
104
|
+
{recentBoards.map((recentBoard) => (
|
|
105
|
+
<li key={recentBoard.id}>{recentBoard.fields.boardRef.id}</li>
|
|
106
|
+
))}
|
|
107
|
+
</ul>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Single-row reads
|
|
113
|
+
|
|
114
|
+
`useRow` is the single-row equivalent of `useQuery`: it polls for changes and re-renders automatically.
|
|
115
|
+
|
|
116
|
+
If you need one row in React, prefer `useRow(db, row)` over calling `db.getRow(...)` in an effect and wiring your own polling loop. `row` can be either a `RowHandle` or a `RowRef`.
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
import { useRow } from "@vennbase/react";
|
|
120
|
+
import { db } from "./db";
|
|
121
|
+
import type { RowRef } from "@vennbase/core";
|
|
122
|
+
|
|
123
|
+
function BoardTitle({ boardRef }: { boardRef: RowRef<"boards"> }) {
|
|
124
|
+
const { data: board, status } = useRow<Schema, "boards">(db, boardRef);
|
|
125
|
+
|
|
126
|
+
if (status !== "success" || !board) return <p>Loading…</p>;
|
|
127
|
+
return <h1>{board.fields.title}</h1>;
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Row Handle Identity
|
|
132
|
+
|
|
133
|
+
`useRow` and `useQuery` keep `RowHandle` identity stable for the life of a row within a `Vennbase` instance. When the row fields change, the same handle object is reused and `row.fields` is replaced with a fresh snapshot object.
|
|
134
|
+
|
|
135
|
+
That means using `[row]` as an effect dependency is safe for subscriptions keyed to the logical row. If your effect depends on row contents, depend on `row.fields` or specific field values instead.
|
|
136
|
+
|
|
137
|
+
```tsx
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (!row) return;
|
|
140
|
+
const connection = row.connectCrdt(callbacks);
|
|
141
|
+
return () => connection.disconnect();
|
|
142
|
+
}, [row]);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
```tsx
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
if (!row) return;
|
|
148
|
+
syncForm(row.fields);
|
|
149
|
+
}, [row?.fields]);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## CRDT adapters
|
|
153
|
+
|
|
154
|
+
Use row fields for queryable metadata and the CRDT document for collaborative value state.
|
|
155
|
+
|
|
156
|
+
`useCrdt` wires any `CrdtAdapter` to a row. For Yjs, inject the app's own `Y` instance so `@vennbase/yjs` never loads a second runtime:
|
|
157
|
+
|
|
158
|
+
```tsx
|
|
159
|
+
import * as Y from "yjs";
|
|
160
|
+
import { useRef } from "react";
|
|
161
|
+
import { useCrdt } from "@vennbase/react";
|
|
162
|
+
import { createYjsAdapter } from "@vennbase/yjs";
|
|
163
|
+
|
|
164
|
+
function Room({ row }: { row: BoardHandle | null }) {
|
|
165
|
+
const adapterRef = useRef(createYjsAdapter(Y));
|
|
166
|
+
const { value: doc, version } = useCrdt(row, adapterRef.current);
|
|
167
|
+
|
|
168
|
+
const entries = doc.getArray<string>("messages").toArray();
|
|
169
|
+
return <pre data-version={version}>{JSON.stringify(entries)}</pre>;
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Invite links
|
|
174
|
+
|
|
175
|
+
`useShareLink` lazily generates (or reuses) a share link for a row. `useAcceptInviteFromUrl` handles the recipient side: it detects Vennbase invite URLs in the current URL, waits for the session, calls `acceptInvite`, waits for `onOpen`, and then clears the invite params.
|
|
176
|
+
|
|
177
|
+
```tsx
|
|
178
|
+
import { useShareLink, useAcceptInviteFromUrl } from "@vennbase/react";
|
|
179
|
+
import { db } from "./db";
|
|
180
|
+
|
|
181
|
+
// Sharer side
|
|
182
|
+
function ShareButton({ board }: { board: BoardHandle }) {
|
|
183
|
+
const { shareLink } = useShareLink(db, board);
|
|
184
|
+
return <button onClick={() => navigator.clipboard.writeText(shareLink ?? "")}>Copy share link</button>;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Recipient side — call once near the app root
|
|
188
|
+
function InviteHandler() {
|
|
189
|
+
useAcceptInviteFromUrl(db, {
|
|
190
|
+
onOpen: (board) => {
|
|
191
|
+
// navigate to the shared board
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Mutations
|
|
199
|
+
|
|
200
|
+
`useMutation` wraps any async call with `loading` / `success` / `error` state:
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
import { useMutation } from "@vennbase/react";
|
|
204
|
+
|
|
205
|
+
function AddCard({ board }: { board: BoardHandle }) {
|
|
206
|
+
const { mutate: addCard, status } = useMutation(async (text: string) => {
|
|
207
|
+
const write = db.create("cards", { text, done: false, createdAt: Date.now() }, { in: board });
|
|
208
|
+
await write.committed;
|
|
209
|
+
return write.value;
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<button disabled={status === "loading"} onClick={() => addCard("New card")}>
|
|
214
|
+
Add card
|
|
215
|
+
</button>
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Hook reference
|
|
221
|
+
|
|
222
|
+
| Hook | Arguments | Returns |
|
|
223
|
+
|------|-----------|---------|
|
|
224
|
+
| `useSession(db)` | `Vennbase` instance | `{ session, status, isRefreshing, error, refreshError, signIn, refresh }` |
|
|
225
|
+
| `useCurrentUser(db)` | `Vennbase` instance | `{ data: VennbaseUser, status, isRefreshing, error, refreshError, refresh }` |
|
|
226
|
+
| `useVennbase()` | — | `Vennbase` instance from context |
|
|
227
|
+
| `useVennbaseReady(db)` | `Vennbase` instance | `{ status, isRefreshing, error, refreshError, refresh }` — resolves when auth + provisioning complete |
|
|
228
|
+
| `useQuery(db, collection, options)` | db, collection name, query options | `{ rows, data, status, isRefreshing, error, refreshError, refresh }` |
|
|
229
|
+
| `useRow(db, row)` | db, row handle or row ref | `{ data: RowHandle, status, isRefreshing, error, refreshError, refresh }` |
|
|
230
|
+
| `useParents(db, row)` | db, row handle or row ref | `{ data: RowRef[], status, isRefreshing, error, refreshError, refresh }` |
|
|
231
|
+
| `useMemberUsernames(db, row)` | db, row handle or row ref | `{ data: string[], status, isRefreshing, error, refreshError, refresh }` |
|
|
232
|
+
| `useDirectMembers(db, row)` | db, row handle or row ref | `{ data: { username, role }[], status, isRefreshing, error, refreshError, refresh }` |
|
|
233
|
+
| `useEffectiveMembers(db, row)` | db, row handle or row ref | `{ data: DbMemberInfo[], status, isRefreshing, error, refreshError, refresh }` |
|
|
234
|
+
| `useShareLink(db, row)` | db, row handle or row ref | `{ shareLink: string, status, isRefreshing, error, refreshError, refresh }` |
|
|
235
|
+
| `useAcceptInviteFromUrl(db, options?)` | db, `{ url?, clearInviteParams?, onOpen?, accept? }` | `{ hasInvite, inviteInput, data, status, isRefreshing, error, refreshError, refresh }` |
|
|
236
|
+
| `useSavedRow(db, options)` | db, `{ key, url?, clearInviteParams?, loadSavedRow?, acceptInvite?, getRow? }` | `{ hasInvite, inviteInput, data, status, isRefreshing, error, refreshError, refresh, save, clear }` |
|
|
237
|
+
| `useMutation(fn)` | async function | `{ mutate, data, status, error, reset }` |
|
|
238
|
+
|
|
239
|
+
All data-fetching hooks return `status: "idle" | "loading" | "success" | "error"`. `loading` means there is no usable data yet. Once a hook has usable data, it stays `success` during background reloads and exposes that work through `isRefreshing` / `refreshError`.
|
|
240
|
+
|
|
241
|
+
## Commonly used types reference
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
interface UseHookOptions {
|
|
245
|
+
enabled?: boolean;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
interface UseResourceResult<TData> {
|
|
249
|
+
data: TData | undefined;
|
|
250
|
+
error: unknown;
|
|
251
|
+
refreshError: unknown;
|
|
252
|
+
isRefreshing: boolean;
|
|
253
|
+
status: "idle" | "loading" | "success" | "error";
|
|
254
|
+
refresh(): Promise<void>;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
interface UseQueryResult<TRow> extends UseResourceResult<TRow[]> {
|
|
258
|
+
rows: TRow[];
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### `useQuery`
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
function useQuery<
|
|
266
|
+
Schema extends DbSchema,
|
|
267
|
+
TCollection extends CollectionName<Schema>,
|
|
268
|
+
>(
|
|
269
|
+
db: Vennbase<Schema>,
|
|
270
|
+
collection: TCollection,
|
|
271
|
+
options: DbQueryOptions<Schema, TCollection> | null | undefined,
|
|
272
|
+
hookOptions?: UseHookOptions,
|
|
273
|
+
): UseQueryResult<
|
|
274
|
+
RowHandle<Schema, TCollection>
|
|
275
|
+
>
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
- `options: null | undefined` keeps the hook idle.
|
|
279
|
+
- `rows` is always an array and mirrors `data ?? []`.
|
|
280
|
+
- The row type matches `db.query(...)`, including parent collection constraints.
|
|
281
|
+
|
|
282
|
+
### `useRow`
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
function useRow<
|
|
286
|
+
Schema extends DbSchema,
|
|
287
|
+
TCollection extends CollectionName<Schema>,
|
|
288
|
+
>(
|
|
289
|
+
db: Vennbase<Schema>,
|
|
290
|
+
row: RowInput<TCollection> | null | undefined,
|
|
291
|
+
hookOptions?: UseHookOptions,
|
|
292
|
+
): UseResourceResult<
|
|
293
|
+
RowHandle<Schema, TCollection>
|
|
294
|
+
>
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
- `row: null | undefined` keeps the hook idle.
|
|
298
|
+
- `row` can be either a `RowHandle` or `RowRef`.
|
|
299
|
+
- `useRow` polls for changes and re-renders automatically. In React, prefer it over manual polling around `db.getRow(...)`.
|
|
300
|
+
- The returned handle matches `db.getRow(...)`, including parent collection constraints.
|
|
301
|
+
|
|
302
|
+
### `useShareLink`
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
function useShareLink<Schema extends DbSchema>(
|
|
306
|
+
db: Vennbase<Schema>,
|
|
307
|
+
row: RowInput | null | undefined,
|
|
308
|
+
options?: UseHookOptions,
|
|
309
|
+
): {
|
|
310
|
+
shareLink: string | undefined;
|
|
311
|
+
...
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
- `row: null | undefined` keeps the hook idle.
|
|
316
|
+
- `row` can be either a `RowHandle` or `RowRef`.
|
|
317
|
+
- `shareLink` is the generated or reused invite URL for the row.
|
|
318
|
+
|
|
319
|
+
### `useAcceptInviteFromUrl`
|
|
320
|
+
|
|
321
|
+
```ts
|
|
322
|
+
interface UseAcceptInviteFromUrlOptions<
|
|
323
|
+
Schema extends DbSchema,
|
|
324
|
+
TResult = AnyRowHandle<Schema>,
|
|
325
|
+
> extends UseHookOptions {
|
|
326
|
+
url?: string | null;
|
|
327
|
+
clearInviteParams?: boolean | ((url: URL) => string);
|
|
328
|
+
onOpen?: (result: TResult) => void | Promise<void>;
|
|
329
|
+
accept?: (inviteInput: string, db: Vennbase<Schema>) => Promise<TResult>;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
interface UseAcceptInviteFromUrlResult<TResult> extends UseResourceResult<TResult> {
|
|
333
|
+
hasInvite: boolean;
|
|
334
|
+
inviteInput: string | null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function useAcceptInviteFromUrl<
|
|
338
|
+
Schema extends DbSchema,
|
|
339
|
+
TResult = AnyRowHandle<Schema>,
|
|
340
|
+
>(
|
|
341
|
+
db: Vennbase<Schema>,
|
|
342
|
+
options?: UseAcceptInviteFromUrlOptions<Schema, TResult>,
|
|
343
|
+
): UseAcceptInviteFromUrlResult<TResult>
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
- `url` defaults to `window.location.href`.
|
|
347
|
+
- `clearInviteParams` defaults to `true`.
|
|
348
|
+
- `onOpen` runs after invite acceptance succeeds and may be async.
|
|
349
|
+
- The hook stays in `loading` until `onOpen` finishes and the invite params are removed from the current URL.
|
|
350
|
+
- Override `accept` when invite acceptance should return something other than `db.acceptInvite(...)`.
|
|
351
|
+
|
|
352
|
+
### `useMutation`
|
|
353
|
+
|
|
354
|
+
```ts
|
|
355
|
+
function useMutation<TArgs extends unknown[], TResult>(
|
|
356
|
+
fn: (...args: TArgs) => Promise<TResult>,
|
|
357
|
+
): {
|
|
358
|
+
mutate: (...args: TArgs) => Promise<TResult>;
|
|
359
|
+
data: TResult | undefined;
|
|
360
|
+
status: "idle" | "loading" | "success" | "error";
|
|
361
|
+
error: unknown;
|
|
362
|
+
reset(): void;
|
|
363
|
+
}
|
|
364
|
+
```
|
|
365
|
+
- Use it to wrap writes like `db.create(...)`, `db.update(...)`, or any other async workflow you want to expose as a React action state.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import type { Vennbase } from "@vennbase/core";
|
|
3
|
+
import type { AuthSession, AnyRowHandle, CollectionName, CrdtAdapter, CrdtConnectCallbacks, CrdtConnection, DbMemberInfo, DbQueryOptions, DbSchema, MemberRole, VennbaseUser, RowRef, RowInput } from "@vennbase/core";
|
|
4
|
+
import type { RowHandle } from "@vennbase/core";
|
|
5
|
+
import type { ActivitySubscriber } from "./polling";
|
|
6
|
+
import { type LoadStatus, type ResourceSnapshot } from "./runtime";
|
|
7
|
+
export type { ActivitySubscriber } from "./polling";
|
|
8
|
+
export type { LoadStatus } from "./runtime";
|
|
9
|
+
export interface UseResourceResult<TData> extends ResourceSnapshot<TData> {
|
|
10
|
+
refresh(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export interface UseQueryResult<TRow> extends UseResourceResult<TRow[]> {
|
|
13
|
+
rows: TRow[];
|
|
14
|
+
}
|
|
15
|
+
export type UseShareLinkResult = Omit<UseResourceResult<string>, "data"> & {
|
|
16
|
+
shareLink: string | undefined;
|
|
17
|
+
};
|
|
18
|
+
export interface UseSessionResult extends UseResourceResult<AuthSession> {
|
|
19
|
+
session: AuthSession | undefined;
|
|
20
|
+
signIn(): Promise<VennbaseUser>;
|
|
21
|
+
}
|
|
22
|
+
export interface UseCrdtResult<TValue> {
|
|
23
|
+
value: TValue;
|
|
24
|
+
version: number;
|
|
25
|
+
status: "idle" | "connected";
|
|
26
|
+
flush(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
export interface UseHookOptions {
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export interface UseAcceptInviteFromUrlOptions<Schema extends DbSchema, TResult = AnyRowHandle<Schema>> extends UseHookOptions {
|
|
32
|
+
url?: string | null;
|
|
33
|
+
clearInviteParams?: boolean | ((url: URL) => string);
|
|
34
|
+
onOpen?: (result: TResult) => void | Promise<void>;
|
|
35
|
+
accept?: (inviteInput: string, db: Vennbase<Schema>) => Promise<TResult>;
|
|
36
|
+
}
|
|
37
|
+
export interface UseAcceptInviteFromUrlResult<TResult> extends UseResourceResult<TResult> {
|
|
38
|
+
hasInvite: boolean;
|
|
39
|
+
inviteInput: string | null;
|
|
40
|
+
}
|
|
41
|
+
export interface UseSavedRowOptions<Schema extends DbSchema, TResult = AnyRowHandle<Schema>> extends UseHookOptions {
|
|
42
|
+
key: string;
|
|
43
|
+
url?: string | null;
|
|
44
|
+
clearInviteParams?: boolean | ((url: URL) => string);
|
|
45
|
+
loadSavedRow?: (row: AnyRowHandle<Schema>, db: Vennbase<Schema>) => Promise<TResult> | TResult;
|
|
46
|
+
acceptInvite?: (inviteInput: string, db: Vennbase<Schema>) => Promise<TResult>;
|
|
47
|
+
getRow?: (result: TResult) => RowRef;
|
|
48
|
+
}
|
|
49
|
+
export interface UseSavedRowResult<TResult> extends UseResourceResult<TResult | null> {
|
|
50
|
+
hasInvite: boolean;
|
|
51
|
+
inviteInput: string | null;
|
|
52
|
+
save(result: TResult): Promise<void>;
|
|
53
|
+
clear(): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
export interface VennbaseProviderProps<Schema extends DbSchema> {
|
|
56
|
+
children: ReactNode;
|
|
57
|
+
db: Vennbase<Schema>;
|
|
58
|
+
subscribeToActivity?: ActivitySubscriber;
|
|
59
|
+
client?: never;
|
|
60
|
+
}
|
|
61
|
+
interface CrdtRowLike {
|
|
62
|
+
ref: RowRef;
|
|
63
|
+
connectCrdt(callbacks: CrdtConnectCallbacks): CrdtConnection;
|
|
64
|
+
}
|
|
65
|
+
export declare function VennbaseProvider<Schema extends DbSchema>({ children, db, subscribeToActivity, }: VennbaseProviderProps<Schema>): import("react/jsx-runtime").JSX.Element;
|
|
66
|
+
export declare function useVennbase<Schema extends DbSchema>(): Vennbase<Schema>;
|
|
67
|
+
export declare function useCrdt<TValue>(row: CrdtRowLike | null | undefined, adapter: CrdtAdapter<TValue>): UseCrdtResult<TValue>;
|
|
68
|
+
export declare function useVennbaseReady<Schema extends DbSchema>(db: Vennbase<Schema>, options?: UseHookOptions): UseResourceResult<void>;
|
|
69
|
+
export declare function useSession<Schema extends DbSchema>(db: Vennbase<Schema>, options?: UseHookOptions): UseSessionResult;
|
|
70
|
+
export declare function useCurrentUser<Schema extends DbSchema>(db: Vennbase<Schema>, options?: UseHookOptions): UseResourceResult<VennbaseUser>;
|
|
71
|
+
export declare function useQuery<Schema extends DbSchema, TCollection extends CollectionName<Schema>>(db: Vennbase<Schema>, collection: TCollection, options: DbQueryOptions<Schema, TCollection> | null | undefined, hookOptions?: UseHookOptions): UseQueryResult<RowHandle<Schema, TCollection>>;
|
|
72
|
+
export declare function useRow<Schema extends DbSchema, TCollection extends CollectionName<Schema>>(db: Vennbase<Schema>, row: RowInput<TCollection> | null | undefined, options?: UseHookOptions): UseResourceResult<RowHandle<Schema, TCollection>>;
|
|
73
|
+
export declare function useParents<Schema extends DbSchema>(db: Vennbase<Schema>, row: RowInput | null | undefined, options?: UseHookOptions): UseResourceResult<Array<RowRef>>;
|
|
74
|
+
export declare function useMemberUsernames<Schema extends DbSchema>(db: Vennbase<Schema>, row: RowInput | null | undefined, options?: UseHookOptions): UseResourceResult<string[]>;
|
|
75
|
+
export declare function useDirectMembers<Schema extends DbSchema>(db: Vennbase<Schema>, row: RowInput | null | undefined, options?: UseHookOptions): UseResourceResult<Array<{
|
|
76
|
+
username: string;
|
|
77
|
+
role: MemberRole;
|
|
78
|
+
}>>;
|
|
79
|
+
export declare function useEffectiveMembers<Schema extends DbSchema>(db: Vennbase<Schema>, row: RowInput | null | undefined, options?: UseHookOptions): UseResourceResult<Array<DbMemberInfo<Schema>>>;
|
|
80
|
+
export declare function useShareLink<Schema extends DbSchema>(db: Vennbase<Schema>, row: RowInput | null | undefined, options?: UseHookOptions): UseShareLinkResult;
|
|
81
|
+
export declare function useAcceptInviteFromUrl<Schema extends DbSchema, TResult = AnyRowHandle<Schema>>(db: Vennbase<Schema>, options?: UseAcceptInviteFromUrlOptions<Schema, TResult>): UseAcceptInviteFromUrlResult<TResult>;
|
|
82
|
+
export declare function useSavedRow<Schema extends DbSchema, TResult = AnyRowHandle<Schema>>(db: Vennbase<Schema>, options: UseSavedRowOptions<Schema, TResult>): UseSavedRowResult<TResult>;
|
|
83
|
+
export interface MutationResult<TResult, TArgs extends unknown[]> {
|
|
84
|
+
data: TResult | undefined;
|
|
85
|
+
error: unknown;
|
|
86
|
+
mutate: (...args: TArgs) => Promise<TResult>;
|
|
87
|
+
reset(): void;
|
|
88
|
+
status: Exclude<LoadStatus, "idle"> | "idle";
|
|
89
|
+
}
|
|
90
|
+
export declare function useMutation<TArgs extends unknown[], TResult>(mutation: (...args: TArgs) => Promise<TResult>): MutationResult<TResult, TArgs>;
|