使用 Looker 组件构建标签页式信息中心

以下是一个简单的示例,展示了界面组件如何增强嵌入式应用:使用界面组件为信息中心创建标签式导航:

包含标签页式界面的信息中心示例。

以下示例向使用 Looker 的扩展框架构建的基本 TypeScript 扩展程序添加了标签页式信息中心。

如需完成此示例,请确保您的设置满足相关要求,然后按以下步骤操作:

  1. 构建基本的 TypeScript 扩展程序。
  2. 创建文件 src/Dashboards.tsx 以连接和存储信息中心
  3. 创建文件 src/Tabs.tsx 以存储 Tabs 组件
  4. 替换 src/App.tsx 中的 HelloWorld 引用
  5. 使用嵌入式授权更新 manifest.lkml 文件
  6. 将扩展程序发布到 Looker 实例

要求

在开始之前,您需要准备好以下几项内容:

第 1 步:构建基本的 TypeScript 扩展程序

如需构建扩展程序,请按照构建 Looker 扩展程序简介文档页面上的说明操作。如需将扩展程序设为 TypeScript 扩展程序,请进行以下修改:

  • 如果您使用 create-looker-extension 工具创建扩展程序模板,请选择 React 作为框架,并选择 TypeScript 作为语言。
  • 如果您克隆 Git 代码库来创建扩展程序模板,请在第二步中前往 extension-examples/react/TypeScript/helloworld-ts 目录。

第 2 步:创建文件 src/Dashboards.tsx 以连接和存储信息中心

在新扩展程序的 src 目录中,创建一个 Dashboards.tsx 文件。此文件将连接并存储您在 Looker 中构建的信息中心。

在文件中,粘贴以下代码。此代码会创建一个可重用的信息中心组件。

实例网址的代码中有一个位置,即 https://mycompany.looker.com。请将此网址更改为您的 Looker 实例网址。

import React, { useCallback } from "react";
import { LookerEmbedSDK } from "@looker/embed-sdk";
import styled from "styled-components";

export const EmbeddedDashboard = (props: { id: number | string }) => {
 const [dashboard, setDashboard] = React.useState();
 const setupDashboard = (dashboard: any) => {
   setDashboard(dashboard);
 };
 const embedCtrRef = useCallback((el) => {
   const hostUrl = "https://mycompany.looker.com";
   if (el && hostUrl) {
     el.innerHTML = "";
     LookerEmbedSDK.init(hostUrl);
     LookerEmbedSDK.createDashboardWithId(props.id)
       .withNext()
       .appendTo(el)
       .build()
       .connect()
       .then(setupDashboard)
       .catch((error) => {
         console.error("Connection error", error);
       });
   }
 }, []);
 return <EmbedContainer ref={embedCtrRef}></EmbedContainer>;
};

export const EmbedContainer = styled.div`
 width: 100%;
 height: 95vh;
 & > iframe {
   width: 100%;
   height: 100%;
 }
`;

在前面的代码示例中,发生了以下情况:

  • import 语句用于引入所需的依赖项。

    import React, { useCallback } from "react";
    import { LookerEmbedSDK } from "@looker/embed-sdk";
    import styled from "styled-components";
    
  • 下一个代码块会创建一个 EmbeddedDashboard 对象,该对象是一个包含信息中心 iframe 的 EmbedContainer 对象。此组件可重复使用;您可以通过传递不同的 id 属性将其用于任何信息中心。iframe 是使用传递给它的信息中心 ID 通过 Looker Embed SDK 生成的。请务必将 https://mycompany.looker.com/ 更新为您的 Looker 实例网址。

    export const EmbeddedDashboard = (props: { id: number | string }) => {
    const [dashboard, setDashboard] = React.useState();
    const setupDashboard = (dashboard: any) => {
      setDashboard(dashboard);
    };
    const embedCtrRef = useCallback((el) => {
      const hostUrl = "https://mycompany.looker.com/";
      if (el && hostUrl) {
        el.innerHTML = "";
        LookerEmbedSDK.init(hostUrl);
        LookerEmbedSDK.createDashboardWithId(props.id)
          .withNext()
          .appendTo(el)
          .build()
          .connect()
          .then(setupDashboard)
          .catch((error) => {
            console.error("Connection error", error);
          });
      }
    }, []);
    return <EmbedContainer ref={embedCtrRef}></EmbedContainer>;
    };
    
  • 最后一个块用于设置 EmbedContainer 的样式。

      export const EmbedContainer = styled.div`
        width: 100%;
        height: 95vh;
        & > iframe {
          width: 100%;
          height: 100%;
        }
    `;
    

第 3 步:创建文件 src/Tabs.tsx 以存储 Tabs 组件

在新扩展程序的 src 目录中,创建一个 Tabs.tsx 文件。此文件存储 Tabs 组件,并引用每个信息中心的 Looker 信息中心 ID。

在该文件中,粘贴以下代码(下一部分将说明代码的作用):

