1
0
Fork 0
mirror of https://github.com/CorentinTh/it-tools.git synced 2025-07-20 13:49:37 +02:00

feat: it-tools v3 base

This commit is contained in:
Corentin Thomasset 2024-09-30 09:04:13 +02:00
parent 1c35ac3704
commit f8b5cbfd87
No known key found for this signature in database
GPG key ID: DBD997E935996158
530 changed files with 7529 additions and 33524 deletions

View file

@ -0,0 +1,31 @@
import type { Accessor, ParentComponent } from 'solid-js';
import type { ToolI18nFactory } from './tools.types';
import { flatten, type Flatten, translator, type Translator } from '@solid-primitives/i18n';
import { merge } from 'lodash-es';
import { createContext, useContext } from 'solid-js';
type ToolProviderContext = {
toolLocaleDict: Accessor<Record<string, string>>;
};
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');
}
return {
t: translator(() => flatten(merge({}, defaultDictionary, context.toolLocaleDict()))),
};
}
export const CurrentToolProvider: ParentComponent<ToolProviderContext> = (props) => {
return (
<CurrentToolContext.Provider value={props}>
{props.children}
</CurrentToolContext.Provider>
);
};