@dune2/tools 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -5
- package/src/factory/createStateContext.tsx +11 -8
- package/src/logger/Logger.ts +2 -2
- package/src/logger/index.ts +6 -5
- package/src/storage/index.ts +4 -3
- package/src/store/index.ts +2 -2
- package/src/niceModal/createModal.tsx +0 -31
- package/src/niceModal/index.tsx +0 -32
- package/src/niceModal/niceModalStore.tsx +0 -40
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dune2/tools",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n"
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
"./storage/*": "./src/storage/*.ts",
|
|
28
28
|
"./logger": "./src/logger/index.ts",
|
|
29
29
|
"./logger/*": "./src/logger/*.ts",
|
|
30
|
-
"./niceModal": "./src/niceModal/index.tsx",
|
|
31
30
|
"./store": "./src/store/index.ts",
|
|
32
31
|
"./package.json": "./package.json",
|
|
33
32
|
".": "./src/index.ts"
|
|
@@ -36,16 +35,14 @@
|
|
|
36
35
|
"src"
|
|
37
36
|
],
|
|
38
37
|
"dependencies": {
|
|
39
|
-
"@ebay/nice-modal-react": "^1.2.13",
|
|
40
38
|
"bignumber.js": "^9.1.2",
|
|
39
|
+
"es-toolkit": "^1.39.5",
|
|
41
40
|
"js-cookie": "^3.0.5",
|
|
42
|
-
"lodash": "^4",
|
|
43
41
|
"store2": "^2.14.3"
|
|
44
42
|
},
|
|
45
43
|
"devDependencies": {
|
|
46
44
|
"@tanstack/react-query": "5.51.11",
|
|
47
45
|
"@types/js-cookie": "3.0.3",
|
|
48
|
-
"@types/lodash": "^4.17.0",
|
|
49
46
|
"axios": "^1.6.8",
|
|
50
47
|
"react": "^19",
|
|
51
48
|
"valtio": "^2"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import type { ComponentType, FC, PropsWithChildren } from "react";
|
|
3
3
|
import React, { useContext } from "react";
|
|
4
4
|
|
|
5
|
-
interface Params<T> {
|
|
5
|
+
interface Params<P, T> {
|
|
6
6
|
/**
|
|
7
7
|
* context 的名字 方便 debug
|
|
8
8
|
*/
|
|
@@ -10,7 +10,7 @@ interface Params<T> {
|
|
|
10
10
|
/**
|
|
11
11
|
* 传入一个 hooks 返回值会给 context 的 value
|
|
12
12
|
*/
|
|
13
|
-
useValueHooks: () => T;
|
|
13
|
+
useValueHooks: (props: P) => T;
|
|
14
14
|
defaultValue?: T;
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -18,12 +18,12 @@ interface Params<T> {
|
|
|
18
18
|
* 快速创建一个 context 和 Provider
|
|
19
19
|
* 通过给定的 hooks 返回值创建 context 的 value
|
|
20
20
|
*/
|
|
21
|
-
export function createStateContext<T>(params: Params<T>) {
|
|
21
|
+
export function createStateContext<P, T>(params: Params<P, T>) {
|
|
22
22
|
const { useValueHooks, defaultValue, name } = params;
|
|
23
23
|
const Context = React.createContext(defaultValue as T);
|
|
24
24
|
|
|
25
|
-
const Provider: FC<PropsWithChildren
|
|
26
|
-
const value = useValueHooks();
|
|
25
|
+
const Provider: FC<PropsWithChildren<P>> = (props) => {
|
|
26
|
+
const value = useValueHooks(props);
|
|
27
27
|
return <Context.Provider value={value}>{props.children}</Context.Provider>;
|
|
28
28
|
};
|
|
29
29
|
Provider.displayName = `${name}Provider`;
|
|
@@ -32,10 +32,13 @@ export function createStateContext<T>(params: Params<T>) {
|
|
|
32
32
|
return useContext(Context)!;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
function withProvider<
|
|
36
|
-
|
|
35
|
+
function withProvider<C extends object>(
|
|
36
|
+
Comp: ComponentType<C>,
|
|
37
|
+
providerProps: P = {} as P,
|
|
38
|
+
) {
|
|
39
|
+
function WithProvider(props: C) {
|
|
37
40
|
return (
|
|
38
|
-
<Provider>
|
|
41
|
+
<Provider {...providerProps}>
|
|
39
42
|
<Comp {...props}></Comp>
|
|
40
43
|
</Provider>
|
|
41
44
|
);
|
package/src/logger/Logger.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { noop } from "es-toolkit";
|
|
2
2
|
import store2 from "store2";
|
|
3
3
|
import { Level, type OnLogParams } from "./shared";
|
|
4
4
|
|
|
@@ -16,7 +16,7 @@ export class Logger {
|
|
|
16
16
|
* 只有大于等于当前等级的日志才会输出
|
|
17
17
|
*/
|
|
18
18
|
public level = Level.Debug,
|
|
19
|
-
public onLog: OnLog =
|
|
19
|
+
public onLog: OnLog = noop,
|
|
20
20
|
) {
|
|
21
21
|
this.level = this.getStorageLevel() ?? level;
|
|
22
22
|
}
|
package/src/logger/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { camelCase } from "es-toolkit";
|
|
2
|
+
import { forEach } from "es-toolkit/compat";
|
|
2
3
|
import { Logger, type OnLog } from "./Logger";
|
|
3
4
|
import { Level } from "./shared";
|
|
4
5
|
|
|
@@ -30,7 +31,7 @@ export function createLogger<Name extends string>(config: Config<Name>) {
|
|
|
30
31
|
}
|
|
31
32
|
// 输入:foo.login
|
|
32
33
|
// 输出:fooLogin
|
|
33
|
-
const key = (name.includes(".") ?
|
|
34
|
+
const key = (name.includes(".") ? camelCase(name) : name) as Key;
|
|
34
35
|
|
|
35
36
|
if (loggerMap[key]) {
|
|
36
37
|
throw new Error(`logger name "${name}" is duplicated`);
|
|
@@ -47,17 +48,17 @@ export function createLogger<Name extends string>(config: Config<Name>) {
|
|
|
47
48
|
|
|
48
49
|
const method = {
|
|
49
50
|
enable() {
|
|
50
|
-
|
|
51
|
+
forEach(loggerMap, (v) => {
|
|
51
52
|
v.enable();
|
|
52
53
|
});
|
|
53
54
|
},
|
|
54
55
|
disable() {
|
|
55
|
-
|
|
56
|
+
forEach(loggerMap, (v) => {
|
|
56
57
|
v.disable();
|
|
57
58
|
});
|
|
58
59
|
},
|
|
59
60
|
setLevel(level: Level) {
|
|
60
|
-
|
|
61
|
+
forEach(loggerMap, (v) => {
|
|
61
62
|
v.setLevel(level);
|
|
62
63
|
});
|
|
63
64
|
},
|
package/src/storage/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { isEqual } from "es-toolkit";
|
|
2
|
+
|
|
2
3
|
import { useSyncExternalStore } from "react";
|
|
3
4
|
import type { StoreType } from "store2";
|
|
4
5
|
import baseStore from "store2";
|
|
@@ -52,7 +53,7 @@ class StorageHelper<V = any> {
|
|
|
52
53
|
|
|
53
54
|
get(): V | undefined {
|
|
54
55
|
const r = this.store.get(this.baseKey) ?? this.defaultValue;
|
|
55
|
-
if (!
|
|
56
|
+
if (!isEqual(r, this.currentValue)) {
|
|
56
57
|
this.currentValue = r;
|
|
57
58
|
}
|
|
58
59
|
return this.currentValue;
|
|
@@ -62,7 +63,7 @@ class StorageHelper<V = any> {
|
|
|
62
63
|
* 设置为 undefined 时,会删除该 key
|
|
63
64
|
*/
|
|
64
65
|
set(v: V | undefined): void {
|
|
65
|
-
if (
|
|
66
|
+
if (isEqual(v, this.currentValue)) {
|
|
66
67
|
return;
|
|
67
68
|
}
|
|
68
69
|
this.currentValue = v;
|
package/src/store/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { isEqual } from "es-toolkit";
|
|
2
2
|
import { useDebugValue, useRef, useSyncExternalStore } from "react";
|
|
3
3
|
import { proxy, snapshot, subscribe, useSnapshot } from "valtio";
|
|
4
4
|
|
|
@@ -69,7 +69,7 @@ export function createStore<
|
|
|
69
69
|
const prev = useRef<T>(undefined);
|
|
70
70
|
const combinedSelector = (state: S) => {
|
|
71
71
|
const next = selector(state);
|
|
72
|
-
if (!
|
|
72
|
+
if (!isEqual(prev.current, next)) {
|
|
73
73
|
prev.current = next;
|
|
74
74
|
}
|
|
75
75
|
return prev.current;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import NiceModal from "@ebay/nice-modal-react";
|
|
2
|
-
import _ from "lodash";
|
|
3
|
-
import { niceModalStore } from "./niceModalStore";
|
|
4
|
-
|
|
5
|
-
export const createModal = <P extends object, Res = unknown>(
|
|
6
|
-
Comp: React.ComponentType<P>,
|
|
7
|
-
) => {
|
|
8
|
-
const customModal = NiceModal.create(Comp);
|
|
9
|
-
|
|
10
|
-
const modalId: string = _.uniqueId("niceModal_");
|
|
11
|
-
|
|
12
|
-
NiceModal.register(modalId, customModal);
|
|
13
|
-
return Object.assign(customModal, {
|
|
14
|
-
async show(props?: P): Promise<Res> {
|
|
15
|
-
await niceModalStore.actions.initModal(modalId);
|
|
16
|
-
return NiceModal.show(modalId, props);
|
|
17
|
-
},
|
|
18
|
-
hide() {
|
|
19
|
-
return NiceModal.hide(modalId);
|
|
20
|
-
},
|
|
21
|
-
remove() {
|
|
22
|
-
return NiceModal.remove(modalId);
|
|
23
|
-
},
|
|
24
|
-
/**
|
|
25
|
-
* 获取当前弹框是否打开
|
|
26
|
-
*/
|
|
27
|
-
getVisible() {
|
|
28
|
-
return !!niceModalStore.getState().modals[modalId]?.visible;
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
};
|
package/src/niceModal/index.tsx
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Provider } from "@ebay/nice-modal-react";
|
|
2
|
-
import type { PropsWithChildren } from "react";
|
|
3
|
-
import { niceModalStore } from "./niceModalStore";
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
antdDrawer,
|
|
7
|
-
antdDrawerV5,
|
|
8
|
-
antdModal,
|
|
9
|
-
antdModalV5,
|
|
10
|
-
bootstrapDialog,
|
|
11
|
-
muiDialog,
|
|
12
|
-
muiDialogV5,
|
|
13
|
-
useModal,
|
|
14
|
-
} from "@ebay/nice-modal-react";
|
|
15
|
-
export { createModal } from "./createModal";
|
|
16
|
-
export {
|
|
17
|
-
/**
|
|
18
|
-
* 一般情况下不需要使用这个,这里导出主要是给 storybook 使用,防止弹出多个 modal
|
|
19
|
-
*/
|
|
20
|
-
Provider as BaseNiceModalProvider,
|
|
21
|
-
niceModalStore,
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export function NiceModalProvider(props: PropsWithChildren) {
|
|
25
|
-
const { modals } = niceModalStore.useSnapshot();
|
|
26
|
-
|
|
27
|
-
return (
|
|
28
|
-
<Provider dispatch={niceModalStore.actions.dispatch} modals={modals}>
|
|
29
|
-
{props.children}
|
|
30
|
-
</Provider>
|
|
31
|
-
);
|
|
32
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
reducer,
|
|
3
|
-
type NiceModalAction,
|
|
4
|
-
type NiceModalStore,
|
|
5
|
-
} from "@ebay/nice-modal-react";
|
|
6
|
-
import { createStore } from "../store";
|
|
7
|
-
|
|
8
|
-
export const niceModalStore = createStore({
|
|
9
|
-
name: "niceModalStore",
|
|
10
|
-
state: {
|
|
11
|
-
modals: {} as NiceModalStore,
|
|
12
|
-
},
|
|
13
|
-
actionsCreator: (state) => {
|
|
14
|
-
return {
|
|
15
|
-
dispatch(action: NiceModalAction) {
|
|
16
|
-
state.modals = reducer(state.modals, action);
|
|
17
|
-
},
|
|
18
|
-
/**
|
|
19
|
-
* 为了让 modal 第一次打开的时候过渡动画存在
|
|
20
|
-
* 目前主要是兼容 mantine ui,它们使用的是 css 动画
|
|
21
|
-
* 要确保 dom 存在
|
|
22
|
-
*/
|
|
23
|
-
async initModal(id: string) {
|
|
24
|
-
if (state.modals[id]) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
state.modals = {
|
|
28
|
-
...state.modals,
|
|
29
|
-
[id]: {
|
|
30
|
-
id,
|
|
31
|
-
visible: false,
|
|
32
|
-
delayVisible: false,
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
// 确保下一帧调用,让 过渡动画有效果
|
|
36
|
-
return Promise.resolve();
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
},
|
|
40
|
-
});
|