Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin-flow-builder: WebviewContentsContext and useWebviewContents #2810

Conversation

Iru89
Copy link
Contributor

@Iru89 Iru89 commented Apr 10, 2024

Description

@botonic/core
Update interface Session to add organization_id

@botonic/react
Create WebviewRequestContext to properly type what is received in the request context of a webview, the functions: getString and closeWebview and the objects session and params.
Also add the closeWebview function in the browser API, now we will be able to do Botonic.closeWebview()

@botonic/plugin-flow-builder
Change BotonicPluginFlowBuilderOptions remove flowUrl and add apiUrl and jsonVersion as optionals params. If this params are undefined the plugin build flowUrl using PROD URL and latest version.
Create a hook useWebviewContents and WebviewContentsContext

Approach taken / Explain the design

@botonic/plugin-flow-builder
The call to fetch the contents should only be made once at the start of the webview.
This hook gets the contents of the webview and returns isLoading, errorand the webviewContentsContext
With the webviewContentsContext the WebviewContentsContext has to be created.
This has the functions getTextContent and getImageSrc to use in the renders of each component.

To document / Usage example

This is just an example to see how to use the hook and context inside a webview, webviews is a folder inside the bot code.

webviews/flow-builder-webview-example/index.tsx

import {
  FlowBuilderJSONVersion,
  useWebviewContents,
  WebviewContentsContext,
} from '@botonic/plugin-flow-builder'
import { WebviewRequestContext } from '@botonic/react'
import React, { useContext, useState } from 'react'

const FLOW_BUILDER_API_URL = 'https://api.dev.flowbuilder.dev.hubtype.com'
const WEBVIEW_ID = 'b4d9a3bd-e19c-4557-8010-93c70d451d93'

export const FlowBuilderWebviewExample = () => {
  const webviewRequestContext = useContext(WebviewRequestContext)

  const { isLoading, error, webviewContentsContext } = useWebviewContents({
    apiUrl: FLOW_BUILDER_API_URL,
    version: FlowBuilderJSONVersion.LATEST,
    orgId: webviewRequestContext.session.organization_id,
    botId: webviewRequestContext.session.bot.id,
    webviewId: WEBVIEW_ID,
    locale: 'es',
  })

  const [stepNum, setStepNum] = useState(0)
  const steps = [<Step1 />, <Step2 />]

  const handleCloseWebview = () => {
    webviewRequestContext.closeWebview()
  }

  if (error) {
    return <div>Error</div>
  }

  return (
    <WebviewContentsContext.Provider value={webviewContentsContext}>
      <div>
        <div>
          <h1>FlowBuilderWebview</h1>
          <button onClick={handleCloseWebview}>Close</button>
        </div>
        {isLoading ? (
          <div>Loading...</div>
        ) : (
          <div>
            {steps[stepNum]}
            <div>
              <button onClick={() => setStepNum(0)}>Step0</button>
              <button onClick={() => setStepNum(1)}>step1</button>
            </div>
          </div>
        )}
      </div>
    </WebviewContentsContext.Provider>
  )
}

webviews/flow-builder-webview-example/step1.tsx

export const Step1 = () => {
  const webviewContentsContext = useContext(WebviewContentsContext)

  return (
    <div>
      <h1>Step1</h1>

      <div>
        <p>{webviewContentsContext.getTextContent('HEADER_WEBVIEW')}</p>
        <img
          style={{ width: '350px' }}
          src={webviewContentsContext.getImageSrc('image')}
        />
      </div>
    </div>
  )
}

webviews/flow-builder-webview-example/step2.tsx

export const Step2 = () => {
  const webviewContentsContext = useContext(WebviewContentsContext)

  return (
    <div>
      <h1>Step2</h1>

      <div>
        <p>{webviewContentsContext.getTextContent('TEXT_INTRO')}</p>
        <img
          style={{ width: '350px' }}
          src={webviewContentsContext.getImageSrc('IMAGE_2')}
        />
      </div>
    </div>
  )
}

Tests

Add organization and organization_id in @botonic/react helpers to have green tests

Copy link

linear bot commented Apr 10, 2024

@Iru89 Iru89 requested a review from AinaVendrell April 15, 2024 08:25
Copy link
Contributor

@asastre asastre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some doubts before approving

@Iru89 Iru89 force-pushed the feature/blt-674-task-plugin-flow-builder-new-endpoint-for-webview-contents branch from fcdc782 to 185ecb6 Compare April 15, 2024 15:02
@Iru89 Iru89 changed the title plugin-flow-builder: create a hook to call webview contents and… plugin-flow-builder: WebviewContentsContext and useWebviewContents Apr 16, 2024
@Iru89 Iru89 merged commit c5f3601 into master-lts Apr 17, 2024
1 check passed
@Iru89 Iru89 deleted the feature/blt-674-task-plugin-flow-builder-new-endpoint-for-webview-contents branch April 17, 2024 09:37
Iru89 added a commit that referenced this pull request Apr 19, 2024
…2810)

## Description

**@botonic/core**
Update interface Session to add `organization_id`

**@botonic/react**
Create `WebviewRequestContext` to properly type what is received in the
request context of a webview, the functions: `getString` and
`closeWebview` and the objects `session` and `params`.
Also add the `closeWebview` function in the browser API, now we will be
able to do `Botonic.closeWebview()`

**@botonic/plugin-flow-builder**
Change `BotonicPluginFlowBuilderOptions` remove `flowUrl` and add
`apiUrl` and `jsonVersion` as optionals params. If this params are
undefined the plugin build flowUrl using PROD URL and latest version.
Create a hook `useWebviewContents` and `WebviewContentsContext`

## Approach taken / Explain the design

**@botonic/plugin-flow-builder**
The call to fetch the contents should only be made once at the start of
the webview.
This hook gets the contents of the webview and returns `isLoading`,
`error`and the `webviewContentsContext`
With the `webviewContentsContext` the `WebviewContentsContext` has to be
created.
This has the functions `getTextContent` and `getImageSrc` to use in the
renders of each component.

## Tests

Add organization and organization_id in @botonic/react helpers to have
green tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants