Skip to content

Commit

Permalink
refactor(plugin-flow-builder): use Record<keyof T, string> to type co…
Browse files Browse the repository at this point in the history
…ntents object
  • Loading branch information
Iru89 committed Jul 8, 2024
1 parent 98c5d88 commit 7a23d15
Showing 1 changed file with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@ import { createContext } from 'react'

import { WebviewContentsContextType } from './types'

export const WebviewContentsContext = createContext<
WebviewContentsContextType<unknown>
>({
getTextContent: () => '',
getImageSrc: () => '',
setCurrentLocale: () => undefined,
contents: {},
})
/*
Define a generic type parameter for your context
WebviewContentsContext is a function that returns a context object with the type parameter T
It is necessary to create the context outside the component
e.g.
interface WebviewContents {
textIntro: string
image2: string
headerWebview: string
image: string
}
export const MyWebviewContentsContext = WebviewContentsContext<WebviewContents>()
Then you can use it in your component like this:
<MyWebviewContentsContext.Provider value={webviewContentsContext}>
...// your components
</MyWebviewContentsContext.Provider>
In your components you can use the context to read contents like this:
const { contents } = useContext(MyWebviewContentsContext)
*/
export const WebviewContentsContext = <T>() =>
createContext<WebviewContentsContextType<T>>({
getTextContent: () => '',
getImageSrc: () => '',

Check warning on line 32 in packages/botonic-plugin-flow-builder/src/webview/contents-context.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/contents-context.ts#L30-L32

Added lines #L30 - L32 were not covered by tests
setCurrentLocale: () => undefined,
contents: {} as Record<keyof T, string>,
})

0 comments on commit 7a23d15

Please sign in to comment.