# Layout and Page Patterns This document describes the page layout patterns used in konflux-ui and how to compose new pages. ## Page Types Every page in the application follows one of four patterns: 1. **List Page** -- Table with filters, toolbar, and empty states 2. **Detail Page** -- Tabs, breadcrumbs, actions dropdown 3. **Form Page** -- Create/edit form with validation and submission 4. **Modal Workflow** -- Dialog-based operations (delete, confirm) ## Layout Components ### `PageLayout` -- List and Form Pages Location: `src/components/PageLayout/PageLayout.tsx` Used for pages without tabs (list views, form pages). Renders a title bar with optional actions. ```tsx import PageLayout from '~/components/PageLayout/PageLayout'; navigate(createPath), disabled: !canCreate, disabledTooltip: 'You do not have permission to create secrets', }, ]} footer={} {/* Optional: for form pages */} > {children} ``` ### `DetailsPage` -- Detail Pages with Tabs Location: `src/components/DetailsPage/DetailsPage.tsx` Used for resource detail views. Renders breadcrumbs, title, actions dropdown, and router-synced tabs. ```tsx import DetailsPage from '~/components/DetailsPage/DetailsPage'; {resourceName} } breadcrumbs={[ ...applicationBreadcrumbs, { path: listPath, name: 'My Resources' }, { path: '#', name: resourceName }, ]} actions={[ { key: 'edit', label: 'Edit', onClick: handleEdit }, { type: 'separator', key: 'sep-1', label: '' }, { key: 'delete', label: 'Delete', onClick: handleDelete, isDisabled: !canDelete, disabledTooltip: 'You do not have permission to delete this resource', }, ]} tabs={[ { key: '', label: 'Overview', isFilled: true }, // index route { key: 'pipelineruns', label: 'Pipeline runs', isFilled: true }, { key: 'activity', label: 'Activity', partial: true }, // matches /activity/* ]} baseURL={baseURL} />; ``` **Action type for DetailsPage:** ```ts type Action = { key: string; label: React.ReactNode; onClick?: () => void; component?: React.ReactElement; // For Link-based actions isDisabled?: boolean; disabledTooltip?: React.ReactNode; type?: 'separator' | 'section-label'; // Structural items hidden?: boolean; // Conditionally hide }; ``` **Tab type:** ```ts type DetailsPageTabProps = { key: string; // Route path segment ('' for index) label: string; // Tab display text isFilled?: boolean; // Tab content fills remaining height partial?: boolean; // Match any path starting with key featureFlag?: string; // Show flag indicator badge }; ``` ### `TabsLayout` -- Router-Synced Tabs Location: `src/components/TabsLayout/TabsLayout.tsx` Used inside `DetailsPage`. Renders PatternFly Tabs wired to React Router. Each tab renders `` for its child route content. ```tsx // Tab navigation is automatic -- TabsLayout reads location.pathname // and matches it against the basePath + tab keys navigate(`${basePath}/${key}`)} mountOnEnter unmountOnExit > ``` Always use `mountOnEnter` and `unmountOnExit` to avoid rendering inactive tab content. ## Page Patterns ### List Page Pattern ``` FilterContextProvider filterParams={['name', 'status']} | +-- PageLayout (title, description, actions) | +-- PageSection isFilled variant="light" | +-- BaseTextFilterToolbar | +-- SearchInput (debounced) | +-- Primary action button (ButtonWithAccessTooltip) | +-- Table (when data exists) | +-- Header: getMyFeatureHeader() | +-- Row: MyFeatureListRow | +-- FilteredEmptyState (filtered to zero results) +-- AppEmptyState (no data at all) ``` Key behaviors: - Filter state lives in URL via `useSearchParamBatch`. - `FilterContextProvider` declares which params are tracked. - Primary action button uses `ButtonWithAccessTooltip` for RBAC gating. - Loading state returns early: ``. ### Detail Page Pattern ``` MyFeatureDetailsView (parent route element) | +-- DetailsPage +-- breadcrumbs: useApplicationBreadcrumbs() + resource-specific +-- title: complex JSX with status icon +-- actions: Action[] (dropdown items) +-- tabs: DetailsPageTabProps[] +-- TabsLayout -> +-- index -> MyFeatureOverviewTab +-- 'pipelineruns' -> PipelineRunsListView +-- 'activity/:activityTab' -> ActivityTab ``` **Overview tab content structure:** ```tsx const MyFeatureOverviewTab: React.FC = () => ( <> Resource details Name {resource.metadata.name} {/* More fields... */} {/* Second column */} ); ``` #### Tab data-fetching responsibilities When implementing tabs within a detail view, split responsibilities cleanly between the **tab container** and the **tab content**: - **Tab container (route-level component):** Fetches the primary resource, handles the loading spinner, error empty state, and not-found case. It only renders the tab content component once data is fully resolved and guaranteed valid. Examples: `VulnerabilitiesTab`, `PipelineRunVulnerabilitiesTab`. - **Tab content (presentational component):** Receives guaranteed non-null resource props. It focuses on display logic rather than the fetch lifecycle of the primary resource. It may perform secondary queries (such as fetching reports or child resources), but it must not duplicate the tab container's loading or error handling for the primary resource. Example: `VulnerabilitiesTabContent`. **Before (anti-pattern):** The tab content component accepts nullable or optional props and handles all fetch lifecycle states internally: ```tsx // Before: tab content handles primary fetch lifecycle and nullable resource type VulnerabilitiesTabContentProps = { taskRun?: TaskRunKind; loaded?: boolean; error?: unknown; }; export const VulnerabilitiesTabContent: React.FC = ({ taskRun, loaded, error, }) => { if (!loaded) { return ( ); } if (error || !taskRun) { return getErrorState(error ?? { code: 404, message: 'Task run not found' }, loaded, 'task run'); } return ( ); }; ``` **After (recommended pattern):** The tab container resolves the resource and handles states; the tab content receives a guaranteed non-null resource: ```tsx // After: tab container handles loading, error, and not-found states export const VulnerabilitiesTab: React.FC = () => { const { taskRunName = '' } = useParams(); const namespace = useNamespace(); const [taskRun, taskRunLoaded, taskRunError] = useTaskRunV2(namespace, taskRunName); if (!taskRunLoaded) { return ( ); } if (taskRunError) { return getErrorState(taskRunError, taskRunLoaded, 'task run'); } if (!taskRun) { return getErrorState({ code: 404, message: 'Task run not found' }, true, 'task run'); } return ; }; // Tab content receives guaranteed non-null resource props type VulnerabilitiesTabContentProps = { taskRun: TaskRunKind; }; export const VulnerabilitiesTabContent: React.FC = ({ taskRun, }) => { const { data, isLoading, error } = useRoxctlCveReport(taskRun); if (isLoading) { return ( ); } if (error) { return getErrorState(error, !isLoading, 'vulnerabilities'); } return ( ); }; ``` **Scope and exceptions:** This pattern applies to detail-view tabs where the tab container represents a distinct route responsible for resolving its primary resource before rendering. Whether this pattern applies universally across all tabs in the application is currently an open question; detail-view tabs must always adhere to this container/content separation. For other tab types or self-contained workflows, evaluate whether hoisting data fetching and lifecycle states to a separate container provides a cleaner design or unnecessary indirection. ### Form Page Pattern ``` MyFeatureView (Formik wrapper) | +-- Formik initialValues, validationSchema, handleSubmit | +-- MyFeatureForm (form fields) | +-- PageLayout (breadcrumbs, title, footer=) | +-- PageSection isFilled variant="light" | +--
+-- +-- InputField, DropdownField, etc. ``` The View-Form separation: - **View component** -- owns Formik context, initial values, validation schema, submit handler - **Form component** -- reads `useFormikContext()`, renders fields, passes `FormFooter` as layout footer ```tsx // MyFeatureView.tsx const MyFeatureView: React.FC = () => { const navigate = useNavigate(); const handleSubmit = async (values: MyFeatureFormValues, actions: FormikHelpers<...>) => { try { await K8sQueryCreateResource(/* ... */); navigate(successPath); } catch (e) { actions.setSubmitting(false); actions.setStatus({ submitError: (e as Error).message }); } }; return ( {(formikProps) => } ); }; // MyFeatureForm.tsx const MyFeatureForm: React.FC = () => { const { handleSubmit, isSubmitting, status, dirty, isValid, handleReset } = useFormikContext(); return ( navigate(-1)} handleReset={handleReset} isSubmitting={isSubmitting} disableSubmit={!dirty || !isValid || isSubmitting} errorMessage={status?.submitError} /> } > ); }; ``` ### Modal Workflow Pattern Never use `useState(isOpen)` + raw ``. Use the modal launcher system: ```tsx // 1. Define a modal content component const MyConfirmModal: React.FC<{ obj: MyKind; modalProps: ModalProps }> = ({ obj, modalProps }) => ( Are you sure you want to process {obj.metadata.name}? ); // 2. Create a launcher factory const myConfirmModalLauncher = createModalLauncher(MyConfirmModal, { 'data-test': 'my-confirm-modal', variant: ModalVariant.small, title: 'Confirm action', }); // 3. Launch from a component const showModal = useModalLauncher(); const handleAction = () => { showModal(myConfirmModalLauncher({ obj: myResource })); }; ``` For modals that need custom headers, descriptions, or footers, use `createRawModalLauncher` instead. See [patternfly-guidelines.md](./patternfly-guidelines.md#when-to-use-which) for the full modal API reference. ## Routing ### Route Definition Routes are defined in `src/routes/page-routes/.tsx` and composed in `src/routes/index.tsx`. ```tsx // src/routes/page-routes/my-feature.tsx import { RouteObject } from 'react-router-dom'; import { MY_FEATURE_LIST_PATH, MY_FEATURE_DETAILS_PATH } from '~/routes/paths'; const myFeatureRoutes: RouteObject[] = [ { path: MY_FEATURE_LIST_PATH.path, element: , }, { path: MY_FEATURE_DETAILS_PATH.path, element: , children: [ { index: true, element: }, { path: 'pipelineruns', element: }, ], }, ]; export default myFeatureRoutes; ``` ### Path Definitions Use `buildRoute()` from `src/routes/utils.ts`: ```tsx // src/routes/paths.ts import { buildRoute } from './utils'; export const MY_FEATURE_LIST_PATH = WORKSPACE_PATH.extend('my-features'); export const MY_FEATURE_DETAILS_PATH = MY_FEATURE_LIST_PATH.extend(':myFeatureName'); // Usage in components const listUrl = MY_FEATURE_LIST_PATH.createPath({ workspaceName: namespace }); const detailUrl = MY_FEATURE_DETAILS_PATH.createPath({ workspaceName: namespace, myFeatureName: resource.metadata.name, }); ``` ## Breadcrumb Conventions Breadcrumbs are built as an array of `{ name: string, path: string }` objects or React elements: ```tsx const breadcrumbs = [ ...useApplicationBreadcrumbs(), // "Applications" link + app name + switcher { path: MY_FEATURE_LIST_PATH.createPath({ workspaceName: namespace }), name: 'My Features' }, { path: '#', name: resource.metadata.name }, // Current page (non-navigable) ]; ``` ## Action Menu Conventions ### For Detail Pages (DetailsPage Actions Dropdown) ```ts const actions: Action[] = [ { key: 'edit', label: 'Edit', onClick: () => navigate(editPath) }, { key: 'download', label: 'Download YAML', onClick: () => downloadYaml(resource) }, { type: 'separator', key: 'sep-1', label: '' }, { key: 'delete', label: 'Delete', onClick: () => showModal(deleteModal(resource)), isDisabled: !canDelete, disabledTooltip: 'You do not have permission to delete this resource', }, ]; ``` ### For List Pages and Shared Components (ActionMenu) ```ts const actions: MenuOption[] = [ { id: 'edit', label: 'Edit', cta: () => navigate(editPath), }, { id: 'delete-group', label: 'Danger zone', children: [ { id: 'delete', label: 'Delete', cta: () => showModal(deleteModal(resource)), disabled: !canDelete, disabledTooltip: 'You do not have permission', }, ], }, ]; ``` ## Loading and Error States ### Standard Loading State ```tsx if (!loaded) { return ( ); } ``` ### Standard Error State ```tsx import { ErrorEmptyState } from '~/shared/components/empty-state'; if (error) { return ; } ``` `ErrorEmptyState` handles: - 403 -> "Access Denied" message - 404 -> Redirects to `NotFoundEmptyState` - Other errors -> Generic error with the message