People, comments and files
A tracker one person fills in needs a status column and little else. Once a team uses it, people want to see who a task is assigned to, who changed the status and when, a thread of comments on the task, and the mockups attached to it, kept somewhere that survives a reload. This page adds those to the task tracker from Build a project tracker.
SvGrid Base is part of the Suite edition of
@svgrid/enterprise. Without a key it runs with a watermark.
In the demo, double-click a candidate's card on the pipeline (or switch to "All candidates" and press Space on a row), change the Stage, leave a comment, and watch the Activity column on the right of the record.
The base cannot know who your users are or where your files and comments live. You tell it with four calls in the browser; the change log needs nothing:
Each step below works on its own.
1. List the people
User fields pick from one list of people that you hand to the base. A person
is a FieldUser:
import { setFieldUsers, type FieldUser } from '@svgrid/enterprise/base'
const team: FieldUser[] = [
{ id: 'u1', name: 'Ana Ruiz', email: '[email protected]', avatarUrl: 'https://cdn.example.com/ana.jpg' },
{ id: 'u2', name: 'Ben Okafor', email: '[email protected]' },
{ id: 'u3', name: 'Cleo Park', email: '[email protected]' },
]
// In the browser only; step 2 shows where in SvelteKit.
setFieldUsers(team)
Give the tasks a user field for the owner and one with multiple for
reviewers. The cell stores ids, never names, so renaming a person renames
them everywhere:
const columns = [
{ field: 'task', header: 'Task', fieldType: 'text' },
{ field: 'status', header: 'Status', fieldType: 'singleSelect', fieldProps: { options: STATUS } },
{ field: 'assignee', header: 'Assignee', fieldType: 'user' },
{ field: 'reviewers', header: 'Reviewers', fieldType: 'user', fieldProps: { multiple: true } },
]
const data = [
{ id: 't1', task: 'Write the brief', status: 'Todo', assignee: 'u1', reviewers: ['u2', 'u3'] },
{ id: 't2', task: 'Pick the fonts', status: 'In progress', assignee: 'u2', reviewers: [] },
]
A cell draws an avatar and the name. With several people it overlaps up to
four avatars and writes the first name with a count: "Ben Okafor +1".
avatarUrl is used when it is an http(s):, blob: or data:image URL;
anything else, or no URL, draws the initials on a color worked out from the
name, so a person keeps the same color on every page. An id that is not in
the list shows as the id itself.
Editing a user cell opens a panel with a "Find a person" box and the people
from the list, each with their avatar; typing filters them. If the list is
empty the panel says "No people (setFieldUsers)". Text pasted or filled into
a user cell is matched against each person's id, name and email, ignoring
case; a multiple field takes them comma-separated and drops repeats. A name
that matches nobody is left out and the page says so, and with nobody left the
cell keeps its value. With no list of people set the field takes what it is
given. Copying a user cell writes the names, so a copy and paste between
records keeps the people, and a copy in the same page keeps the very person when
two share a name.
fieldProps.default takes an id for new rows. The field reference has the
user field's properties.
A Kanban view can stack by a single user field (stackBy: 'assignee'): one
lane per person in the list, plus "Uncategorized" while some task has nobody.
Dragging a card to a lane reassigns the task through the grid's edit path,
so it counts as an edit.
2. Say who is signed in
setCurrentUser names the person using the page. Several things read it:
createdByfills new rows with their id, andlastModifiedBywrites it on every edit it watches.- Each comment is saved with their id as
by. - Each change in a record's activity is noted with their id.
- A filter condition with the value
'@me'(ME) matches them. - Your own code can read it with
getCurrentUser().
In SvelteKit the session is on the server, so load the person and the team in a layout and hand both to the base in the browser:
// src/routes/(app)/+layout.server.ts
import { redirect } from '@sveltejs/kit'
import type { LayoutServerLoad } from './$types'
import { db } from '$lib/server/db'
export const load: LayoutServerLoad = async ({ locals }) => {
if (!locals.user) redirect(303, '/login')
const team = await db.query(
'select id, name, email, avatar_url as "avatarUrl" from users where team_id = $1 order by name',
[locals.user.teamId],
)
return { me: { id: locals.user.id, name: locals.user.name }, team: team.rows }
}
<!-- src/routes/(app)/+layout.svelte -->
<script lang="ts">
import type { Snippet } from 'svelte'
import { setCurrentUser, setFieldUsers, type FieldUser } from '@svgrid/enterprise/base'
let { data, children }: { data: { me: FieldUser; team: FieldUser[] }; children: Snippet } = $props()
// Again when the data changes: a sign-in as someone else.
$effect.pre(() => {
setFieldUsers(data.team)
setCurrentUser(data.me)
})
</script>
{@render children()}
The four setters on this page write state shared by the whole module, so
call them in the browser only: an $effect as here (it does not run during
server rendering), onMount, or behind browser from $app/environment.
Called at the top of a component's <script>, they would also run on the
server and hand one request's team and user to the next. Cells and pickers
read both values reactively, so the base can draw before they arrive. The signed-in person does not have to be in the team list for
their name to show, but they only appear in the picker if they are.
Now add who made each task and when, and give a new task to whoever adds it:
import { getCurrentUser, type BaseTable } from '@svgrid/enterprise/base'
const tasks: BaseTable = {
id: 'tasks',
name: 'Tasks',
primaryField: 'task',
data,
columns: [
...columns,
{ field: 'createdBy', header: 'Created by', fieldType: 'createdBy' },
{ field: 'createdAt', header: 'Created', fieldType: 'createdTime', fieldProps: { dateFormat: 'friendly' } },
],
props: {
rowAdder: { label: 'Add task', defaults: () => ({ status: 'Todo', assignee: getCurrentUser()?.id ?? null }) },
},
}
Both fields are read-only. Every record the base makes gets the current user and the time: "+ Add row", a form, the record menu, "+ Add a card" on a Kanban, a calendar day. Last modified fields start out the same way. Rows you load keep whatever your database sends.
On sign-out, call closeRecord(), then setCurrentUser(null) and
setFieldUsers([]).
3. Add a "My tasks" view
A filter condition on a user field compares it with a person's id. The value
'@me' (exported as ME) stands for whoever is signed in when the filter
runs, so one view works for everyone:
import { ME, type FieldView } from '@svgrid/enterprise/base'
const views: FieldView[] = [
{
id: 'mine',
name: 'My tasks',
type: 'grid',
state: {
advancedFilter: {
kind: 'fieldFilter',
conjunction: 'or',
conditions: [
{ column: 'assignee', kind: 'select', op: 'is', value: ME },
{ column: 'reviewers', kind: 'multi', op: 'hasAnyOf', value: [ME] },
],
},
},
},
{ id: 'all', name: 'All tasks', type: 'grid' },
]
In the Filter panel the person picker lists "Me" first, so users can build the same view themselves. The condition reads "Assignee is me".
A single user field filters with the select operators (is, isNot,
isAnyOf, isNoneOf), a multiple one with hasAnyOf, hasAllOf,
isExactly and hasNoneOf; both also have isEmpty and isNotEmpty.
Created by and last modified by filter the same way, so "Created by is me"
works too. The filter conditions table
lists them all.
The view stores '@me', not an id, so it can be saved with the other views
(onViewsChange, or the views inside onTablesChange) and Ben sees his own
tasks when he opens it.
4. Open a record
expandRows with showRowNumbers turns the row number into a checkbox and an
expand button:
<SvBase bind:tables editable showRowNumbers expandRows onTablesChange={save} />
A record opens from the expand button, from Space on a selected cell (not a
checkbox cell, and only with expandRows), from "Expand record" in the
right-click menu, from a gallery card, from a double click (or F2) on a
Kanban card, from a calendar event and from a Gantt task.

