1
0
Fork 0
mirror of https://github.com/CorentinTh/it-tools.git synced 2025-07-19 05:09:37 +02:00
it-tools/packages/app/src/modules/tools/tools.provider.tsx

36 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-09-30 09:04:13 +02:00
import type { Accessor, ParentComponent } from 'solid-js';
2024-10-06 11:32:23 +02:00
import type { ToolDefinition } from './tools.types';
import { flatten, translator } from '@solid-primitives/i18n';
2024-09-30 09:04:13 +02:00
import { merge } from 'lodash-es';
import { createContext, useContext } from 'solid-js';
type ToolProviderContext = {
toolLocaleDict: Accessor<Record<string, string>>;
2024-10-06 11:32:23 +02:00
tool: Accessor<Pick<ToolDefinition, 'icon' | 'dirName' | 'createdAt'> & { name: string; description: string }>;
2024-09-30 09:04:13 +02:00
};
const CurrentToolContext = createContext<ToolProviderContext>();
export function useCurrentTool<T>({ defaultDictionary }: { defaultDictionary: T }) {
const context = useContext(CurrentToolContext);
if (!context) {
throw new Error('useCurrentTool must be used within a CurrentToolProvider');
}
2024-10-06 11:32:23 +02:00
const { toolLocaleDict, tool } = context;
2024-09-30 09:04:13 +02:00
return {
2024-10-06 11:32:23 +02:00
t: translator(() => flatten(merge({}, defaultDictionary, toolLocaleDict()))),
getTool: tool,
2024-09-30 09:04:13 +02:00
};
}
export const CurrentToolProvider: ParentComponent<ToolProviderContext> = (props) => {
return (
<CurrentToolContext.Provider value={props}>
{props.children}
</CurrentToolContext.Provider>
);
};