-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌟 Added custom hook to debug recoil state
- Loading branch information
Showing
3 changed files
with
88 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,9 @@ | ||
import React from 'react'; | ||
import { useRecoilCallback } from 'recoil'; | ||
import EnvironmentService from '../../features/global/framework/environment-service'; | ||
import { useDebugRecoilState } from 'app/features/debug/hooks/use-debug-recoil-state'; | ||
|
||
interface TwakeDebugState { | ||
dumpStateSnapshot?(): void; | ||
get?(key: string): void; | ||
getAllAtoms?(): void; | ||
} | ||
|
||
const twakeDebugState: TwakeDebugState = {}; | ||
|
||
export default (): React.ReactElement => { | ||
if (EnvironmentService.isProduction()) return <></>; | ||
|
||
/** | ||
* lists the value of all atoms | ||
* | ||
* @memberof TwakeDebugState | ||
* @returns {void} | ||
*/ | ||
twakeDebugState.getAllAtoms = useRecoilCallback( | ||
({ snapshot }) => | ||
async () => { | ||
console.debug('Atom values:'); | ||
for (const node of snapshot.getNodes_UNSTABLE()) { | ||
const value = await snapshot.getPromise(node); | ||
|
||
console.debug(node.key, value); | ||
} | ||
}, | ||
[], | ||
); | ||
|
||
/** | ||
* Get the value of an atom by key | ||
* | ||
* @param {string} key - The key of the atom | ||
* @memberof TwakeDebugState | ||
* @returns {void} | ||
*/ | ||
twakeDebugState.get = useRecoilCallback( | ||
({ snapshot }) => | ||
async (key: string) => { | ||
const allNodes = Array.from(snapshot.getNodes_UNSTABLE()); | ||
const node = allNodes.find(node => node.key === key); | ||
|
||
if (node) { | ||
const value = await snapshot.getPromise(node); | ||
|
||
console.debug(key, value); | ||
} | ||
}, | ||
[], | ||
); | ||
|
||
/** | ||
* Dump the current state of the application to a json file | ||
* | ||
* @memberof TwakeDebugState | ||
* @returns {void} | ||
*/ | ||
twakeDebugState.dumpStateSnapshot = useRecoilCallback( | ||
({ snapshot }) => | ||
async () => { | ||
const result: Record<string, any> = { | ||
localStorage: {}, | ||
}; | ||
|
||
for (const node of snapshot.getNodes_UNSTABLE()) { | ||
const value = await snapshot.getPromise(node); | ||
|
||
result[node.key] = value; | ||
} | ||
|
||
for (const key of Object.keys(window.localStorage)) { | ||
result.localStorage[key] = window.localStorage.getItem(key); | ||
} | ||
|
||
const json = JSON.stringify(result, null, 2); | ||
const blob = new Blob([json], { type: 'application/json' }); | ||
const url = URL.createObjectURL(blob); | ||
const link = document.createElement('a'); | ||
|
||
link.href = url; | ||
link.download = `twake-state-${new Date().toISOString()}.json`; | ||
|
||
link.click(); | ||
URL.revokeObjectURL(url); | ||
}, | ||
[], | ||
); | ||
|
||
(window as any).twakeDebugState = twakeDebugState; | ||
useDebugRecoilState(); | ||
|
||
return <></>; | ||
}; |
81 changes: 81 additions & 0 deletions
81
twake/frontend/src/app/features/debug/hooks/use-debug-recoil-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useRecoilCallback } from 'recoil'; | ||
import { TwakeDebugState } from '../types/debug-recoil-state-types'; | ||
|
||
const twakeDebugState: TwakeDebugState = {}; | ||
|
||
export const useDebugRecoilState = () => { | ||
|
||
/** | ||
* Get the value of an atom by key | ||
* | ||
* @param {string} key - The key of the atom | ||
* @returns {void} | ||
*/ | ||
twakeDebugState.get = useRecoilCallback( | ||
({ snapshot }) => | ||
async (key: string) => { | ||
const allNodes = Array.from(snapshot.getNodes_UNSTABLE()); | ||
const node = allNodes.find(node => node.key === key); | ||
|
||
if (node) { | ||
console.debug(key, await snapshot.getPromise(node)); | ||
} | ||
}, | ||
[], | ||
); | ||
|
||
/** | ||
* Dump the current state of the application to a json file | ||
* | ||
* @returns {void} | ||
*/ | ||
twakeDebugState.dumpStateSnapshot = useRecoilCallback( | ||
({ snapshot }) => | ||
async () => { | ||
const result: Record<string, any> = { | ||
localStorage: {}, | ||
}; | ||
|
||
for (const node of snapshot.getNodes_UNSTABLE()) { | ||
const value = await snapshot.getPromise(node); | ||
|
||
result[node.key] = value; | ||
} | ||
|
||
for (const key of Object.keys(window.localStorage)) { | ||
result.localStorage[key] = window.localStorage.getItem(key); | ||
} | ||
|
||
const json = JSON.stringify(result, null, 2); | ||
const blob = new Blob([json], { type: 'application/json' }); | ||
const url = URL.createObjectURL(blob); | ||
const link = document.createElement('a'); | ||
|
||
link.href = url; | ||
link.download = `twake-state-${new Date().toISOString()}.json`; | ||
|
||
link.click(); | ||
URL.revokeObjectURL(url); | ||
}, | ||
[], | ||
); | ||
|
||
/** | ||
* lists the value of all atoms | ||
* | ||
* @returns {void} | ||
*/ | ||
twakeDebugState.getAllAtoms = useRecoilCallback( | ||
({ snapshot }) => | ||
async () => { | ||
for (const node of snapshot.getNodes_UNSTABLE()) { | ||
const value = await snapshot.getPromise(node); | ||
|
||
console.debug(node.key, value); | ||
} | ||
}, | ||
[], | ||
); | ||
|
||
(window as any).twakeDebugState = twakeDebugState; | ||
}; |
5 changes: 5 additions & 0 deletions
5
twake/frontend/src/app/features/debug/types/debug-recoil-state-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface TwakeDebugState { | ||
dumpStateSnapshot?(): void; | ||
get?(key: string): void; | ||
getAllAtoms?(): void; | ||
} |