mirror of
https://github.com/idanoo/autobrr
synced 2025-07-24 01:09:13 +00:00
feat(web): vendor react-hot-toast (#1883)
* feat(web): vendor react-hot-toast * vendor react-hot-toast to be react 19 compatible * feat: remove react-hot-toast and add goober * chore: fix lint warnings
This commit is contained in:
parent
43c28fc0c6
commit
d8f578b5ea
46 changed files with 1071 additions and 59 deletions
92
web/src/components/hot-toast/core/toast.ts
Normal file
92
web/src/components/hot-toast/core/toast.ts
Normal file
|
@ -0,0 +1,92 @@
|
|||
import {
|
||||
Renderable,
|
||||
Toast,
|
||||
ToastOptions,
|
||||
ToastType,
|
||||
DefaultToastOptions,
|
||||
ValueOrFunction,
|
||||
resolveValue,
|
||||
} from './types';
|
||||
import { genId } from './utils';
|
||||
import { dispatch, ActionType } from './store';
|
||||
|
||||
type Message = ValueOrFunction<Renderable, Toast>;
|
||||
|
||||
type ToastHandler = (message: Message, options?: ToastOptions) => string;
|
||||
|
||||
const createToast = (
|
||||
message: Message,
|
||||
type: ToastType = 'blank',
|
||||
opts?: ToastOptions
|
||||
): Toast => ({
|
||||
createdAt: Date.now(),
|
||||
visible: true,
|
||||
type,
|
||||
ariaProps: {
|
||||
role: 'status',
|
||||
'aria-live': 'polite',
|
||||
},
|
||||
message,
|
||||
pauseDuration: 0,
|
||||
...opts,
|
||||
id: opts?.id || genId(),
|
||||
});
|
||||
|
||||
const createHandler =
|
||||
(type?: ToastType): ToastHandler =>
|
||||
(message, options) => {
|
||||
const toast = createToast(message, type, options);
|
||||
dispatch({ type: ActionType.UPSERT_TOAST, toast });
|
||||
return toast.id;
|
||||
};
|
||||
|
||||
const toast = (message: Message, opts?: ToastOptions) =>
|
||||
createHandler('blank')(message, opts);
|
||||
|
||||
toast.error = createHandler('error');
|
||||
toast.success = createHandler('success');
|
||||
toast.loading = createHandler('loading');
|
||||
toast.custom = createHandler('custom');
|
||||
|
||||
toast.dismiss = (toastId?: string) => {
|
||||
dispatch({
|
||||
type: ActionType.DISMISS_TOAST,
|
||||
toastId,
|
||||
});
|
||||
};
|
||||
|
||||
toast.remove = (toastId?: string) =>
|
||||
dispatch({ type: ActionType.REMOVE_TOAST, toastId });
|
||||
|
||||
toast.promise = <T>(
|
||||
promise: Promise<T>,
|
||||
msgs: {
|
||||
loading: Renderable;
|
||||
success: ValueOrFunction<Renderable, T>;
|
||||
error: ValueOrFunction<Renderable, unknown>;
|
||||
},
|
||||
opts?: DefaultToastOptions
|
||||
) => {
|
||||
const id = toast.loading(msgs.loading, { ...opts, ...opts?.loading });
|
||||
|
||||
promise
|
||||
.then((p) => {
|
||||
toast.success(resolveValue(msgs.success, p), {
|
||||
id,
|
||||
...opts,
|
||||
...opts?.success,
|
||||
});
|
||||
return p;
|
||||
})
|
||||
.catch((e) => {
|
||||
toast.error(resolveValue(msgs.error, e), {
|
||||
id,
|
||||
...opts,
|
||||
...opts?.error,
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
export { toast };
|
Loading…
Add table
Add a link
Reference in a new issue