Skip to content

Commit

Permalink
refactor(plugin-flow-builder): pass a generic T to type contents
Browse files Browse the repository at this point in the history
  • Loading branch information
Iru89 committed Jul 2, 2024
1 parent 35a4be0 commit 85e2146
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { createContext } from 'react'

import { WebviewContentsContextType } from './types'

export const WebviewContentsContext = createContext<WebviewContentsContextType>(
{
getTextContent: () => '',
getImageSrc: () => '',
setCurrentLocale: () => undefined,
contents: {},
}
)
export const WebviewContentsContext = createContext<
WebviewContentsContextType<unknown>
>({
getTextContent: () => '',
getImageSrc: () => '',
setCurrentLocale: () => undefined,
contents: {},
})
12 changes: 6 additions & 6 deletions packages/botonic-plugin-flow-builder/src/webview/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ export interface WebviewImageContent {
}
}

export interface UseWebviewContentsProps {
export interface UseWebviewContentsProps<T> {
apiUrl?: string
version?: FlowBuilderJSONVersion
orgId: string
botId: string
webviewId: string
locale: string
mapContents: Record<string, string>
mapContents: Record<keyof T, string>
}

export interface UseWebviewContents {
export interface UseWebviewContents<T> {
isLoading: boolean
error: boolean
webviewContentsContext: WebviewContentsContextType
webviewContentsContext: WebviewContentsContextType<T>
}

export interface WebviewContentsContextType {
export interface WebviewContentsContextType<T> {
getTextContent: (code: string) => string
getImageSrc: (code: string) => string
setCurrentLocale: (locale: string) => void
contents: Record<string, string>
contents: Record<keyof T, string>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ import {
WebviewTextContent,
} from './types'

export function useWebviewContents({
export function useWebviewContents<T>({
apiUrl = FLOW_BUILDER_API_URL_PROD,
version = FlowBuilderJSONVersion.LATEST,
orgId,
botId,
webviewId,
locale,
mapContents,
}: UseWebviewContentsProps): UseWebviewContents {
}: UseWebviewContentsProps<T>): UseWebviewContents<T> {
const [textContents, setTextContents] = useState<WebviewTextContent[]>()
const [imageContents, setImageContents] = useState<WebviewImageContent[]>()
const [contents, setContents] = useState({})
const [contents, setContents] = useState<Record<keyof T, string>>(
{} as Record<keyof T, string>
)
const [currentLocale, setCurrentLocale] = useState(locale)
const [isLoading, setLoading] = useState(true)
const [error, setError] = useState(false)
Expand All @@ -47,9 +49,9 @@ export function useWebviewContents({
}

const createContentsObject = () => {
const contentsObject = {}
for (const [key, value] of Object.entries(mapContents)) {
contentsObject[key] = getTextContent(value)
const contentsObject = {} as Record<keyof T, string>
for (const [key, value] of Object.entries<string>(mapContents)) {
contentsObject[key] = getTextContent(value) || getImageSrc(value)
}
setContents(contentsObject)
}
Expand Down

0 comments on commit 85e2146

Please sign in to comment.