Skip to main content
Glama
FocusDictionaryContext.tsx2.38 kB
import { MessageKey } from '@intlayer/editor'; import type { KeyPath } from '@intlayer/types'; import { type Component, createContext, type ParentProps, type Setter, useContext, } from 'solid-js'; import { useCrossFrameState } from './useCrossFrameState'; type DictionaryPath = string; export type FileContent = { dictionaryKey: string; keyPath?: KeyPath[]; dictionaryPath?: DictionaryPath; }; type FocusDictionaryState = { focusedContent: FileContent | null; }; type FocusDictionaryActions = { setFocusedContent: Setter<FileContent | null>; setFocusedContentKeyPath: (keyPath: KeyPath[]) => void; }; const FocusDictionaryStateContext = createContext< FocusDictionaryState | undefined >(undefined); const FocusDictionaryActionsContext = createContext< FocusDictionaryActions | undefined >(undefined); export const FocusDictionaryProvider: Component<ParentProps> = (props) => { const [focusedContent, setFocusedContent] = useCrossFrameState<FileContent | null>( MessageKey.INTLAYER_FOCUSED_CONTENT_CHANGED, null ); const setFocusedContentKeyPath = (keyPath: KeyPath[]) => { setFocusedContent((prev) => { if (!prev) { return prev; // nothing to update if there's no focused content } return { ...prev, keyPath }; }); }; return ( <FocusDictionaryStateContext.Provider value={{ focusedContent: focusedContent() }} > <FocusDictionaryActionsContext.Provider value={{ setFocusedContent: setFocusedContent as Setter<FileContent | null>, setFocusedContentKeyPath, }} > {props.children} </FocusDictionaryActionsContext.Provider> </FocusDictionaryStateContext.Provider> ); }; export const useFocusDictionaryActions = () => { const context = useContext(FocusDictionaryActionsContext); if (context === undefined) { throw new Error( 'useFocusDictionaryActions must be used within a FocusDictionaryProvider' ); } return context; }; export const useFocusDictionary = () => { const actionContext = useFocusDictionaryActions(); const stateContext = useContext(FocusDictionaryStateContext); if (stateContext === undefined) { throw new Error( 'useFocusDictionaryState must be used within a FocusDictionaryProvider' ); } return { ...stateContext, ...actionContext }; };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aymericzip/intlayer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server