-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add toasts and new app error handling
- Loading branch information
Showing
8 changed files
with
102 additions
and
6 deletions.
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,12 @@ | ||
import { Toaster as HotToaster } from "react-hot-toast"; | ||
|
||
export default function Toaster() { | ||
return ( | ||
<HotToaster | ||
position="bottom-center" | ||
toastOptions={{ | ||
duration: 4_000, | ||
}} | ||
/> | ||
); | ||
} |
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,45 @@ | ||
import { ReactNode } from "react"; | ||
import { | ||
CheckmarkIcon, | ||
ErrorIcon, | ||
ToastOptions, | ||
toast as hotToast, | ||
} from "react-hot-toast"; | ||
|
||
type ToastType = "error" | "success"; | ||
|
||
interface ToastMethods { | ||
success: (message: string | ReactNode, options?: ToastOptions) => void; | ||
error: (message: string | ReactNode, options?: ToastOptions) => void; | ||
custom: ( | ||
children: ReactNode, | ||
type: ToastType, | ||
options?: ToastOptions | ||
) => void; | ||
} | ||
|
||
const toast: ToastMethods = { | ||
success: (message: string | ReactNode, options?: ToastOptions) => { | ||
toast.custom(message, "success", options); | ||
}, | ||
error: (message: string | ReactNode, options?: ToastOptions) => { | ||
toast.custom(message, "error", { duration: options?.duration ?? 8_000 }); | ||
}, | ||
custom: (children: ReactNode, type: ToastType, options?: ToastOptions) => { | ||
hotToast.custom( | ||
(t: { visible: boolean; id: string }) => | ||
t.visible ? ( | ||
<div className="bg-white dark:bg-surface-02dp px-4 py-3 drop-shadow-lg rounded-lg overflow-hidden flex flex-row items-center gap-3 text-gray-800 dark:text-neutral-200"> | ||
<div className="shrink-0"> | ||
{type == "success" && <CheckmarkIcon />} | ||
{type == "error" && <ErrorIcon />} | ||
</div> | ||
<div>{children}</div> | ||
</div> | ||
) : null, | ||
options | ||
); | ||
}, | ||
}; | ||
|
||
export default 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
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,18 @@ | ||
import toast from "../components/Toast"; | ||
|
||
export async function validateFetchResponse(response: Response) { | ||
if (!response.ok) { | ||
let reason = "unknown"; | ||
try { | ||
reason = await response.text(); | ||
} catch (error) { | ||
console.error("Failed to read response text", error); | ||
} | ||
throw new Error("Unexpected response: " + response.status + " " + reason); | ||
} | ||
} | ||
|
||
export function handleFetchError(message: string, error: unknown) { | ||
console.error(message, error); | ||
toast.error(message + ": " + error); | ||
} |
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