UNPKG

bigblocks

Version:

Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React

18 lines 4.64 kB
{ "name": "social-hooks-usefriendrequests", "type": "registry:hook", "dependencies": [ "@tanstack/react-query", "bmap-api-types" ], "devDependencies": [], "registryDependencies": [], "files": [ { "path": "components/social/hooks/useFriendRequests.ts", "type": "registry:hook", "content": "// useFriendRequests Hook - Manage friend requests\nimport { useMutation, useQuery, useQueryClient } from \"@tanstack/react-query\";\nimport type { BapIdentity } from \"bmap-api-types\";\nimport { useBitcoinAuth } from \"../../../hooks/useBitcoinAuth.js\";\nimport { API_ENDPOINTS, DEFAULT_SOCIAL_API_URL } from \"../constants.js\";\nimport type { SocialError } from \"../types/social.js\";\n\ninterface UseFriendRequestsResult {\n\trequests: BapIdentity[];\n\tsentRequests: BapIdentity[];\n\tisLoading: boolean;\n\terror: SocialError | null;\n\tacceptRequest: (idKey: string) => void;\n\trejectRequest: (idKey: string) => void;\n\trefetch: () => void;\n}\n\nexport function useFriendRequests(): UseFriendRequestsResult {\n\tconst { user, isAuthenticated, config } = useBitcoinAuth();\n\tconst queryClient = useQueryClient();\n\n\tconst {\n\t\tdata: requestsData = { requests: [], sentRequests: [] },\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t} = useQuery<\n\t\t{ requests: BapIdentity[]; sentRequests: BapIdentity[] },\n\t\tSocialError\n\t>({\n\t\tqueryKey: [\"friendRequests\", user?.idKey],\n\t\tqueryFn: async () => {\n\t\t\tif (!user?.idKey) {\n\t\t\t\tthrow {\n\t\t\t\t\tcode: \"UNAUTHORIZED\",\n\t\t\t\t\tmessage: \"User must be authenticated\",\n\t\t\t\t} as SocialError;\n\t\t\t}\n\n\t\t\tconst apiUrl = config?.apiUrl || DEFAULT_SOCIAL_API_URL;\n\t\t\tconst [requestsRes, sentRes] = await Promise.all([\n\t\t\t\tfetch(`${apiUrl}${API_ENDPOINTS.FRIEND_REQUESTS_INCOMING(user.idKey)}`),\n\t\t\t\tfetch(`${apiUrl}${API_ENDPOINTS.FRIEND_REQUESTS_OUTGOING(user.idKey)}`),\n\t\t\t]);\n\n\t\t\tif (!requestsRes.ok || !sentRes.ok) {\n\t\t\t\tthrow {\n\t\t\t\t\tcode: \"NETWORK_ERROR\",\n\t\t\t\t\tmessage: \"Failed to fetch friend requests\",\n\t\t\t\t} as SocialError;\n\t\t\t}\n\n\t\t\tconst [requests, sentRequests] = await Promise.all([\n\t\t\t\trequestsRes.json(),\n\t\t\t\tsentRes.json(),\n\t\t\t]);\n\n\t\t\treturn { requests, sentRequests };\n\t\t},\n\t\tenabled: isAuthenticated && !!user?.idKey,\n\t\tstaleTime: 30 * 1000, // 30 seconds\n\t\trefetchOnWindowFocus: false,\n\t});\n\n\tconst acceptMutation = useMutation<void, SocialError, string>({\n\t\tmutationFn: async (idKey: string) => {\n\t\t\tif (!user?.idKey) {\n\t\t\t\tthrow {\n\t\t\t\t\tcode: \"UNAUTHORIZED\",\n\t\t\t\t\tmessage: \"User must be authenticated\",\n\t\t\t\t} as SocialError;\n\t\t\t}\n\n\t\t\tconst apiUrl = config?.apiUrl || DEFAULT_SOCIAL_API_URL;\n\t\t\tconst response = await fetch(\n\t\t\t\t`${apiUrl}${API_ENDPOINTS.FRIEND_REQUESTS_ACCEPT}`,\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t\tbody: JSON.stringify({ idKey }),\n\t\t\t\t},\n\t\t\t);\n\n\t\t\tif (!response.ok) {\n\t\t\t\tthrow {\n\t\t\t\t\tcode: \"NETWORK_ERROR\",\n\t\t\t\t\tmessage: \"Failed to accept friend request\",\n\t\t\t\t} as SocialError;\n\t\t\t}\n\t\t},\n\t\tonSuccess: () => {\n\t\t\tqueryClient.invalidateQueries({ queryKey: [\"friendRequests\"] });\n\t\t\tqueryClient.invalidateQueries({ queryKey: [\"friends\"] });\n\t\t},\n\t});\n\n\tconst rejectMutation = useMutation<void, SocialError, string>({\n\t\tmutationFn: async (idKey: string) => {\n\t\t\tif (!user?.idKey) {\n\t\t\t\tthrow {\n\t\t\t\t\tcode: \"UNAUTHORIZED\",\n\t\t\t\t\tmessage: \"User must be authenticated\",\n\t\t\t\t} as SocialError;\n\t\t\t}\n\n\t\t\tconst apiUrl = config?.apiUrl || DEFAULT_SOCIAL_API_URL;\n\t\t\tconst response = await fetch(\n\t\t\t\t`${apiUrl}${API_ENDPOINTS.FRIEND_REQUESTS_REJECT}`,\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t\tbody: JSON.stringify({ idKey }),\n\t\t\t\t},\n\t\t\t);\n\n\t\t\tif (!response.ok) {\n\t\t\t\tthrow {\n\t\t\t\t\tcode: \"NETWORK_ERROR\",\n\t\t\t\t\tmessage: \"Failed to reject friend request\",\n\t\t\t\t} as SocialError;\n\t\t\t}\n\t\t},\n\t\tonSuccess: () => {\n\t\t\tqueryClient.invalidateQueries({ queryKey: [\"friendRequests\"] });\n\t\t},\n\t});\n\n\treturn {\n\t\trequests: requestsData.requests,\n\t\tsentRequests: requestsData.sentRequests,\n\t\tisLoading,\n\t\terror: error || null,\n\t\tacceptRequest: acceptMutation.mutate,\n\t\trejectRequest: rejectMutation.mutate,\n\t\trefetch,\n\t};\n}\n", "target": "<%- config.aliases.hooks %>/usefriendrequests.ts" } ] }