<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-PP7S83N" height="0" width="0" style="display:none;visibility:hidden">
Guide

Create a Superblocks app with Weavy components

This guide will help you create a Superblocks example app and add Weavy components to it, quickly get a proof-of-concept up and running, and understand what Weavy can do for your Superblocks apps.

1. Weavy API Key

We need an API key to communicate with the Weavy backend.
 

2. Create REST API Integration

We need to create a bridge between Weavy and Superblocks.

  • Open the integrations page in Superblocks.
  • Add REST API
  • Name the integration WeavyAPI
  • Use WY_BACKEND_URL as Base URL
  • In Authentication, select Bearer Token.
  • Copy the masked API key below as the Token and click Create
Weavy API Key
WY_API_*****************

3. Create a new app

Create a new application from scratch and add a table component. This will render a table with sample data already in place.
superblocks-table

4. Add Backend API

We need to connect the app with the Rest API we created to get an access token from Weavy for the signed-in Superblocks user, this is going to be used later for our custom component.

For this, we need to Add Backend API

  • Click Add Backend API and selectJavaScript Function and name the step GetUser
    • Copy and paste the snippet below into the code editor;
      return Global.user;
  • Next, add WeavyAPI
    • Name the step UpsertWeavyUser
    • Change to PUT and paste /api/users/{{GetUser.output.id}} in the URL field
    • In the JSON Body copy and paste below;
      {
        "email" : "{{ GetUser.output.email }}",
        "name" : "{{ GetUser.output.name }}"
      }
  • Next, add WeavyAPI
    • Name the step GetWeavyAccessToken
    • Change to POST and paste /api/users/{{GetUser.output.id}}/tokens in the URL field
  • Next, add a JavaScript Function and name the step Return
    • Copy and paste the snippet below into the code editor;
      return {
        access_token: GetWeavyAccessToken.output.access_token,
        url: "WY_BACKEND_URL"
      };
  • Lastly, name the Backend API GetWeavyToken

Run the API. If successful, you should get a response containing the current user's access token and URL to the Weavy backend in JSON format on the Return step.

Superblocks-Backend-APIv2

5. Initialize Superblocks

Create a new local directory for your Superblocks resources & navigate into that directory.

Next is to initialize Superblocks, and for that we need the URL for your Superblocks app.

Run this in your terminal window
superblocks init APP_URL -m latest-edits
After the script finishes, navigate to the folder of your app - apps/[MY_APPLICATION_DIRECTORY]

6. Create component

With the app directory set up, let’s create the component. 

Run in your terminal window
superblocks components create

For display name set Weavy Feeds and machine-readable name set weavy_feeds

7. Install Weavy UIKit

Weavy has a dedicated UIKit for React, which includes regular React components and additional React hooks for easy configuration and usage.

Run in your terminal window
npm install @weavy/uikit-react

8. Edit config.ts

Open the config.ts file in the components/weavy folder. Replace properties and events (should be empty) with the code snippet below.

config.ts
properties: [
    {
      path: "weavy_url",
      dataType: "string",
      propertiesPanelDisplay: {
        label: "WeavyUrl",
        controlType: "js-expr",
        defaultValue: "GetWeavyToken.response.url",
      },
      isExternallyReadable: true,
      isExternallySettable: true,
    },
    {
      path: "token",
      dataType: "string",
      propertiesPanelDisplay: {
        label: "Token",
        controlType: "js-expr",
        defaultValue: "GetWeavyToken.response.access_token",
      },
      isExternallyReadable: true,
      isExternallySettable: true,
    },
    {
      path: "uid",
      dataType: "string",
      propertiesPanelDisplay: {
        label: "ContextId",
        controlType: "text",
      },
      isExternallyReadable: true,
      isExternallySettable: true,
    },
  ],
  events: [
    {
      label: "On Token Expired",
      path: "onTokenExpired",
    },
  ],

9. Edit component.tsx

Open the component.tsx file in  the components/weavy folder. Replace all code with the snippet below.

component.tsx
import { useState } from "react";
import {
  useSuperblocksContext,
  useSuperblocksIsLoading,
} from "@superblocksteam/custom-components";
import { type Props, type EventTriggers } from "./types";
import { useWeavy, WyPosts } from "@weavy/uikit-react";

export default function Component({ weavy_url, token, uid }: Props) {
  const weavyStyle = {
    display: "flex",
    height: "100%",
    width: "100%",
  };

  const weavy = useWeavy({});

  const isLoading = useSuperblocksIsLoading();

  const {
    events: { onTokenExpired },
  } = useSuperblocksContext<Props, EventTriggers>();

  if (weavy && weavy_url && token) {
    weavy.url = weavy_url;
    weavy.tokenFactory = async (refresh: boolean) => {
      if (refresh) {
        console.log("onTokenExpired: " + token);
        onTokenExpired();
      }
      return token;
    };
  }

  return (
    <>
      {isLoading ? (
        <div>Loading...</div>
      ) : uid && token ? (
        <WyPosts style={weavyStyle} uid={uid} />
      ) : (
        <div>Set the UID</div>
      )}
    </>
  );
}

10. Start local development

To deploy and work with the component, you will use Superblocks Local Development Mode.
Run in your terminal window
superblocks components watch
Follow the link in the output, which will also enable local development for your app.

11. Add component to app

Now it's time to add the component to your app;

  • Click Add Components and search for Weavy Feeds, drag it into the app
  • Set ContextId to feeds-{{ Table1.selectedRowIndex }} (this assumes the table has the name Table1 we added in step 3)  

Now, we want to configure an event in case the access token expires;

  • Click the plus sign next to On Token Expired
  • Select Run APIs and select GetWeavyToken
  • Click the plus sign next to onSuccess and select Set component property.
  • Select the Weavy Chat as the component and token as the property.
  • For value set  {{ GetWeavyToken.response.access_token }}

12. Done!

We now have a complete Superblocks app running using Weavy Feeds. Click around in the table and see the Feed get updated enabling contextual collaboration. 

Now, imagine that in your app!

superblocks-app-v2
Ask AI
Support

To access live chat with our developer success team you need a Weavy account.

Sign in or create a Weavy account