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

supabase-comments-x

v0.1.8

Published

Comments powered by React Query and Supabase

Downloads

33

Readme

Supabase Comments X

Just a fork of the excellent Supabase-Comments-Extension by Austin Malerba. Give the project a like!

Whats different?

BREAKING CHANGES

  • Fully ESM.
  • Now on Tanstack/@tanstack/react-query
  • Removed all Date-related packages. Using native JS.
  • Removed color for tinycolor2
  • Auth is fully social. Its just easier to maintain. If theres any demand for email auth, leave me a message.

Size

  • Biggest change by far. Over 50% in reduction in overall project size, much of it due to the deprecated @supabase/ui library which re-exported almost all of Feather icons. So that was scrapped and replaced with Lucide React which already tree-shakes any unused icons.

REACT 18

  • All dependencies have been updated

and

  • Personalized stickers 👺

Add a robust comment system to your react app in ~10 minutes!

This library provides comments, replies, reactions, mentions, and authentication all out of the box.

Getting Started

First things first, this project is powered by supabase so if you don't already have a supabase db, head over there and make one (it's super simple and literally takes a few seconds)

Installation

Install this package and its peer dependencies

bun i supabase-comments-x @supabase/ssr @supabase/supabase-js @tanstack/react-query

Running Migrations

Once you've got yourself a supabase db, you'll need to add a few tables and other sql goodies to it with the following command

bunx supabase-comments-x run-migrations <supabase-connection-string>

You can find your connection string on the supabase dashboard: https://app.supabase.io/project/PUT-YOUR-PROJECT-ID-HERE/settings/database

It should look something like this: postgresql://postgres:[email protected]:5432/postgres

Usage With Auth

Then in your app code you can add comments with the following

import { createBrowserClient } from '@supabase/ssr'

export function createClient() {
  return createBrowserClient(process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!)
}
import 'supabase-comments-x/styles'
import { useState } from 'react'
import {
  AuthModal,
  Comments,
  CommentsProvider,
} from 'supabase-comments-x'
import { createClient } from '@/utils/supabase/client'

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

function CommentSection({ slug }: { slug: string }) {
  const [modalVisible, setModalVisible] = useState(false)

  return (
    <CommentsProvider
      supabaseClient={supabase}
      onAuthRequested={() => setModalVisible(true)}
    >
      <AuthModal
        visible={modalVisible}
        onAuthenticate={() => setModalVisible(false)}
        onClose={() => setModalVisible(false)}
        providers={['google', 'facebook']}
      />
      <Comments topic={slug} />
    </CommentsProvider>
  )
}

Next.JS

If you're using Next.JS, for best performance take advantage of next/dynamic.

'use client'
import 'supabase-comments-x/styles'
import { useState } from 'react'

import lazy from 'next/dynamic'
const CommentsProvider = lazy(() => import('supabase-comments-x/commentsprovider'), { ssr: false })
const AuthModal = lazy(() => import('supabase-comments-x/authmodal'), { ssr: false })
const Comments = lazy(() => import('scx-tan-2').then((m) => m.Comments), { ssr: false })
import { createClient } from '@/utils/supabase/client'

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

function CommentSection({ slug }: { slug: string }) {
  return (
    <CommentsProvider
      supabaseClient={supabase}
      onAuthRequested={() => setModalVisible(true)}
    >
      <AuthModal
        visible={modalVisible}
        onAuthenticate={() => setModalVisible(false)}
        onClose={() => setModalVisible(false)}
        providers={['google', 'facebook']}
      />
      <Comments topic={slug} />
    </CommentsProvider>
  )
}

Note that supabase supports social auth with dozens of providers out-of-the-box so you can sign in with Google, Facebook, Twitter, Github and many more.

supabase-comments-x exports two auth components, Auth and AuthModal. The Auth component is a small adaptation of @supabase/ui's Auth component and supports all of the same props. AuthModal also supports all of the same props as the Auth component along with a few additional props.

Lastly, if you want to write your own authentication ui, then know that the supbase client provides a method supabase.auth.signIn which can authenticate the supabase client without forcing any ui on you.

'use client'
import 'supabase-comments-x/styles'
import { createClient } from '@/utils/supabase/client'
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

// Social Auth
const { user, error } = await supabase.auth.signInWithOAuth({
  provider: 'facebook',
})

// Email/Password Auth
const { user, error } = await supabase.auth.signInWithPassword({
  email: '[email protected]',
  password: 'example-password',
})

Usage Without Auth

If you already have an app set up with supabase authentication then you can skip the AuthModal and direct the user to your existing sign-in system.

import 'supabase-comments-x/styles'
import { useState } from 'react'
import { Comments, CommentsProvider } from 'supabase-comments-x'
import { createClient } from '@/utils/supabase/client'

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

