Skip to content

Commit 9f4054b

Browse files
authored
Merge pull request #103 from graphprotocol/chris.whited/feat-react-pkg
feat(react package): move react usage out of @graphprotocol/hypergraph and into own @graphprotocol/hypergraph-react
2 parents 98d553a + 607bc85 commit 9f4054b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1804
-1082
lines changed

apps/events/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
"@automerge/automerge-repo": "^2.0.0-alpha.14",
1313
"@automerge/automerge-repo-react-hooks": "^2.0.0-alpha.14",
1414
"@graphprotocol/hypergraph": "workspace:*",
15+
"@graphprotocol/hypergraph-react": "workspace:*",
1516
"@noble/hashes": "^1.7.0",
1617
"@privy-io/react-auth": "^2.0.3",
1718
"@radix-ui/react-avatar": "^1.1.2",
1819
"@radix-ui/react-icons": "^1.3.2",
1920
"@radix-ui/react-slot": "^1.1.1",
2021
"@tanstack/react-router": "^1.97.1",
22+
"@xstate/store": "^2.6.2",
2123
"class-variance-authority": "^0.7.1",
2224
"clsx": "^2.1.1",
2325
"effect": "^3.12.4",

apps/events/src/Boot.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { RouterProvider, createRouter } from '@tanstack/react-router';
22

3-
import { Identity } from '@graphprotocol/hypergraph';
3+
import { Auth } from '@graphprotocol/hypergraph-react';
44
import { PrivyProvider } from '@privy-io/react-auth';
5+
56
import { routeTree } from './routeTree.gen';
67

78
// Create a new router instance
@@ -32,9 +33,9 @@ export function Boot() {
3233
},
3334
}}
3435
>
35-
<Identity.GraphLogin storage={localStorage}>
36+
<Auth.HypergraphAuthProvider storage={localStorage}>
3637
<RouterProvider router={router} />
37-
</Identity.GraphLogin>
38+
</Auth.HypergraphAuthProvider>
3839
</PrivyProvider>
3940
);
4041
}

apps/events/src/components/dev-tool.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
import { store, useGraphFramework, useSelector } from '@graphprotocol/hypergraph';
1+
import { store } from '@graphprotocol/hypergraph';
2+
import { Hypergraph } from '@graphprotocol/hypergraph-react';
3+
import { useSelector } from '@xstate/store/react';
24
import { useEffect, useState } from 'react';
5+
36
import { Button } from './ui/button';
47

58
export function DevTool({ spaceId }: { spaceId: string }) {
69
const [isOpen, setIsOpen] = useState(false);
710

811
const spaces = useSelector(store, (state) => state.context.spaces);
912
const updatesInFlight = useSelector(store, (state) => state.context.updatesInFlight);
10-
const { subscribeToSpace, isLoading } = useGraphFramework();
13+
const { subscribeToSpace, loading } = Hypergraph.useHypergraphApp();
1114

1215
useEffect(() => {
13-
if (!isLoading) {
16+
if (!loading) {
1417
subscribeToSpace({ spaceId });
1518
}
16-
}, [isLoading, subscribeToSpace, spaceId]);
19+
}, [loading, subscribeToSpace, spaceId]);
1720

1821
const space = spaces.find((space) => space.id === spaceId);
1922

apps/events/src/components/logout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Identity } from '@graphprotocol/hypergraph';
1+
import { Auth } from '@graphprotocol/hypergraph-react';
22
import { usePrivy } from '@privy-io/react-auth';
33
import { useRouter } from '@tanstack/react-router';
44
import { Button } from './ui/button';
55

