Skip to content

Commit 0c93171

Browse files
committed
refactor(hooks): createGlobalState export state and setState
1 parent a225419 commit 0c93171

5 files changed

Lines changed: 31 additions & 17 deletions

File tree

packages/hooks/src/createGlobalState.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ import { useState } from 'react';
55

66
import { useUnmount } from './useUnmount';
77

8-
export function createGlobalState<S>(): { (): ImmerHook<S | undefined>; set: Updater<S | undefined> };
9-
export function createGlobalState<S>(initialValue: S): { (): ImmerHook<S>; set: Updater<S> };
10-
export function createGlobalState<S>(initialValue?: S): { (): ImmerHook<S | undefined>; set: Updater<S | undefined> } {
8+
interface GlobalStateHook<S> {
9+
(): ImmerHook<S>;
10+
readonly state: S;
11+
setState: Updater<S>;
12+
}
13+
14+
export function createGlobalState<S>(): GlobalStateHook<S | undefined>;
15+
export function createGlobalState<S>(initialValue: S): GlobalStateHook<S>;
16+
export function createGlobalState<S>(initialValue?: S): GlobalStateHook<S | undefined> {
1117
const store = {
1218
state: freeze(typeof initialValue === 'function' ? initialValue() : initialValue, true),
1319
setState(updater: any) {
@@ -24,7 +30,7 @@ export function createGlobalState<S>(initialValue?: S): { (): ImmerHook<S | unde
2430
updates: new Set<(...args: any[]) => any>(),
2531
};
2632

27-
return Object.assign<any, any>(
33+
return new Proxy(
2834
() => {
2935
const [state, setState] = useState(store.state);
3036

@@ -39,7 +45,15 @@ export function createGlobalState<S>(initialValue?: S): { (): ImmerHook<S | unde
3945
return [state, store.setState];
4046
},
4147
{
42-
set: store.setState,
48+
get(target, prop, receiver) {
49+
if (prop === 'state') {
50+
return store.state;
51+
}
52+
if (prop === 'setState') {
53+
return store.setState;
54+
}
55+
return Reflect.get(target, prop, receiver);
56+
},
4357
}
44-
);
58+
) as any;
4559
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,21 @@ export function useHttp() {
7171
if (error.response) {
7272
switch (error.response.status) {
7373
case 401:
74-
useToasts.set((draft) => {
74+
useToasts.setState((draft) => {
7575
const key = getGlobalKey();
7676
draft.push({
7777
key,
7878
children: t('User not authorized'),
7979
dVisible: true,
8080
dType: 'error',
8181
onClose: () => {
82-
useToasts.set((draft) => {
82+
useToasts.setState((draft) => {
8383
draft.find((n) => n.key === key)!.dVisible = false;
8484
});
8585
},
8686
afterVisibleChange: (visible) => {
8787
if (!visible) {
88-
useToasts.set((draft) => {
88+
useToasts.setState((draft) => {
8989
draft.splice(
9090
draft.findIndex((n) => n.key === key),
9191
1

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function useInit() {
1313
const refreshToken = useRefreshToken();
1414

1515
const handleUser = (user: UserState) => {
16-
useUserState.set(user);
16+
useUserState.setState(user);
1717

1818
//#region ACL
1919
acl.setFull(user.role === 'admin');
@@ -22,7 +22,7 @@ export function useInit() {
2222
};
2323

2424
const getNotification = () => {
25-
useNotificationState.set(undefined);
25+
useNotificationState.setState(undefined);
2626
http<NotificationItem[]>(
2727
{
2828
url: '/notification',
@@ -31,13 +31,13 @@ export function useInit() {
3131
{ unmount: false }
3232
).subscribe({
3333
next: (res) => {
34-
useNotificationState.set(res);
34+
useNotificationState.setState(res);
3535
},
3636
});
3737
};
3838

3939
const resetMenu = () => {
40-
useMenu.set((draft) => {
40+
useMenu.setState((draft) => {
4141
draft.expands = undefined;
4242
});
4343
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ export function useMenu() {
9595
setMenuState,
9696
] as const;
9797
}
98-
useMenu.set = useMenuState.set;
98+
useMenu.setState = useMenuState.setState;

packages/platform/src/app/routes/login/Login.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export default function Login(): JSX.Element | null {
7474
},
7575
error: (error) => {
7676
setLoginLoading(false);
77-
useNotifications.set((draft) => {
77+
useNotifications.setState((draft) => {
7878
const key = getGlobalKey();
7979
draft.push({
8080
key,
@@ -83,13 +83,13 @@ export default function Login(): JSX.Element | null {
8383
dDescription: error.response!.statusText,
8484
dType: 'error',
8585
onClose: () => {
86-
useNotifications.set((draft) => {
86+
useNotifications.setState((draft) => {
8787
draft.find((n) => n.key === key)!.dVisible = false;
8888
});
8989
},
9090
afterVisibleChange: (visible) => {
9191
if (!visible) {
92-
useNotifications.set((draft) => {
92+
useNotifications.setState((draft) => {
9393
draft.splice(
9494
draft.findIndex((n) => n.key === key),
9595
1

0 commit comments

Comments
 (0)