Skip to content

Commit 929e1ea

Browse files
authored
Merge pull request #98 from geobrowser/ng/fix-app
2 parents 8adca2e + 9a505e4 commit 929e1ea

File tree

6 files changed

+29
-35
lines changed

6 files changed

+29
-35
lines changed

apps/events/src/components/auth.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { useEffect, useState } from 'react';
44

55
function DoGraphLogin() {
66
const { login } = Identity.useGraphLogin();
7+
// biome-ignore lint/correctness/useExhaustiveDependencies: this is an issue and will make sure login is not run in a useEffect
78
useEffect(() => {
89
console.log('Logging in to The Graph');
910
login();
10-
}, [login]);
11+
}, []);
1112
return <div />;
1213
}
1314

apps/events/src/routes/__root.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Logout } from '@/components/logout';
22
import { GraphFramework, Identity } from '@graphprotocol/hypergraph';
3-
import { Link, Outlet, createRootRoute } from '@tanstack/react-router';
3+
import { Link, Outlet, createRootRoute, useLayoutEffect, useRouter } from '@tanstack/react-router';
44
import { TanStackRouterDevtools } from '@tanstack/router-devtools';
55

66
export const Route = createRootRoute({
@@ -10,6 +10,20 @@ export const Route = createRootRoute({
1010
const graphIdentity = getIdentity();
1111
const loggedInSessionToken = getSessionToken();
1212

13+
const router = useRouter();
14+
15+
useLayoutEffect(() => {
16+
if (router.state.location.href.startsWith('/login')) {
17+
return;
18+
}
19+
20+
if (!authenticated) {
21+
router.navigate({
22+
to: '/login',
23+
});
24+
}
25+
}, [authenticated]);
26+
1327
return (
1428
<>
1529
<div className="flex flex-col min-h-screen">

apps/events/src/routes/index.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Identity, store, useGraphFramework, useSelector } from '@graphprotocol/hypergraph';
1+
import { store, useGraphFramework, useSelector } from '@graphprotocol/hypergraph';
22
import { Link, createFileRoute } from '@tanstack/react-router';
33

44
import { Button } from '@/components/ui/button';
@@ -12,30 +12,18 @@ export const Route = createFileRoute('/')({
1212
function Index() {
1313
const spaces = useSelector(store, (state) => state.context.spaces);
1414
const { createSpace, listSpaces, listInvitations, invitations, acceptInvitation, isLoading } = useGraphFramework();
15-
const { authenticated } = Identity.useGraphLogin();
1615

1716
useEffect(() => {
18-
if (authenticated && !isLoading) {
17+
if (!isLoading) {
1918
listSpaces();
2019
listInvitations();
2120
}
22-
}, [authenticated, listSpaces, listInvitations, isLoading]);
21+
}, [listSpaces, listInvitations, isLoading]);
2322

2423
if (isLoading) {
2524
return <div className="flex justify-center items-center h-screen">Loading …</div>;
2625
}
2726

28-
if (!authenticated) {
29-
return (
30-
<div className="flex flex-col gap-4 justify-center items-center h-screen">
31-
<h1 className="text-2xl font-bold">Not authenticated</h1>
32-
<Link to="/login" className="text-blue-500 hover:text-blue-600 underline">
33-
Go to Login
34-
</Link>
35-
</div>
36-
);
37-
}
38-
3927
return (
4028
<div className="flex flex-col gap-4 max-w-screen-sm mx-auto py-8">
4129
<h2 className="text-lg font-bold">Invitations</h2>

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Identity, Schema, store, useGraphFramework, useSelector } from '@graphprotocol/hypergraph';
2-
import { Link, createFileRoute } from '@tanstack/react-router';
1+
import { Schema, store, useGraphFramework, useSelector } from '@graphprotocol/hypergraph';
2+
import { createFileRoute } from '@tanstack/react-router';
33

44
import { DevTool } from '@/components/dev-tool';
55
import { Todos } from '@/components/todos';
@@ -15,12 +15,11 @@ function Space() {
1515
const { spaceId } = Route.useParams();
1616
const spaces = useSelector(store, (state) => state.context.spaces);
1717
const { subscribeToSpace, inviteToSpace, isLoading } = useGraphFramework();
18-
const { authenticated } = Identity.useGraphLogin();
1918
useEffect(() => {
20-
if (!isLoading && authenticated) {
19+
if (!isLoading) {
2120
subscribeToSpace({ spaceId });
2221
}
23-
}, [isLoading, subscribeToSpace, spaceId, authenticated]);
22+
}, [isLoading, subscribeToSpace, spaceId]);
2423

2524
const space = spaces.find((space) => space.id === spaceId);
2625

@@ -32,17 +31,6 @@ function Space() {
3231
return <div className="flex justify-center items-center h-screen">Space not found</div>;
3332
}
3433

35-
if (!authenticated) {
36-
return (
37-
<div className="flex flex-col gap-4 justify-center items-center h-screen">
38-
<h1 className="text-2xl font-bold">Not authenticated</h1>
39-
<Link to="/login" className="text-blue-500 hover:text-blue-600 underline">
40-
Go to Login
41-
</Link>
42-
</div>
43-
);
44-
}
45-
4634
return (
4735
<div className="flex flex-col gap-4 max-w-screen-sm mx-auto py-8">
4836
<Schema.SpacesProvider defaultSpace={space.id}>

packages/hypergraph/src/core.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export function GraphFramework({
106106
const syncServerWsUrl = new URL(`/?token=${sessionToken}`, syncServerUrl.toString());
107107
syncServerWsUrl.protocol = 'ws:';
108108
const syncServerWsUrlString = syncServerWsUrl.toString();
109+
109110
// Create a stable WebSocket connection that only depends on accountId
110111
useEffect(() => {
111112
if (!sessionToken) {

packages/hypergraph/src/identity/graph-login.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Schema } from 'effect';
2-
import { createContext, useCallback, useContext, useEffect, useState } from 'react';
2+
import { createContext, useCallback, useContext, useLayoutEffect, useState } from 'react';
33
import { SiweMessage } from 'siwe';
44
import type { Hex } from 'viem';
55
import { privateKeyToAccount } from 'viem/accounts';
@@ -317,7 +317,9 @@ export function GraphLogin({
317317
store.send({ type: 'reset' });
318318
};
319319

320-
useEffect(() => {
320+
// using a layout effect to set the state before the component is mounted and
321+
// avoid a flash of unauthenticated content
322+
useLayoutEffect(() => {
321323
const accountId = loadAccountId(storage);
322324
if (accountId) {
323325
const sessionToken = loadSyncServerSessionToken(storage, accountId);

0 commit comments

Comments
 (0)