Skip to content

Commit

Permalink
Context (#2)
Browse files Browse the repository at this point in the history
* init

* WIP traverse

* working types

* optimize
  • Loading branch information
hasnainroopawalla authored Apr 7, 2024
1 parent b7c6fe2 commit e7ff8fd
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/config-context.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export type IBaseConfig<TConfigValueType> = { value: TConfigValueType };

type IConfigWithFilters<TConfigValueType> = (
| { browser: string }
| { platform: string }
| { os: string }
| { engine: string }
) &
IBaseConfig<TConfigValueType>;

type IConfigFilters<TConfigFilter> = [
IBaseConfig<TConfigFilter>,
...IConfigWithFilters<TConfigFilter>[]
];

/**
* A strongly typed `userConfig` object required to create the `IConfig` object.
*/
export type IUserConfig<TConfig> = {
[K in keyof TConfig]: IConfigFilters<TConfig[K]>;
};

/**
* A type-safe object of the config parameters and corresponding values returned by the `useConfig` hook.
*/
export type IConfig<TConfig> = {
[K in keyof TConfig]: IBaseConfig<TConfig[K]>["value"];
};

/**
* A dynamic type to infer the filters of a specific config parameter.
*/
export type IDynamicConfigFilters<TUserConfig> =
IUserConfig<TUserConfig>[Extract<keyof TUserConfig, string>];
28 changes: 28 additions & 0 deletions src/config-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type {
IConfig,
IDynamicConfigFilters,
IUserConfig,
} from "./config-context.interface";

// TODO: move to utils

Check warning on line 7 in src/config-context.tsx

View workflow job for this annotation

GitHub Actions / build-createTag

Unexpected 'TODO' comment: 'TODO: move to utils'
const getBaseConfig = <TUserConfig,>(
configFilters: IDynamicConfigFilters<TUserConfig>
) =>
configFilters.find(
(configValue) =>
Object.keys(configValue).length === 1 && "value" in configValue
)!;

export function createConfig<TUserConfig>(
userConfig: IUserConfig<TUserConfig>
) {
console.log("createConfig");

Check warning on line 19 in src/config-context.tsx

View workflow job for this annotation

GitHub Actions / build-createTag

Unexpected console statement
const configValues: IConfig<TUserConfig> = {} as IConfig<TUserConfig>;
for (const key in userConfig) {
const configFilters = userConfig[key];
const baseConfig = getBaseConfig(configFilters);
configValues[key] = baseConfig.value;
}

return () => configValues;
}
19 changes: 19 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createConfig } from "./config-context";
import { Browser } from "./enums";

export const useConfig = createConfig({
showButton: [
{
value: true,
},
{
browser: Browser.Chrome,
value: false,
},
],
enableModernUi: [
{
value: 33,
},
],
});
35 changes: 34 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { useConfig } from "./config";

const App = () => (
<div>
<Component1 />
<br />
<Component2 />
<br />
<Component3 />
</div>
);

const Component1 = () => {
const _config = useConfig();
console.log("<Component1 />", _config);

Check warning on line 17 in src/index.tsx

View workflow job for this annotation

GitHub Actions / build-createTag

Unexpected console statement
return <span>Component 1</span>;
};

const Component2 = () => {
const _config = useConfig();
console.log("<Component2 />", _config);

Check warning on line 23 in src/index.tsx

View workflow job for this annotation

GitHub Actions / build-createTag

Unexpected console statement
return <span>Component 2</span>;
};

const Component3 = () => {
const [counter, setCounter] = React.useState(0);

const _config = useConfig();
const onClick = () => setCounter(counter + 1);

console.log("<Component3 />", _config);

Check warning on line 33 in src/index.tsx

View workflow job for this annotation

GitHub Actions / build-createTag

Unexpected console statement
return <button onClick={onClick}>Component 3</button>;
};

const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);

root.render(<div>hi</div>);
root.render(<App />);
Empty file added src/project-config.ts
Empty file.

0 comments on commit e7ff8fd

Please sign in to comment.