import React from "react";
import { ComponentsProvider, Tabs2, Tab2 } from "@looker/components";
import { EmbeddedDashboard } from "./Dashboards";

export const Tabs = () => (
 <ComponentsProvider>
   <Tabs2>
     <Tab2 id="5" label="Order Analysis Dashboard">
      Order data from the last 12 months
       <EmbeddedDashboard1 id={5} />
     </Tab2>
     <Tab2 id="2" label="Inventory Dashboard">
       Current global inventory
       <EmbeddedDashboard2 id={2} />
     </Tab2>
     <Tab2 id="7" label="Customer Dashboard">
       Anonymized customer data
       <EmbeddedDashboard3 id={7} />
     </Tab2>
   </Tabs2>
 </ComponentsProvider>
)

在前面的代码示例中,发生了以下情况:

  • 导入语句用于引入所需的依赖项和组件,以及在 Dashboards.tsx 文件中创建的 EmbeddedDashboard 对象。

    import React from "react";
    import { ComponentsProvider, Tabs2, Tab2 } from "@looker/components";
    import { EmbeddedDashboard } from "./Dashboard";
    
  • 导出语句使 Tabs 对象可供导入到其他组件中。

    export const Tabs = () => (
    
  • ComponentsProvider 用于封装各个组件,以帮助实现主题设置。

    <ComponentsProvider>
    </ComponentsProvider>
    
  • Tabs2 组件及其子组件 Tab2 会创建三个标签页,并将它们与 Looker 信息中心相关联。

     <Tabs2>
     <Tab2 id="5" label="Order Analysis Dashboard">
      Order data from the last 12 months
       <EmbeddedDashboard id={5} />
     </Tab2>
     <Tab2 id="2" label="Inventory Dashboard">
       Current global inventory
       <EmbeddedDashboard id={2} />
     </Tab2>
     <Tab2 id="7" label="Customer Dashboard">
       Anonymized customer data
       <EmbeddedDashboard id={7} />
     </Tab2>
    </Tabs2>
    
    • Tab2id 属性接受唯一标签页 ID。根据需要更新 ID 以适应您的环境。
    • label 属性接受将显示在每个标签页上的标签。根据您使用的信息中心相应地更新 ID。
    • 放置在 Tab2 标记内的字符串将显示在该标签页的内容区域顶部。根据需要更新或移除该字符串。
    • EmbeddedDashboard 对象放置在相应标签页中。其 id 属性接受要嵌入相应标签页中的信息中心的 ID。在构建自己的标签页式信息中心时,请将此值替换为您要使用的信息中心的 ID。您可以在网址中 dashboards/ 之后找到数字信息中心 ID。例如,如果网址为 https://example.looker.com/dashboards/61?Recording+Date=10+weeks&Country=US,则信息中心 ID 为 61

第 4 步:替换 src/App.tsx 中的 HelloWorld 引用

前往 src 目录中的 App.tsx 文件。移除 HelloWorld import 语句:

import { HelloWorld } from './HelloWorld'

并将其替换为:

import { Tabs } from './Tabs'

此外,在 src/App.tsx 文件中,将 <HelloWorld/> 替换为 <Tabs/>

(可选)您还可以删除此目录中的 HelloWorld.tsx 文件,因为您将不再使用它。

第 5 步:使用嵌入授权更新 manifest.lkml 文件

将以下 entitlement 添加到 LookML 项目中 manifest.lkml 文件的 entitlements 部分:

use_embeds: yes

manifest.lkml 文件应如下所示:

application: name {
  label: "label"
  url: "http://localhost:8080/bundle.js"
  # file: "bundle.js
  entitlements: {
    core_api_methods: ["me"] #Add more entitlements here as you develop new functionality
    use_embeds: yes
  }
}

现在,您可以前往扩展程序,该扩展程序会显示在左侧导航面板的 Applications 文件夹中。如果您已使用 yarn develop 启动本地开发服务器,则可以查看嵌入式标签页式信息中心。

第 6 步:将扩展程序发布到 Looker 实例

如需向其他 Looker 用户显示扩展程序,请按照以下步骤将扩展程序发布到您的 Looker 实例:

  1. 在开发服务器运行的情况下,前往 localhost:8080/bundle.js
  2. 将浏览器窗口的内容以 .js 文件的形式保存到本地计算机上。
  3. 确保您处于开发模式,然后将 .js 文件拖放到扩展程序项目中。保存更改。
  4. manifest.lkml 文件中,注释掉 url: "http://localhost:8080/bundle.js" 行。
  5. manifest.lkml 文件中,取消注释 # file: "bundle.js" 行,并确保文件名与您上传到项目的 .js 文件的文件名一致。保存更改。
  6. 提交并部署您的更改。

部署更改后,您将不再需要启动本地开发服务器即可查看扩展程序,并且 Looker 实例中的用户应该能够通过在主导航面板的 Applications 文件夹中找到该扩展程序。