Subjects & Annotations
An annotation surface — a feedback button, a comment widget, a review prompt — has to say what it is about. The client already knows: it fetched the record.
import { createFoirClient, createFoirSubjectRegistry } from '@foir/sdk/data';
import * as generated from './foir.generated';
const subjects = createFoirSubjectRegistry();
const foir = createFoirClient({ apiUrl, apiKey, generated, subjects });
await foir.article.findUnique({ naturalKey: 'about-us' });
subjects.subject();
// { modelKey: 'article', recordId: 'kx7p2m9q4wr8n3vt6ybc1zjd' }That is the whole setup. No route table, no per-page configuration, and nothing to keep in step when your URLs change.
Why the record and not the URL
Every other way of anchoring an annotation breaks on something you will do anyway:
| Anchor | Breaks when |
|---|---|
| A URL | you change your routes, add a locale prefix, or the page is reachable at two addresses |
| A CSS selector | you redesign |
| A rendered heading | you edit the copy |
| The record | never — it is the thing the page is about |
The record survives a redesign, a URL change and a locale variant, because none
of those change which record was read. A comment filed against
article/kx7p2m9q4wr8n3vt6ybc1zjd also lands in the notes panel your team opens
when they edit that article, which is the point: the annotation and the content
are already in the same place.
It is off unless you build one
There is no subjects: true. You construct a registry and pass it, or the
client records nothing.
A list of the records a page fetched is worth being deliberate about. It names content by model key and id — including records the reader never saw rendered: a draft pulled for a preview, a related record fetched and filtered out in the template, the twelve nav pages behind a menu. Whether anything else running on that page can read that list is your call, not the SDK’s.
The same reasoning gives you the one rule to follow: build a registry per client, never at module scope. A registry shared at module scope on an edge runtime is shared across every request the isolate serves, which on a multi-tenant deployment means across tenants.
What gets recorded
Anything the client fetched that the API describes as a record.
// One record, addressed by name.
await foir.article.findUnique({ naturalKey: 'about-us' });
// Twelve records, in a list.
await foir.page.findMany({ first: 12 });
// A record carried inside a search result.
await foir.ns.search.searchRecords({ query: 'returns' });All three record. What separates them is how the record was reached, and that
decides what subject() answers with.
subject() — the one to annotate
subject() returns the last record you addressed by name. A record that only
ever appeared inside a list is never promoted to being the subject.
await foir.page.findMany({ first: 12 }); // nav
await foir.article.findUnique({ id: 'kx7p' }); // the page's own content
subjects.subject(); // the article
subjects.fetched(); // the article and all twelve pagesSo a page that renders from a record gets its subject for free, and a page whose
data all came out of lists gets undefined — which is the honest answer. Twelve
nav entries and no subject is true; picking one of the twelve is not.
For those pages, either offer the reader a choice from fetched() or say what
the subject is:
subjects.setSubject({ modelKey: 'article', recordId: 'kx7p2m9q4wr8n3vt6ybc1zjd' });
subjects.setSubject(null); // back to whatever was fetchedfetched() — everything, in order
Every record recorded so far, de-duplicated, least-recently-fetched first. Use it to let the reader pick what they are commenting on.
Labels
A widget reads better saying “About Us” than article/kx7p…. Tell the registry
which field to read:
createFoirSubjectRegistry({
label: (record) => (typeof record.title === 'string' ? record.title : null),
});There is no default that reaches into your fields, and that is deliberate. Your
project decides whether the human-readable field is title, name, headline
or something else — so a client that picked title when it saw one would label
most projects correctly and quietly mislabel the rest, in the one string a reader
actually reads.
Return null for “no label”: the subject is still recorded, without one. The
function must not throw — see Failures are not swallowed.
Where a record carries _naturalKey and you have given no labeller, that is used
instead. It is the platform’s own human-addressable name for a record, so reading
it is not a guess about your schema.
In a single-page app: clear on navigation
A registry accumulates. In an app that navigates without a page load, call
clear() when the route changes — otherwise subject() keeps answering with a
record from a page the reader has left.
router.subscribe('onLoad', () => subjects.clear());clear() forgets the recorded records and any override, which is what a new
page means.
Reacting to it
subscribe() runs a listener after every change and returns the function that
stops it — for a widget mounted at app root, before any data has resolved.
useEffect(() => subjects.subscribe(() => setSubject(subjects.subject())), []);Server rendering, and raw()
Two cases the data path cannot see, and one call that covers both.
A record read on the server never passed through the browser’s client, so seed it where you hydrate:
subjects.add({ modelKey: 'article', recordId: article._id, label: article.title });foir.raw() also records nothing. A hand-written document has no published
result shape, so there is nowhere for the client to know a record is — seed those
the same way.
Failures are not swallowed
If your label function or a subscribe listener throws, the call you made
raises an error saying so, and saying that the request itself succeeded.
That is on purpose. A subject that silently failed to record does not leave the widget with nothing to annotate — it leaves the previous subject standing, so the widget offers to comment on the record the reader was looking at a moment ago, and the feedback is filed against the wrong page. A loud failure in a function you wrote is better than that.
Every listener still runs before the error is raised, so one bad subscriber does not mute the rest.
Reference
interface FoirSubject {
modelKey: string;
recordId: string;
label?: string;
}
interface FoirSubjectRegistry {
add(entry: FoirSubjectEntry): void;
fetched(): readonly FoirSubject[];
subject(): FoirSubject | undefined;
setSubject(subject: FoirSubject | null): void;
clear(): void;
subscribe(listener: () => void): () => void;
field(subject: FoirSubject, field: string, options?: { label?: string }): FoirFieldMarkAttributes;
pointedAt(token: string): FoirFieldMark | undefined;
}field and pointedAt mark an element as showing one field of a record, or with { node: entry._id } one entry of a prose field, for a widget that lets the reader point at what they mean. RichTextRenderer and a content renderer mark themselves, given a field. See Marking fields and entries.
modelKey and recordId are spelled the way record-change
events spell them, so a subject and an event are the
same pair and there is nothing to convert between them.
Writing the annotation
Foir has a place to put it. A signed-in user can write notes against a
record through foir.ns.notes, and they land in the notes panel your team
already triages — no second system, no webhook, no separate inbox.
const subject = subjects.subject();
if (subject) {
await foir.ns.notes.writeNote({
subjectType: 'record',
subjectId: subject.recordId,
subjectModelKey: subject.modelKey,
body: feedback,
});
}See Notes & Comments for the scopes and the user
role this needs, and Operation Reference for the
notes namespace.
If you would rather not build the form, <FoirFeedback> is
that code already written: it takes this registry and the client, resolves the
subject exactly as above, and says so honestly when there is not one.