Concrete
GuidesSource a value

Compute a value from facts

The value you need is a function of the property's facts and no single field holds it.

Goal

Derive a slot's value from the property — a price label that changes for a rental, a one-line summary of the unit mix — without asking anyone to type it.

Prerequisites

  • The workspace Start leaves you with.
  • Facts and provenance for why a derivation is a source rather than a fill, and why an edit to it stays on the page.

Steps

1. Declare what you read, then name the function

compute({ reads }, fn) lists the facts the derivation needs and hands the callback one value per ref, in that order. Write the derivation as an exported function of those values and pass the name. An inlined arrow works and is a dead end: it is unreachable from a test, so the one part of your section with real logic in it is the part nothing grades.

sections/sheet.tsx
import { compute, fact, text } from "@homepages/concrete";

export const priceLine = (intent: string | null): string =>
  intent === "for_rent" ? "Per month" : "Asking price";

const label = text.short({
  label: "Price label",
  source: compute({ reads: [fact.property.listing_intent] }, priceLine),
});

compute is a source:, so the slot admits no fill beside it — a slot has one source:. A derivation holds the slot's own value, so a user's edit overrides it on this page and writes nothing back to the facts it reads.

The callback sees exactly what reads lists and nothing else, and those refs are also what the section asks of the property: a fact the callback reads is a fact it declared. A derivation that reads nothing is a constant — compute(() => "Asking price") — called with no arguments.

2. Name the refs with the spellings you already know

A ref is any fact.* entry, and one namespace serves both axes: what a slot BINDS and what a derivation READS are the same handles. Some entries are read-only — the property's media lists, fact.property.uploads, fact.property.property_media and fact.property.floor_plans — legal in any reads and never as a source:.

One row of a table is read through a row(), and its members are refs like any fact: const agent = row({ label: "Agent", from: fact.property.contacts }), then agent.name in the reads of a slot of the same section — beside the row in its object(), or anywhere else in the section when the row is a top-level slot or a top-level object's field.

Each ref hands the callback its own value type: a scalar fact its nullable value, a table its rows. The row types are exported — FactsUnit, FactsCommercialSpace, FactsContact and FactsComparable — so a named helper can say what it takes, and an address reads as an AddressValue. A table's row is what the property holds: its id, each member under its own name, null where unanswered, an image as its asset's record.

The property's media comes in three lists, each read as readonly Media[] in the list's order: fact.property.property_media holds what shows the property — its photos, videos and tours — fact.property.floor_plans its floor plans, and fact.property.uploads every upload it holds, documents included. Each item is its asset's record, one shape for every kind, narrowed by kind to ImageMedia, VideoMedia, TourMedia or DocumentMedia. A record carries the upload's kind, its name, and what was learned about it, each null when unknown — every member is listed under compute. The subset you want is the list that holds it, filtered by kind in the callback:

sections/sheet.tsx
import { type Media, compute, fact, text } from "@homepages/concrete";

const isPhoto = (asset: Media): boolean => asset.kind === "image";

const mediaLine = text.short({
  label: "Media line",
  cap: 60,
  source: compute({ reads: [fact.property.property_media] }, (media) => {
    const photos = media.filter(isPhoto).length;
    const tours = media.filter((asset) => asset.kind === "tour").length;
    return tours > 0 ? `${String(photos)} photos · 3D tour` : `${String(photos)} photos`;
  }),
});

The floor plans are a read of fact.property.floor_plans; the documents a filter on kind over fact.property.uploads.

3. Handle absence, every time

A row's members are nullable — a property may carry no unit and no contact card — and the property may hold no photos, videos or documents at all. A derivation that assumes otherwise throws under the render stage over the first fixture that lacks one:

sections/sheet.tsx
import { compute, fact, type FactsUnit, text } from "@homepages/concrete";

export const unitSummary = (homes: readonly FactsUnit[]): string => {
  if (homes.length === 0) return "";
  const beds = homes.map((home) => home.beds).filter((value): value is number => value !== null);
  return beds.length === 0 ? `${String(homes.length)} homes` : `${String(homes.length)} homes · from ${String(Math.min(...beds))} bed`;
};