66
export function Logout() {
7-
const { logout: graphLogout } = Identity.useGraphLogin();
7+
const { logout: graphLogout } = Auth.useHypergraphAuth();
88
const { logout: privyLogout } = usePrivy();
99
const router = useRouter();
1010
const disconnectWallet = async () => {

apps/events/src/components/todos.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { useState } from 'react';
22

3-
import { Schema } from '@graphprotocol/hypergraph';
3+
import { Space } from '@graphprotocol/hypergraph-react';
44

55
import { Todo } from '../schema';
66
import { Button } from './ui/button';
77
import { Input } from './ui/input';
88

99
export const Todos = () => {
10-
const todos = Schema.useQuery(Todo);
11-
const createEntity = Schema.useCreateEntity(Todo);
12-
const updateEntity = Schema.useUpdateEntity(Todo);
13-
const deleteEntity = Schema.useDeleteEntity();
10+
const todos = Space.useQueryEntities(Todo);
11+
const createEntity = Space.useCreateEntity(Todo);
12+
const updateEntity = Space.useUpdateEntity(Todo);
13+
const deleteEntity = Space.useDeleteEntity();
1414
const [newTodoTitle, setNewTodoTitle] = useState('');
1515

1616
return (
@@ -27,13 +27,13 @@ export const Todos = () => {
2727
Create Todo
2828
</Button>
2929
</div>
30-
{todos.map((todo) => (
30+
{(todos ?? []).map((todo) => (
3131
<div key={todo.id} className="flex flex-row items-center gap-2">
3232
<h2>{todo.name}</h2>
3333
<input
3434
type="checkbox"
3535
checked={todo.completed}
36-
onChange={(e) => updateEntity(todo.id, { completed: e.target.checked })}
36+
onChange={(e) => updateEntity({ id: todo.id, data: { completed: e.target.checked } })}
3737
/>
3838
<Button onClick={() => deleteEntity(todo.id)}>Delete</Button>
3939
</div>

apps/events/src/routes/__root.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Logout } from '@/components/logout';
2-
import { GraphFramework, Identity } from '@graphprotocol/hypergraph';
2+
import { Auth, Hypergraph } from '@graphprotocol/hypergraph-react';
33
import { Link, Outlet, createRootRoute, useLayoutEffect, useRouter } from '@tanstack/react-router';
44
import { TanStackRouterDevtools } from '@tanstack/router-devtools';
55

66
export const Route = createRootRoute({
77
component: () => {
8-
const { authenticated, getIdentity, getSessionToken } = Identity.useGraphLogin();
8+
const { authenticated, getIdentity, getSessionToken } = Auth.useHypergraphAuth();
9+
910
const graphIdentity = getIdentity();
1011
const loggedInSessionToken = getSessionToken();
1112

@@ -44,7 +45,7 @@ export const Route = createRootRoute({
4445
<hr />
4546

4647
{authenticated && graphIdentity && loggedInSessionToken ? (
47-
<GraphFramework
48+
<Hypergraph.HypergraphAppProvider
4849
accountId={graphIdentity.accountId}
4950
sessionToken={loggedInSessionToken}
5051
encryptionPublicKey={graphIdentity.encryptionPublicKey}
@@ -53,7 +54,7 @@ export const Route = createRootRoute({
5354
signaturePublicKey={graphIdentity.signaturePublicKey}
5455
>
5556
<Outlet />
56-
</GraphFramework>
57+
</Hypergraph.HypergraphAppProvider>
5758
) : (
5859
<Outlet />
5960
)}

apps/events/src/routes/index.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
import { store, useGraphFramework, useSelector } from '@graphprotocol/hypergraph';
1+
import { store } from '@graphprotocol/hypergraph';
2+
import { Hypergraph } from '@graphprotocol/hypergraph-react';
23
import { Link, createFileRoute } from '@tanstack/react-router';
4+
import { useSelector } from '@xstate/store/react';
5+
import { useEffect } from 'react';
36

47
import { Button } from '@/components/ui/button';
58
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
6-
import { useEffect } from 'react';
79

810
export const Route = createFileRoute('/')({
911
component: Index,
1012
});
1113

1214
function Index() {
1315
const spaces = useSelector(store, (state) => state.context.spaces);
14-
const { createSpace, listSpaces, listInvitations, invitations, acceptInvitation, isLoading } = useGraphFramework();
16+
const { createSpace, listSpaces, listInvitations, invitations, acceptInvitation, loading } =
17+
Hypergraph.useHypergraphApp();
18+
19+
console.log('Home page', { loading });
1520

1621
useEffect(() => {
17-
if (!isLoading) {
22+
if (!loading) {
1823
listSpaces();
1924
listInvitations();
2025
}
21-
}, [listSpaces, listInvitations, isLoading]);
26+
}, [listSpaces, listInvitations, loading]);
2227

23-
if (isLoading) {
28+
if (loading) {
2429
return <div className="flex justify-center items-center h-screen">Loading …</div>;
2530
}
2631

apps/events/src/routes/login.lazy.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
import { Button } from '@/components/ui/button';
2-
import { availableAccounts } from '@/lib/availableAccounts';
3-
import { Identity } from '@graphprotocol/hypergraph';
1+
import type { Identity } from '@graphprotocol/hypergraph';
2+
import { Auth } from '@graphprotocol/hypergraph-react';
43
import { usePrivy, useWallets } from '@privy-io/react-auth';
54
import { createLazyFileRoute, useRouter } from '@tanstack/react-router';
65
import { Loader2 } from 'lucide-react';
76
import { useEffect, useState } from 'react';
87
import { createWalletClient, custom } from 'viem';
98
import { mainnet } from 'viem/chains';
109

10+
import { Button } from '@/components/ui/button';
11+
import { availableAccounts } from '@/lib/availableAccounts';
12+
1113
export const Route = createLazyFileRoute('/login')({
1214
component: () => <Login />,
1315
});
1416

1517
function Login() {
1618
const { ready: privyReady, login: privyLogin, signMessage, authenticated: privyAuthenticated } = usePrivy();
1719
const { ready: walletsReady, wallets } = useWallets();
18-
const { setIdentityAndSessionToken, login: hypergraphLogin } = Identity.useGraphLogin();
20+
const { setIdentityAndSessionToken, login: hypergraphLogin } = Auth.useHypergraphAuth();
1921
const { navigate } = useRouter();
2022
const [hypergraphLoginStarted, setHypergraphLoginStarted] = useState(false);
2123

apps/events/src/routes/space/$spaceId.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { Schema, store, useGraphFramework, useSelector } from '@graphprotocol/hypergraph';
1+
import { store } from '@graphprotocol/hypergraph';
2+
import { Hypergraph, Space as HypergraphSpace } from '@graphprotocol/hypergraph-react';
23
import { createFileRoute } from '@tanstack/react-router';
4+
import { useSelector } from '@xstate/store/react';
35

46
import { DevTool } from '@/components/dev-tool';
57
import { Todos } from '@/components/todos';
68
import { Button } from '@/components/ui/button';
79
import { availableAccounts } from '@/lib/availableAccounts';
810
import { useEffect } from 'react';
11+
import { getAddress } from 'viem';
912

1013
export const Route = createFileRoute('/space/$spaceId')({
1114
component: Space,
@@ -14,16 +17,16 @@ export const Route = createFileRoute('/space/$spaceId')({
1417
function Space() {
1518
const { spaceId } = Route.useParams();
1619
const spaces = useSelector(store, (state) => state.context.spaces);
17-
const { subscribeToSpace, inviteToSpace, isLoading } = useGraphFramework();
20+
const { subscribeToSpace, inviteToSpace, loading } = Hypergraph.useHypergraphApp();
1821
useEffect(() => {
19-
if (!isLoading) {
22+
if (!loading) {
2023
subscribeToSpace({ spaceId });
2124
}
22-
}, [isLoading, subscribeToSpace, spaceId]);
25+
}, [loading, subscribeToSpace, spaceId]);
2326

2427
const space = spaces.find((space) => space.id === spaceId);
2528

26-
if (isLoading) {
29+
if (loading) {
2730
return <div className="flex justify-center items-center h-screen">Loading …</div>;
2831
}
2932

@@ -33,7 +36,7 @@ function Space() {
3336

3437
return (
3538
<div className="flex flex-col gap-4 max-w-screen-sm mx-auto py-8">
36-
<Schema.SpacesProvider defaultSpace={space.id}>
39+
<HypergraphSpace.HypergraphSpaceProvider defaultSpaceId={space.id} spaces={[space.id]}>
3740
<Todos />
3841
<h3 className="text-xl font-bold">Invite people</h3>
3942
<div className="flex flex-row gap-2">
@@ -44,7 +47,7 @@ function Space() {
4447
onClick={() => {
4548
inviteToSpace({
4649
space,
47-
invitee,
50+
invitee: { accountId: getAddress(invitee.accountId) },
4851
});
4952
}}
5053
>
@@ -56,7 +59,7 @@ function Space() {
5659
<div className="mt-12">
5760
<DevTool spaceId={spaceId} />
5861
</div>
59-
</Schema.SpacesProvider>
62+
</HypergraphSpace.HypergraphSpaceProvider>
6063
</div>
6164
);
6265
}

apps/events/src/schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Schema } from '@graphprotocol/hypergraph';
22

3-
export class Todo extends Schema.Model.Class<Todo>('Todo')({
4-
id: Schema.Model.Generated(Schema.Types.Text),
5-
name: Schema.Types.Text,
6-
completed: Schema.Types.Checkbox,
3+
export class Todo extends Schema.Class<Todo>('Todo')({
4+
id: Schema.Generated(Schema.Text),
5+
name: Schema.Text,
6+
completed: Schema.Checkbox,
77
}) {}

0 commit comments

Comments
 (0)