1- import { useEffect , useRef } from 'react'
2- import { UseLog , UseLogReturn , Log , PrintTypes , PrintProps } from './types'
3- import { getComponentName , print } from './utils'
1+ // Copyright (c) Dolf Barr <[email protected] >. All rights reserved. Licensed under the MIT license. 42
5- const CSS_COMPONENT = 'color: DodgerBlue'
6- const CSS_CHANGE = 'color: green; font-weight: bold;'
7- const CSS_SUB_VALUE = 'color: SlateGray; font-weight: thin;'
3+ /**
4+ * A react hook for logging through component lifecycle.
5+ *
6+ * @packageDocumentation
7+ */
88
9- const ALLOWED_NODE_ENVS = [ 'dev' , 'development' ]
9+ import { useEffect , useRef } from 'react'
10+ import {
11+ ALLOWED_NODE_ENVS ,
12+ CSS_CHANGE ,
13+ CSS_COMPONENT ,
14+ CSS_SUB_VALUE ,
15+ } from './constants'
16+ import {
17+ UseLogConfig ,
18+ UseLogReturn ,
19+ LogConfig ,
20+ _PrintTypes ,
21+ _PrintConfig ,
22+ } from './types'
23+ import { getComponentName , print } from './utils'
1024
25+ /**
26+ * Provides a function to log through react component lifecycle.
27+ *
28+ * @param config - component level configuration for any log function in the component
29+ * @see {@link UseLogConfig } for the config data structure
30+ *
31+ * @returns set of functions suitable for logging
32+ *
33+ * @example
34+ * ```ts
35+ * const {log} = useLog({environments: ['dev']})
36+ * ```
37+ */
1138export function useLog ( {
1239 styles : {
1340 componentCSS = CSS_COMPONENT ,
1441 changeCSS = CSS_CHANGE ,
1542 subValueCSS = CSS_SUB_VALUE ,
1643 } = { } ,
1744 environments = ALLOWED_NODE_ENVS ,
18- } : UseLog = { } ) : UseLogReturn {
45+ } : UseLogConfig = { } ) : UseLogReturn {
1946 const componentName = getComponentName ( )
2047
21- function log < T > ( value : T , props ?: Log ) : void {
48+ /**
49+ * Logging function to log through react component lifecycle.
50+ *
51+ * @param value - a value which changes will be logged
52+ * @typeParam T - type of the tracking value
53+ * @param config - component level configuration for any log function in the component
54+ * @see {@link LogConfig } for the config data structure
55+ *
56+ * @example
57+ * ```ts
58+ * log(someState, {environments: ['production']})
59+ * ```
60+ */
61+ function log < T > ( value : T , props ?: LogConfig ) : void {
2262 const clonedValue = JSON . parse ( JSON . stringify ( value ) ) as T
2363 const prevValueRef = useRef < T > ( )
2464 const printProps : Pick <
25- PrintProps < T > ,
65+ _PrintConfig < T > ,
2666 'value' | 'styles' | 'componentName'
2767 > = {
2868 value : clonedValue ,
@@ -37,6 +77,7 @@ export function useLog({
3777 if ( environments . includes ( process . env . NODE_ENV ?? 'production' ) ) {
3878 function logHooks ( ) : void {
3979 const isUnmounting = useRef ( false )
80+
4081 useEffect ( function setIsUnmounting ( ) {
4182 return function setIsUnmountingOnMount ( ) {
4283 isUnmounting . current = true
@@ -46,7 +87,7 @@ export function useLog({
4687 useEffect ( function onMount ( ) {
4788 print ( {
4889 label : 'On mount' ,
49- type : PrintTypes . Mount ,
90+ type : _PrintTypes . Mount ,
5091 ...printProps ,
5192 } )
5293
@@ -55,7 +96,7 @@ export function useLog({
5596 return function onUnmount ( ) {
5697 print ( {
5798 label : 'On unmount' ,
58- type : PrintTypes . Unmount ,
99+ type : _PrintTypes . Unmount ,
59100 prevValue : prevValueRef . current ,
60101 ...printProps ,
61102 } )
@@ -66,7 +107,7 @@ export function useLog({
66107 function onChange ( ) {
67108 print ( {
68109 label : 'On change' ,
69- type : PrintTypes . Change ,
110+ type : _PrintTypes . Change ,
70111 prevValue : prevValueRef . current ,
71112 ...printProps ,
72113 } )
0 commit comments