npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aha-app/aha-develop-react

v1.4.3

Published

Common React patterns for Aha! develop

Downloads

34

Readme

React helper utilities for Aha! develop extensions

Install:

npm install @aha-develop/aha-develop-react

or

yarn add @aha-develop/aha-develop-react

Hooks

useAuth

Run a callback with authentication to an external service and return the data and/or the auth and loading state.

import { useAuth } from "@aha-develop/aha-develop-react";

function LoadPullRequests(props) {
  const { loading, authed, data, fetchData } = useAuth(async (authData) => {
    const api = octokit.defaults({
      headers: { authorization: `token ${authData.token}` },
    });

    return await api(FETCH_PULL_REQUESTS_QUERY);
  });

  if (loading) {
    return <aha-spinner />;
  }

  if (!authed) {
    return <button onClick={fetchData}>Load pull requests</button>;
  }

  const pullRequests = data.map((pr, idx) => <PullRequest key={idx} pr={pr} />);

  return <ul>{pullRequests}</ul>;
}

In this example component the callback fetches the data from github. The callback will only be run if the user is authenticated with github. When the component loads it will check the authentication status. If they are not authenticated then authed will be false and the component can display a button to prompt the user to authenticate.

If loading is true then the user is authenticated and the data is being requested.

The component must be wrapped in an AuthProvider. This would usually be done at the root node:

import { AuthProvider } from "@aha-develop/aha-develop-react";

aha.on("pullRequests", () => {
  return (
    <AuthProvider serviceName="github" serviceParameters={{ scope: "repo" }}>
      <App />
    </AuthProvider>
  );
});

Multiple services

It's possible to use multiple services in one extension with useAuth:

<AuthProvider serviceName="github">
  <AuthProvider serviceName="gitlab">
    <App />
  </AuthProvider>
</AuthProvider>
const { data, loading, authed, fetchData } = useAuth(fetchGitlabPrs, {
  serviceName: "gitlab",
});
const { data, loading, authed, fetchData } = useAuth(fetchGithubPrs, {
  serviceName: "github",
});

If no serviceName is provided then the first registered AuthProvider is used.

useOutsideAlerter

If there is a modal or popup component controlled by the extension then capturing mouse clicks outside the area of the popup can be tricky due to the shadowRoot container. This hook will set up a container aware event handler:

function Popup({ onClose }) {
  const popupEl = useRef(null);
  useOutsideAlerter(popupEl, () => onClose());

  return (
    <div ref={popupEl}>
      I'm a popup
      <button onClick={onClose}>Close</button>
    </div>
  );
}

The event defaults to "mousedown" and can be customised:

useOutsideAlerter(popupEl, onClose, { event: "mousemove" });

useClipboard

This is a simple clipboard helper for React components to copy text to the clipboard and show an indicator for a short time:

function CopyableId({ id }) {
  const [onCopy, copied] = useClipboard();

  return <div>
    <span>{id}</span>
    <button onClick={() => onCopy(id)}>
      <i className="fa-regular fa-copy"></i>
      {copied ? "Copied" : "Copy"}
    </button>
  </div>
}

Components

EmbeddedContent

Embeds content via an iframe. Constrains element size based on aspect ratio. Sanitizes user input.

<EmbeddedContent
  src="https://my.site.xzy/design.png"
  aspectRatio={1.333}
/>

Patterns

EmbeddedContentAttribute

Fully-featured component for managing embedded content associated with an Aha! Record. Collects a URL as user input, stores it as extension data on the record, and displays the URL at a fixed aspect ratio when set.

import React from "react";
import { EmbeddedContentAttribute } from "@aha-develop/aha-develop-react";

aha.on("myServiceAttribute", ({ record, fields }, { identifier }) => {
  const ensureEmbedFlags = async (url) => {
    if (url.includes('emb=1')) {
      return url;
    } else {
      return `${url}?emb=1&ios=false&frameless=false`;
    }
  };

  return (
    <EmbeddedContentAttribute
      identifier={identifier}
      record={record}
      fields={fields}
      product="MyService"
      placeholder="https://my.site.xyz/embed/..."
      onLinkUpdated={ensureEmbedFlags}
    />
  );
});