You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/client/shared/manager/ui.ts

58 lines
1.2 KiB
TypeScript

import { t } from '../i18n';
import { buildRegFn } from './buildReg';
/**
* UI api
*/
type ToastsType = 'info' | 'success' | 'error' | 'warning';
export const [showToasts, setToasts] =
buildRegFn<(message: string, type?: ToastsType) => void>('toasts');
/**
* ,
* @param error
*/
export function showErrorToasts(error: unknown) {
let msg = '';
if (error instanceof Error) {
msg = error.message;
} else {
msg = String(error);
}
showToasts(msg, 'error');
}
/**
*
*/
export function showSuccessToasts(msg = t('操作成功')) {
showToasts(msg, 'success');
}
interface AlertOptions {
message: React.ReactNode;
onConfirm?: () => void | Promise<void>;
}
export const [showAlert, setAlert] =
buildRegFn<(options: AlertOptions) => void>('alert');
/**
* Loading
*
*/
export const [showGlobalLoading, setGlobalLoading] = buildRegFn<
(text: string) => () => void
>('global-loading', () => {
return () => {};
});
/**
*
*/
export const [showNotification, setNotification] =
buildRegFn<(message: React.ReactNode, duration?: number) => () => void>(
'notification'
);