-
Notifications
You must be signed in to change notification settings - Fork 11
/
GatherContractLoader.tsx
146 lines (133 loc) · 3.62 KB
/
GatherContractLoader.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
AoGatherProvider,
type ArweaveAddress,
type ContractPost,
type ContractUser,
type ContractWorld,
type ContractWorldIndex,
} from "@/features/ao/lib/ao-gather";
import { useSuspenseQuery } from "@tanstack/react-query";
import { useState } from "react";
export type GatherContractState = {
worldIndex: ContractWorldIndex;
worldId: string;
world: ContractWorld;
users: Record<ArweaveAddress, ContractUser>;
posts: Record<string, ContractPost>;
};
export type GatherContactEvents = {
setWorldId: (worldId: string) => Promise<void>;
} & Pick<
AoGatherProvider,
"register" | "updateUser" | "updatePosition" | "post" | "follow" | "unfollow"
>;
const aoGather = new AoGatherProvider({});
interface Props {
children: (
gatherContractState: GatherContractState,
gatherContactEvents: GatherContactEvents,
) => React.ReactNode;
initialWorldId?: string;
}
export const GatherContractLoader = ({ children, initialWorldId }: Props) => {
const [worldId, setWorldId] = useState(initialWorldId ?? "WelcomeLobby");
const {
data: users,
error: errorUsers,
refetch: refetchUsers,
} = useSuspenseQuery({
queryKey: ["users"],
queryFn: async () => {
console.log("fetching users");
aoGather.ensureStarted();
return aoGather.getUsers();
},
refetchInterval: 10000,
});
const { data: worldIndex } = useSuspenseQuery({
queryKey: ["worldIndex"],
queryFn: async () => {
console.log("fetching world Index");
aoGather.ensureStarted();
return aoGather.getWorldIndex();
},
refetchInterval: 10000,
});
const {
data: world,
error: errorWorld,
refetch: refetchWorld,
} = useSuspenseQuery({
queryKey: ["world", worldId],
queryFn: async () => {
console.log("fetching world");
aoGather.ensureStarted();
return aoGather.getWorld({
worldId: worldId,
});
},
refetchInterval: 500,
});
const {
data: posts,
error: errorPosts,
refetch: refetchPosts,
} = useSuspenseQuery({
queryKey: ["posts", /* worldId */],
queryFn: async () => {
console.log("fetching posts");
aoGather.ensureStarted();
return aoGather.getPosts();
// If you want to fetch posts for a specific world, just pass the worldId
// return aoGather.getPosts({ worldId });
},
refetchInterval: 5000,
});
if (errorUsers !== null || errorPosts !== null || errorWorld !== null) {
return (
<div className="h-screen w-screen text-center flex flex-col justify-center">
<p className="text-xl">Error Loading</p>
</div>
);
}
const state: GatherContractState = {
worldId,
worldIndex,
users,
world,
posts,
};
const events: GatherContactEvents = {
setWorldId: async (worldId) => {
setWorldId(worldId);
await aoGather.updateUser({ currentWorldId: worldId })
await refetchUsers();
// Will automatically refetch new world and posts, so no need to do this manually
},
register: async (args) => {
await aoGather.register(args);
await refetchUsers();
},
updateUser: async (args) => {
await aoGather.updateUser(args);
await refetchUsers();
},
updatePosition: async (args) => {
await aoGather.updatePosition(args);
await refetchWorld();
},
post: async (args) => {
await aoGather.post(args);
await refetchPosts();
},
follow: async (args) => {
await aoGather.follow(args);
await refetchUsers();
},
unfollow: async (args) => {
await aoGather.unfollow(args);
await refetchUsers();
},
};
return children(state, events);
};