-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(packages/ui): Add
ToastHelper
(#15565)
* add dependency * add ui components * define exports * update yarn.lock * update yarn.lock
- Loading branch information
Showing
4 changed files
with
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
Renderable, | ||
toast, | ||
Toast, | ||
ToastOptions, | ||
Toaster, | ||
ValueOrFunction, | ||
} from 'react-hot-toast' | ||
import { ReactNode } from 'react' | ||
|
||
export interface ToastHelperProps { | ||
success: (message: string) => void | ||
error: (message: string) => void | ||
promise: ( | ||
promise: Promise<any>, | ||
msgs: { | ||
loading: Renderable | ||
success: ValueOrFunction<Renderable, any> | ||
error: ValueOrFunction<Renderable, any> | ||
}, | ||
opts?: | ||
| Partial< | ||
Pick< | ||
Toast, | ||
| 'id' | ||
| 'icon' | ||
| 'duration' | ||
| 'ariaProps' | ||
| 'className' | ||
| 'style' | ||
| 'position' | ||
| 'iconTheme' | ||
> | ||
> | ||
| undefined | ||
) => Promise<any> | ||
redirectErrorPage: (errorPage: '404' | '500') => void | ||
} | ||
|
||
const options: ToastOptions = { | ||
style: { | ||
wordBreak: 'break-word', | ||
}, | ||
} | ||
|
||
export const ToastHelper: ToastHelperProps = { | ||
success: (message) => toast.success(message, options), | ||
error: (message) => toast.error(message, options), | ||
promise: async (promise, msgs, opts = {}) => { | ||
const start = new Date().getTime() | ||
const result = await toast.promise(promise, msgs, opts) | ||
if (new Date().getTime() - start < 300) { | ||
toast.remove() // This cancels the toast immediately | ||
} | ||
return result | ||
}, | ||
redirectErrorPage: (page) => { | ||
const redirectPage = `/${page}` | ||
window.location.href = redirectPage | ||
}, | ||
} | ||
|
||
interface ToastProviderProps { | ||
children: ReactNode | ||
position?: | ||
| 'top-left' | ||
| 'top-right' | ||
| 'top-center' | ||
| 'bottom-left' | ||
| 'bottom-right' | ||
| 'bottom-center' | ||
} | ||
|
||
export function ToastProvider({ | ||
children, | ||
position = 'top-center', | ||
}: ToastProviderProps) { | ||
return ( | ||
<> | ||
{children} | ||
<Toaster position={position} /> | ||
</> | ||
) | ||
} |
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