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';
|
2024-10-02 22:30:45 +02:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
};
|