11import { useEffect , useRef } from 'react'
2+ import { UseLog , UseLogReturn , Log , PrintTypes , PrintProps } from './types'
3+ import { getComponentName , print } from './utils'
24
35const CSS_COMPONENT = 'color: DodgerBlue'
46const CSS_CHANGE = 'color: green; font-weight: bold;'
57const CSS_SUB_VALUE = 'color: SlateGray; font-weight: thin;'
68
79const ALLOWED_NODE_ENVS = [ 'dev' , 'development' ]
810
9- export interface UseLog {
10- styles ?: {
11- componentCSS ?: string
12- changeCSS ?: string
13- subValueCSS ?: string
14- }
15- environments ?: string [ ]
16- }
17-
18- export type Log = UseLog
19-
20- export interface UseLogReturn {
21- log : < T > ( value : T , props ?: Log ) => void
22- }
23-
24- interface PrintProps < T > {
25- value : T
26- prevValue ?: T
27- label : string
28- group ?: string
29- type ?: PrintTypes
30- }
31-
32- export enum PrintTypes {
33- Mount = 'Mount' ,
34- Unmount = 'Unmount' ,
35- Change = 'Change' ,
36- }
37-
3811export function useLog ( {
3912 styles : {
4013 componentCSS = CSS_COMPONENT ,
@@ -43,64 +16,26 @@ export function useLog({
4316 } = { } ,
4417 environments = ALLOWED_NODE_ENVS ,
4518} : UseLog = { } ) : UseLogReturn {
46- const componentName =
47- ( function getComponentName ( ) {
48- try {
49- throw new Error ( )
50- } catch ( error ) {
51- if ( error instanceof Error ) {
52- const re = / ( \w + ) @ | a t ( \w + ) \( / g
53-
54- re . exec ( error ?. stack ?? '' )
55- re . exec ( error ?. stack ?? '' )
56- const m = re . exec ( error ?. stack ?? '' ) ?? [ ]
57-
58- return String ( m [ 1 ] || m [ 2 ] )
59- }
60- }
61- } ) ( ) ?? ''
62-
63- function getGroupLabel ( type : PrintTypes ) : string {
64- return `${ String ( type ) } ${
65- componentName ? 'in %c<' + String ( componentName ) + ' /> ' : '%c'
66- } %c@ ${ new Date ( ) . toLocaleTimeString ( ) } `
67- }
19+ const componentName = getComponentName ( )
6820
6921 function log < T > ( value : T , props ?: Log ) : void {
70- const clonedValue = JSON . parse ( JSON . stringify ( value ) )
22+ const clonedValue = JSON . parse ( JSON . stringify ( value ) ) as T
7123 const prevValueRef = useRef < T > ( )
72-
73- function print < T > ( {
74- value,
75- label,
76- prevValue,
77- type = PrintTypes . Change ,
78- group = getGroupLabel ( type ) ,
79- } : PrintProps < T > ) : void {
80- console . group (
81- group ,
82- props ?. styles ?. componentCSS ?? componentCSS ,
83- props ?. styles ?. subValueCSS ?? subValueCSS ,
84- )
85-
86- if ( ! ( 'prevValue' in arguments [ 0 ] ) ) {
87- console . log ( `${ label . padStart ( 14 , ' ' ) } : ${ String ( value ) } ` )
88- } else {
89- console . log (
90- `Previous value: %c${ String ( arguments [ 0 ] . prevValue ) } ` ,
91- props ?. styles ?. subValueCSS ?? subValueCSS ,
92- )
93- console . log (
94- ` Current value: %c${ String ( value ) } ` ,
95- props ?. styles ?. changeCSS ?? changeCSS ,
96- )
97- }
98-
99- console . groupEnd ( )
24+ const printProps : Pick <
25+ PrintProps < T > ,
26+ 'value' | 'styles' | 'componentName'
27+ > = {
28+ value : clonedValue ,
29+ styles : {
30+ componentCSS : props ?. styles ?. componentCSS ?? componentCSS ,
31+ subValueCSS : props ?. styles ?. subValueCSS ?? subValueCSS ,
32+ changeCSS : props ?. styles ?. changeCSS ?? changeCSS ,
33+ } ,
34+ componentName,
10035 }
10136
102- if ( environments . includes ( process . env . NODE_ENV ?? '' ) ) {
103- return ( function logHooks ( ) {
37+ if ( environments . includes ( process . env . NODE_ENV ?? 'production ' ) ) {
38+ function logHooks ( ) : void {
10439 const isUnmounting = useRef ( false )
10540 useEffect ( function setIsUnmounting ( ) {
10641 return function setIsUnmountingOnMount ( ) {
@@ -111,18 +46,18 @@ export function useLog({
11146 useEffect ( function onMount ( ) {
11247 print ( {
11348 label : 'On mount' ,
114- value : clonedValue ,
11549 type : PrintTypes . Mount ,
50+ ...printProps ,
11651 } )
11752
11853 prevValueRef . current = value
11954
12055 return function onUnmount ( ) {
12156 print ( {
12257 label : 'On unmount' ,
123- value : clonedValue ,
12458 type : PrintTypes . Unmount ,
12559 prevValue : prevValueRef . current ,
60+ ...printProps ,
12661 } )
12762 }
12863 } , [ ] )
@@ -131,16 +66,18 @@ export function useLog({
13166 function onChange ( ) {
13267 print ( {
13368 label : 'On change' ,
134- value : clonedValue ,
13569 type : PrintTypes . Change ,
13670 prevValue : prevValueRef . current ,
71+ ...printProps ,
13772 } )
13873
13974 prevValueRef . current = value
14075 } ,
14176 [ value ] ,
14277 )
143- } ) ( )
78+ }
79+
80+ return logHooks ( )
14481 }
14582 }
14683
0 commit comments