Skip to Content

Add Comments, Mentions, Activity and Version History

Comment threads, notifying at-mentions, a per-record timeline and undo on any org-scoped record.

Install

npm i @dapplelight/signet-server @dapplelight/signet-ui @dapplelight/signet-db hono drizzle-orm

Add the tables and track versions

src/db/schema.ts
import { collabTables } from "@dapplelight/signet-db/columns/sqlite"

export const { comments, recordVersions } = collabTables()
src/server/db.ts
import { createDb } from "@dapplelight/signet-db/d1"
import { recordVersions, tasks } from "../db/schema"
import { audit } from "./audit"
import { bindings } from "./env"

export const db = createDb(bindings.DB, { audit, versions: { table: recordVersions, track: [tasks] } })

Each tracked table needs an orgId column.

Create the collab service

src/server/collab.ts
import { createCollab } from "@dapplelight/signet-db/collab"
import { notifyMentions } from "@dapplelight/signet-server/http/collab"
import { comments, tasks } from "../db/schema"
import { db } from "./db"
import { notifier } from "./notifier"
import { orgs } from "./orgs"
import { namesById } from "./people"

export const collab = createCollab(db, {
  tables: { comments },
  records: { tasks },
  orgs,
  people: (ids) => namesById(ids),
  notify: notifyMentions(notifier),
})

Mount the routes after the tenant

src/app.ts
import { collabRoutes, mentionTemplates } from "@dapplelight/signet-server/http/collab"
import { tenant } from "@dapplelight/signet-server/http/orgs"
import { Hono } from "hono"
import { collab } from "./server/collab"
import { orgs } from "./server/orgs"
import { appTemplates } from "./server/templates"

const app = new Hono()

const templates = { ...appTemplates, ...mentionTemplates({ url: (m) => `https://app.example.com/${m.table}/${m.rowId}` }) }

app.use("/api/*", tenant({ orgs, rootDomain: "app.example.com" }))
app.route("/api/collab", collabRoutes({ collab, userId: (c) => c.get("session")?.sub }))

Pass templates to createNotifier.

Use it from code

src/server/tasks.ts
import { collab } from "./collab"

export async function review(orgId: string, userId: string, taskId: string) {
  const who = { orgId, userId }
  const ref = { table: "tasks", id: taskId }

  await collab.addComment(who, ref, { body: "Ready? @[Asha Rao](user:u_1)" })
  await collab.timeline(who, ref, { limit: 20 })
  await collab.restore(who, ref, 2)
  await collab.undo(who, ref, { expected: 5 })
}

Render it

See the collab screens for the thread, mention picker and history UI.

Next