|
1 | | -import { analyticsServerProxy, apiServerProxy } from "@/actions/proxies"; |
| 1 | +import { apiServerProxy } from "@/actions/proxies"; |
2 | 2 | import type { Project } from "@/api/projects"; |
3 | 3 | import type { Team } from "@/api/team"; |
4 | 4 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
5 | | -import { useAllChainsData } from "hooks/chains/allChains"; |
6 | 5 | import { useActiveAccount } from "thirdweb/react"; |
7 | | -import type { UserOpStats } from "types/analytics"; |
8 | 6 | import { accountKeys, authorizedWallets } from "../cache-keys"; |
9 | 7 |
|
10 | 8 | // FIXME: We keep repeating types, API server should provide them |
@@ -142,132 +140,6 @@ export function useAccountCredits() { |
142 | 140 | }); |
143 | 141 | } |
144 | 142 |
|
145 | | -type UserOpUsageQueryResult = (UserOpStats & { chainId?: string })[]; |
146 | | - |
147 | | -async function getUserOpUsage(args: { |
148 | | - clientId: string; |
149 | | - from?: Date; |
150 | | - to?: Date; |
151 | | - period?: "day" | "week" | "month" | "year" | "all"; |
152 | | -}) { |
153 | | - const { clientId, from, to, period } = args; |
154 | | - |
155 | | - const searchParams: Record<string, string> = { |
156 | | - clientId, |
157 | | - }; |
158 | | - |
159 | | - if (from) { |
160 | | - searchParams.from = from.toISOString(); |
161 | | - } |
162 | | - if (to) { |
163 | | - searchParams.to = to.toISOString(); |
164 | | - } |
165 | | - if (period) { |
166 | | - searchParams.period = period; |
167 | | - } |
168 | | - |
169 | | - const res = await analyticsServerProxy<{ data: UserOpUsageQueryResult }>({ |
170 | | - pathname: "/v1/user-ops", |
171 | | - method: "GET", |
172 | | - searchParams: searchParams, |
173 | | - headers: { |
174 | | - "Content-Type": "application/json", |
175 | | - }, |
176 | | - }); |
177 | | - |
178 | | - if (!res.ok) { |
179 | | - throw new Error(res.error); |
180 | | - } |
181 | | - |
182 | | - const json = res.data; |
183 | | - |
184 | | - return json.data; |
185 | | -} |
186 | | - |
187 | | -// TODO - remove this hook, fetch this on server |
188 | | -export function useUserOpUsageAggregate(args: { |
189 | | - clientId: string; |
190 | | - from?: Date; |
191 | | - to?: Date; |
192 | | -}) { |
193 | | - const { clientId, from, to } = args; |
194 | | - const address = useActiveAccount()?.address; |
195 | | - const chainStore = useAllChainsData(); |
196 | | - |
197 | | - return useQuery<UserOpStats>({ |
198 | | - queryKey: accountKeys.userOpStats( |
199 | | - address || "", |
200 | | - clientId, |
201 | | - from?.toISOString() || "", |
202 | | - to?.toISOString() || "", |
203 | | - "all", |
204 | | - ), |
205 | | - queryFn: async () => { |
206 | | - const userOpStats = await getUserOpUsage({ |
207 | | - clientId, |
208 | | - from, |
209 | | - to, |
210 | | - period: "all", |
211 | | - }); |
212 | | - |
213 | | - // Aggregate stats across wallet types |
214 | | - return userOpStats.reduce( |
215 | | - (acc, curr) => { |
216 | | - // Skip testnets from the aggregated stats |
217 | | - if (curr.chainId) { |
218 | | - const chain = chainStore.idToChain.get(Number(curr.chainId)); |
219 | | - if (chain?.testnet) { |
220 | | - return acc; |
221 | | - } |
222 | | - } |
223 | | - |
224 | | - acc.successful += curr.successful; |
225 | | - acc.failed += curr.failed; |
226 | | - acc.sponsoredUsd += curr.sponsoredUsd; |
227 | | - return acc; |
228 | | - }, |
229 | | - { |
230 | | - date: (from || new Date()).toISOString(), |
231 | | - successful: 0, |
232 | | - failed: 0, |
233 | | - sponsoredUsd: 0, |
234 | | - }, |
235 | | - ); |
236 | | - }, |
237 | | - enabled: !!clientId && !!address, |
238 | | - }); |
239 | | -} |
240 | | - |
241 | | -// TODO - remove this hook, fetch this on server |
242 | | -export function useUserOpUsagePeriod(args: { |
243 | | - clientId: string; |
244 | | - from?: Date; |
245 | | - to?: Date; |
246 | | - period: "day" | "week" | "month" | "year"; |
247 | | -}) { |
248 | | - const { clientId, from, to, period } = args; |
249 | | - const address = useActiveAccount()?.address; |
250 | | - |
251 | | - return useQuery({ |
252 | | - queryKey: accountKeys.userOpStats( |
253 | | - address || "", |
254 | | - clientId as string, |
255 | | - from?.toISOString() || "", |
256 | | - to?.toISOString() || "", |
257 | | - period, |
258 | | - ), |
259 | | - queryFn: async () => { |
260 | | - return getUserOpUsage({ |
261 | | - clientId, |
262 | | - from, |
263 | | - to, |
264 | | - period, |
265 | | - }); |
266 | | - }, |
267 | | - enabled: !!clientId && !!address, |
268 | | - }); |
269 | | -} |
270 | | - |
271 | 143 | export function useUpdateAccount() { |
272 | 144 | const queryClient = useQueryClient(); |
273 | 145 | const address = useActiveAccount()?.address; |
|
0 commit comments