@cripty2001/utils 0.0.146 → 0.0.148

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.
@@ -1,5 +1,5 @@
1
1
  import { Whispr } from "@cripty2001/whispr";
2
- type LoggerItem = {
2
+ export type LoggerItem = {
3
3
  id: string;
4
4
  date: Date;
5
5
  message: string;
@@ -10,21 +10,6 @@ const Logger_1 = __importDefault(require("../Logger/Logger"));
10
10
  const react_whispr_1 = require("../react-whispr");
11
11
  function LoggerReactDisplay() {
12
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
13
  return ((0, jsx_runtime_1.jsx)("div", { style: {
29
14
  position: 'fixed',
30
15
  bottom: 0,
@@ -43,30 +28,48 @@ function LoggerReactDisplay() {
43
28
  }, children: [...messages]
44
29
  .reverse()
45
30
  .slice(0, 3)
46
- .map((message) => ((0, jsx_runtime_1.jsxs)("div", { style: {
47
- padding: '0.5rem',
48
- borderRadius: '0.375rem',
49
- boxShadow: '0 1px 3px rgba(0,0,0,0.2)',
50
- color: '#fff',
51
- width: '100%',
52
- maxWidth: '65ch',
53
- cursor: 'pointer',
54
- backgroundColor: getColor(message.severity)
55
- }, onClick: message.dismiss, children: [(0, jsx_runtime_1.jsxs)("div", { style: {
56
- display: 'flex',
57
- justifyContent: 'space-between',
58
- alignItems: 'center',
59
- }, 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", { style: {
60
- marginLeft: '0.5rem',
61
- }, children: [" - ", message.context, " "] })] }), (0, jsx_runtime_1.jsx)("button", { onClick: message.dismiss, style: {
62
- marginLeft: '1rem',
63
- color: '#fff',
64
- fontWeight: 'bold',
65
- background: 'none',
66
- border: 'none',
67
- cursor: 'pointer',
68
- padding: 0,
69
- }, children: (0, jsx_runtime_1.jsx)(lucide_react_1.X, { size: 16 }) })] }), (0, jsx_runtime_1.jsx)("div", { style: {
70
- marginTop: '0.25rem',
71
- }, children: message.message })] }, message.id))) }) }));
31
+ .map((message) => ((0, jsx_runtime_1.jsx)(LoggerReactDisplayItem, { message: message }, message.id))) }) }));
32
+ }
33
+ function LoggerReactDisplayItem({ message }) {
34
+ const getColor = (severity) => {
35
+ switch (severity) {
36
+ case 'success':
37
+ return '#4CAF50';
38
+ case 'info':
39
+ return '#2196F3';
40
+ case 'warning':
41
+ return '#FF9800';
42
+ case 'error':
43
+ return '#F44336';
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));
72
75
  }
@@ -110,9 +110,11 @@ export declare function useAsync<I, O>(f: (input: I, setProgress: (p: number) =>
110
110
  export declare function useAsyncEffect<I>(f: (input: I, setProgress: (p: number) => void, signal: AbortSignal) => Promise<void>, data: I, debounce?: number): void;
111
111
  /**
112
112
  * Format a timestamp into a relative time string (e.g. "5 minutes ago", "in 2 hours"), using the browser locale.
113
- * @returns A callback (reactive, will change on refresh) that formats a given timestamp into a relative time string.
113
+ *
114
+ * @param data The data to format. Reactive
115
+ * @returns The formatted timestamp
114
116
  */
115
- export declare function useRelTime(): (ts: Date | number) => string;
117
+ export declare function useRelTime(_data: number | Date): string;
116
118
  /**
117
119
  * React shorthand for the Searcher
118
120
  * @param data The data to search on
@@ -214,59 +214,59 @@ function useAsyncEffect(f, data, debounce = 200) {
214
214
  }
215
215
  /**
216
216
  * Format a timestamp into a relative time string (e.g. "5 minutes ago", "in 2 hours"), using the browser locale.
217
- * @returns A callback (reactive, will change on refresh) that formats a given timestamp into a relative time string.
217
+ *
218
+ * @param data The data to format. Reactive
219
+ * @returns The formatted timestamp
218
220
  */
219
- function useRelTime() {
220
- const currTs = useCurrentTimestamp(1000);
221
- const rtf = (0, react_1.useRef)(new Intl.RelativeTimeFormat(navigator.language, { numeric: "auto" })).current;
222
- const getFormat = (_diff) => {
223
- const diff = Math.abs(_diff);
224
- const breakpoints = [
225
- {
226
- base: 1,
227
- limit: 60,
228
- unit: "second"
229
- },
230
- {
231
- base: 60,
232
- limit: 60,
233
- unit: "minute"
234
- },
235
- {
236
- base: 60 * 60,
237
- limit: 24,
238
- unit: "hour"
239
- },
240
- {
241
- base: 60 * 60 * 24,
242
- limit: 45,
243
- unit: "day"
244
- },
245
- ];
246
- for (const { base, limit, unit } of breakpoints) {
247
- if (diff < limit * base)
248
- return {
249
- base,
250
- unit
251
- };
252
- }
253
- return {
254
- base: 60 * 60 * 24 * 7,
255
- unit: "week"
256
- };
257
- };
258
- const cb = (0, react_1.useCallback)((ts) => {
259
- const now = currTs;
260
- const then = ts instanceof Date ? ts.getTime() : ts;
261
- const delta = then - now;
262
- const seconds = Math.round(delta / 1000);
263
- const { base, unit } = getFormat(seconds);
221
+ function useRelTime(_data) {
222
+ const rtf = new Intl.RelativeTimeFormat(navigator.language, { numeric: "auto" });
223
+ const data = useWhispr(_data);
224
+ const toReturn = useSafeRef(() => whispr_1.Whispr.from({ data, curr: (0, _1.CURRENT_TS_MS)() }, ({ data, curr }) => {
225
+ const then = data instanceof Date ? data.getTime() : data;
226
+ const seconds = Math.round(then - curr) / 1000;
227
+ const { base, unit } = useRelTimeFormat(seconds);
264
228
  const rounded = seconds > 0 ?
265
229
  Math.floor(seconds / base) :
266
230
  Math.ceil(seconds / base);
267
231
  return rtf.format(rounded, unit);
268
- }, [currTs, rtf]);
269
- return cb;
232
+ }));
233
+ return useWhisprValue(toReturn);
234
+ }
235
+ function useRelTimeFormat(_diff) {
236
+ const diff = Math.abs(_diff);
237
+ const breakpoints = [
238
+ {
239
+ base: 1,
240
+ limit: 60,
241
+ unit: "second"
242
+ },
243
+ {
244
+ base: 60,
245
+ limit: 60,
246
+ unit: "minute"
247
+ },
248
+ {
249
+ base: 60 * 60,
250
+ limit: 24,
251
+ unit: "hour"
252
+ },
253
+ {
254
+ base: 60 * 60 * 24,
255
+ limit: 45,
256
+ unit: "day"
257
+ },
258
+ ];
259
+ for (const { base, limit, unit } of breakpoints) {
260
+ if (diff < limit * base)
261
+ return {
262
+ base,
263
+ unit
264
+ };
265
+ }
266
+ return {
267
+ base: 60 * 60 * 24 * 7,
268
+ unit: "week"
269
+ };
270
270
  }
271
271
  /**
272
272
  * React shorthand for the Searcher
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.146",
3
+ "version": "0.0.148",
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": {