@cripty2001/utils 0.0.39 → 0.0.41
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.
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Whispr } from "@cripty2001/whispr";
|
|
2
|
+
type LoggerItem = {
|
|
3
|
+
id: string;
|
|
4
|
+
date: Date;
|
|
5
|
+
message: string;
|
|
6
|
+
severity: "success" | "info" | "warning" | "error";
|
|
7
|
+
context: string | undefined;
|
|
8
|
+
dismiss: () => void;
|
|
9
|
+
trace: string | undefined;
|
|
10
|
+
};
|
|
11
|
+
declare class Logger {
|
|
12
|
+
private static instance;
|
|
13
|
+
lines: Whispr<LoggerItem[]>;
|
|
14
|
+
private setLines;
|
|
15
|
+
private constructor();
|
|
16
|
+
private reconstructError;
|
|
17
|
+
static getInstance(): Logger;
|
|
18
|
+
log(message: string, severity?: LoggerItem['severity'], context?: string): void;
|
|
19
|
+
logError(error: Error | DOMException | any): void;
|
|
20
|
+
}
|
|
21
|
+
declare const _default: Logger;
|
|
22
|
+
export default _default;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const whispr_1 = require("@cripty2001/whispr");
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
class Logger {
|
|
6
|
+
static instance;
|
|
7
|
+
lines;
|
|
8
|
+
setLines;
|
|
9
|
+
constructor() {
|
|
10
|
+
[this.lines, this.setLines] = whispr_1.Whispr.create([]);
|
|
11
|
+
window.addEventListener("unhandledrejection", (e) => this.log(e.reason, "error"));
|
|
12
|
+
window.addEventListener("error", (e) => this.logError(this.reconstructError(e)));
|
|
13
|
+
}
|
|
14
|
+
reconstructError(e) {
|
|
15
|
+
if (e.error && e.error instanceof Error) {
|
|
16
|
+
return e.error;
|
|
17
|
+
}
|
|
18
|
+
if (e.message && typeof e.message === 'string') {
|
|
19
|
+
return new Error(e.message);
|
|
20
|
+
}
|
|
21
|
+
return new Error("Unknown error");
|
|
22
|
+
}
|
|
23
|
+
static getInstance() {
|
|
24
|
+
if (!Logger.instance) {
|
|
25
|
+
Logger.instance = new Logger();
|
|
26
|
+
}
|
|
27
|
+
return Logger.instance;
|
|
28
|
+
}
|
|
29
|
+
log(message, severity = "info", context) {
|
|
30
|
+
const item = {
|
|
31
|
+
id: (0, __1.getRandomId)(),
|
|
32
|
+
date: new Date(),
|
|
33
|
+
message,
|
|
34
|
+
severity,
|
|
35
|
+
context,
|
|
36
|
+
dismiss: () => {
|
|
37
|
+
this.setLines(this.lines.value.filter(i => i.id !== item.id));
|
|
38
|
+
},
|
|
39
|
+
trace: (new Error()).stack
|
|
40
|
+
};
|
|
41
|
+
this.setLines([...this.lines.value, item]);
|
|
42
|
+
}
|
|
43
|
+
logError(error) {
|
|
44
|
+
if (error instanceof DOMException ||
|
|
45
|
+
error instanceof Error ||
|
|
46
|
+
false) {
|
|
47
|
+
return this.log(error.message, "error", error.stack);
|
|
48
|
+
}
|
|
49
|
+
console.trace('UNKNOWN ERROR', error);
|
|
50
|
+
return this.log("Unknown error - See Console", "error");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.default = Logger.getInstance();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function LoggerReactDisplay(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.LoggerReactDisplay = LoggerReactDisplay;
|
|
7
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
+
const react_whispr_1 = require("../react-whispr");
|
|
9
|
+
const Logger_1 = __importDefault(require("./Logger"));
|
|
10
|
+
const lucide_react_1 = require("lucide-react");
|
|
11
|
+
function LoggerReactDisplay() {
|
|
12
|
+
const messages = (0, react_whispr_1.useWhisprValue)(Logger_1.default.lines);
|
|
13
|
+
const getColor = (severity) => {
|
|
14
|
+
switch (severity) {
|
|
15
|
+
case 'success':
|
|
16
|
+
return '#4CAF50';
|
|
17
|
+
case 'info':
|
|
18
|
+
return '#2196F3';
|
|
19
|
+
case 'warning':
|
|
20
|
+
return '#FF9800';
|
|
21
|
+
case 'error':
|
|
22
|
+
return '#F44336';
|
|
23
|
+
default:
|
|
24
|
+
return '#333';
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const relTs = (0, react_whispr_1.useRelTime)();
|
|
28
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: "fixed bottom-0 right-0 w-full z-50 p-4 rounded ", children: (0, jsx_runtime_1.jsx)("div", { className: "flex flex-col gap-2 items-center w-full justify-center", children: messages
|
|
29
|
+
.slice(0, 3)
|
|
30
|
+
.map((message) => ((0, jsx_runtime_1.jsxs)("div", { className: `p-2 rounded shadow-md text-white ng-black w-full max-w-prose cursor-pointer`, onClick: message.dismiss, style: {
|
|
31
|
+
backgroundColor: getColor(message.severity)
|
|
32
|
+
}, children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex justify-between items-center", children: [(0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("strong", { children: ["[", relTs(message.date), "] "] }), message.context && (0, jsx_runtime_1.jsxs)("span", { className: "ml-2", children: [" - ", message.context, " "] })] }), (0, jsx_runtime_1.jsx)("button", { onClick: message.dismiss, className: "ml-4 text-white font-bold", children: (0, jsx_runtime_1.jsx)(lucide_react_1.X, { size: 16 }) })] }), (0, jsx_runtime_1.jsxs)("div", { className: "mt-1", children: [" ", message.message, " "] })] }, message.id))) }) }));
|
|
33
|
+
}
|
package/dist/react-whispr.d.ts
CHANGED
|
@@ -56,3 +56,10 @@ export declare function useDebounced<T>(value: T): T;
|
|
|
56
56
|
* @type O Output for the async function
|
|
57
57
|
*/
|
|
58
58
|
export declare function useAsync<I, O>(f: (input: I, setProgress: (p: number) => void, signal: AbortSignal) => Promise<O>, data: I, debouce?: number): Dispatcher<I, O>;
|
|
59
|
+
/**
|
|
60
|
+
* Format a timestamp into a relative time string (e.g. "5 minutes ago", "in 2 hours"), using the browser locale.
|
|
61
|
+
* The refreshed time is reactive.
|
|
62
|
+
* @param refresh The refresh interval, in milliseconds. Default to 1000ms.
|
|
63
|
+
* @returns A callback (reactive, will change on refresh) that formats a given timestamp into a relative time string.
|
|
64
|
+
*/
|
|
65
|
+
export declare function useRelTime(refresh?: number): (ts: Date | number) => string;
|
package/dist/react-whispr.js
CHANGED
|
@@ -6,6 +6,7 @@ exports.useOnWhispr = useOnWhispr;
|
|
|
6
6
|
exports.useCurrentTimestamp = useCurrentTimestamp;
|
|
7
7
|
exports.useDebounced = useDebounced;
|
|
8
8
|
exports.useAsync = useAsync;
|
|
9
|
+
exports.useRelTime = useRelTime;
|
|
9
10
|
const whispr_1 = require("@cripty2001/whispr");
|
|
10
11
|
const react_1 = require("react");
|
|
11
12
|
const lodash_1 = require("lodash");
|
|
@@ -118,3 +119,21 @@ function useAsync(f, data, debouce = 200) {
|
|
|
118
119
|
// Returning dispatcher
|
|
119
120
|
return dispatcher;
|
|
120
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Format a timestamp into a relative time string (e.g. "5 minutes ago", "in 2 hours"), using the browser locale.
|
|
124
|
+
* The refreshed time is reactive.
|
|
125
|
+
* @param refresh The refresh interval, in milliseconds. Default to 1000ms.
|
|
126
|
+
* @returns A callback (reactive, will change on refresh) that formats a given timestamp into a relative time string.
|
|
127
|
+
*/
|
|
128
|
+
function useRelTime(refresh = 1000) {
|
|
129
|
+
const currTs = useCurrentTimestamp(refresh);
|
|
130
|
+
const rtf = (0, react_1.useRef)(new Intl.RelativeTimeFormat(navigator.language, { numeric: "auto" })).current;
|
|
131
|
+
const cb = (0, react_1.useCallback)((ts) => {
|
|
132
|
+
const now = currTs;
|
|
133
|
+
const then = ts instanceof Date ? ts.getTime() : ts;
|
|
134
|
+
const delta = then - now;
|
|
135
|
+
const seconds = Math.round(delta / 1000);
|
|
136
|
+
return rtf.format(seconds, "second");
|
|
137
|
+
}, [currTs, rtf]);
|
|
138
|
+
return cb;
|
|
139
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cripty2001/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.41",
|
|
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": {
|
|
@@ -34,11 +34,12 @@
|
|
|
34
34
|
"lodash": "^4.17.21"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
+
"lucide-react": "^0.552.0",
|
|
38
|
+
"@msgpack/msgpack": "^3.1.2",
|
|
37
39
|
"@sinclair/typebox": "^0.34.41",
|
|
38
40
|
"dotenv": "^17.2.3",
|
|
39
41
|
"express": "^5.1.0",
|
|
40
|
-
"react": "^19"
|
|
41
|
-
"@msgpack/msgpack": "^3.1.2"
|
|
42
|
+
"react": "^19"
|
|
42
43
|
},
|
|
43
44
|
"peerDependenciesMeta": {
|
|
44
45
|
"react": {
|
|
@@ -71,4 +72,4 @@
|
|
|
71
72
|
"types": "./dist/Appserver/client.d.ts"
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
|
-
}
|
|
75
|
+
}
|