mirror of
				https://github.com/CorentinTh/it-tools.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	feat: it-tools v3 base
This commit is contained in:
		
							
								
								
									
										31
									
								
								packages/app/src/modules/tools/tools.provider.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								packages/app/src/modules/tools/tools.provider.tsx
									
									
									
									
									
										Normal 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>
 | 
			
		||||
  );
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user