-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add default toast on API error
- Loading branch information
1 parent
998660a
commit b36724f
Showing
10 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use client'; | ||
|
||
import { | ||
CircleCheckIcon, | ||
CircleXIcon, | ||
InfoIcon, | ||
LoaderIcon, | ||
TriangleAlertIcon, | ||
} from 'lucide-react'; | ||
import { useTheme } from 'next-themes'; | ||
import { Toaster as Sonner, toast } from 'sonner'; | ||
|
||
import { useMediaQuery } from '@/lib/hooks/useMediaQuery'; | ||
|
||
type ToasterProps = React.ComponentProps<typeof Sonner>; | ||
|
||
const Toaster = ({ ...props }: ToasterProps) => { | ||
const { theme = 'system' } = useTheme(); | ||
const isDesktop = useMediaQuery('(min-width: 768px)'); | ||
|
||
return ( | ||
<Sonner | ||
theme={theme as ToasterProps['theme']} | ||
className='toaster group' | ||
position={isDesktop ? 'bottom-right' : 'top-center'} | ||
toastOptions={{ | ||
classNames: { | ||
toast: | ||
'group toast group-[.toaster]:bg-background group-[.toaster]:text-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg', | ||
description: 'group-[.toast]:text-muted-foreground', | ||
actionButton: | ||
'group-[.toast]:bg-primary group-[.toast]:text-primary-foreground', | ||
cancelButton: | ||
'group-[.toast]:bg-muted group-[.toast]:text-muted-foreground', | ||
}, | ||
}} | ||
icons={{ | ||
success: <CircleCheckIcon />, | ||
info: <InfoIcon />, | ||
warning: <TriangleAlertIcon />, | ||
error: <CircleXIcon />, | ||
loading: <LoaderIcon />, | ||
}} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export { Toaster, toast }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { toast } from '@/components/ui/Toaster'; | ||
import type { TRPCClientError } from '@/lib/api/types'; | ||
|
||
function handleGlobalError(error: Error) { | ||
const TRPCError = error as TRPCClientError; | ||
|
||
if (TRPCError.toast) { | ||
switch (TRPCError.toast) { | ||
case 'success': | ||
toast.success(TRPCError.message, { richColors: true }); | ||
break; | ||
case 'info': | ||
toast.info(TRPCError.message, { richColors: true }); | ||
break; | ||
case 'warning': | ||
toast.warning(TRPCError.message, { richColors: true }); | ||
break; | ||
default: | ||
toast.error(TRPCError.message, { richColors: true }); | ||
break; | ||
} | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
export { handleGlobalError }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { router } from '@/server/api'; | ||
import type { TRPCClientError as BaseTRPCClientError } from '@trpc/client'; | ||
|
||
type TRPCClientError = BaseTRPCClientError<typeof router> & { | ||
toast?: 'success' | 'info' | 'warning' | 'error'; | ||
}; | ||
|
||
export type { TRPCClientError }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { publicProcedure } from '@/server/api/procedures'; | ||
import { RefillingTokenBucket } from '@/server/api/rate-limit/refillingTokenBucket'; | ||
import { createRouter } from '@/server/api/trpc'; | ||
import { getFeideAuthorizationURL } from '@/server/auth/feide'; | ||
|
||
import { TRPCError } from '@trpc/server'; | ||
import { headers } from 'next/headers'; | ||
|
||
const ipBucket = new RefillingTokenBucket<string>(5, 60); | ||
|
||
const authRouter = createRouter({ | ||
getFeideAuthorizationURL: publicProcedure.query(async () => { | ||
const headerStore = await headers(); | ||
const clientIP = headerStore.get('X-Forwarded-For'); | ||
|
||
if (clientIP !== null && !ipBucket.check(clientIP, 1)) { | ||
throw new TRPCError({ | ||
code: 'TOO_MANY_REQUESTS', | ||
message: 'Rate limit exceeded. Please try again later.', | ||
}); | ||
} | ||
return getFeideAuthorizationURL(); | ||
}), | ||
}); | ||
|
||
export { authRouter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './test'; | ||
export * from './auth'; |