Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 339x 339x 339x 339x 339x 648x | import { ViewMode } from "@/components/buttons/ViewToggle/ViewToggle";
import {
CollectionView,
CollectionViewProps,
EmptyStateProps,
SectionHeading,
} from "@/components/collection/CollectionView";
import { ResourceCollectionSection } from "@/components/collection/sections";
import StatsBar, { CollectionStat } from "@/components/collection/StatsBar";
import Toolbar, {
SearchInputProps,
} from "@/components/collection/Toolbar";
import React from "react";
/**
* One resource page's body, end to end: intro copy → stats strip → search /
* filter / action toolbar → grid or list, with skeletons and empty states.
*
* `StatsBar`, `Toolbar` and `CollectionView` were already shared, but each
* page re-assembled them by hand and the assembly is where they drifted —
* spacing, whether "Shown" appears while filtering, whether the empty state
* offers the create action, whether the description sits above or below the
* stats. This owns the arrangement so a page only supplies what is genuinely
* its own: its stats, its filters, its cards.
*
* Everything nuanced stays the page's call — the filter controls are
* children, the card and row renderers are callbacks, `summary` holds an
* overview of the page's own between the stats and the toolbar, `sections`
* splits the body where a resource has a meaningful grouping (yours vs. the
* platform's), and any part can be omitted for a resource that has no use
* for it.
*/
/** Everything both forms share: the header, the toolbar, and the rendering
* defaults a single body or every section inherits. */
interface ResourceCollectionCommon<T>
extends Omit<CollectionViewProps<T>, "items" | "itemKey" | "empty"> {
/** Intro copy explaining what the resource is and when to reach for it. */
description?: React.ReactNode;
/** Summary strip. Omit for a collection with nothing worth counting. */
stats?: CollectionStat[];
/** Rendered between the stats and the toolbar: a page's own overview of
* its collection that the stats strip cannot say — the Rules tab's layer
* flow, where each step counts its rules and filters to them. */
summary?: React.ReactNode;
/** Post-filter count, rendered as a trailing "Shown" stat. Pass the
* filtered length and it appears only while filters are active. */
shown?: number;
search?: SearchInputProps;
/** Filter/sort controls, rendered after the search input. */
filters?: React.ReactNode;
activeFilterCount?: number;
onClear?: () => void;
onViewChange?: (view: ViewMode) => void;
/** Right-aligned toolbar actions — a `GenerateButton` then a
* `CreateButton`, by convention, so the primary action sits nearest the
* view toggle on every page. */
actions?: React.ReactNode;
/** Rendered between the toolbar and the body — a banner, a callout. */
children?: React.ReactNode;
}
/** One body (the common case) or several titled groups (yours vs. the
* platform's). A single body needs its own empty state; grouped ones each
* carry theirs. */
export type ResourceCollectionProps<T> = ResourceCollectionCommon<T> &
(
| {
items: T[];
itemKey: (item: T) => string;
empty: EmptyStateProps;
sections?: never;
}
| {
/** Wrap each entry in `collectionSection` so groups of different
* types can sit side by side. */
sections: ResourceCollectionSection<unknown>[];
items?: never;
itemKey?: never;
empty?: never;
}
);
export function ResourceCollection<T>({
description,
stats,
summary,
shown,
search,
filters,
activeFilterCount = 0,
onClear,
view,
onViewChange,
actions,
items,
itemKey,
empty,
sections,
children,
...viewProps
}: ResourceCollectionProps<T>) {
const { renderCard, renderRow } = viewProps;
const filtered = viewProps.filtered ?? activeFilterCount > 0;
// The grid/list toggle only earns its place when there are two views to
// switch between: a collection with rows alone (or cards alone) would
// otherwise show a control that changes nothing. Pages pass the whole of
// `useCollectionFilters().toolbarProps` regardless, so the decision lives
// here rather than in every page.
// A section may bring its own renderers or inherit the shared ones.
const first = sections?.[0];
const switchable =
!!(first?.renderCard ?? renderCard) && !!(first?.renderRow ?? renderRow);
return (
<div className="space-y-4">
{description && (
<p className="text-sm text-gray-500 dark:text-gray-400 leading-relaxed">
{description}
</p>
)}
{stats && stats.length > 0 && (
<StatsBar stats={stats} shown={filtered ? shown : undefined} />
)}
{summary}
{(search || filters || actions || (switchable && view && onViewChange)) && (
<Toolbar
search={search}
activeFilterCount={activeFilterCount}
onClear={onClear}
view={switchable ? view : undefined}
onViewChange={switchable ? onViewChange : undefined}
actions={actions}
>
{filters}
</Toolbar>
)}
{children}
{sections
? sections.map(
({ label, hint, count, action, hideWhenEmpty, ...section }) =>
(!hideWhenEmpty ||
viewProps.loading ||
section.items.length > 0) && (
<div key={label}>
<SectionHeading
label={label}
hint={hint}
count={count ?? section.items.length}
action={action}
/>
<CollectionView
{...(viewProps as CollectionViewProps<unknown>)}
{...section}
view={view}
filtered={filtered}
/>
</div>
),
)
: items &&
itemKey && (
<CollectionView
{...(viewProps as Omit<CollectionViewProps<T>, "items" | "itemKey">)}
items={items}
itemKey={itemKey}
empty={empty}
view={view}
filtered={filtered}
/>
)}
</div>
);
}
export default ResourceCollection;
|