From 6c19459b568d239a82c59c1b22c9e25b408417d8 Mon Sep 17 00:00:00 2001 From: luna Date: Thu, 2 Jan 2025 13:35:16 +1030 Subject: [PATCH] feat: sky zoo --- src/hooks/useSkyZoo.test.tsx | 24 ++++++++++++++++++++++++ src/hooks/useSkyZoo.ts | 17 +++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/hooks/useSkyZoo.test.tsx create mode 100644 src/hooks/useSkyZoo.ts diff --git a/src/hooks/useSkyZoo.test.tsx b/src/hooks/useSkyZoo.test.tsx new file mode 100644 index 0000000..515b306 --- /dev/null +++ b/src/hooks/useSkyZoo.test.tsx @@ -0,0 +1,24 @@ +import { renderHook, waitFor } from '@testing-library/react'; +import { describe, expect, test } from 'vitest'; +import { useSkyZoo } from './useSkyZoo'; +import { TestRouterProvider } from '@/test/helpers/test-router-provider'; + +describe('useSkyZoo', () => { + test('returns user position', async () => { + const { result } = renderHook(() => useSkyZoo({ did: 'did:plc:k6acu4chiwkixvdedcmdgmal' }), { + wrapper: ({ children }) => {children}, + }); + + expect(result.current.data).toBe(undefined); + expect(result.current.error).toBe(null); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + + expect(result.current.data).toEqual({ + did: 'did:plc:k6acu4chiwkixvdedcmdgmal', + pos_atproto: 13658725, + pos_bsky: 13591459, + createdAt: '2024-10-21T12:09:43.477Z', + }); + }); +}); diff --git a/src/hooks/useSkyZoo.ts b/src/hooks/useSkyZoo.ts new file mode 100644 index 0000000..1e4e58d --- /dev/null +++ b/src/hooks/useSkyZoo.ts @@ -0,0 +1,17 @@ +import { useQuery } from '@tanstack/react-query'; + +export function useSkyZoo({ did }: { did: string }) { + return useQuery({ + queryKey: ['sky-zoo', { did }], + queryFn: async () => { + const response = await fetch(`https://skyzoo.blue/stats/plc/${did}`); + if (!response.ok) throw new Error('Failed to fetch SkyZoo stats'); + return (await response.json()) as { + did: string; + pos_atproto: number; + pos_bsky: number; + createdAt: string; + }; + }, + }); +}