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

gatsby-theme-comments

v0.1.0

Published

Provides drop-in comment functionality for static Gatsby site, powered by Firebase

Downloads

5

Readme

gatsby-theme-comments

Provides drop-in comment functionality for static Gatsby site, powered by Firebase

The problem

You want to drive engagement for your Gatsby blog via comments.

This solution

This is a Gatsby theme that let you add comment section to your blog.

This differs from other solutions in that it gives you complete control of your data and UI. You store your data on your own database, and you can modify the design as you see fit.

Table of Contents

Installation

npm install gatsby-theme-comments gatsby-plugin-firebase
npm install -D dotenv

Usage

1. Register plugins

In gatsby-config.js:

require("dotenv").config()

module.exports = {
  plugins: [
    ...otherPlugins,

    "gatsby-plugin-firebase",
    "gatsby-theme-comments",
  ],
}

2. Set up Firestore and environment variables

  • Create a Firebase project
  • Create a new Firestore database
  • Inside the Database page, open the Indexes tab and add a composite index like so:

Firestore index instruction

  • Create a .env in your root directory:
GATSBY_FIREBASE_API_KEY=<YOUR_FIREBASE_API_KEY>
GATSBY_FIREBASE_AUTH_DOMAIN=<YOUR_FIREBASE_AUTH_DOMAIN>
GATSBY_FIREBASE_DATABASE_URL=<YOUR_FIREBASE_DATABASE_URL>
GATSBY_FIREBASE_PROJECT_ID=<YOUR_FIREBASE_PROJECT_ID>
GATSBY_FIREBASE_STORAGE_BUCKET=<YOUR_FIREBASE_STORAGE_BUCKET>
GATSBY_FIREBASE_MESSAGING_SENDER_ID=<YOUR_FIREBASE_MESSAGING_SENDER_ID>
GATSBY_FIREBASE_APP_ID=<YOUR_FIREBASE_APP_ID>

3. Use in React

In your post template (src/templates/post.js), you can use CommentSection in your JSX:

import { CommentSection } from "gatsby-theme-comments"

...

return (
  <Layout>
    <Article />
    <Author />

    <CommentSection id={slug} />
  </Layout>
)

The id prop must be a unique string or number that identifies the post. It can be the post's id, slug, or title, but do be aware that you'd lose track of the comments if you change that id.

For example, let's say that you use slug as the identifier. If you want to change the post's slug, remember to go into your Firebase database and change that value too.

Showcase

UI child themes of gatsby-theme-comments

If you wrote a child theme for gatsby-theme-comments, please submit a PR to add your theme to the list.

Sites that uses gatsby-theme-comments

Nobody is using this plugin. Sad face 😢

If you use gatsby-theme-comments, please submit a PR to add your site to the list.

Feel free to reach out or open an issue if you're interested in using this. I'm available to answer any questions or take feature requests.

APIs

Comment

The Comment interface is an object that looks like this:

interface Comment {
  id: string;
  content: string;
  name: string;
  createdAt: Timestamp;
  updatedAt: Timestamp;
}

interface Timestamp {
  nanoseconds: number;
  milliseconds: number;
  toDate: function;
}

Exports

CommentSection

A component that renders a form for users to add comment as well as all the comments of the post. This is the simplest way to use gatsby-theme-comments. You can use this component in your Post template.

id

string | required

A unique identifier used to identify each post. It cannot contain special characters such as "/".

CommentCount

A component that renders the comment count of the post

id

string | required

A unique identifier used to identify each post

useComments

function(id: string): { loading: boolean, error: Error, comments: Array<Comment> }

This hook takes the identifier as argument and gives you the corresponding array of comments.

If the identifier is invalid, the comment array is empty.

function Comments({ id }) {
  const { loading, comments } = useComments()

  if (loading) {
    return <p>Loading...</p>
  }

  return (
    <ul>
      {comments.map(comment) => (
        <li key={comment.id}>{comment.content}</li>
      )}
    </ul>
  )
}

useCommentCount

function(id: string): { loading: boolean, error: Error, commentCount: number }

This hook takes the identifier as argument and gives you the corresponding number of comments.

If the identifier is invalid, the count is 0. The reason for this is that the plugin only keeps track of posts with at least 1 comment. Therefore, if a post doesn't have any, its id would not be available in database.

function CommentCount({ id }) {
  const { loading, commentCount } = useCommentCount()

  if (loading) {
    return <p>0 comment</p>
  }

  return <p>{commentCount} comment{commentCount > 1 ? "s" : ""}</p>
}

useAddComment

function(): { loading: boolean, error: Error, addComment: function(comment: Comment): void }

This hook returns a function for you to add comment to database.

function AddComment({ comment }) {
  const { addComment } = useAddComment()

  return (
    <button onClick={() => addComment(comment)}>Add</button>
  )
}

Note: The Comment you pass to the addComment function is one without Timestamp. The createdAt field will be included whenever the plugin makes a call to Firebase. You don't have to construct that value.

Shadowable Components

Here is a list of recommended components that you can shadow:

  • components/Button.js
  • components/Comment.js
  • components/CommentCount.js
  • components/Comments.js
  • components/Form.js
  • components/Loading.js
  • components/TextField.js

License

MIT