refactor: 错误抛出与样式管理

pull/13/head
moonrailgun 4 years ago
parent 2af5f16e73
commit 6339efcba5

@ -28,6 +28,13 @@ export { useRafState } from './hooks/useRafState';
export { getStorage, setStorage, useStorage } from './manager/storage';
export { setTokenGetter } from './manager/request';
export { setServiceUrl } from './manager/service';
export {
showToasts,
setToasts,
showAlert,
setAlert,
showErrorToasts,
} from './manager/ui';
// model
export type { UserBaseInfo } from './model/user';

@ -0,0 +1,31 @@
import { buildRegFn } from './buildRegFn';
/**
* 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: Error) {
let msg = '';
if (error instanceof Error) {
msg = error.message;
} else {
msg = String(error);
}
showToasts(msg, 'error');
}
interface AlertOptions {
message: React.ReactNode;
onConfirm?: () => void | Promise<void>;
}
export const [showAlert, setAlert] =
buildRegFn<(options: AlertOptions) => void>('alert');

@ -4,8 +4,6 @@ import { useStorage } from 'pawchat-shared';
import clsx from 'clsx';
import { Loadable } from './components/Loadable';
import './dark.less';
const MainRoute = Loadable(() =>
import('./routes/Main').then((module) => module.MainRoute)
);

@ -4,6 +4,7 @@ import ReactDOM from 'react-dom';
import { App } from './App';
import 'antd/dist/antd.css';
import './styles/antd.less';
import 'tailwindcss/tailwind.css';
ReactDOM.render(<App />, document.querySelector('#app'));

@ -1,7 +1,10 @@
import { message, Modal } from 'antd';
import {
buildStorage,
setAlert,
setServiceUrl,
setStorage,
setToasts,
setTokenGetter,
} from 'pawchat-shared';
import { getUserJWT } from './utils/jwt-helper';
@ -18,3 +21,22 @@ if (window.localStorage.getItem('serviceUrl')) {
} else if (process.env.SERVICE_URL) {
setServiceUrl(() => String(process.env.SERVICE_URL));
}
setToasts((msg, type = 'info') => {
message.open({
type,
duration: 30000,
content: String(msg),
});
});
setAlert((options) => {
Modal.confirm({
content: options.message,
onOk: async () => {
if (typeof options.onConfirm === 'function') {
await options.onConfirm();
}
},
});
});

@ -3,14 +3,19 @@ import { Highlight } from '@/components/Highlight';
import { Button, Divider, Empty } from 'antd';
import {
searchUserWithUniqueName,
showErrorToasts,
useAsyncFn,
UserBaseInfo,
} from 'pawchat-shared';
import React, { useState } from 'react';
import React, { useCallback, useState } from 'react';
const SearchFriendResult: React.FC<{
result: UserBaseInfo | undefined | null;
}> = React.memo(({ result }) => {
const handleAddFriend = useCallback((userId: string) => {
console.log(userId);
}, []);
if (result === undefined) {
return null;
}
@ -39,7 +44,11 @@ const SearchFriendResult: React.FC<{
</div>
</div>
<Button type="primary" className="bg-green-700 border-0">
<Button
type="primary"
className="bg-green-600 border-0"
onClick={() => handleAddFriend(result._id)}
>
</Button>
</div>
@ -57,7 +66,7 @@ export const AddFriend: React.FC = React.memo(() => {
return data;
} catch (err) {
console.error(err);
showErrorToasts(err);
}
}, [uniqueName]);
@ -68,7 +77,7 @@ export const AddFriend: React.FC = React.memo(() => {
使 <Highlight>#</Highlight>
</div>
<div className="px-4 my-3 flex border border-black border-opacity-30 rounded items-center">
<div className="px-4 my-3 flex border border-black border-opacity-30 rounded items-center bg-black bg-opacity-10">
<input
className="bg-transparent flex-1 text-base leading-13 outline-none"
placeholder="用户昵称#0000"
@ -77,7 +86,8 @@ export const AddFriend: React.FC = React.memo(() => {
<Button
type="primary"
className="bg-indigo-600 border-none"
className="bg-indigo-600 disabled:opacity-80 border-none"
disabled={uniqueName === ''}
loading={loading}
onClick={searchUser}
>

@ -0,0 +1,2 @@
@import "./overwrite.less";
@import "./dark.less";

@ -0,0 +1,8 @@
/**
* 这里主要是处理tailwindcss与 antd 一起用的时候的兼容问题
* 与重写部分antd的样式
*/
.ant-message .anticon {
vertical-align: text-top;
}
Loading…
Cancel
Save