@cripty2001/utils 0.0.225 → 0.0.227
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/dist/Logger/Logger.d.ts +3 -1
- package/dist/Logger/Logger.js +28 -9
- package/dist/react/index.d.ts +1 -1
- package/dist/react/logger-display.d.ts +4 -1
- package/dist/react/logger-display.js +39 -63
- package/dist/react-ui/button.d.ts +2 -1
- package/dist/react-ui/button.js +4 -2
- package/package.json +4 -4
package/dist/Logger/Logger.d.ts
CHANGED
|
@@ -12,11 +12,13 @@ declare class Logger {
|
|
|
12
12
|
private static instance;
|
|
13
13
|
lines: Whispr<LoggerItem[]>;
|
|
14
14
|
private setLines;
|
|
15
|
+
private readonly dismissTimers;
|
|
15
16
|
private constructor();
|
|
16
17
|
private reconstructError;
|
|
17
18
|
static getInstance(): Logger;
|
|
19
|
+
private removeItem;
|
|
18
20
|
log(message: string, severity?: LoggerItem['severity'], context?: string): void;
|
|
19
|
-
logError(error: Error | DOMException |
|
|
21
|
+
logError(error: Error | DOMException | unknown): void;
|
|
20
22
|
}
|
|
21
23
|
declare const _default: Logger;
|
|
22
24
|
export default _default;
|
package/dist/Logger/Logger.js
CHANGED
|
@@ -2,14 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const whispr_1 = require("@cripty2001/whispr");
|
|
4
4
|
const __1 = require("..");
|
|
5
|
+
const AUTO_DISMISS_MS = {
|
|
6
|
+
success: 5000,
|
|
7
|
+
info: 5000,
|
|
8
|
+
warning: 6000,
|
|
9
|
+
error: 8000,
|
|
10
|
+
};
|
|
5
11
|
class Logger {
|
|
6
12
|
static instance;
|
|
7
13
|
lines;
|
|
8
14
|
setLines;
|
|
15
|
+
dismissTimers = new Map();
|
|
9
16
|
constructor() {
|
|
10
17
|
[this.lines, this.setLines] = whispr_1.Whispr.create([]);
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
if (typeof window !== "undefined") {
|
|
19
|
+
window.addEventListener("unhandledrejection", (e) => this.log(e.reason, "error"));
|
|
20
|
+
window.addEventListener("error", (e) => this.logError(this.reconstructError(e)));
|
|
21
|
+
}
|
|
13
22
|
}
|
|
14
23
|
reconstructError(e) {
|
|
15
24
|
if (e.error && e.error instanceof Error) {
|
|
@@ -26,24 +35,34 @@ class Logger {
|
|
|
26
35
|
}
|
|
27
36
|
return Logger.instance;
|
|
28
37
|
}
|
|
38
|
+
removeItem(id) {
|
|
39
|
+
const timer = this.dismissTimers.get(id);
|
|
40
|
+
if (timer !== undefined) {
|
|
41
|
+
clearTimeout(timer);
|
|
42
|
+
this.dismissTimers.delete(id);
|
|
43
|
+
}
|
|
44
|
+
this.setLines(this.lines.value.filter((i) => i.id !== id));
|
|
45
|
+
}
|
|
29
46
|
log(message, severity = "info", context) {
|
|
47
|
+
const id = (0, __1.getRandomId)();
|
|
30
48
|
const item = {
|
|
31
|
-
id
|
|
49
|
+
id,
|
|
32
50
|
date: new Date(),
|
|
33
51
|
message,
|
|
34
52
|
severity,
|
|
35
53
|
context,
|
|
36
|
-
dismiss: () =>
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
trace: (new Error()).stack
|
|
54
|
+
dismiss: () => this.removeItem(id),
|
|
55
|
+
trace: (new Error()).stack,
|
|
40
56
|
};
|
|
41
57
|
this.setLines([...this.lines.value, item]);
|
|
58
|
+
if (typeof window !== "undefined") {
|
|
59
|
+
const timer = window.setTimeout(() => item.dismiss(), AUTO_DISMISS_MS[severity]);
|
|
60
|
+
this.dismissTimers.set(id, timer);
|
|
61
|
+
}
|
|
42
62
|
}
|
|
43
63
|
logError(error) {
|
|
44
64
|
if (error instanceof DOMException ||
|
|
45
|
-
error instanceof Error
|
|
46
|
-
false) {
|
|
65
|
+
error instanceof Error) {
|
|
47
66
|
return this.log(error.message, "error", error.stack);
|
|
48
67
|
}
|
|
49
68
|
console.trace('UNKNOWN ERROR', error);
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { LoggerReactDisplay } from './logger-display';
|
|
1
|
+
export { LoggerReactDisplay, type LoggerReactDisplayProps } from './logger-display';
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type LoggerReactDisplayProps = {
|
|
2
|
+
zIndex?: number;
|
|
3
|
+
};
|
|
4
|
+
export declare function LoggerReactDisplay({ zIndex }: LoggerReactDisplayProps): import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -8,68 +8,44 @@ const jsx_runtime_1 = require("react/jsx-runtime");
|
|
|
8
8
|
const lucide_react_1 = require("lucide-react");
|
|
9
9
|
const Logger_1 = __importDefault(require("../Logger/Logger"));
|
|
10
10
|
const react_whispr_1 = require("../react-whispr");
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
11
|
+
const SEVERITY_STYLES = {
|
|
12
|
+
error: {
|
|
13
|
+
icon: lucide_react_1.AlertCircle,
|
|
14
|
+
container: "border-red-200 bg-red-50 dark:border-red-900/50 dark:bg-red-950/40",
|
|
15
|
+
iconClass: "text-red-600 dark:text-red-400",
|
|
16
|
+
textClass: "text-red-800 dark:text-red-200",
|
|
17
|
+
},
|
|
18
|
+
warning: {
|
|
19
|
+
icon: lucide_react_1.TriangleAlert,
|
|
20
|
+
container: "border-amber-200 bg-amber-50 dark:border-amber-900/50 dark:bg-amber-950/40",
|
|
21
|
+
iconClass: "text-amber-600 dark:text-amber-400",
|
|
22
|
+
textClass: "text-amber-900 dark:text-amber-200",
|
|
23
|
+
},
|
|
24
|
+
success: {
|
|
25
|
+
icon: lucide_react_1.CheckCircle2,
|
|
26
|
+
container: "border-emerald-200 bg-emerald-50 dark:border-emerald-900/50 dark:bg-emerald-950/40",
|
|
27
|
+
iconClass: "text-emerald-600 dark:text-emerald-400",
|
|
28
|
+
textClass: "text-emerald-900 dark:text-emerald-200",
|
|
29
|
+
},
|
|
30
|
+
info: {
|
|
31
|
+
icon: lucide_react_1.Info,
|
|
32
|
+
container: "border-blue-200 bg-blue-50 dark:border-blue-900/50 dark:bg-blue-950/40",
|
|
33
|
+
iconClass: "text-blue-600 dark:text-blue-400",
|
|
34
|
+
textClass: "text-blue-900 dark:text-blue-200",
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
const MAX_VISIBLE_TOASTS = 3;
|
|
38
|
+
/** ~5 lines at text-sm / leading-snug */
|
|
39
|
+
const MESSAGE_MAX_HEIGHT_CLASS = "max-h-[6.875rem]";
|
|
33
40
|
function LoggerReactDisplayItem({ message }) {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
default:
|
|
45
|
-
return '#333';
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
const relTs = (0, react_whispr_1.useRelTime)(message.date);
|
|
49
|
-
return ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
50
|
-
padding: '0.5rem',
|
|
51
|
-
borderRadius: '0.375rem',
|
|
52
|
-
boxShadow: '0 1px 3px rgba(0,0,0,0.2)',
|
|
53
|
-
color: '#fff',
|
|
54
|
-
width: '100%',
|
|
55
|
-
maxWidth: '65ch',
|
|
56
|
-
cursor: 'pointer',
|
|
57
|
-
backgroundColor: getColor(message.severity)
|
|
58
|
-
}, onClick: message.dismiss, children: [(0, jsx_runtime_1.jsxs)("div", { style: {
|
|
59
|
-
display: 'flex',
|
|
60
|
-
justifyContent: 'space-between',
|
|
61
|
-
alignItems: 'center',
|
|
62
|
-
}, children: [(0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("strong", { children: ["[", relTs, "] "] }), message.context && (0, jsx_runtime_1.jsxs)("span", { style: {
|
|
63
|
-
marginLeft: '0.5rem',
|
|
64
|
-
}, children: [" - ", message.context, " "] })] }), (0, jsx_runtime_1.jsx)("button", { onClick: message.dismiss, style: {
|
|
65
|
-
marginLeft: '1rem',
|
|
66
|
-
color: '#fff',
|
|
67
|
-
fontWeight: 'bold',
|
|
68
|
-
background: 'none',
|
|
69
|
-
border: 'none',
|
|
70
|
-
cursor: 'pointer',
|
|
71
|
-
padding: 0,
|
|
72
|
-
}, children: (0, jsx_runtime_1.jsx)(lucide_react_1.X, { size: 16 }) })] }), (0, jsx_runtime_1.jsx)("div", { style: {
|
|
73
|
-
marginTop: '0.25rem',
|
|
74
|
-
}, children: message.message })] }, message.id));
|
|
41
|
+
const styles = SEVERITY_STYLES[message.severity];
|
|
42
|
+
const Icon = styles.icon;
|
|
43
|
+
return ((0, jsx_runtime_1.jsxs)("div", { role: "alert", className: `flex items-start gap-3 rounded-lg border px-3 py-2.5 shadow-lg ${styles.container}`, children: [(0, jsx_runtime_1.jsx)(Icon, { size: 16, className: `mt-0.5 shrink-0 ${styles.iconClass}` }), (0, jsx_runtime_1.jsx)("p", { className: `min-w-0 flex-1 overflow-y-auto text-sm leading-snug ${MESSAGE_MAX_HEIGHT_CLASS} ${styles.textClass}`, children: message.message }), (0, jsx_runtime_1.jsx)("button", { type: "button", onClick: message.dismiss, className: "shrink-0 rounded p-0.5 text-gray-400 hover:bg-black/5 hover:text-gray-600 dark:hover:bg-white/10 dark:hover:text-gray-200", "aria-label": "Dismiss", children: (0, jsx_runtime_1.jsx)(lucide_react_1.X, { size: 14 }) })] }));
|
|
44
|
+
}
|
|
45
|
+
function LoggerReactDisplay({ zIndex = 5000 }) {
|
|
46
|
+
const messages = (0, react_whispr_1.useWhisprValue)(Logger_1.default.lines);
|
|
47
|
+
const visible = [...messages].reverse().slice(0, MAX_VISIBLE_TOASTS);
|
|
48
|
+
if (visible.length === 0)
|
|
49
|
+
return null;
|
|
50
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: "pointer-events-none fixed inset-x-0 bottom-4 flex justify-center px-4", style: { zIndex }, children: (0, jsx_runtime_1.jsx)("div", { className: "pointer-events-auto flex w-full max-w-sm flex-col gap-2", children: visible.map((message) => ((0, jsx_runtime_1.jsx)(LoggerReactDisplayItem, { message: message }, message.id))) }) }));
|
|
75
51
|
}
|
|
@@ -2,5 +2,6 @@ export type ButtonProps = {
|
|
|
2
2
|
onClick: () => unknown | Promise<unknown>;
|
|
3
3
|
children: React.ReactElement;
|
|
4
4
|
className?: string;
|
|
5
|
+
onError?: (error: unknown) => void;
|
|
5
6
|
};
|
|
6
|
-
export default function Button({ onClick, children, className }: ButtonProps): import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
export default function Button({ onClick, children, className, onError }: ButtonProps): import("react/jsx-runtime").JSX.Element;
|
package/dist/react-ui/button.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.default = Button;
|
|
|
4
4
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
5
|
const lucide_react_1 = require("lucide-react");
|
|
6
6
|
const react_1 = require("react");
|
|
7
|
-
function Button({ onClick, children, className }) {
|
|
7
|
+
function Button({ onClick, children, className, onError }) {
|
|
8
8
|
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
9
9
|
const [error, setError] = (0, react_1.useState)(null);
|
|
10
10
|
const handleClick = async () => {
|
|
@@ -20,7 +20,9 @@ function Button({ onClick, children, className }) {
|
|
|
20
20
|
})()
|
|
21
21
|
.catch(e => {
|
|
22
22
|
console.trace("Caught error in button", e);
|
|
23
|
-
|
|
23
|
+
const handleError = onError
|
|
24
|
+
?? ((err) => setError(err instanceof Error ? err.message : "Unknown error"));
|
|
25
|
+
handleError(e);
|
|
24
26
|
})
|
|
25
27
|
.finally(() => setLoading(false));
|
|
26
28
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cripty2001/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.227",
|
|
4
4
|
"description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
|
|
5
5
|
"homepage": "https://github.com/cripty2001/utils#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -87,9 +87,9 @@
|
|
|
87
87
|
"require": "./dist/Logger/Logger.js"
|
|
88
88
|
},
|
|
89
89
|
"./logger/react": {
|
|
90
|
-
"import": "./dist/
|
|
91
|
-
"types": "./dist/
|
|
92
|
-
"require": "./dist/
|
|
90
|
+
"import": "./dist/react/logger-display.js",
|
|
91
|
+
"types": "./dist/react/logger-display.d.ts",
|
|
92
|
+
"require": "./dist/react/logger-display.js"
|
|
93
93
|
},
|
|
94
94
|
"./appserver/server": {
|
|
95
95
|
"import": "./dist/Appserver/server.js",
|