function App() {
  return (
    <CommentsProvider
      supabaseClient={supabase}
      onAuthRequested={() => {
        window.location.href = '/sign-in'
      }}
    >
      <Comments topic="tutorial-one" />
    </CommentsProvider>
  )
}

Advanced Features

supabase-comments-x includes a handful of customization options to meet your app's needs

Bring Your Own Reactions

You can add your own reactions by adding rows to the reactions table.

It's easy to add rows via the supabase dashboard or if you prefer you can write some sql to insert new rows.

INSERT into
    reactions (type, label, url)
values (
        'heart',
        'Bulma',
        'https://i.postimg.cc/8zHSsSRD/bulma.webp'
    );

INSERT into
    reactions (type, label, url)
values (
        'like',
        'Like',
        'https://i.postimg.cc/PqVnzQVR/nofucks.webp'
    );

INSERT into
    reactions (type, label, url)
values (
        'suss-cat',
        'Suss Cat',
        'https://i.postimg.cc/sXBdnGtD/suss.webp'
    );

Custom Reaction Rendering

If you want to customize the way comment reactions are rendered then you're in luck! You can pass your own CommentReactions component to control exactly how reactions are rendered beneath each comment.

import 'supabase-comments-x/styles'
import { useState } from 'react'
import { Button } from '@supabase/ui'
import {
  CommentReactionsProps,
  Comments,
  CommentsProvider,
} from 'supabase-comments-x'
import { createClient } from '@/utils/supabase/client'

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

function CustomCommentReactions({
  activeReactions,
  toggleReaction,
}) {
  return (
    <Button className="!py-0.5" onClick={() => toggleReaction('like')}>
      {activeReactions.has('like') ? 'unlike' : 'like'}
    </Button>
  )
}

function App() {
  return (
    <CommentsProvider
      supabaseClient={supabase}
      components={{
        CommentReactions: CustomCommentReactions,
      }}
    >
      <Comments topic="custom-reactions" />
    </CommentsProvider>
  )
}

The above code will render the following ui

Handling Mentions

This library includes support for mentions, however mentions are fairly useless without a way to notify the users who are mentioned. You can listen to mentions via postgres triggers and perform some action in response such as insert into a notifications table or send an http request to a custom endpoint.

CREATE OR REPLACE FUNCTION notify_mentioned_users()
  RETURNS trigger AS
$$
DECLARE
  mentioned_user_id uuid;
BEGIN
  FOREACH mentioned_user_id IN ARRAY NEW.mentioned_user_ids LOOP
	  INSERT INTO your_notifications_table (actor, action, receiver) VALUES(NEW.user_id, 'mention', mentioned_user_id);
  END LOOP;
RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';

CREATE TRIGGER comment_insert_trigger
  AFTER INSERT
  ON comments
  FOR EACH ROW
  EXECUTE PROCEDURE notify_mentioned_users();

If you don't care about mentions, then you can disable them via the CommentsProvider

<CommentsProvider supabaseClient={supabase} enableMentions={false}>
  <Comments topic="mentions-disabled" />
</CommentsProvider>

API

Here's the prop options for primary components you'll be working with

interface CommentsProviderProps {
  queryClient?: QueryClient
  supabaseClient: SupabaseClient
  onAuthRequested?: () => void
  onUserClick?: (user: DisplayUser) => void
  mode?: 'light' | 'dark'
  accentColor?: string
  onError?: (error: ApiError, query: Query) => void
  components?: {
    CommentReactions?: ComponentType<{
      activeReactions: Set<string>
      reactionsMetadata: api.CommentReactionMetadata[]
      toggleReaction: (reactionType: string) => void
    }>
  }
  enableMentions?: boolean
}

interface CommentsProps {
  topic: string
}

interface AuthModalProps extends AuthProps {
  visible: boolean
  onClose?: () => void
  onAuthenticate?: (session: Session) => void
  title?: string
  description?: string
}

// This comes from @supabase/ui (https://ui.supabase.io/components/auth)
// supabase-comments-x provides an adapted version of supabase ui's
// Auth component with support for display names/avatars
interface AuthProps {
  supabaseClient: SupabaseClient
  className?: string
  children?: React.ReactNode
  style?: React.CSSProperties
  socialLayout?: 'horizontal' | 'vertical'
  socialColors?: boolean
  socialButtonSize?: 'tiny' | 'small' | 'medium' | 'large' | 'xlarge'
  providers?: Provider[]
  verticalSocialLayout?: any
  view?: ViewType
  redirectTo?: RedirectTo
  onlyThirdPartyProviders?: boolean
  magicLink?: boolean
}