The record shows:
- The primary field as the title.
- Every field with its type icon and name, drawn as its cell draws it. A click
opens the cell's own editor; the value is written through the grid's edit
path, so undo, validation,
onCellValueChangeand last-modified fields work the same as in the cell. Read-only fields say "read-only". - Fields hidden in the view, folded under "Show N hidden fields" and marked "hidden in the grid". They are read-only here.
- Activity on the right: changes and comments, oldest first, with "just now",
"5 minutes ago", "3 hours ago" or the date. The box under them takes a
comment; Ctrl+Enter (Cmd+Enter) or Comment sends it. A base without
editablestill takes comments. Text not sent stays with its record until the page reloads: stepping to the next record leaves it behind, and coming back finds it. Closing the record says so. - Previous and next buttons that step through the rows in the view's order, and "3 of 12".
A record with comments shows how many by its row number (the pointer over the row swaps the count for the checkbox and expand button) and as a badge on its Kanban card. The card's badge opens the record with the comment box focused, as does "Comment" in the right-click menu of a row or a card.
On a narrow screen the activity moves under the fields. If an edit makes the record leave the view's filter, the record says "This row is not in the grid's view any more." and the footer "Not in view (12 records)". Previous and next still work: they move to the records that were before and after it.
Where the record opens
Three buttons in the record's header pick where it opens, and the choice is
kept on the device (localStorage):
- Open in the middle: a dialog over the grid, as before.
- Open beside the table: a panel on the right that leaves the grid
usable. The base (
.sv-fbase-tables, or the grid's root) gives up the panel's width, and the panel starts where the base starts, under the app's own bar. Picking another row in the grid (a click, the arrow keys) shows that record in the panel. Its left edge is dragged, or moved with the arrow keys, to set the width (360 to 900 px, 480 at first). Escape closes it. It is not modal: the focus is not held in it. - Open full page: the dialog fills the window.
Below 760 px of window width the record always opens in the middle. Records opened from a link chip keep their own dialog over the others.
import { recordDock, setRecordDock, setRecordDockWidth } from '@svgrid/enterprise/base'
setRecordDock('right') // what this device opens records as from now on
App code opens a record with the grid api that onApiReady hands it:
import { openRecord, closeRecord } from '@svgrid/enterprise/base'
let api: unknown
// <SvBase ... onApiReady={(a) => (api = a)} />
openRecord({ api, rowId: 't1' })
openRecord({ api, rowId: 't1', focus: 'comment' }) // with the comment box focused
closeRecord(api)
closeRecord() closes whatever record is open, from anywhere: call it on
sign-out or when the route changes. closeRecord(api) closes it only when it
is open on that grid. A custom field type's cell component gets the grid
as ctrl (with row.id), and openRecord({ ctrl, rowId }) takes that too.
A button field's fieldProps.onClick receives { row, rowId, columnId, api },
so a button can open its own record.
5. Keep comments in your backend
Without a store, comments live in memory and are gone on reload. In
<SvBase tables> they are kept by table id and record id for the whole
page, so two bases that show the same table, or a base mounted again, show
the same comments. Give unrelated bases on one page different table ids.
With one table (data and columns) they are kept per grid. A comment is
a RecordComment, and a store is two functions:
export type RecordComment = { id: string; rowId: string; tableId?: string; text: string; at: number; by?: string; mentions?: string[]; files?: Attachment[]; internal?: boolean; replyTo?: string; resolved?: { at: number; by?: string } }
export type RecordCommentContext = { tableId?: string; api: unknown }
export type RecordCommentStore = {
list: (rowId: string, api: unknown, context: RecordCommentContext) => ReadonlyArray<RecordComment>
add: (rowId: string, comment: RecordComment, api: unknown, context: RecordCommentContext) => void
update?: (rowId: string, comment: RecordComment, api: unknown, context: RecordCommentContext) => void
remove?: (rowId: string, commentId: string, api: unknown, context: RecordCommentContext) => void
onMention?: (event: MentionEvent) => void
files?: boolean
internal?: boolean
replies?: boolean
resolve?: (rowId: string, comment: RecordComment, resolved: boolean, api: unknown, context: RecordCommentContext) => void
}
Files and internal notes
A store with files: true gets an Attach files button in the comment
box. Each file goes through the app's attachment uploader
(setAttachmentUploader, see Upload files) and the comment carries the
results in files; a comment of files alone has empty text. The record
view lists them under the comment as links.
A store with internal: true gets an Internal note box to tick. The
comment comes with internal: true and shows with an "Internal" mark. Hiding
it from people outside the team (the clients of a portal, say) is the
store's job: list should leave it out for them, and the server should
refuse it from them.
import { setRecordComments, type RecordComment } from '@svgrid/enterprise/base'
const kept: RecordComment[] = []
setRecordComments({ list: () => kept, add: (_rowId, comment) => kept.push(comment), files: true, internal: true })
Votes on comments
A store with vote shows Up and Down under every comment, with how
many voted each way and, on hover, who. A person has one vote on a comment
and none on their own: pressing their vote again takes it back, and the
other one replaces it. The comment comes to vote with its new votes
({ up?: string[], down?: string[] }, user ids), and the vote ('up',
'down' or null). Without a store, votes are kept in the page; a store
without vote shows none.
import { setRecordComments, type RecordComment } from '@svgrid/enterprise/base'
let kept: RecordComment[] = []
setRecordComments({
list: () => kept,
add: (_rowId, comment) => kept.push(comment),
vote: (_rowId, comment) => (kept = kept.map((c) => (c.id === comment.id ? comment : c))),
})
Replies and resolved threads
A store with replies: true gets Reply under each comment. The comment
box then says who it answers, and the answer comes to add with replyTo:
the id of the first comment of the thread, so an answer to an answer joins
the same thread. An answer to an internal note is internal too. The record
view shows the answers under the comment, oldest first; an answer whose
comment is gone shows on its own.
A store with resolve gets Resolve under each thread. A resolved thread
folds to one line, its first words and who resolved it, with Show to read
it again and Reopen to take the mark off. resolve hears the thread's
first comment, with resolved: { at, by } when it was resolved and without
it when it was opened again, and true or false. Without a store, both are
kept in the page.
import { commentThreads, setRecordComments, type RecordComment } from '@svgrid/enterprise/base'
let kept: RecordComment[] = []
setRecordComments({
list: () => kept,
add: (_rowId, comment) => (kept = [...kept, comment]),
replies: true,
resolve: (_rowId, comment) => (kept = kept.map((c) => (c.id === comment.id ? comment : c))),
})
// The threads of a record, as the record view draws them.
const { top, replies } = commentThreads(kept)
Saved replies
setCommentSnippets gives the comment box a Saved replies button: a
service desk's standard answers. list is asked for the record open (its
table, its id and its values), so the app can fill each reply in from the
record before it is shown ("Hi Ana, we have your invoice"). The person
searches, picks one, and its text (and its files) go into the box to change
before sending; picking sends nothing. With save, the panel also offers
Save this text as a reply, which hands the app the text and a name.
import { setCommentSnippets } from '@svgrid/enterprise/base'
setCommentSnippets({
list: async (tableId, { row }) => [
{ id: 'received', title: 'Invoice received', text: `Hi ${String(row.contact ?? '')}, we have your invoice and will pay it this week.` },
],
save: async (tableId, { title, text }) => {
await fetch('/replies', { method: 'POST', body: JSON.stringify({ tableId, title, text }) })
},
})
rowId is the record's id (its id, or the table's getRowId). In
<SvBase tables>, context.tableId (and the comment's tableId) names the
table, so Tasks 1 and Milestones 1 are two records; with one table it is
undefined. at is milliseconds since 1970, by the id of the person who was
signed in. The record view makes id, at and by itself and calls add.
A comment's author sees Edit and Delete under it, with no one signed in that is
whoever is at the page. An edited comment shows "(edited)" and carries editedAt;
someone newly mentioned in it hears of it once. A store without update or
remove offers neither.
A store written for list(rowId, api) and add(rowId, comment, api) still
works.
Both functions are synchronous, and list runs often: for the open record,
and for every row the grid draws (with expandRows) and every Kanban card,
to show the comment count. So fetch a whole table's comments once into reactive state and answer
from it. Because list runs inside the base's reactive code, the counts and
the record draw again when the fetch lands:
// src/lib/comments.svelte.ts
import { browser } from '$app/environment'
import { toast } from '@svgrid/grid'
import { setRecordComments, type RecordComment, type RecordCommentStore } from '@svgrid/enterprise/base'
type ByRecord = Record<string, ReadonlyArray<RecordComment>>
// Each table's comments by record id, fetched once per table.
let byTable = $state.raw<Record<string, ByRecord>>({})
const asked = new Set<string>()
function put(table: string, rowId: string, list: ReadonlyArray<RecordComment>) {
byTable = { ...byTable, [table]: { ...byTable[table], [rowId]: list } }
}
async function load(table: string) {
asked.add(table)
const res = await fetch(`/api/comments?table=${encodeURIComponent(table)}`)
if (!res.ok) return void asked.delete(table)
const grouped: Record<string, RecordComment[]> = {}
for (const c of (await res.json()) as RecordComment[]) (grouped[c.rowId] ??= []).push(c)
byTable = { ...byTable, [table]: grouped }
}
// `update` and `remove` are optional: without them a comment stays as it was written.
const store: RecordCommentStore = {
list(rowId, _api, { tableId = '' }) {
if (!asked.has(tableId)) void load(tableId)
return byTable[tableId]?.[rowId] ?? []
},
add(rowId, comment, _api, { tableId = '' }) {
const mine = () => byTable[tableId]?.[rowId] ?? []
// Shown at once; the server's copy replaces it, or it goes away again.
put(tableId, rowId, [...mine(), comment])
fetch('/api/comments', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ id: comment.id, tableId, rowId, text: comment.text }),
})
.then(async (res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`)
const saved = (await res.json()) as RecordComment
put(tableId, rowId, mine().map((c) => (c.id === comment.id ? saved : c)))
})
.catch(() => {
put(tableId, rowId, mine().filter((c) => c.id !== comment.id))
toast.error('Your comment was not saved')
})
},
}
// On the server this cache would be shared by every request.
if (browser) setRecordComments(store)
Import the file once, in the page that shows the base. The failure notice shows
as a base's other notices do: see Notices.
setRecordComments(null) goes back to memory. For a table with a very large
discussion, have the endpoint return counts per record and fetch a record's
comments when it opens.
The endpoint takes the author and the time from the server, never from the request body:
// src/routes/api/comments/+server.ts
import { error, json } from '@sveltejs/kit'
import type { RequestHandler } from './$types'
import { db } from '$lib/server/db'
export const GET: RequestHandler = async ({ url, locals }) => {
if (!locals.user) error(401, 'Sign in first')
const tableId = url.searchParams.get('table') ?? ''
// `at` is a bigint column; the cast hands it back as a number, not a string.
const { rows } = await db.query(
'select id, table_id as "tableId", row_id as "rowId", text, at::float8 as at, author as "by" from comments where team_id = $1 and table_id = $2 order by at',
[locals.user.teamId, tableId],
)
return json(rows)
}
export const POST: RequestHandler = async ({ request, locals }) => {
if (!locals.user) error(401, 'Sign in first')
const body = (await request.json()) as { id?: unknown; tableId?: unknown; rowId?: unknown; text?: unknown }
const text = String(body.text ?? '').trim().slice(0, 4000)
if (typeof body.id !== 'string' || typeof body.rowId !== 'string' || !text) error(400, 'Bad comment')
const tableId = typeof body.tableId === 'string' ? body.tableId : ''
const comment = { id: body.id, tableId, rowId: body.rowId, text, at: Date.now(), by: locals.user.id }
await db.query(
'insert into comments (id, team_id, table_id, row_id, text, at, author) values ($1, $2, $3, $4, $5, $6, $7)',
[comment.id, locals.user.teamId, comment.tableId, comment.rowId, comment.text, comment.at, comment.by],
)
return json(comment, { status: 201 })
}
The activity names the author from the people list: a by that is not in
setFieldUsers (and is not the current user) shows as "Someone", and a
comment with no by shows as "You". Keep people who left the team in the
list, or return their name from your own data.
Comments other people add show once the cache changes. To see them while the
base is open, call put from your realtime channel or a poll.
Mentions
Typing @ in the comment box lists the people from setFieldUsers, narrowed
by the letters after it (the start of any word of a name, or of an email).
Up and Down move through the list, Enter or Tab picks, Escape closes the
list and leaves the record open. An @ inside a word, as in an email
address, does not open it.
Escape in the comment box leaves the box and keeps the text. On a comment being changed it is Cancel. While a comment has text not sent, or is being changed, Escape does not close the record and says why, a click outside does nothing, and the Close button still closes it. An empty box holds nothing open.
A mention is kept in the comment's text as a token, and its id in
mentions:
{ text: 'Numbers match. @[Ana Ruiz](u1) can you get legal to sign off?', mentions: ['u1'] }
The record view draws the token as a chip with the person's avatar and their
name from the people list today, so renaming someone renames their mentions.
A person no longer in the list shows with the name written in the token. The
rest of the text is drawn as text, never as HTML. mentionText(text) turns
the tokens back into @Ana Ruiz for an email or a chat message, and
mentionIds(text) reads the ids, for a server that does not trust the
mentions the browser sent.
To send notifications, pass onMention to <SvBase>, give the store an
onMention hook, or call setMentionHandler (they work with the in-memory
default too). Each runs once
per comment that mentions someone, after add:
import { mentionText, setMentionHandler, type MentionEvent } from '@svgrid/enterprise/base'
setMentionHandler((e: MentionEvent) => {
// e.comment, e.mentioned (FieldUser[]), e.rowId, e.tableId, e.record, e.api
const to = e.mentioned.filter((p) => p.id !== e.comment.by && p.email).map((p) => p.email)
if (!to.length) return
void fetch('/api/notify', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ to, record: e.record?.task, text: mentionText(e.comment.text) }),
})
})
mentioned lists each person once, in the order they first appear,
including the author when they mention themselves. Someone not in
setFieldUsers comes with the name written in the token. A hook that throws
is logged, and the comment stays. When your comments endpoint already saves
mentions, sending from the server, where the author is known, is the safer
place for the email.
6. Upload files
An attachment cell holds an array of Attachment: id, url, name,
type (the MIME type), and optional size, width, height and
thumbnailUrl (a smaller image the cell uses instead of url). The
attachments section of the field reference has
an example value.
Without an uploader, added files stay in the page as blob: URLs and are
gone on reload. setAttachmentUploader sets the function every attachment
column uses. It gets the File and an AttachmentUploadContext: row (the
record), columnId, a signal that aborts when the user removes the file
or closes the dialog, and onProgress(fraction) with a value from 0 to 1.
It returns the attachment.
This one asks your server for a presigned URL and sends the bytes straight
to object storage. It uses XMLHttpRequest because fetch reports no upload
progress:
// src/lib/uploads.ts
import { browser } from '$app/environment'
import { setAttachmentUploader, type AttachmentUploader } from '@svgrid/enterprise/base'
const upload: AttachmentUploader = async (file, { columnId, signal, onProgress }) => {
const res = await fetch('/api/uploads', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ name: file.name, type: file.type, size: file.size, columnId }),
signal,
})
if (!res.ok) throw new Error(res.status === 413 ? 'File is too large' : `Upload refused (${res.status})`)
const { id, uploadUrl, fileUrl } = (await res.json()) as { id: string; uploadUrl: string; fileUrl: string }
await new Promise<void>((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open('PUT', uploadUrl)
xhr.setRequestHeader('content-type', file.type || 'application/octet-stream')
xhr.upload.onprogress = (e) => e.lengthComputable && onProgress(e.loaded / e.total)
xhr.onload = () => (xhr.status < 300 ? resolve() : reject(new Error(`Storage said ${xhr.status}`)))
xhr.onerror = () => reject(new Error('Network error'))
xhr.onabort = () => reject(new Error('Cancelled'))
signal.addEventListener('abort', () => xhr.abort(), { once: true })
xhr.send(file)
})
return { id, url: fileUrl, name: file.name, type: file.type, size: file.size }
}
if (browser) setAttachmentUploader(upload)
The message of the error you throw is what the user reads on the failed tile, next to Retry and Remove, so write it for them.
The server checks the user and the size, signs a URL for one key, and keeps a row per file:
// src/routes/api/uploads/+server.ts
import { error, json } from '@sveltejs/kit'
import type { RequestHandler } from './$types'
import { db } from '$lib/server/db'
import { presignPut } from '$lib/server/storage'
const MAX = 25 * 1024 * 1024
export const POST: RequestHandler = async ({ request, locals, url }) => {
if (!locals.user) error(401, 'Sign in first')
const { name, type, size } = (await request.json()) as { name: string; type: string; size: number }
if (!(size > 0 && size <= MAX)) error(413, 'File is too large')
const id = crypto.randomUUID()
const safe = String(name).replace(/[^\w.-]+/g, '_')
const key = `${locals.user.teamId}/${id}/${safe}`
await db.query('insert into files (id, team_id, key, type) values ($1, $2, $3, $4)', [id, locals.user.teamId, key, type])
return json({ id, uploadUrl: await presignPut(key, type), fileUrl: `${url.origin}/api/files/${id}/${safe}` })
}
fileUrl points at your app, not at the bucket. A presigned read URL
expires, and a cell keeps its URL for as long as the record exists. Serve
/api/files/[id]/[name] with a GET that checks the user and the team,
looks up the key by id and answers redirect(303, signedReadUrl):
thumbnails, the preview and Download all follow the redirect, and old records
keep working.
The URL is absolute and ends in the file name for the sake of copy and
paste. Copying an attachment cell writes the files' URLs, and a paste
attaches each http(s) URL it finds, taking the name from the path and the
type from its extension. A relative URL would show fine in the cell but be
dropped on paste, and a URL without the name would come back as a file named
by its id, an image drawn as a chip.
The column limits what the dialog takes with
AttachmentFieldProps: accept (as for
<input accept>, such as 'image/*,.pdf'), maxFiles (default 20) and
maxSize in bytes (default 25 MB). A file over a limit is listed under the
tiles with the reason, such as "At most 1 file", "Larger than 2 MB" or "Not
an accepted type". A URL added in the dialog, and text pasted into a cell,
are checked the same way, their type read from the extension: what fails is
dropped, the rest is kept. The field editor offers accept and maxFiles;
maxSize is set in code:
const photo = {
field: 'photo',
header: 'Photo',
fieldType: 'attachment',
fieldProps: { accept: 'image/*', maxFiles: 1, maxSize: 2 * 1024 * 1024 },
}
fieldProps.upload sets an uploader for one column instead of the app's.
More places to add files from
"Add files" lists where files come from: the person's device (choose or
drop), a link, the camera on a phone, and any sources the app adds with
setAttachmentSources: Google Drive, Dropbox or your own storage, each
through its own picker. A source answers the files chosen, in any of three
forms: a File (uploaded through the uploader, with progress), a file to
fetch first ({ name, size?, type?, load(signal) }: it shows as a tile at
once, and load brings it), or an attachment kept as it is (a link).
import { setAttachmentSources, type AttachmentSource } from '@svgrid/enterprise/base'
const drive: AttachmentSource = {
id: 'drive',
label: 'Google Drive',
icon: '/icons/drive.svg',
hint: 'A copy of each file is kept here.',
// When the dialog opens: load the picker's script, so a click opens it at once.
prepare: () => loadDrivePicker(),
// Called straight from the click, so a sign-in window it opens is not blocked.
async pick({ accept, multiple, max, signal }) {
const picked = await openDrivePicker({ accept, multiple, max, signal })
return picked.map((f) => ({ name: f.name, size: f.size, load: (s) => downloadFromDrive(f.id, s) }))
},
}
setAttachmentSources([drive])
The same checks as a file from the device apply to every answer (maxFiles,
maxSize, accept), and a file fetched with load is checked again once it
is here. While a source's picker is open, Escape and a click outside belong
to the picker: the dialog stays open. A pick that throws shows its message
under the source's name. fieldProps.sources sets the list for one column.
What the user sees
The cell shows images as thumbnails and other files as chips with the extension and the name, as many as the column width holds, then "+N". The first click selects the cell, as on any cell; then a click on a thumbnail or a chip opens that file full size, as Airtable does: pictures large, PDFs in the page, films and sounds in the browser's player, anything else with Download; Previous and Next (or the arrow keys) go through the cell's files, Escape closes. Enter or a double click opens the dialog. An empty cell opens on the places to add from (Your device, Link (URL), the app's sources); a cell with files shows them, with "Add files" for more. Files can also be dropped or pasted anywhere in the dialog, and a pasted URL is added as a link. Each upload is a tile with its percent and a bar; Done reads "Uploading 2..." and stays disabled until they finish. Tiles can be dragged to reorder, or moved with Alt+Left and Alt+Right; Delete removes one. Cancel or Escape leaves the cell as it was and aborts the uploads still running.
Files can also be dropped straight on a cell. Each one uploads on its own,
with a thin progress bar along the bottom of the cell, and the ones that
upload are written to the cell in one edit. A file over a limit shows in a
"Not added" toast and an upload that throws in an "Upload failed" toast, each
with the file name and the reason; the others still land. The toasts need
the same <SvToaster /> as the comment store. Uploads still running when the
cell goes away (scrolled out, or the table switched) are aborted through
signal.
Enter on a tile, or a click on its picture, opens the preview: images inline,
PDFs in an iframe, films (mp4, webm, mov, ogv) and sounds (mp3, wav, m4a,
ogg, aac) in the browser's own player, anything else as its name with a
Download link. Left and Right step through the files, except inside a player,
where they seek. For a film to start fast and jump ahead, and to play at all in
Safari, your storage should answer byte ranges (Range, 206). A PDF is framed only when the page could read its
bytes and they are a PDF, so your storage has to allow the page's origin with CORS
(Access-Control-Allow-Origin). A file it cannot read, or one that is not a PDF
whatever it calls itself, offers its Download link instead.
In the grid, one click on a picture in a cell opens the same preview and selects the cell with it. A file's chip fills most of its cell, so it works like a link: the first click selects the cell, and a click on the chip of the selected cell opens the file. A double-click still opens the cell's editor.
7. Track who changed what
lastModifiedTime and lastModifiedBy change on every edit to the record by
default. With watch: 'specific' they change only when one of watchFields
does, which turns them into "status changed at" and "status changed by":
const stamps = [
{ field: 'updatedAt', header: 'Last modified', fieldType: 'lastModifiedTime', fieldProps: { dateFormat: 'friendly' } },
{ field: 'statusAt', header: 'Status changed', fieldType: 'lastModifiedTime', fieldProps: { watch: 'specific', watchFields: ['status'] } },
{ field: 'statusBy', header: 'Status changed by', fieldType: 'lastModifiedBy', fieldProps: { watch: 'specific', watchFields: ['status'] } },
]
watchFields holds column ids: the column's id, or its field when it has
none. The time is an ISO string; the person is the current user's id. With
nobody signed in, the time still moves and the person stays as it was. A
commit that leaves the value as it was (an editor opened and closed with
Enter) is not an edit: nothing is stamped and onCellValueChange does not
fire. An automation's updateRecord moves the time but not the person.
The stamp is written into the row before onCellValueChange fires, so the
event's row already holds it. If you save cell by cell, the event names
only the edited column: send the stamps along, or have the server write
updated_at and updated_by from its own clock and session, which a
browser cannot fake. With onTablesChange or a table source (Keep a base
in a database) the stamps travel with the rest of the row.
Protected fields
A protected field takes edits from the people it lists. Everyone else sees it read-only, with a lock after its name that says who can edit it:
const columns = [
{ field: 'priority', header: 'Priority', fieldType: 'singleSelect', fieldProps: { options: PRIORITY, editors: ['u1', 'u2'] } },
{ field: 'approved', header: 'Approved', fieldType: 'checkbox', fieldProps: { editors: [] } },
]
editors holds user ids, matched against setCurrentUser. An empty list
means nobody can edit the values, and with nobody signed in a protected
field is read-only. In the base, Edit field > Who can edit its values sets
the list: "Only these people" starts with the person setting it, so they do
not lock themselves out. protectFields={false} on <SvBase> takes that
choice away from someone your app does not let change it.
What the protection covers, for the person in front of the page:
- typing, pasting, the fill handle, Delete, and the record view;
- a board stacked by the field (its cards do not move) and a calendar or timeline whose dates it holds (its events do not drag);
- an import into the table, which does not offer the field as a target.
Whoever the record names, and only some choices
An entry field:<field> names whoever a person field of the record holds
(a person field, or Created by), record by record. With editorChoices, a
choice field holds only those choices for the people named: everyone else
picks the other choices, and cannot change a record that has one. That is
an approval: only the request's approver approves or declines it.
const columns = [
{ field: 'approver', header: 'Approver', fieldType: 'user' },
{ field: 'status', header: 'Status', fieldType: 'singleSelect', fieldProps: {
options: [{ value: 'Open' }, { value: 'Approved' }, { value: 'Declined' }],
editors: ['field:approver'],
editorChoices: ['Approved', 'Declined'],
} },
{ field: 'budget', header: 'Budget', fieldType: 'currency', fieldProps: { editors: ['u1', 'field:approver'] } },
]
The cell, the record view and the choice picker ask each record: the picker
leaves out the choices a person may not pick, and the lock says "Only
whoever is in Approver can choose Approved or Declined." In the base, Edit
field > Who can edit its values > Only these people lists the table's
person fields as "Whoever is in Approver", and a choice field offers "Only
for some choices". A question about the field as a whole (a board's lanes,
an import) cannot know the record, so it lets the change through to your
server, which decides. A canEditField check of your own gets the record
as its third argument.
A field's name, type and options stay editable: protection is about the values. A new record still takes its group's value, so "+" on a group still adds a record to it, and a form fills the fields its author put on it.
With roles of your own, pass a check in place of the lists. It decides for
every field of every base on the page; defaultCanEditField is the
editors check, to fall back on:
<script lang="ts">
import { SvBase, defaultCanEditField, type CanEditField } from '@svgrid/enterprise/base'
let { tables, role } = $props()
// Admins edit everything; everyone else follows each field's editors.
const canEditField: CanEditField = (field, user) => role === 'admin' || defaultCanEditField(field, user)
</script>
<SvBase bind:tables {canEditField} protectFields={role === 'admin'} editable />
The browser enforces this. A base whose records live in a database still needs the server to refuse a write to a protected field, as it would any write the person is not allowed.
Share a base read-only
For people who may look but not change anything, such as a client opening a
share link your app serves, pass readOnly:
<SvBase {tables} readOnly sortable expandRows fieldMenu />
What it closes:
- cells and the record view: no typing, pasting, fill handle, Delete or checkbox clicks, and buttons do not run automations;
- the record menu keeps Expand record and Copy; the field menu keeps Filter, Group, Hide and Color rows by this field;
- no "+ Add row", no "+" column, no "+" on a group, and the selection bar drops Edit fields and Delete;
- a board's cards do not move and its lanes add none; a calendar's or timeline's events do not drag, and a day picked adds no record;
- the tab bar has no table menu, no renaming, no Add table, no import, no trash, no undo, and no Download as SQL; it shows a "View only" badge;
- no Automations button, and scheduled automations do not run in the page;
- comments are read, and the box to write one is gone;
- form views are not shared.
Viewers still switch tables and views, search, filter, sort, group, and open
records with their activity. What they change in a view (a sort, a filter,
a new view) stays in the page while it is open, also across table switches,
and never reaches onTablesChange or a table's source.
A share on the same page as the editing base (a preview, or the demo below)
shows the comments written in the editor, with their counts on rows and
cards: comments are kept by table id and record id, not by base. The
changes in a record's activity and the unseen marks stay with the base that
noted them. On a separate page, the share sees the comments your
setRecordComments store returns; in-page comments do not travel between
browsers.
An object picks what the share shows. Every list is optional:
<SvBase
{tables}
readOnly={{
tables: ['projects', 'clients'],
views: ['all', 'board'],
hideFields: ['budget', 'clients.notes'],
}}
/>
tables and views are ids; a table none of whose listed views exists
shows its first view. hideFields takes a column id, which hides it in
every table, or tableId.columnId. The primary field always shows. A hidden
field takes along every field that would show what it holds: the formulas
that read it, a rollup or lookup of it in another table, and the lookups,
rollups and counts of a link to a table the share leaves out.
Hiding is for display. A page handed the whole base still holds the hidden
values in memory, so for anything secret build the shared copy on the server
with sharedTables(tables, share), which also takes the hidden fields'
values out of the records, and send only that.
The activity log
Every edit through the grid is noted for the record: a typed value, a paste,
a fill, a clear, a checkbox click, a card dragged to another lane, an edit in
the record view, files added in the dialog or dropped on a cell. Each note
holds the time, the current user's id, the column, the new value and the one
before it, and the record view writes it as "Ana Ruiz changed Status" over the
old value and the new one as the cell shows them, or "(cleared)". Undo and redo
are noted too, as "You undid a change to Status" and "You redid a change to
Status", so the log ends on what the record holds. A field an automation's updateRecord sets is
noted under the automation's name: "Automation: Done ticks the box changed
Done".
A change someone else made in the database, read in through a table's
realtime source, is noted too, under the name in the record's
lastModifiedBy field, with the value before it: "Triage agent changed
Priority", Low → Urgent. Until the record is opened, its changed cells are
tinted and its row number has a dot. See Changes made
elsewhere.
By default the log lives in memory and keeps the latest 50 changes of each
record, for the 20,000 records of a table changed longest ago last: a bulk edit of a
million records does not stay in the tab. In <SvBase tables> it is kept by base, table and record id, so it
survives switching tabs; with one table it is kept per grid. Changes made by
your code replacing tables or data are not noted.
A history that lasts
Your audit log already has every change. setRecordHistory lets the record
view read it, and hands it each change made in the page to keep:
import { setRecordHistory, type RecordChange } from '@svgrid/enterprise/base'
setRecordHistory({
// Each time a record is opened. A promise is fine: the activity says
// "Loading the history..." until it settles.
list: async (rowId, { tableId }) => {
const res = await fetch(`/api/history/${tableId}/${rowId}`)
return (await res.json()) as RecordChange[]
},
// Each change made in this page, as it is made.
add: (rowId, change, { tableId }) => {
void fetch(`/api/history/${tableId}/${rowId}`, { method: 'POST', body: JSON.stringify(change) })
},
})
A change is { at, by, columnId, value, from? } (at in milliseconds,
by a user id from setFieldUsers), plus automation for a change an
automation made, via for a proposal someone accepted ("CRM agent (accepted
by Ana)"), and through for a change a person made through an app as
themselves, an AI chat client say ("Ana via Claude"). The record view
shows what list gave with this session's changes it does not have yet;
give back the at that add received, and a change shows once. A change
made elsewhere and read in through realtime is not passed to add:
whoever made it keeps it.
Restore an earlier value
A change that knows what the field held before (from) has Restore
under it. It writes from back through the grid's edit path, as typing it in
the cell would: one undo step, the field's validation and protection, and a
new change in the activity. It shows only to someone who may edit that field,
and only while the field holds something else; a link to other records or a
file list is left out, since a history keeps their names, not their ids.
historyOf(ctrl, rowId) gives a record's history as the record view shows
it, for a panel of your own. setRecordHistory(null) goes back to memory.
The record menu
Right-click a record in a base's grid:
| Item | What it does |
|---|---|
| Expand record | Opens the record, with or without expandRows |
| Comment | Opens the record with the comment box focused |
| Insert record above, Insert record below | A new record beside this one with the values of the fields the view groups by, so it stays in the group; editing starts on its first field |
| Duplicate record | A copy below, with lists copied; autonumber, created and last modified fields get fresh values |
| Copy, Paste | The grid's own cell copy and paste |
| Delete record | Asks "Delete record?" ("Write the brief is deleted, and its links from other records go with it."), then removes it and fires onRowsDelete |
Without editable, only Expand record, Comment and Copy show. A new record
from the menu is made the way "+ Add row" makes one: the rowAdder defaults,
a new id, the next autonumber, the current user in created by, and onRowAdd
fired, so a table with a database writes it. The grid scrolls to it. A Kanban
card's right-click menu has Expand record and Comment, and with editable
"Move to" a lane, Duplicate record and Delete record. Passing your own
contextMenu to SvBase replaces the grid's menu.
Pitfalls
- No current user, no author. Without
setCurrentUser, new rows getnullin created by, and an edit leaves last modified by as it was, so the field names whoever edited last while signed in. - People are page-wide.
setFieldUsersandsetCurrentUserset one list and one person for every base on the page. Two bases for two teams on one page share them. - Records need real ids. Comments and the activity are keyed by row id:
each record's
id, orgetRowId. With one table a record without anidfalls back to its index, so its comments move to another record when a row above it is deleted. In<SvBase tables>every record without anidshares the id'', and with it one thread. Give every record an id. - Your store tells tables apart by id only. In memory, each
<SvBase>keeps its own comments and activity, even when two bases on one page use the same table ids. Your comment store gets the table id and nothing about the base: if two bases share one store and their table ids, key the store by something of your own as well. WithserverIds: truea new record keeps the page's id until the table loads again; re-key its comments when the insert returns. '@me'is whoever runs the filter. BeforesetCurrentUserit matches no one; the grid filters again once a user is set. In an automation's condition it is the person whose edit set it off, and on the server runner, where nobody is signed in, it matches no one.- A URL's type is its extension. Under
accept: 'image/*'a URL with no extension is refused, and a URL's size is checked only when it is known. The browser checks are for the user; check types and sizes on the server too. - Storage keeps what the cell drops. Removing a file, Cancel after an upload finished, or deleting the record leaves the object in the bucket. Clean up on the server, for example files no record has pointed at for a day.
fieldProps.uploaddoes not survive JSON. A function in a column is lost when you store columns in a database throughonTablesChange. UsesetAttachmentUploaderand branch oncolumnIdinstead.
Next
- Linked records: tasks that point at milestones, people that point at teams, and the records that link back.
- Formulas: fields worked out from other fields.
- Views: grids, boards, calendars and galleries over the same records.
- Forms: let people outside the team add records.
- Your own field type: a cell of your own, with the
grid's
ctrlto open records from it. - Automations: tell the assignee when a task is blocked.
Live examples
- Hiring tracker - A hiring tracker, ready to use: Candidates and Roles as two linked tables. Candidates have photo, role, stage, recruiter, rating, interview date and resume; each role lists its candidates. Views for the pipeline as a Kanban, all candidates, a gallery of faces, interviews and an application form.
- @mentions and notifications - Comments that mention people. Type @ in a record's comment box to pick someone; the mention shows as a chip with their name. The comment store's onMention hook fills each person's inbox, as an app would send an email, and Signed in as switches whose inbox you read.
- Personal views, locked views, protected fields - A personal view shows only for the person it belongs to, under My views; a locked view keeps its filter, sorts, groups and colors and says who locked it, while records stay editable; a protected field takes edits only from the people it lists. Switch who is signed in to see each person's own views, who may lock them, and who may set Priority.
- Read-only share - The same base as the team edits it and as a client sees it through a share link. readOnly turns off every way to change it: cells, records, card and calendar drags, the record and field menus, import, trash, undo and the comment box. Viewers still switch tables and views, search, sort and filter, and nothing they change reaches onTablesChange. A share can hide fields, and the formulas and rollups that read them go too.