Related items panel
The records linked to a record, with a picker to add or remove a link. It loads its data on the server through the block's client.
pnpm dev:example) to see it.Add the components
terminal npx shadcn add badge button card empty field selectInstall the glue
Install the block with
npx better-supabase add relations. The glue items are copied into your app, so edit them as you need.None: the page calls the block's client on the server.
Write the component
React and shadcn
View in the examplerelated-items-panel.tsx /** * Records linked to this one (the `relates` kind), and a picker to link more of * its type. */ export function RelatedItemsPanel({ subject, relations, canManage, }: { subject: RecordSubject; relations: Loaded<Relations>; canManage: boolean; }) { const t = useExtracted("records"); return ( <Card size="sm" data-recipe="related-items-panel"> <CardHeader> <CardTitle>{t("Related records")}</CardTitle> <CardDescription> {t("Link records that belong together, such as a parent company.")} </CardDescription> </CardHeader> <CardContent className="flex flex-col gap-4"> {relations.kind === "error" ? ( <RecordLoadError title={t("The related records didn't load")} /> ) : ( <> {relations.data.links.length === 0 ? ( <Empty className="border"> <EmptyHeader> <EmptyMedia variant="icon"> <LinkIcon /> </EmptyMedia> <EmptyTitle>{t("No related records")}</EmptyTitle> <EmptyDescription> {canManage ? t("Pick a record below to link it.") : t("Nobody linked a record yet.")} </EmptyDescription> </EmptyHeader> </Empty> ) : ( <RelatedList subject={subject} links={relations.data.links} canManage={canManage} /> )} {canManage ? ( <LinkForm subject={subject} options={relations.data.options.filter( (option) => !relations.data.links.some( (link) => link.subjectType === subject.type && link.subjectId === option.id, ), )} /> ) : null} </> )} </CardContent> </Card> ); } function RelatedList({ subject, links, canManage, }: { subject: RecordSubject; links: readonly RelatedRecord[]; canManage: boolean; }) { const t = useExtracted("records"); const errorMessage = useErrorMessage(); const unlink = useAction(recordActions[subject.type].unlink, { onSuccess: () => { toast.success(t("Link removed")); }, onError: (error) => { toast.error(errorMessage(error)); }, }); return ( <ul className="flex flex-col divide-y" data-testid="related-records"> {links.map((link) => ( <li key={link.id} className="flex items-center justify-between gap-2 py-2" > <div className="flex min-w-0 items-center gap-2"> <SubjectLink subject={{ type: link.subjectType, id: link.subjectId }} /> {link.kind === "relates" ? null : ( <Badge variant="outline">{link.kind}</Badge> )} </div> {canManage ? ( <Button size="icon-sm" variant="ghost" aria-label={t("Remove link")} disabled={unlink.pendingInputs.some( (input) => !(input instanceof FormData) && input.input.target.id === link.subjectId, )} onClick={() => { void unlink.run({ id: subject.id, input: { kind: link.kind, target: { type: link.subjectType, id: link.subjectId }, }, }); }} > <UnlinkIcon /> </Button> ) : null} </li> ))} </ul> ); } function LinkForm({ subject, options, }: { subject: RecordSubject; options: readonly { readonly id: string; readonly name: string }[]; }) { const t = useExtracted("records"); const fieldId = useId(); const errorMessage = useErrorMessage(); const [targetId, setTargetId] = useState<string | null>(null); const link = useAction(recordActions[subject.type].link, { onSuccess: () => { setTargetId(null); toast.success(t("Record linked")); }, }); const items = options.map((option) => ({ value: option.id, label: option.name, })); if (items.length === 0) { return null; } return ( <form className="flex items-end gap-2" onSubmit={(event) => { event.preventDefault(); if (targetId) { void link.run({ id: subject.id, input: { kind: "relates", target: { type: subject.type, id: targetId }, }, }); } }} > <Field data-invalid={link.error ? true : undefined}> <FieldLabel htmlFor={`${fieldId}-related`}> {t("Link a record")} </FieldLabel> <Select value={targetId} items={items} onValueChange={(value) => { setTargetId(value); }} > <SelectTrigger id={`${fieldId}-related`} className="w-full"> <SelectValue placeholder={t("Pick a record")} /> </SelectTrigger> <SelectContent> {items.map((item) => ( <SelectItem key={item.value} value={item.value}> {item.label} </SelectItem> ))} </SelectContent> </Select> {link.error ? ( <FieldError>{errorMessage(link.error)}</FieldError> ) : null} </Field> <Button type="submit" variant="outline" disabled={!targetId || link.pending} > {t("Link")} </Button> </form> ); } /** The related customers card while its rows load. */ export function RelatedItemsPanelSkeleton() { return <RecordCardSkeleton rows={2} />; }