Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/events/src/Boot.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RouterProvider, createRouter } from '@tanstack/react-router';

import { Hypergraph } from '@graphprotocol/hypergraph-react';
import { HypergraphAppProvider } from '@graphprotocol/hypergraph-react';
import { PrivyProvider } from '@privy-io/react-auth';

import { routeTree } from './routeTree.gen';
Expand Down Expand Up @@ -33,9 +33,9 @@ export function Boot() {
},
}}
>
<Hypergraph.HypergraphAppProvider storage={localStorage}>
<HypergraphAppProvider storage={localStorage}>
<RouterProvider router={router} />
</Hypergraph.HypergraphAppProvider>
</HypergraphAppProvider>
</PrivyProvider>
);
}
4 changes: 2 additions & 2 deletions apps/events/src/components/dev-tool.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { store } from '@graphprotocol/hypergraph';
import { Hypergraph } from '@graphprotocol/hypergraph-react';
import { useHypergraphApp } from '@graphprotocol/hypergraph-react';
import { useSelector } from '@xstate/store/react';
import { useEffect, useState } from 'react';

Expand All @@ -10,7 +10,7 @@ export function DevTool({ spaceId }: { spaceId: string }) {

const spaces = useSelector(store, (state) => state.context.spaces);
const updatesInFlight = useSelector(store, (state) => state.context.updatesInFlight);
const { subscribeToSpace, loading } = Hypergraph.useHypergraphApp();
const { subscribeToSpace, loading } = useHypergraphApp();

useEffect(() => {
if (!loading) {
Expand Down
4 changes: 2 additions & 2 deletions apps/events/src/components/logout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Hypergraph } from '@graphprotocol/hypergraph-react';
import { useHypergraphApp } from '@graphprotocol/hypergraph-react';
import { usePrivy } from '@privy-io/react-auth';
import { useRouter } from '@tanstack/react-router';
import { Button } from './ui/button';

export function Logout() {
const { logout: graphLogout } = Hypergraph.useHypergraphApp();
const { logout: graphLogout } = useHypergraphApp();
const { logout: privyLogout } = usePrivy();
const router = useRouter();
const disconnectWallet = async () => {
Expand Down
10 changes: 5 additions & 5 deletions apps/events/src/components/todos.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Space } from '@graphprotocol/hypergraph-react';
import { useCreateEntity, useDeleteEntity, useQueryEntities, useUpdateEntity } from '@graphprotocol/hypergraph-react';
import { useState } from 'react';
import { Todo } from '../schema';
import { Button } from './ui/button';
import { Input } from './ui/input';

export const Todos = () => {
const todos = Space.useQueryEntities(Todo);
const createEntity = Space.useCreateEntity(Todo);
const updateEntity = Space.useUpdateEntity(Todo);
const deleteEntity = Space.useDeleteEntity();
const todos = useQueryEntities(Todo);
const createEntity = useCreateEntity(Todo);
const updateEntity = useUpdateEntity(Todo);
const deleteEntity = useDeleteEntity();
const [newTodoTitle, setNewTodoTitle] = useState('');

return (
Expand Down
7 changes: 3 additions & 4 deletions apps/events/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Logout } from '@/components/logout';
import { Hypergraph } from '@graphprotocol/hypergraph-react';
import { useHypergraphAuth } from '@graphprotocol/hypergraph-react';
import { Link, Outlet, createRootRoute, useLayoutEffect, useRouter } from '@tanstack/react-router';
import { TanStackRouterDevtools } from '@tanstack/router-devtools';

export const Route = createRootRoute({
component: () => {
const authenticated = Hypergraph.useAuthenticated();
const graphIdentity = Hypergraph.useHypergraphIdentity();
const { authenticated, identity } = useHypergraphAuth();

const router = useRouter();

Expand All @@ -33,7 +32,7 @@ export const Route = createRootRoute({
{authenticated ? (
<div className="flex items-center gap-2">
<span className="text-xs text-gray-500 dark:text-gray-400">
{graphIdentity?.accountId.substring(0, 6)}
{identity?.accountId.substring(0, 6)}
</span>
<Logout />
</div>
Expand Down
5 changes: 2 additions & 3 deletions apps/events/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { store } from '@graphprotocol/hypergraph';
import { Hypergraph } from '@graphprotocol/hypergraph-react';
import { useHypergraphApp } from '@graphprotocol/hypergraph-react';
import { Link, createFileRoute } from '@tanstack/react-router';
import { useSelector } from '@xstate/store/react';
import { useEffect } from 'react';
Expand All @@ -13,8 +13,7 @@ export const Route = createFileRoute('/')({

function Index() {
const spaces = useSelector(store, (state) => state.context.spaces);
const { createSpace, listSpaces, listInvitations, invitations, acceptInvitation, loading } =
Hypergraph.useHypergraphApp();
const { createSpace, listSpaces, listInvitations, invitations, acceptInvitation, loading } = useHypergraphApp();

console.log('Home page', { loading });

Expand Down
4 changes: 2 additions & 2 deletions apps/events/src/routes/login.lazy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Identity } from '@graphprotocol/hypergraph';
import { Hypergraph } from '@graphprotocol/hypergraph-react';
import { useHypergraphApp } from '@graphprotocol/hypergraph-react';
import { usePrivy, useWallets } from '@privy-io/react-auth';
import { createLazyFileRoute, useRouter } from '@tanstack/react-router';
import { Loader2 } from 'lucide-react';
Expand All @@ -17,7 +17,7 @@ export const Route = createLazyFileRoute('/login')({
function Login() {
const { ready: privyReady, login: privyLogin, signMessage, authenticated: privyAuthenticated } = usePrivy();
const { ready: walletsReady, wallets } = useWallets();
const { setIdentityAndSessionToken, login: hypergraphLogin } = Hypergraph.useHypergraphApp();
const { setIdentityAndSessionToken, login: hypergraphLogin } = useHypergraphApp();
const { navigate } = useRouter();
const [hypergraphLoginStarted, setHypergraphLoginStarted] = useState(false);

Expand Down
8 changes: 4 additions & 4 deletions apps/events/src/routes/space/$spaceId.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { store } from '@graphprotocol/hypergraph';
import { Hypergraph, Space as HypergraphSpace } from '@graphprotocol/hypergraph-react';
import { HypergraphSpaceProvider, useHypergraphApp } from '@graphprotocol/hypergraph-react';
import { createFileRoute } from '@tanstack/react-router';
import { useSelector } from '@xstate/store/react';

Expand All @@ -17,7 +17,7 @@ export const Route = createFileRoute('/space/$spaceId')({
function Space() {
const { spaceId } = Route.useParams();
const spaces = useSelector(store, (state) => state.context.spaces);
const { subscribeToSpace, inviteToSpace, loading } = Hypergraph.useHypergraphApp();
const { subscribeToSpace, inviteToSpace, loading } = useHypergraphApp();
useEffect(() => {
if (!loading) {
subscribeToSpace({ spaceId });
Expand All @@ -36,7 +36,7 @@ function Space() {

return (
<div className="flex flex-col gap-4 max-w-screen-sm mx-auto py-8">
<HypergraphSpace.HypergraphProvider space={spaceId}>
<HypergraphSpaceProvider space={spaceId}>
<Todos />
<h3 className="text-xl font-bold">Invite people</h3>
<div className="flex flex-row gap-2">
Expand All @@ -59,7 +59,7 @@ function Space() {
<div className="mt-12">
<DevTool spaceId={spaceId} />
</div>
</HypergraphSpace.HypergraphProvider>
</HypergraphSpaceProvider>
</div>
);
}
16 changes: 4 additions & 12 deletions packages/hypergraph-react/src/HypergraphAppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,9 @@ export function useHypergraphApp() {
return useContext<HypergraphAppCtx>(HypergraphAppContext);
}

export function useAuthenticated() {
return useSelectorStore(store, (state) => state.context.authenticated);
}
export function useHypergraphAccountId() {
return useSelectorStore(store, (state) => state.context.accountId);
}
export function useHypergraphIdentity() {
const accountId = useHypergraphAccountId();
export function useHypergraphAuth() {
const authenticated = useSelectorStore(store, (state) => state.context.authenticated);
const accountId = useSelectorStore(store, (state) => state.context.accountId);
const keys = useSelectorStore(store, (state) => state.context.keys);
const identity: Identity.Identity | null =
accountId && keys
Expand All @@ -95,10 +90,7 @@ export function useHypergraphIdentity() {
signaturePrivateKey: keys.signaturePrivateKey,
}
: null;
return identity;
}
export function useHypergraphSessionToken() {
return useSelectorStore(store, (state) => state.context.sessionToken);
return { authenticated, identity };
}

export type HypergraphAppProviderProps = Readonly<{
Expand Down
2 changes: 1 addition & 1 deletion packages/hypergraph-react/src/HypergraphSpaceContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useHypergraph() {
return context as HypergraphContext;
}

export function HypergraphProvider({ space, children }: { space: string; children: ReactNode }) {
export function HypergraphSpaceProvider({ space, children }: { space: string; children: ReactNode }) {
const repo = useRepo();
const ref = useRef<HypergraphContext | undefined>(undefined);

Expand Down
15 changes: 13 additions & 2 deletions packages/hypergraph-react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
export * as Hypergraph from './HypergraphAppContext.js';
export * as Space from './HypergraphSpaceContext.js';
export {
HypergraphAppProvider,
useHypergraphApp,
useHypergraphAuth,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any better ideas for names than useHypergraphApp and useHypergraphAuth?

} from './HypergraphAppContext.js';
export {
HypergraphSpaceProvider,
useCreateEntity,
useDeleteEntity,
useQueryEntities,
useQueryEntity,
useUpdateEntity,
} from './HypergraphSpaceContext.js';
9 changes: 4 additions & 5 deletions packages/hypergraph-react/test/HypergraphAppContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { cleanup, renderHook } from '@testing-library/react';
import React from 'react';
import { afterEach, describe, expect, it } from 'vitest';

import { HypergraphAppProvider, useAuthenticated, useHypergraphAccountId } from '../src/HypergraphAppContext.js';
import { HypergraphAppProvider, useHypergraphAuth } from '../src/HypergraphAppContext.js';

afterEach(() => {
cleanup();
Expand All @@ -29,9 +29,8 @@ describe('HypergraphAppContext', () => {
<HypergraphAppProvider storage={storageMock}>{children}</HypergraphAppProvider>
);

const { result: authenticatedResult } = renderHook(() => useAuthenticated(), { wrapper });
expect(authenticatedResult.current).toEqual(false);
const { result: accountIdResult } = renderHook(() => useHypergraphAccountId(), { wrapper });
expect(accountIdResult.current).toBeNull();
const { result: authenticatedResult } = renderHook(() => useHypergraphAuth(), { wrapper });
expect(authenticatedResult.current.authenticated).toEqual(false);
expect(authenticatedResult.current.identity).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import '@testing-library/jest-dom/vitest';
import { type AnyDocumentId, Repo } from '@automerge/automerge-repo';
import { RepoContext } from '@automerge/automerge-repo-react-hooks';
import { Entity, Utils } from '@graphprotocol/hypergraph';
import '@testing-library/jest-dom/vitest';
import { act, cleanup, renderHook, waitFor } from '@testing-library/react';
// biome-ignore lint/style/useImportType: <explanation>
import React from 'react';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
HypergraphProvider,
HypergraphSpaceProvider,
useCreateEntity,
useDeleteEntity,
useQueryEntities,
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('HypergraphSpaceContext', () => {
let repo = new Repo({});
let wrapper = ({ children }: Readonly<{ children: React.ReactNode }>) => (
<RepoContext.Provider value={repo}>
<HypergraphProvider space={spaceId}>{children}</HypergraphProvider>
<HypergraphSpaceProvider space={spaceId}>{children}</HypergraphSpaceProvider>
</RepoContext.Provider>
);

Expand All @@ -54,7 +54,7 @@ describe('HypergraphSpaceContext', () => {

wrapper = ({ children }: Readonly<{ children: React.ReactNode }>) => (
<RepoContext.Provider value={repo}>
<HypergraphProvider space={spaceId}>{children}</HypergraphProvider>
<HypergraphSpaceProvider space={spaceId}>{children}</HypergraphSpaceProvider>
</RepoContext.Provider>
);
});
Expand Down
Loading