Skip to content

Commit

Permalink
fix(react-properties): add config debugging utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Jul 22, 2024
1 parent f818ee1 commit eedb11e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useContext, useEffect, useMemo, useState } from "react";
import { Compose, Decorator, makeDecoratable } from "@webiny/react-composition";
import { Property, Properties, toObject } from "~/index";
import { GenericComponent } from "@webiny/react-composition/types";
import { Property, Properties, toObject } from "~/index";
import { useDebugConfig } from "./useDebugConfig";

const createHOC =
(newChildren: React.ReactNode): Decorator<GenericComponent<{ children?: React.ReactNode }>> =>
Expand Down Expand Up @@ -68,6 +69,7 @@ export function createConfigurableComponent<TConfig>(name: string) {

const WithConfig = ({ onProperties, children }: WithConfigProps) => {
const [properties, setProperties] = useState<Property[]>([]);
useDebugConfig(name, properties);
const context = { properties };

useEffect(() => {
Expand Down
27 changes: 27 additions & 0 deletions packages/react-properties/src/useDebugConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useEffect } from "react";
import { Property } from "./Properties";
import { toObject } from "./utils";

declare global {
interface Window {
__debugConfigs: Record<string, () => void>;
}
}

export function useDebugConfig(name: string, properties: Property[]) {
useEffect(() => {
if (process.env.NODE_ENV !== "development") {
return;
}

const configs = window.__debugConfigs ?? {};
configs[name] = () => console.log(toObject(properties));
window.__debugConfigs = configs;

return () => {
const configs = window.__debugConfigs ?? {};
delete configs[name];
window.__debugConfigs = configs;
};
}, [properties]);
}

0 comments on commit eedb11e

Please sign in to comment.