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

@mapc/airtable-cms

v1.3.0

Published

Custom React Hook for using Airtable as a CMS in MAPC web apps

Downloads

308

Readme

airtable-cms

Overview

This is a custom React Hook for using Airtable as a Content Management System (CMS), made specifically for web applications at MAPC.

Installation

Add as a dependency: yarn add -D @mapc/airtable-cms

Requires react >= 18 as a peer dependency: yarn add 'react@>=18'

Usage

The useAirtableCMS hook is the primary export of this module. It takes a number of arguments:

Arguments

Required

  • baseID - The ID of the Airtable Base you're using as a CMS
  • tableName - The name of the table you want to fetch records from
  • fieldMapping - An object that maps Field names to the variable names used in your app. It's also used to limit which fields are fetched from the API when requesting data
  • keyField - The primary field of the table you're fetching data from (note: this should be the mapped variable name specified in fieldMapping, not the Field name as it appears in Airtable)

Optional

  • viewName - A specific view you want to use. Default: "Grid view" (which is the default view created for each table in an Airtable Base)
  • asList - By default, this hook returns an array of objects, but setting this to false will return an object (keyed by keyField). Default: true
  • sortBy - If asList is true, you can also pass in a custom comparator function that will be used to sort the array that is returned. Default: null

Return value

The hook returns an object with two attributes: data and metadata. data will be either a list or an object (depending on the value of the asList argument). metadata is an object with three attributes:

  • loading - A boolean value indicating whether or not the hook is currently fetching data from the Airtable API
  • done - A boolean value indicating whether or not the hook has finished fetching data from the Airtable API
  • error - The error returned by the Airtable API, or null

Together, the data and metadata objects can be used in a variety of ways to handle different states, for example:

  • data.length === 0 && metadata.loading: We have no cached values and we're loading data from the API (i.e. the initial state)
  • data.length > 0 && metadata.loading: We have cached values that can be displayed, but we're checking for updates from the API
  • data.length === 0 && metadata.done: We have no cached values to display, and the API didn't return data either (i.e. the empty state)
  • data.length === 0 && metadata.error: We have no cached values to display, and calling the API was unsuccessful
  • data.length > 0 && metadata.error: We have cached values we can display, but calling the API for updates was unsuccessful

Furthermore, you can use the metadata.error object to potentially retry requests (e.g. if the metadata.error.code is 429, you've hit a rate limit, and you can wait a bit before trying again, or 502/503 might indicate a temporary service outage that could resolve itself after some time).

Caching

Records fetched from Airtable are saved in localStorage, in order to reduce the amount of data loaded over the network, and to make already-loaded data available for immediate rendering. The hook will still make a request to the Airtable API to check for updates, filtering records to just those that have been updated since the last time we wrote to the local cache.

Example

import Spinner from "react-bootstrap/Spinner";
import { useAirtableCMS } from "@mapc/airtable-cms";

const MyComponent = () => {
  const {
    data,
    metadata: { loading },
  } = useAirtableCMS({
    baseID: "app1234xyz567",
    tableName: "Table 1",
    keyField: "name",
    fieldMapping: {
      name: "Name",
      description: "Full Description",
    },
    sortBy: (a, b) => a.name.localeCompare(b.name),
  });

  let sectionContent;
  if (loading && data.length === 0) {
    // If this is the first time we're loading content, show the loading indicator
    sectionContent = (
      <Spinner animation="border" role="status">
        <span className="visually-hidden">Loading...</span>
      </Spinner>
    );
  } else {
    sectionContent = data
      .map((record) => (
        <div key={record.name}>
          <h1>{record.name}</h1>
          <p>{record.description}</p>
        </div>
      ));
  }

  return (
    <section>
      {sectionContent}
    </section>
  );
};

export default MyComponent;