• Stars
    star
    171
  • Rank 222,266 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React hook to effortlessly add a comment section to your website, and start the discussion on your content.

npm

useComments

React hook to effortlessly add a comment section to your website, and start the discussion on your content.

Demo: https://use-comments.netlify.app/

What is it?

πŸŽƒ Headless React Hook

useComments just cares about the data. You write your own UI.

πŸ‘Ί Backed with Hasura

We need to keep all of these comments somewhere, right? Hasura, a GraphQL layer on top of a database, handles this for us! Don't worry, you'll get your Hasura up and running in 30 seconds!

πŸš€ Quick and easy

The setup will take less than 5 minutes.

Getting started

Let's add comments to a website in five simple steps!

  1. Deploy Hasura

    Click this πŸ‘‡ to deploy a fresh Hasura instance.

    Deploy Hasura to Heroku

    We need to keep all of these comments somewhere, right? Hasura handles this for us.

    Check out the docs for more details. You can find more options for one-click deployment there.

  2. Set config vars in Heroku

    In order to use our Hasura backend we need to set two enviromental variables in Heroku. HASURA_GRAPHQL_ADMIN_SECRET will make sure that your GraphQL endpoint and the Hasura console are not publicly accessible. HASURA_GRAPHQL_UNAUTHORIZED_ROLE will allow unauthenticated (non-logged in) users add and view comments.

  3. Import database schema and metadata

    In the next step we need to import database schema. Click here and copy paste the content.

    We also need to import Hasura metadata to set all the permissions. Save this file, and import it in Hasura Console:

  4. Install use-comments

    npm install use-comments
    
    yarn add use-comments
    

    or add it from CDN

    <script
      crossorigin
      src="https://unpkg.com/[email protected]/dist/index.umd.js"
    ></script>
    
    <script type="module" async>
       const { useComments } = useComments;
    
       const { comments, error } = useComments(
         'https://use-comments-app.herokuapp.com/v1/graphql',
         'landing-page',
       );
    </script>
  5. Create beautiful UI for your comments

    Start off from one of the examples or write it from scratch.

    1. Theme UI

    2. Tailwind

API Reference

useComments

Fetches comments from Hasura backend specified in hasuraUrl on mount and whenever config.limit or config.offset change.

Parameters

  • hasuraUrl: URL of your Hasura instance. Your comments will be stored there.
  • postId: Comments will be fetched for the post with identifier postId
  • config: Configurable offset and limit for the GraphQL query to Hasura. See UseCommentsConfig

TypeScript Signature

const useComments: (
  hasuraUrl: string,
  postId: string,
  config?: UseCommentsConfig | undefined
) => UseComentsResult;

Returns UseComentsResult

interface UseComentsResult {
  comments: Comment[];
  addComment: ({
    content,
    author,
  }: Pick<Comment, 'content' | 'author'>) => void;
  refetch: () => void;
  count: number;
  loading: boolean;
  error: UseCommentsError | null;
}

Comment

export interface Comment {
  post_id: string;
  author: string;
  content: string;
  created_at: string;
  status?: CommentStatus;
}

UseCommentsConfig

Allows to implement pagination for the comments. Learn more about implementing pagination..

export interface UseCommentsConfig {
  limit?: number;
  offset?: number;
}

CommentStatus

When user is adding a new comment it will be in one of four states:

  • sending β€” add comment request is still pending.
  • added β€” the comment was successfully added and is visible for other people.
  • delivered-awaiting-approval β€” the comment was successfully added, but it's not yet visible for other people. You can make comments to require approval before being visible to others. Read more about it here.
  • failed β€” adding a comment was unsuccessful.
export declare type CommentStatus =
  | 'sending'
  | 'added'
  | 'delivered-awaiting-approval'
  | 'failed';

UseCommentsError

interface UseCommentsError {
  error: string;
  details: string;
}

Recipes

  1. Pagination

  2. Comments with approval

  3. Send an email after receiving a new comment

More Repositories

1

simple-zustand-devtools

Inspect your zustand store in React DevTools πŸ»βš›οΈ
TypeScript
334
star
2

theme-ui-sketchy

Sketchy Theme UI Preset
TypeScript
201
star
3

semantic-search

πŸ•΅οΈβ€β™€οΈ An OpenAI-powered CLI to build a semantic search index from your MDX files.
TypeScript
93
star
4

tsc-diagnostics-diff-action

TypeScript
65
star
5

animated-emojis

Animated emojis πŸ˜’πŸ’—πŸ˜‘πŸ˜¨πŸ˜Š
TypeScript
43
star
6

edgedb-ai-booking-assistant

AI Booking Assistant with EdgeDB and Vercel AI SDK
TypeScript
33
star
7

go-group-imports-vscode

VSCode extension for separating imports in go files into three groups: stdlib, 3rd party, own.
TypeScript
31
star
8

olang

a minimal programming language written in TypeScript with ts-parsec
TypeScript
23
star
9

wtf-js-quiz

TypeScript
23
star
10

aleksandra.codes

personal website
MDX
19
star
11

no-profanity

No more bad words in your console logs!
TypeScript
10
star
12

gptql

TypeScript
9
star
13

proof-checker

Proof checker for natural deduction in propositional logic
OCaml
9
star
14

next13-blitz-auth

TypeScript
9
star
15

hasura-interactive-cli

TypeScript
8
star
16

ts-performance-talk

TypeScript
8
star
17

diversity.lol

TypeScript
6
star
18

text-to-graphql

TypeScript
6
star
19

ocaml-parser-example

OCaml
5
star
20

everybody-gives-astro-supabase

TypeScript
5
star
21

supabase-realtime-math-game

TypeScript
5
star
22

ts-workshops

TypeScript
5
star
23

blog

TypeScript
4
star
24

pp6

Haskell
2
star
25

chatty

gRPC + elixir
Elixir
2
star
26

hasura-vscode

TypeScript
2
star
27

adventofcode2020

Rust
2
star
28

fets-spotify-demo-app

TypeScript
2
star
29

wroc-ts-08-talk

TypeScript
2
star
30

ai-project

C#
1
star
31

personal-blog

TypeScript
1
star
32

go-fixer-api

Go
1
star
33

ts-in-css-css-prop

Created with CodeSandbox
TypeScript
1
star
34

everybody-gives-ui

TypeScript
1
star
35

numerical-analysis-app

TypeScript
1
star
36

graphiql-gpt-plugin

TypeScript
1
star
37

savant

CSS
1
star
38

css-to-js-vscode

TypeScript
1
star
39

codingame-ES6-eslint-babel-webpack-starter

Minimal starter for sane coding in JavaScript on CodinGame.
JavaScript
1
star
40

tag-sql

πŸ’₯Build sql queries for mysqljs in a safe and comfortable way
TypeScript
1
star
41

backend-interview-questions-and-answers

1
star
42

ink-examples

TypeScript
1
star
43

mips-assembler

Python
1
star
44

algorithms

TypeScript
1
star
45

go-rest-openapi

Go
1
star
46

hackathon

TypeScript
1
star
47

aleksandrasays

Personal site
TypeScript
1
star