The states round a screen's data
Loading, failed, first run, nothing found
Copy the file below whole (src/screens/states.tsx) beside frame.tsx (docs/screen-frame.md), add the frame block's row to the frame's NAVIGATION before Settings (node goop/agent/start.mjs states does it), and keep the parts and their props. Each state is a part your other screens import (import { LoadFailed, LoadingRows } from './states'); the default export, a page of the crew's sites at /sites, wires them to one load: the rows load, the first run shows how to begin, a search that leaves nothing says so. The first run is the reference's empty home, as Meadow's Empty variant="page" draws it (the centred column, a large title and a sentence, an outline card of how-to rows, the dark button over the outline one); the other states are built from Meadow's parts. The sample answers with no site (so you see the first run) after a moment, fetching nothing: put your fetch in loadSites. failFirst is a sample switch only: set on the screen (<SitesScreen failFirst />), the first load fails, showing the failed state and Retry; remove it, and a real failure shows the same. The states are the parts' own looks, light and dark, phone and desktop: add no class for them.
Which one
| When | The state | What it shows | | --- | --- | --- | | Rows loading (a list, a card's rows) | LoadingRows | the card list's rows as grey placeholders (Skeleton variant="row"), inside a SkeletonContainer so a fast answer never flashes them and the rows fade in over them | | The data did not load | LoadFailed | a red Alert in the data's place, its Retry running the load again | | Nothing made yet (a first visit, a new workspace) | FirstRun | the empty home: a title, a sentence, a card of how-to rows, then its buttons | | A search or filter leaves nothing, though records exist | NothingFound | the outline row: what found nothing, and Show all; no picture, no how-to card | | An address leads nowhere (an old link, a deleted record) | none here | App.tsx's "No page here" and the record's "No note here" (docs/screen-app.md, docs/screen-record.md) | | One action running (a save, a send) | none here | Button loading: its spinner over its words | | The app is offline or read-only | none here | one Banner first in the panel, its kind warning (Banner's entry in EXAMPLES.md) | | An empty card or section in a filled page | none here | CardList empty, or the bare Empty slot (the overview's sections) |
No record at all: the first run; records the search hides: nothing found, never the one for the other. A failure replaces the rows, never a toast alone: the data's place says why it is empty.
Show the code244 lines
// src/screens/states.tsx: the states round a screen's data, and the sites page that shows them
import { type ReactNode, useEffect, useState } from 'react';
import { AppFramePanel } from '@/components/goop/patterns/app-frame';
import { PageBody, pageRoles } from '@/components/goop/patterns/page-body';
import { Alert, AlertAction, AlertDescription, AlertIcon, AlertTitle } from '@/components/ui/goop/alert';
import { Button } from '@/components/ui/goop/button';
import { Card, CardDescription, CardMedia, CardText, CardTitle } from '@/components/ui/goop/card';
import { CardList } from '@/components/ui/goop/card-list';
import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyTitle } from '@/components/ui/goop/empty';
import { IconTile, IconTileIcon } from '@/components/ui/goop/icon-tile';
import {
PageHeader,
PageHeaderContent,
PageHeaderDescription,
PageHeaderTitle,
} from '@/components/ui/goop/page-header';
import { SearchInput } from '@/components/ui/goop/search-input';
import { Skeleton, SkeletonContainer, SkeletonMedia } from '@/components/ui/goop/skeleton';
import { Glyph, type IconName } from '@/lib/goop/meadow/glyph';
import { ScreenFrame, ScreenTopBar } from './frame';
// ---- the states: your screens import them
/** Rows still loading: the card list's rows as placeholders, as many as the rows that will come. */
export function LoadingRows({ count = 3, icon = 'file-text' }: { count?: number; icon?: IconName }) {
return (
<div>
{Array.from({ length: count }, (_, at) => (
// biome-ignore lint/suspicious/noArrayIndexKey: the placeholders have no identity of their own
<Skeleton key={at} variant="row" shape={at === 0 ? 'rounded-top' : 'square'}>
<SkeletonMedia>
<Glyph icon={icon} />
</SkeletonMedia>
</Skeleton>
))}
</div>
);
}
/** The data did not load: said in its place, with the way to try again. */
export function LoadFailed({ what, onRetry }: { what: string; onRetry: () => void }) {
return (
<Alert variant="destructive">
<AlertIcon />
<AlertTitle>Couldn’t load {what}</AlertTitle>
<AlertDescription>Check your connection, then try again.</AlertDescription>
<AlertAction>
<Button size="sm" onClick={onRetry}>
Retry
</Button>
</AlertAction>
</Alert>
);
}
/** One how-to row of a first run: its icon, what to do and why. */
export interface FirstStep {
icon: IconName;
title: string;
line: string;
}
/**
* Nothing made yet: the whole view, the title (the page's one `h1`) and a sentence, the how-to rows in an outline card,
* then `children`, the view's buttons (the main one `variant="inverse" size="lg"`, another `variant="outline"`).
*/
export function FirstRun({
title,
line,
steps,
children,
}: {
title: string;
line: string;
steps: readonly FirstStep[];
children: ReactNode;
}) {
return (
<Empty variant="page">
<EmptyHeader>
<EmptyTitle asChild>
<h1>{title}</h1>
</EmptyTitle>
<EmptyDescription>{line}</EmptyDescription>
</EmptyHeader>
<Card variant="outline">
<CardList aria-label={`How ${title.toLowerCase()} work`}>
{steps.map((step) => (
<li key={step.title}>
<CardMedia>
<IconTile>
<IconTileIcon icon={step.icon} />
</IconTile>
</CardMedia>
<CardText>
<CardTitle>{step.title}</CardTitle>
<CardDescription>{step.line}</CardDescription>
</CardText>
</li>
))}
</CardList>
</Card>
<EmptyContent>{children}</EmptyContent>
</Empty>
);
}
/** A search or a filter left nothing, though there are records: what found nothing, and Show all, the way back to them. */
export function NothingFound({ words, onClear }: { words: string; onClear: () => void }) {
return (
<Empty variant="outline">
<EmptyHeader>
<EmptyTitle>Nothing matches “{words}”</EmptyTitle>
<EmptyDescription>Try other words, or see them all.</EmptyDescription>
</EmptyHeader>
<EmptyContent>
<Button variant="ghost" size="row" onClick={onClear}>
Show all
</Button>
</EmptyContent>
</Empty>
);
}
// ---- the sample data: swap it for yours
interface Site {
id: string;
name: string;
line: string;
icon: IconName;
}
const SAMPLE_SITES: readonly Site[] = [
{ id: 'north-hedge', name: 'North hedge', line: 'Along the top field, 400 m of new planting', icon: 'map-2' },
{ id: 'riverside', name: 'Riverside orchards', line: 'Twelve rows of apple and pear', icon: 'map-2' },
{ id: 'greenhouse', name: 'Greenhouse', line: 'Benches along the south wall', icon: 'home' },
{ id: 'lower-beds', name: 'Lower beds', line: 'Six beds, limed before the spring sowing', icon: 'map-2' },
];
const FIRST_STEPS: readonly FirstStep[] = [
{ icon: 'map-2', title: 'Add a site', line: 'A field, an orchard or a bed the crew looks after.' },
{ icon: 'calendar-plus', title: 'Plan its visits', line: 'Each visit lands on the calendar.' },
{ icon: 'users', title: 'Share it with the crew', line: 'Everyone sees the same notes.' },
];
/**
* Loads the sites: your fetch goes here. The sample answers after a moment with none (a first visit), fetching nothing;
* with `fail`, it refuses instead (the failed state).
*/
function loadSites(fail: boolean): Promise<readonly Site[]> {
return new Promise((resolve, reject) => {
setTimeout(() => (fail ? reject(new Error('The sites did not load')) : resolve([])), 600);
});
}
// ---- the page
/** The crew's sites: one load and every state round it. `failFirst` is the sample's switch: the first load fails. */
export default function SitesScreen({ failFirst = false }: { failFirst?: boolean }) {
const [attempt, setAttempt] = useState(0);
// null while loading; the rows once they come
const [sites, setSites] = useState<readonly Site[] | null>(null);
const [failed, setFailed] = useState(false);
const [query, setQuery] = useState('');
useEffect(() => {
let shown = true;
setSites(null);
setFailed(false);
loadSites(failFirst && attempt === 0).then(
(rows) => {
if (shown) setSites(rows);
},
() => {
if (shown) setFailed(true);
},
);
return () => {
shown = false;
};
}, [attempt, failFirst]);
const retry = () => setAttempt((count) => count + 1);
const words = query.trim().toLowerCase();
const found = (sites ?? []).filter((site) => `${site.name} ${site.line}`.toLowerCase().includes(words));
return (
<ScreenFrame>
<AppFramePanel header={<ScreenTopBar title="Sites" />}>
{sites?.length === 0 ? (
<FirstRun title="Sites" line="Each place the crew plants, prunes and checks, with its visits." steps={FIRST_STEPS}>
<Button variant="inverse" size="lg" onClick={() => setSites(SAMPLE_SITES)}>
Add the sample sites
</Button>
<Button variant="outline" size="lg" onClick={retry}>
Check again
</Button>
</FirstRun>
) : (
<PageBody variant="wide">
<PageHeader>
<PageHeaderContent>
<PageHeaderTitle asChild>
<h1>Sites</h1>
</PageHeaderTitle>
<PageHeaderDescription>Each place the crew plants, prunes and checks.</PageHeaderDescription>
</PageHeaderContent>
</PageHeader>
{failed ? (
<LoadFailed what="the sites" onRetry={retry} />
) : (
<SkeletonContainer loading={sites === null} skeleton={<LoadingRows count={4} icon="map-2" />}>
<div className={`flex flex-col ${pageRoles.block}`}>
<SearchInput
label="Search sites"
placeholder="Search"
value={query}
onChange={(event) => setQuery(event.currentTarget.value)}
/>
{found.length === 0 ? (
<NothingFound words={query.trim()} onClear={() => setQuery('')} />
) : (
<Card variant="outline">
<CardList aria-label="Sites">
{found.map((site) => (
<li key={site.id}>
<CardMedia>
<IconTile>
<IconTileIcon icon={site.icon} />
</IconTile>
</CardMedia>
<CardText>
<CardTitle>{site.name}</CardTitle>
<CardDescription>{site.line}</CardDescription>
</CardText>
</li>
))}
</CardList>
</Card>
)}
</div>
</SkeletonContainer>
)}
</PageBody>
)}
</AppFramePanel>
</ScreenFrame>
);
}`tsx frame { words: 'Sites', path: '/sites', icon: 'map-2' },