-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
77 lines (70 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { formatTime } from './utils'
import { useTransactionObservation_UNSTABLE } from 'recoil'
const generateColors = (isDark = true) => {
const colors = {
error: 'color: ',
info: 'color: ',
previous: 'color: ',
base: 'color: ',
}
if (isDark) {
colors.error += '#ef6e70'
colors.info += '#9bceff'
colors.previous += '#d4d4d4'
colors.base += '#fff'
} else {
colors.error += '#c41518'
colors.info += '#2d02cc'
colors.previous += '#444'
colors.base += '#000'
}
return colors
}
const setColors = () => {
const isDark =
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
return generateColors(isDark)
}
const logAction = (action) => {
const colors = setColors()
console.groupCollapsed(
'%c%s %s %s',
colors.base,
formatTime(new Date()),
'Atom name',
action.name,
)
if (action.persistence) {
console.log('%cValue of atom cannot be read', colors.error)
console.log(
'%cPlease add: %c`persistence_UNSTABLE: { type: "log" }` %cto see the values for atom object: ',
colors.base,
colors.info,
colors.base,
action.name,
)
} else {
console.log('%cAtom value %o', colors.base, action.atomValue)
console.log(
'%cPrevious atom value %o',
colors.previous,
action.previousAtomValue,
)
}
console.groupEnd()
}
export function RecoilLogger() {
useTransactionObservation_UNSTABLE((e) => {
e.modifiedAtoms.forEach((name) => {
logAction({
name,
atomValue: e.atomValues.get(name),
previousAtomValue: e.previousAtomValues.get(name),
persistence: e.atomInfo.get(name).persistence_UNSTABLE.type === 'none',
})
})
})
return null
}
export default RecoilLogger