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

Context #2

Merged
merged 4 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 / lint-build-test

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 / lint-build-test

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 / lint-build-test

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 / lint-build-test

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 / lint-build-test

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.
Loading