Skip to content

Instantly share code, notes, and snippets.

@robbeman
Created April 21, 2020 18:47
Show Gist options
  • Save robbeman/9617a4ab406d7eb98d94823296deed21 to your computer and use it in GitHub Desktop.
Save robbeman/9617a4ab406d7eb98d94823296deed21 to your computer and use it in GitHub Desktop.
Headway react component
/* global Headway */
import React, { memo } from 'react';
const HW_CONFIG_ACCOUNT = 'your-id-here'; // get this from env-variables
const ELEMENT_ID = 'headway-updates-widget';
function HeadwayWidget() {
// No need to render if no account is configured
if (!HW_CONFIG_ACCOUNT) {
return null;
}
// Use a function ref to determine if the DOM is ready for init
const initWidget = element => {
if (element) {
Headway.init({
selector: `#${ELEMENT_ID}`,
account: HW_CONFIG_ACCOUNT,
});
}
};
return <div id={ELEMENT_ID} ref={initWidget} />;
}
// prevent too much re-rendering with memo
export default memo(HeadwayWidget);
<!-- Put this somewhere in your document to load the widget script, make sure it is loade before react kicks in. -->
<script src="//cdn.headwayapp.co/widget.js" />
@josh-vouched
Copy link

This is great! When you were building this, did you ever have trouble referencing the Headway.init() call? I keep getting this error:

TypeScript error in /opt/app/src/components/account/sidebar/headway-widget.tsx(15,7):
Cannot find name 'Headway'.  TS2304

    13 |   const initWidget = element => {
    14 |     if (element) {
  > 15 |       Headway.init({
       |       ^
    16 |         selector: `#${ELEMENT_ID}`,
    17 |         account: HW_CONFIG_ACCOUNT,
    18 |       });```

@jirsbek
Copy link

jirsbek commented Jan 11, 2022

I'm having the same problem. 'Headway' is not defined.

@josh-vouched
Copy link

@jirsbek are you using Typescript? I figured out that's what my problem was. I added //@ts-ignore to ignore it and it worked fine after that:

//@ts-ignore
Headway.init(config);

@jirsbek
Copy link

jirsbek commented Jan 11, 2022

@josh-vouched Nope, but at the beginning of the component there is /* global Headway */. Anyway I've solved this by using official component https://github.com/headwayappco/widget/blob/master/packages/react-widget/src/HeadwayWidget.js

@robbeman
Copy link
Author

At the time I made this gist there was no official react widget, so it's probably best y'all follow @jirsbek's suggestion and use the official widget. Sorry for not replying to previous comments.

https://www.npmjs.com/package/@headwayapp/react-widget

@stashit-app
Copy link

stashit-app commented Jul 21, 2023

Just a head up for anyone looking for this. The official react widget hasn't been updated in a while and only works with react 16. @robbeman I haven't tried it yet but your solution still seems to be the viable one. I'll try and implement this.

@paulkulesha
Copy link

import React, { useEffect } from 'react';

const HeadwayWidget = () => {
  useEffect(() => {
    let script;
    const config = {
      account: process.env.HEADWAY_APP_ID,
      selector: '#headway_badge',
    };

    if (document) {
      script = document.createElement('script');
      script.async = true;
      script.src = 'https://cdn.headwayapp.co/widget.js';
      document.head.appendChild(script);

      script.onload = function () {
        window.Headway.init(config);
      };
    }
  }, []);

  return <div id="headway_badge"></div>;
};

export default HeadwayWidget;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment