Skip to content

Commit

Permalink
Error handling ala Leo
Browse files Browse the repository at this point in the history
  • Loading branch information
zencephalon committed Nov 6, 2024
1 parent 8a610a6 commit 7de653e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';

import type { ManagedAddressesResponse } from '../../../../../src/types/ManagedAddresses';
import type { ManagedAddressesResponse } from 'apps/web/src/types/ManagedAddresses';

export async function GET(request: NextRequest) {
const address = request.nextUrl.searchParams.get('address');
Expand Down
12 changes: 11 additions & 1 deletion apps/web/src/components/Basenames/ManageNames/NamesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ function NamesLayout({ children }: { children: React.ReactNode }) {
}

export default function NamesList() {
const { namesData, isLoading } = useNameList();
const { namesData, isLoading, error } = useNameList();

if (error) {
return (
<NamesLayout>
<div className="text-palette-error">
<span className="text-lg">Failed to load names. Please try again later.</span>
</div>
</NamesLayout>
);
}

if (isLoading) {
return (
Expand Down
26 changes: 18 additions & 8 deletions apps/web/src/components/Basenames/ManageNames/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,34 @@ import { Basename } from '@coinbase/onchainkit/identity';
export function useNameList() {
const { address } = useAccount();
const chainId = useChainId();
const { logError } = useErrors();

const network = chainId === 8453 ? 'base-mainnet' : 'base-sepolia';

const { data: namesData, isLoading } = useQuery<ManagedAddressesResponse>({
const {
data: namesData,
isLoading,
error,
} = useQuery<ManagedAddressesResponse>({
queryKey: ['usernames', address, network],
queryFn: async (): Promise<ManagedAddressesResponse> => {
const response = await fetch(
`/api/basenames/getUsernames?address=${address}&network=${network}`,
);
if (!response.ok) {
throw new Error('Failed to fetch usernames');
try {
const response = await fetch(
`/api/basenames/getUsernames?address=${address}&network=${network}`,
);
if (!response.ok) {
throw new Error(`Failed to fetch usernames: ${response.statusText}`);
}
return (await response.json()) as ManagedAddressesResponse;
} catch (err) {
logError(err, 'Failed to fetch usernames');
throw err;
}
return response.json() as Promise<ManagedAddressesResponse>;
},
enabled: !!address,
});

return { namesData, isLoading };
return { namesData, isLoading, error };
}

export function useRemoveNameFromUI(domain: Basename) {
Expand Down

0 comments on commit 7de653e

Please sign in to comment.