Skip to Content

Add CSV Import, an Editable Grid and a Rich Text Editor

Bulk-import customers from a CSV, edit them in a spreadsheet-style grid, and store sanitised rich notes.

Install

npm i @dapplelight/signet-ui @dapplelight/signet-server @dapplelight/signet-db zod
npm i @tiptap/react @tiptap/pm @tiptap/starter-kit @tiptap/extension-mention @tiptap/extension-link

The Tiptap packages are only for the rich text editor.

Import a CSV

src/web/customers-import.tsx
import { CsvImport } from "@dapplelight/signet-ui/domain/csv-import"
import { z } from "zod"
import { api } from "./api"

const schema = z.object({ name: z.string().min(1), email: z.email(), plan: z.enum(["free", "pro"]) })

<CsvImport
  fields={[{ key: "name", required: true }, { key: "email", aliases: ["E-mail"] }, { key: "plan" }]}
  schema={schema}
  onImport={(rows) => api.post("/customers/import", { rows })}
/>

Insert the rows on the server

src/server/customers.ts
import { bulkInsert } from "@dapplelight/signet-db/import"
import { audited, scoped } from "@dapplelight/signet-db"
import { customers } from "../db/schema"
import { app } from "./app"
import { db } from "./db"

app.post("/customers/import", async (c) => {
  const { rows } = await c.req.json()
  const ops = audited(scoped(db, { orgId: c.get("orgId") }))
  return c.json(await bulkInsert(ops, customers, rows, { key: "externalId" }))
})

Importing the same file twice inserts nothing new.

Edit in a grid

src/web/customers-grid.tsx
import { DataGrid } from "@dapplelight/signet-ui/components/data-grid"
import { useState } from "react"
import { api } from "./api"
import { applyChanges } from "./rows"

export function CustomersGrid() {
  const [rows, setRows] = useState<{ id: string }[]>([])

  return (
    <DataGrid
      aria-label="Customers"
      rows={rows}
      getRowId={(r) => r.id}
      columns={[
        { id: "name", header: "Name" },
        { id: "seats", header: "Seats", type: "number" },
        { id: "plan", header: "Plan", type: "select", options: [{ value: "free", label: "Free" }, { value: "pro", label: "Pro" }] },
      ]}
      onChange={async (changes) => {
        await api.patch("/customers", { changes })
        setRows((all) => applyChanges(all, changes))
      }}
    />
  )
}

A throw from onChange reverts the cells and marks them with the error.

Write rich notes

src/web/note.tsx
import { RichTextEditor, RichTextContent } from "@dapplelight/signet-ui/components/rich-text-editor"
import { useState } from "react"
import { api } from "./api"

export function Note() {
  const [body, setBody] = useState("")

  return (
    <>
      <RichTextEditor aria-label="Note" value={body} onValueChange={setBody} onMentionSearch={(q) => api.get(`/mentions?q=${q}`)} />
      <RichTextContent value={body} />
    </>
  )
}

Sanitise on save

src/server/notes.ts
import { sanitizeMarkdown } from "@dapplelight/signet-server/http/sanitize"
import { z } from "zod"

const NoteInput = z.object({ body: z.string().max(20_000).transform(sanitizeMarkdown) })

Next