const summary = text.short({
  label: "Unit summary",
  cap: 60,
  source: compute({ reads: [fact.property.units] }, unitSummary),
});

Returning "" for "nothing to say" is the ordinary empty value — the slot still renders its marked element, so the editor keeps the field and the page keeps its shape.

4. Stay inside what a logic bundle may do

The callback is compiled into the section's logic bundle, which runs at derive time and carries no React. Three constraints follow, each with its own rule:

  • Synchronous. An asynchronous callback collapses to an unassignable return type and fails to compile, rather than running async at derive time.
  • No React. Reaching a component or a hook through the closure fails the bundle — logic/section-build-failed.
  • No clock, no random source. The derivation must produce the same value twice from the same facts (server/no-nondeterminism). A year is not a property fact and no ref reads one: render a copyright year from a "use client" island instead, which that rule exempts. The island still server-renders, so the published HTML carries the year and a reader with JavaScript off sees one.

The return type is checked against the slot's own value type, so a computation returning the wrong shape is a compile error rather than a rendered [object Object].

5. Reach for it on a display entry too

A section's visibility or variant axis takes the same binding, and this is where a derivation usually beats a model: when a fact already answers, deriving is free, deterministic and explainable.

sections/sheet.tsx
import { compute, fact, select } from "@homepages/concrete";

const visibility = select({
  from: ["visible", "hidden"],
  source: compute({ reads: [fact.property.property_media] }, (media) =>
    media.some((asset) => asset.kind === "image") ? "visible" : "hidden",
  ),
});

6. Read another slot, or mirror one

reads takes slots as well as facts. Another section's slot is a ref off its value (Hero.slots.title); a slot of this same section is its own const, hoisted out of the slots record and named directly. A slot that should simply HOLD another's value names it as its source: — a mirror, with no callback at all:

sections/sheet.tsx
import { compute, fact, fill, text } from "@homepages/concrete";

const headline = text.medium({
  label: "Headline",
  cap: 80,
  source: fill.write({ reads: [fact.property.description], brief: "One confident, specific sentence on what makes the property worth seeing." }),
});

const eyebrow = text.short({
  label: "Eyebrow",
  cap: 80,
  source: compute({ reads: [headline] }, (value) => value?.toUpperCase() ?? ""),
});

const footerLine = text.medium({ label: "Footer line", cap: 80, source: headline });

A slot read is | undefined — the upstream may be empty — and a mirror must sit at its upstream's value type (relations/mirror-type). A mirror is the upstream on this page, so an edit to the footer line edits the headline; one seeded from the headline but free to differ is a compute() over it, as the eyebrow is (where an edit goes). The full contract is sources.

7. Render it like anything else

sections/sheet.tsx
import { compute, fact, Section, section, Slot, text } from "@homepages/concrete";

export const priceLine = (intent: string | null): string =>
  intent === "for_rent" ? "Per month" : "Asking price";

export const Sheet = section(
  {
    key: "sheet",
    label: "Fact sheet",
    slots: {
      price_label: text.short({
        label: "Price label",
        source: compute({ reads: [fact.property.listing_intent] }, priceLine),
      }),
    },
  },
  ({ slot }) => (
    <Section>
      <Slot.Text slot={slot.price_label} as="p" />
    </Section>
  ),
);

A computed slot is still a slot: render it or render/missing-slot-marker fires. A fixture may not state its value — it is resolved from the property, so the way to change it is to change the property or write a second fixture on another one.

Verify

Run concrete dev and select the slot: its card carries the computed provenance chip and no Update everywhere toggle. Switch to a fixture on a rental property and the label changes — the derivation is the proof, not the value.

Then run concrete check. The logic stage builds the bundle exactly as concrete build does, and the render stage renders every instance twice and compares:

concrete check
concrete check passed: 13 stages.

On this page