Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌟 Added console helpers to debug the app state #2259 #2271

Merged
merged 4 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions twake/frontend/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useTimeout from 'app/features/global/hooks/use-timeout';
import ApplicationLoader from './components/loader/application-loader';

import 'app/styles/index.less';
import DebugState from './components/debug/debug-state';

const delayMessage = 5000;

Expand Down Expand Up @@ -51,6 +52,7 @@ export default () => {

return (
<RecoilRoot>
<DebugState />
<MobileRedirect>
<Integration>
<Router history={RouterServices.history}>
Expand Down
97 changes: 97 additions & 0 deletions twake/frontend/src/app/components/debug/debug-state.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React from 'react';
import { useRecoilCallback } from 'recoil';
import EnvironmentService from '../../features/global/framework/environment-service';

interface TwakeDebugState {
dumpStateSnapshot?(): void;
get?(key: string): void;
getAllAtoms?(): void;
}

const twakeDebugState: TwakeDebugState = {};

export default (): React.ReactElement => {
rezk2ll marked this conversation as resolved.
Show resolved Hide resolved
if (EnvironmentService.isProduction()) return <></>;
rezk2ll marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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;

return <></>;
};