Skip to content

Commit

Permalink
feat(plugin-flow-builder): receives mapContents and returns an object…
Browse files Browse the repository at this point in the history
… contents with key: getTextContent(value)
  • Loading branch information
Iru89 committed Jun 27, 2024
1 parent 25f4d45 commit 35a4be0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { WebviewContentsContextType } from './types'

export const WebviewContentsContext = createContext<WebviewContentsContextType>(
{
getTextContent: () => undefined,
getImageSrc: () => undefined,
getTextContent: () => '',
getImageSrc: () => '',

Check warning on line 8 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#L7-L8

Added lines #L7 - L8 were not covered by tests
setCurrentLocale: () => undefined,
contents: {},
}
)
6 changes: 4 additions & 2 deletions packages/botonic-plugin-flow-builder/src/webview/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface UseWebviewContentsProps {
botId: string
webviewId: string
locale: string
mapContents: Record<string, string>
}

export interface UseWebviewContents {
Expand All @@ -41,7 +42,8 @@ export interface UseWebviewContents {
}

export interface WebviewContentsContextType {
getTextContent: (code: string) => string | undefined
getImageSrc: (code: string) => string | undefined
getTextContent: (code: string) => string
getImageSrc: (code: string) => string
setCurrentLocale: (locale: string) => void
contents: Record<string, string>
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,43 @@ export function useWebviewContents({
botId,
webviewId,
locale,
mapContents,
}: UseWebviewContentsProps): UseWebviewContents {
const [textContents, setTextContents] = useState<WebviewTextContent[]>()
const [imageContents, setImageContents] = useState<WebviewImageContent[]>()
const [contents, setContents] = useState({})

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

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/use-webview-contents.ts#L26

Added line #L26 was not covered by tests
const [currentLocale, setCurrentLocale] = useState(locale)
const [isLoading, setLoading] = useState(false)
const [isLoading, setLoading] = useState(true)

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

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/use-webview-contents.ts#L28

Added line #L28 was not covered by tests
const [error, setError] = useState(false)

const getTextContent = (contentID: string): string => {
return (

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

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/use-webview-contents.ts#L31-L32

Added lines #L31 - L32 were not covered by tests
textContents
?.find(textContent => textContent.code === contentID)
?.content.text.find(text => text.locale === currentLocale)?.message ||

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

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/use-webview-contents.ts#L34-L35

Added lines #L34 - L35 were not covered by tests
''
)
}

const getImageSrc = (contentID: string): string => {
return (

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

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/use-webview-contents.ts#L40-L41

Added lines #L40 - L41 were not covered by tests
imageContents
?.find(imageContent => imageContent.code === contentID)
?.content.image.find(image => image.locale === currentLocale)?.file ||

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

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/use-webview-contents.ts#L43-L44

Added lines #L43 - L44 were not covered by tests
''
)
}

const createContentsObject = () => {
const contentsObject = {}
for (const [key, value] of Object.entries(mapContents)) {
contentsObject[key] = getTextContent(value)

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

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/use-webview-contents.ts#L49-L52

Added lines #L49 - L52 were not covered by tests
}
setContents(contentsObject)

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

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/use-webview-contents.ts#L54

Added line #L54 was not covered by tests
}

useEffect(() => {
const getResponseContents = async () => {
setLoading(true)
const url = `${apiUrl}/webview/${version}`
try {
const response = await axios.get<WebviewContentsResponse>(url, {
Expand All @@ -44,6 +71,7 @@ export function useWebviewContents({
webviewContent => webviewContent.type === WebviewContentType.IMAGE
) as WebviewImageContent[]
setImageContents(imageResponseContents)
createContentsObject()

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

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/webview/use-webview-contents.ts#L74

Added line #L74 was not covered by tests
} catch (error) {
console.error('Error fetching webview contents:', error)
setError(true)
Expand All @@ -54,25 +82,14 @@ export function useWebviewContents({
getResponseContents()
}, [])

const getTextContent = (contentID: string): string | undefined => {
return textContents
?.find(textContent => textContent.code === contentID)
?.content.text.find(text => text.locale === currentLocale)?.message
}

const getImageSrc = (contentID: string): string | undefined => {
return imageContents
?.find(imageContent => imageContent.code === contentID)
?.content.image.find(image => image.locale === currentLocale)?.file
}

return {
isLoading,
error,
webviewContentsContext: {
getTextContent,
getImageSrc,
setCurrentLocale,
contents,
},
}
}

0 comments on commit 35a4be0

Please sign in to comment.