Skip to content

Commit 098af66

Browse files
committed
refactor(ui): remove static method that create notification or toast
1 parent 05b3b61 commit 098af66

30 files changed

+664
-911
lines changed

packages/hooks/src/createGlobalState.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ImmerHook } from './useImmer';
22

3-
import { freeze } from 'immer';
3+
import { freeze, produce } from 'immer';
4+
import { useState } from 'react';
45

5-
import { useImmer } from './useImmer';
66
import { useUnmount } from './useUnmount';
77

88
export function createGlobalState<S>(): () => ImmerHook<S | undefined>;
@@ -11,16 +11,21 @@ export function createGlobalState<S>(initialValue?: S): () => ImmerHook<S | unde
1111
const store = {
1212
state: freeze(typeof initialValue === 'function' ? initialValue() : initialValue, true),
1313
setState(updater: any) {
14+
if (typeof updater === 'function') {
15+
store.state = produce(store.state, updater);
16+
} else {
17+
store.state = freeze(updater);
18+
}
19+
1420
for (const update of store.updates) {
15-
update(updater);
21+
update(store.state);
1622
}
1723
},
1824
updates: new Set<(...args: any[]) => any>(),
1925
};
2026

2127
return () => {
22-
const [state, setState] = useImmer(store.state);
23-
store.state = state;
28+
const [state, setState] = useState(store.state);
2429

2530
if (!store.updates.has(setState)) {
2631
store.updates.add(setState);

packages/platform/src/app/App.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import { useTranslation } from 'react-i18next';
77
import { useNavigate } from 'react-router-dom';
88

99
import { useAsync, useMount, useStorage } from '@react-devui/hooks';
10+
import { DNotification, DToast } from '@react-devui/ui';
1011
import { DRoot } from '@react-devui/ui';
1112

1213
import { AppRoutes } from './Routes';
1314
import { LOGIN_PATH } from './config/other';
1415
import { STORAGE_KEY } from './config/storage';
1516
import { useHttp, useInit } from './core';
17+
import { useNotifications, useToasts } from './core/state';
1618

1719
export type AppTheme = 'light' | 'dark';
1820

@@ -25,6 +27,8 @@ export function App() {
2527
const [loading, setLoading] = useState(true);
2628
const languageStorage = useStorage<DLang>(...STORAGE_KEY.language);
2729
const themeStorage = useStorage<AppTheme>(...STORAGE_KEY.theme);
30+
const [notifications] = useNotifications();
31+
const [toasts] = useToasts();
2832

2933
useMount(() => {
3034
i18n.changeLanguage(languageStorage.value);
@@ -77,7 +81,17 @@ export function App() {
7781
[languageStorage.value]
7882
);
7983

80-
return <DRoot context={rootContext}>{loading ? null : <AppRoutes />}</DRoot>;
84+
return (
85+
<DRoot context={rootContext}>
86+
{loading ? null : <AppRoutes />}
87+
{notifications.map(({ key, ...props }) => (
88+
<DNotification {...props} key={key}></DNotification>
89+
))}
90+
{toasts.map(({ key, ...props }) => (
91+
<DToast {...props} key={key}></DToast>
92+
))}
93+
</DRoot>
94+
);
8195
}
8296

8397
export default App;

packages/platform/src/app/core/http/useHttp.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import { concatMap, of } from 'rxjs';
1010
import { catchError, EMPTY, from, map, Subject, takeUntil, throwError } from 'rxjs';
1111

1212
import { useEventCallback, useUnmount } from '@react-devui/hooks';
13-
import { ToastService } from '@react-devui/ui';
1413

1514
import { environment } from '../../../environments';
1615
import { LOGIN_PATH, PREV_ROUTE_KEY } from '../../config/other';
16+
import { getGlobalKey } from '../../utils/vars';
17+
import { useToasts } from '../state';
1718
import { TOKEN } from '../token';
1819
import './mock';
1920

@@ -27,6 +28,7 @@ export function useHttp() {
2728
const navigate = useNavigate();
2829
const location = useLocation();
2930
const { t } = useTranslation();
31+
const [, setToasts] = useToasts();
3032

3133
useUnmount(() => {
3234
for (const abort of dataRef.current.abortFns) {
@@ -68,9 +70,19 @@ export function useHttp() {
6870
if (error.response) {
6971
switch (error.response.status) {
7072
case 401:
71-
ToastService.open({
72-
dContent: t('User not authorized'),
73-
dType: 'error',
73+
setToasts((draft) => {
74+
const key = getGlobalKey();
75+
draft.push({
76+
key,
77+
children: t('User not authorized'),
78+
dVisible: true,
79+
dType: 'error',
80+
onClose: () => {
81+
setToasts((draft) => {
82+
draft.find((n) => n.key === key)!.dVisible = false;
83+
});
84+
},
85+
});
7486
});
7587
navigate(LOGIN_PATH, { state: { [PREV_ROUTE_KEY]: location } });
7688
return EMPTY;

packages/platform/src/app/core/state.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { DNotificationProps, DToastProps } from '@react-devui/ui';
2+
13
import { createGlobalState } from '@react-devui/hooks';
24

35
export interface UserState {
@@ -17,3 +19,7 @@ export interface NotificationItem {
1719
}[];
1820
}
1921
export const useNotificationState = createGlobalState<NotificationItem[]>();
22+
23+
export const useNotifications = createGlobalState<(DNotificationProps & { key: string | number })[]>([]);
24+
25+
export const useToasts = createGlobalState<(DToastProps & { key: string | number })[]>([]);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let globalKey = 0;
2+
export function getGlobalKey() {
3+
globalKey += 1;
4+
return globalKey;
5+
}

packages/ui/src/components/alert/Alert.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export function DAlert(props: DAlertProps): JSX.Element | null {
9292
<DToastPanel
9393
{...restProps}
9494
ref={collapseRef}
95+
children={dTitle}
9596
className={getClassName(restProps.className, `${dPrefix}alert--toast`)}
9697
style={{
9798
...restProps.style,
@@ -100,7 +101,6 @@ export function DAlert(props: DAlertProps): JSX.Element | null {
100101
dClassNamePrefix="alert"
101102
dType={dType}
102103
dIcon={dIcon}
103-
dContent={dTitle}
104104
dActions={dActions}
105105
onClose={() => {
106106
changeVisible(false);

packages/ui/src/components/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export type { DModalProps, DModalHeaderProps, DModalFooterProps } from './modal'
6868
export { DModal } from './modal';
6969

7070
export type { DNotificationProps } from './notification';
71-
export { NotificationService } from './notification';
71+
export { DNotification } from './notification';
7272

7373
export type { DPaginationProps } from './pagination';
7474
export { DPagination } from './pagination';
@@ -139,7 +139,7 @@ export type { DTimelineProps } from './timeline';
139139
export { DTimeline } from './timeline';
140140

141141
export type { DToastProps } from './toast';
142-
export { ToastService } from './toast';
142+
export { DToast } from './toast';
143143

144144
export type { DTooltipProps } from './tooltip';
145145
export { DTooltip } from './tooltip';

0 commit comments

Comments
 (0)