@cripty2001/utils 0.0.147 → 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
@@ -36,18 +36,11 @@ const Searcher_1 = require("./Searcher");
36
36
  function useWhisprValue(w, computer = (d) => d) {
37
37
  const value_w = (0, react_1.useRef)(whispr_1.Whispr.from({ w }, ({ w }) => computer(w))).current;
38
38
  const [value, setValue] = (0, react_1.useState)(value_w.value);
39
- const valueRef = (0, react_1.useRef)(value);
40
- (0, react_1.useEffect)(() => {
41
- valueRef.current = value;
42
- }, [value]);
43
- (0, react_1.useEffect)(() => {
44
- const unsub = value_w.subscribe((newValue) => {
45
- if ((0, lodash_1.isEqual)(newValue, valueRef.current))
46
- return;
47
- setValue(newValue);
48
- }, false);
49
- return () => unsub();
50
- }, [value_w]);
39
+ value_w.subscribe((newValue) => {
40
+ if ((0, lodash_1.isEqual)(newValue, value))
41
+ return;
42
+ setValue(newValue);
43
+ }, false); // Already got the initial value, and this will call syncronously generate a react warning as called on an not yet mounted component
51
44
  return value;
52
45
  }
53
46
  /**
@@ -221,59 +214,59 @@ function useAsyncEffect(f, data, debounce = 200) {
221
214
  }
222
215
  /**
223
216
  * Format a timestamp into a relative time string (e.g. "5 minutes ago", "in 2 hours"), using the browser locale.
224
- * @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
225
220
  */
226
- function useRelTime() {
227
- const currTs = useCurrentTimestamp(1000);
228
- const rtf = (0, react_1.useRef)(new Intl.RelativeTimeFormat(navigator.language, { numeric: "auto" })).current;
229
- const getFormat = (_diff) => {
230
- const diff = Math.abs(_diff);
231
- const breakpoints = [
232
- {
233
- base: 1,
234
- limit: 60,
235
- unit: "second"
236
- },
237
- {
238
- base: 60,
239
- limit: 60,
240
- unit: "minute"
241
- },
242
- {
243
- base: 60 * 60,
244
- limit: 24,
245
- unit: "hour"
246
- },
247
- {
248
- base: 60 * 60 * 24,
249
- limit: 45,
250
- unit: "day"
251
- },
252
- ];
253
- for (const { base, limit, unit } of breakpoints) {
254
- if (diff < limit * base)
255
- return {
256
- base,
257
- unit
258
- };
259
- }
260
- return {
261
- base: 60 * 60 * 24 * 7,
262
- unit: "week"
263
- };
264
- };
265
- const cb = (0, react_1.useCallback)((ts) => {
266
- const now = currTs;
267
- const then = ts instanceof Date ? ts.getTime() : ts;
268
- const delta = then - now;
269
- const seconds = Math.round(delta / 1000);
270
- 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);
271
228
  const rounded = seconds > 0 ?
272
229
  Math.floor(seconds / base) :
273
230
  Math.ceil(seconds / base);
274
231
  return rtf.format(rounded, unit);
275
- }, [currTs, rtf]);
276
- 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
+ };
277
270
  }
278
271
  /**
279
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.147",
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": {